1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/autofill_private/autofill_private_event_router.h"
10 #include "base/bind_helpers.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/autofill/personal_data_manager_factory.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/api/autofill_private/autofill_util.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/common/extensions/api/autofill_private.h"
18 #include "components/autofill/core/browser/personal_data_manager.h"
19 #include "content/public/browser/browser_context.h"
21 using AddressEntryList
= std::vector
<linked_ptr
<
22 extensions::api::autofill_private::AddressEntry
> >;
23 using CreditCardEntryList
= std::vector
<linked_ptr
<
24 extensions::api::autofill_private::CreditCardEntry
> >;
26 namespace extensions
{
28 AutofillPrivateEventRouter::AutofillPrivateEventRouter(
29 content::BrowserContext
* context
)
31 event_router_(nullptr),
32 personal_data_(nullptr),
34 // Register with the event router so we know when renderers are listening to
35 // our events. We first check and see if there *is* an event router, because
36 // some unit tests try to create all context services, but don't initialize
37 // the event router first.
38 event_router_
= EventRouter::Get(context_
);
42 personal_data_
= autofill::PersonalDataManagerFactory::GetForProfile(
43 Profile::FromBrowserContext(context_
));
47 event_router_
->RegisterObserver(
49 api::autofill_private::OnAddressListChanged::kEventName
);
50 event_router_
->RegisterObserver(
52 api::autofill_private::OnCreditCardListChanged::kEventName
);
53 StartOrStopListeningForChanges();
56 AutofillPrivateEventRouter::~AutofillPrivateEventRouter() {
59 void AutofillPrivateEventRouter::Shutdown() {
61 event_router_
->UnregisterObserver(this);
64 void AutofillPrivateEventRouter::OnListenerAdded(
65 const EventListenerInfo
& details
) {
66 // Start listening to change events and propagate the original lists to
68 StartOrStopListeningForChanges();
69 OnPersonalDataChanged();
72 void AutofillPrivateEventRouter::OnListenerRemoved(
73 const EventListenerInfo
& details
) {
74 // Stop listening to events if there are no more listeners.
75 StartOrStopListeningForChanges();
78 void AutofillPrivateEventRouter::OnPersonalDataChanged() {
79 DCHECK(personal_data_
&& personal_data_
->IsDataLoaded());
81 scoped_ptr
<AddressEntryList
> addressList
=
82 extensions::autofill_util::GenerateAddressList(*personal_data_
);
83 scoped_ptr
<base::ListValue
> args(
84 api::autofill_private::OnAddressListChanged::Create(*addressList
)
86 scoped_ptr
<Event
> extension_event(new Event(
87 events::AUTOFILL_PRIVATE_ON_ADDRESS_LIST_CHANGED
,
88 api::autofill_private::OnAddressListChanged::kEventName
, args
.Pass()));
89 event_router_
->BroadcastEvent(extension_event
.Pass());
91 scoped_ptr
<CreditCardEntryList
> creditCardList
=
92 extensions::autofill_util::GenerateCreditCardList(*personal_data_
);
94 api::autofill_private::OnCreditCardListChanged::Create(*creditCardList
)
96 extension_event
.reset(new Event(
97 events::AUTOFILL_PRIVATE_ON_CREDIT_CARD_LIST_CHANGED
,
98 api::autofill_private::OnCreditCardListChanged::kEventName
, args
.Pass()));
99 event_router_
->BroadcastEvent(extension_event
.Pass());
102 void AutofillPrivateEventRouter::StartOrStopListeningForChanges() {
103 if (!personal_data_
|| !personal_data_
->IsDataLoaded())
106 bool should_listen_for_address_changes
= event_router_
->HasEventListener(
107 api::autofill_private::OnAddressListChanged::kEventName
);
108 bool should_listen_for_credit_card_changes
= event_router_
->HasEventListener(
109 api::autofill_private::OnCreditCardListChanged::kEventName
);
110 bool should_listen
= should_listen_for_address_changes
||
111 should_listen_for_credit_card_changes
;
113 if (should_listen
&& !listening_
)
114 personal_data_
->AddObserver(this);
115 else if (!should_listen
&& listening_
)
116 personal_data_
->RemoveObserver(this);
118 listening_
= should_listen
;
121 AutofillPrivateEventRouter
* AutofillPrivateEventRouter::Create(
122 content::BrowserContext
* context
) {
123 return new AutofillPrivateEventRouter(context
);
126 } // namespace extensions