Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/browser/extensions/api/autofill_private/autofill_private_event_router.cc

Issue 1957043002: Separate the listeners and getters for the Autofill Private API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@autofill-lists.gitbr
Patch Set: update histogram.xml and test Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/autofill_private/autofill_private_event_ router.h" 5 #include "chrome/browser/extensions/api/autofill_private/autofill_private_event_ router.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 22 matching lines...) Expand all
33 // the event router first. 33 // the event router first.
34 event_router_ = EventRouter::Get(context_); 34 event_router_ = EventRouter::Get(context_);
35 if (!event_router_) 35 if (!event_router_)
36 return; 36 return;
37 37
38 personal_data_ = autofill::PersonalDataManagerFactory::GetForProfile( 38 personal_data_ = autofill::PersonalDataManagerFactory::GetForProfile(
39 Profile::FromBrowserContext(context_)); 39 Profile::FromBrowserContext(context_));
40 if (!personal_data_) 40 if (!personal_data_)
41 return; 41 return;
42 42
43 event_router_->RegisterObserver( 43 personal_data_->AddObserver(this);
44 this,
45 api::autofill_private::OnAddressListChanged::kEventName);
46 event_router_->RegisterObserver(
47 this,
48 api::autofill_private::OnCreditCardListChanged::kEventName);
49 StartOrStopListeningForChanges();
50 } 44 }
51 45
52 AutofillPrivateEventRouter::~AutofillPrivateEventRouter() { 46 AutofillPrivateEventRouter::~AutofillPrivateEventRouter() {
53 } 47 }
54 48
55 void AutofillPrivateEventRouter::Shutdown() { 49 void AutofillPrivateEventRouter::Shutdown() {
56 if (event_router_) 50 if (personal_data_)
57 event_router_->UnregisterObserver(this); 51 personal_data_->RemoveObserver(this);
58 }
59
60 void AutofillPrivateEventRouter::OnListenerAdded(
61 const EventListenerInfo& details) {
62 // Start listening to change events and propagate the original lists to
63 // listeners.
64 StartOrStopListeningForChanges();
65 OnPersonalDataChanged();
66 }
67
68 void AutofillPrivateEventRouter::OnListenerRemoved(
69 const EventListenerInfo& details) {
70 // Stop listening to events if there are no more listeners.
71 StartOrStopListeningForChanges();
72 } 52 }
73 53
74 void AutofillPrivateEventRouter::OnPersonalDataChanged() { 54 void AutofillPrivateEventRouter::OnPersonalDataChanged() {
75 DCHECK(personal_data_ && personal_data_->IsDataLoaded()); 55 DCHECK(personal_data_ && personal_data_->IsDataLoaded());
76 56
77 autofill_util::AddressEntryList addressList = 57 autofill_util::AddressEntryList addressList =
78 extensions::autofill_util::GenerateAddressList(*personal_data_); 58 extensions::autofill_util::GenerateAddressList(*personal_data_);
79 std::unique_ptr<base::ListValue> args( 59 std::unique_ptr<base::ListValue> args(
80 api::autofill_private::OnAddressListChanged::Create(addressList) 60 api::autofill_private::OnAddressListChanged::Create(addressList)
81 .release()); 61 .release());
82 std::unique_ptr<Event> extension_event( 62 std::unique_ptr<Event> extension_event(
83 new Event(events::AUTOFILL_PRIVATE_ON_ADDRESS_LIST_CHANGED, 63 new Event(events::AUTOFILL_PRIVATE_ON_ADDRESS_LIST_CHANGED,
84 api::autofill_private::OnAddressListChanged::kEventName, 64 api::autofill_private::OnAddressListChanged::kEventName,
85 std::move(args))); 65 std::move(args)));
86 event_router_->BroadcastEvent(std::move(extension_event)); 66 event_router_->BroadcastEvent(std::move(extension_event));
87 67
88 autofill_util::CreditCardEntryList creditCardList = 68 autofill_util::CreditCardEntryList creditCardList =
89 extensions::autofill_util::GenerateCreditCardList(*personal_data_); 69 extensions::autofill_util::GenerateCreditCardList(*personal_data_);
90 args.reset( 70 args.reset(
91 api::autofill_private::OnCreditCardListChanged::Create(creditCardList) 71 api::autofill_private::OnCreditCardListChanged::Create(creditCardList)
92 .release()); 72 .release());
93 extension_event.reset( 73 extension_event.reset(
94 new Event(events::AUTOFILL_PRIVATE_ON_CREDIT_CARD_LIST_CHANGED, 74 new Event(events::AUTOFILL_PRIVATE_ON_CREDIT_CARD_LIST_CHANGED,
95 api::autofill_private::OnCreditCardListChanged::kEventName, 75 api::autofill_private::OnCreditCardListChanged::kEventName,
96 std::move(args))); 76 std::move(args)));
97 event_router_->BroadcastEvent(std::move(extension_event)); 77 event_router_->BroadcastEvent(std::move(extension_event));
98 } 78 }
99 79
100 void AutofillPrivateEventRouter::StartOrStopListeningForChanges() {
101 if (!personal_data_ || !personal_data_->IsDataLoaded())
102 return;
103
104 bool should_listen_for_address_changes = event_router_->HasEventListener(
105 api::autofill_private::OnAddressListChanged::kEventName);
106 bool should_listen_for_credit_card_changes = event_router_->HasEventListener(
107 api::autofill_private::OnCreditCardListChanged::kEventName);
108 bool should_listen = should_listen_for_address_changes ||
109 should_listen_for_credit_card_changes;
110
111 if (should_listen && !listening_)
112 personal_data_->AddObserver(this);
113 else if (!should_listen && listening_)
114 personal_data_->RemoveObserver(this);
115
116 listening_ = should_listen;
117 }
118
119 AutofillPrivateEventRouter* AutofillPrivateEventRouter::Create( 80 AutofillPrivateEventRouter* AutofillPrivateEventRouter::Create(
120 content::BrowserContext* context) { 81 content::BrowserContext* context) {
121 return new AutofillPrivateEventRouter(context); 82 return new AutofillPrivateEventRouter(context);
122 } 83 }
123 84
124 } // namespace extensions 85 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698