Skip to content

Commit

Permalink
Separate the listeners and getters for the Autofill Private API.
Browse files Browse the repository at this point in the history
OnAddressListChanged and OnCreditCardListChanged used to trigger an
update when a listener was added. This is now de-coupled so that a list
can be retrieved separately from listening for changes.

BUG=607348,607347
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/1957043002
Cr-Commit-Position: refs/heads/master@{#395462}
  • Loading branch information
hcarmona authored and Commit bot committed May 23, 2016
1 parent 2d0c9ae commit f900222
Show file tree
Hide file tree
Showing 16 changed files with 909 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/values.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/autofill_private/autofill_util.h"
#include "chrome/common/extensions/api/autofill_private.h"
#include "chrome/grit/generated_resources.h"
#include "components/autofill/core/browser/autofill_profile.h"
Expand Down Expand Up @@ -299,6 +300,29 @@ ExtensionFunction::ResponseAction
return RespondNow(OneArgument(components.ToValue().release()));
}

////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetAddressListFunction

AutofillPrivateGetAddressListFunction::AutofillPrivateGetAddressListFunction()
: chrome_details_(this) {}

AutofillPrivateGetAddressListFunction::
~AutofillPrivateGetAddressListFunction() {}

ExtensionFunction::ResponseAction AutofillPrivateGetAddressListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());

DCHECK(personal_data && personal_data->IsDataLoaded());

autofill_util::AddressEntryList addressList =
extensions::autofill_util::GenerateAddressList(*personal_data);

return RespondNow(ArgumentList(
api::autofill_private::GetAddressList::Results::Create(addressList)));
}

////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateSaveCreditCardFunction

Expand Down Expand Up @@ -437,4 +461,30 @@ ExtensionFunction::ResponseAction AutofillPrivateMaskCreditCardFunction::Run() {
return RespondNow(NoArguments());
}

////////////////////////////////////////////////////////////////////////////////
// AutofillPrivateGetCreditCardListFunction

AutofillPrivateGetCreditCardListFunction::
AutofillPrivateGetCreditCardListFunction()
: chrome_details_(this) {}

AutofillPrivateGetCreditCardListFunction::
~AutofillPrivateGetCreditCardListFunction() {}

ExtensionFunction::ResponseAction
AutofillPrivateGetCreditCardListFunction::Run() {
autofill::PersonalDataManager* personal_data =
autofill::PersonalDataManagerFactory::GetForProfile(
chrome_details_.GetProfile());

DCHECK(personal_data && personal_data->IsDataLoaded());

autofill_util::CreditCardEntryList creditCardList =
extensions::autofill_util::GenerateCreditCardList(*personal_data);

return RespondNow(
ArgumentList(api::autofill_private::GetCreditCardList::Results::Create(
creditCardList)));
}

} // namespace extensions
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ class AutofillPrivateGetAddressComponentsFunction :
DISALLOW_COPY_AND_ASSIGN(AutofillPrivateGetAddressComponentsFunction);
};

class AutofillPrivateGetAddressListFunction : public UIThreadExtensionFunction {
public:
AutofillPrivateGetAddressListFunction();
DECLARE_EXTENSION_FUNCTION("autofillPrivate.getAddressList",
AUTOFILLPRIVATE_GETADDRESSLIST);

protected:
~AutofillPrivateGetAddressListFunction() override;

// ExtensionFunction overrides.
ResponseAction Run() override;

private:
ChromeExtensionFunctionDetails chrome_details_;

DISALLOW_COPY_AND_ASSIGN(AutofillPrivateGetAddressListFunction);
};

class AutofillPrivateSaveCreditCardFunction : public UIThreadExtensionFunction {
public:
AutofillPrivateSaveCreditCardFunction();
Expand Down Expand Up @@ -119,6 +137,25 @@ class AutofillPrivateMaskCreditCardFunction : public UIThreadExtensionFunction {
DISALLOW_COPY_AND_ASSIGN(AutofillPrivateMaskCreditCardFunction);
};

class AutofillPrivateGetCreditCardListFunction
: public UIThreadExtensionFunction {
public:
AutofillPrivateGetCreditCardListFunction();
DECLARE_EXTENSION_FUNCTION("autofillPrivate.getCreditCardList",
AUTOFILLPRIVATE_GETCREDITCARDLIST);

protected:
~AutofillPrivateGetCreditCardListFunction() override;

// ExtensionFunction overrides.
ResponseAction Run() override;

private:
ChromeExtensionFunctionDetails chrome_details_;

DISALLOW_COPY_AND_ASSIGN(AutofillPrivateGetCreditCardListFunction);
};

} // namespace extensions

#endif // CHROME_BROWSER_EXTENSIONS_API_AUTOFILL_PRIVATE_AUTOFILL_PRIVATE_API_H_
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,15 @@ AutofillPrivateEventRouter::AutofillPrivateEventRouter(
if (!personal_data_)
return;

event_router_->RegisterObserver(
this,
api::autofill_private::OnAddressListChanged::kEventName);
event_router_->RegisterObserver(
this,
api::autofill_private::OnCreditCardListChanged::kEventName);
StartOrStopListeningForChanges();
personal_data_->AddObserver(this);
}

AutofillPrivateEventRouter::~AutofillPrivateEventRouter() {
}

void AutofillPrivateEventRouter::Shutdown() {
if (event_router_)
event_router_->UnregisterObserver(this);
}

void AutofillPrivateEventRouter::OnListenerAdded(
const EventListenerInfo& details) {
// Start listening to change events and propagate the original lists to
// listeners.
StartOrStopListeningForChanges();
OnPersonalDataChanged();
}

void AutofillPrivateEventRouter::OnListenerRemoved(
const EventListenerInfo& details) {
// Stop listening to events if there are no more listeners.
StartOrStopListeningForChanges();
if (personal_data_)
personal_data_->RemoveObserver(this);
}

void AutofillPrivateEventRouter::OnPersonalDataChanged() {
Expand Down Expand Up @@ -97,25 +77,6 @@ void AutofillPrivateEventRouter::OnPersonalDataChanged() {
event_router_->BroadcastEvent(std::move(extension_event));
}

void AutofillPrivateEventRouter::StartOrStopListeningForChanges() {
if (!personal_data_ || !personal_data_->IsDataLoaded())
return;

bool should_listen_for_address_changes = event_router_->HasEventListener(
api::autofill_private::OnAddressListChanged::kEventName);
bool should_listen_for_credit_card_changes = event_router_->HasEventListener(
api::autofill_private::OnCreditCardListChanged::kEventName);
bool should_listen = should_listen_for_address_changes ||
should_listen_for_credit_card_changes;

if (should_listen && !listening_)
personal_data_->AddObserver(this);
else if (!should_listen && listening_)
personal_data_->RemoveObserver(this);

listening_ = should_listen;
}

AutofillPrivateEventRouter* AutofillPrivateEventRouter::Create(
content::BrowserContext* context) {
return new AutofillPrivateEventRouter(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ class AutofillPrivateEventRouter :
// KeyedService overrides:
void Shutdown() override;

// EventRouter::Observer overrides:
void OnListenerAdded(const EventListenerInfo& details) override;
void OnListenerRemoved(const EventListenerInfo& details) override;

// PersonalDataManagerObserver implementation.
void OnPersonalDataChanged() override;

private:
// Either listens or unlistens for changes to |personal_data_|, depending on
// whether clients are listening to the autofillPrivate API events.
void StartOrStopListeningForChanges();

content::BrowserContext* context_;

EventRouter* event_router_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
</neon-animatable>
<template is="dom-if" name="manage-autofill">
<settings-subpage page-title="$i18n{autofill}">
<settings-autofill-section id="autofillSection">
<settings-autofill-section id="autofillSection"
addresses="[[addresses]]" credit-cards="[[creditCards]]">
</settings-autofill-section>
</settings-subpage>
</template>
Expand Down
Loading

0 comments on commit f900222

Please sign in to comment.