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

Side by Side Diff: extensions/browser/event_listener_map.cc

Issue 2886923002: [extension SW]: Support event listener registration and event dispatching. (Closed)
Patch Set: removed debug logs Created 3 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "extensions/browser/event_listener_map.h" 5 #include "extensions/browser/event_listener_map.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "content/public/browser/render_process_host.h" 13 #include "content/public/browser/render_process_host.h"
14 #include "extensions/browser/event_router.h" 14 #include "extensions/browser/event_router.h"
15 #include "extensions/common/constants.h"
15 #include "ipc/ipc_message.h" 16 #include "ipc/ipc_message.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
17 #include "url/origin.h" 18 #include "url/origin.h"
18 19
19 using base::DictionaryValue; 20 using base::DictionaryValue;
20 21
21 namespace extensions { 22 namespace extensions {
22 23
23 typedef EventFilter::MatcherID MatcherID; 24 typedef EventFilter::MatcherID MatcherID;
24 25
25 // static 26 // static
26 std::unique_ptr<EventListener> EventListener::ForExtension( 27 std::unique_ptr<EventListener> EventListener::ForExtension(
27 const std::string& event_name, 28 const std::string& event_name,
28 const std::string& extension_id, 29 const std::string& extension_id,
29 content::RenderProcessHost* process, 30 content::RenderProcessHost* process,
30 std::unique_ptr<base::DictionaryValue> filter) { 31 std::unique_ptr<base::DictionaryValue> filter) {
31 return base::WrapUnique(new EventListener(event_name, extension_id, GURL(), 32 return base::WrapUnique(new EventListener(event_name, extension_id, GURL(),
32 process, std::move(filter))); 33 process, kNonWorkerThreadId,
34 std::move(filter)));
33 } 35 }
34 36
35 // static 37 // static
36 std::unique_ptr<EventListener> EventListener::ForURL( 38 std::unique_ptr<EventListener> EventListener::ForURL(
37 const std::string& event_name, 39 const std::string& event_name,
38 const GURL& listener_url, 40 const GURL& listener_url,
39 content::RenderProcessHost* process, 41 content::RenderProcessHost* process,
40 std::unique_ptr<base::DictionaryValue> filter) { 42 std::unique_ptr<base::DictionaryValue> filter) {
41 // Use only the origin to identify the event listener, e.g. chrome://settings 43 // Use only the origin to identify the event listener, e.g. chrome://settings
42 // for chrome://settings/accounts, to avoid multiple events being triggered 44 // for chrome://settings/accounts, to avoid multiple events being triggered
43 // for the same process. See crbug.com/536858 for details. // TODO(devlin): If 45 // for the same process. See crbug.com/536858 for details. // TODO(devlin): If
44 // we dispatched events to processes more intelligently this could be avoided. 46 // we dispatched events to processes more intelligently this could be avoided.
45 return base::WrapUnique(new EventListener(event_name, "", 47 return base::WrapUnique(
46 url::Origin(listener_url).GetURL(), 48 new EventListener(event_name, "", url::Origin(listener_url).GetURL(),
47 process, std::move(filter))); 49 process, kNonWorkerThreadId, std::move(filter)));
50 }
51
52 std::unique_ptr<EventListener> EventListener::ForExtensionServiceWorker(
53 const std::string& event_name,
54 const std::string& extension_id,
55 content::RenderProcessHost* process,
56 int worker_thread_id,
57 std::unique_ptr<base::DictionaryValue> filter) {
58 return base::WrapUnique(new EventListener(event_name, extension_id, GURL(),
59 process, worker_thread_id,
60 std::move(filter)));
48 } 61 }
49 62
50 EventListener::~EventListener() {} 63 EventListener::~EventListener() {}
51 64
52 bool EventListener::Equals(const EventListener* other) const { 65 bool EventListener::Equals(const EventListener* other) const {
53 // We don't check matcher_id equality because we want a listener with a 66 // We don't check matcher_id equality because we want a listener with a
54 // filter that hasn't been added to EventFilter to match one that is 67 // filter that hasn't been added to EventFilter to match one that is
55 // equivalent but has. 68 // equivalent but has.
56 return event_name_ == other->event_name_ && 69 return event_name_ == other->event_name_ &&
57 extension_id_ == other->extension_id_ && 70 extension_id_ == other->extension_id_ &&
58 listener_url_ == other->listener_url_ && process_ == other->process_ && 71 listener_url_ == other->listener_url_ && process_ == other->process_ &&
72 worker_thread_id_ == other->worker_thread_id_ &&
59 ((!!filter_.get()) == (!!other->filter_.get())) && 73 ((!!filter_.get()) == (!!other->filter_.get())) &&
60 (!filter_.get() || filter_->Equals(other->filter_.get())); 74 (!filter_.get() || filter_->Equals(other->filter_.get()));
61 } 75 }
62 76
63 std::unique_ptr<EventListener> EventListener::Copy() const { 77 std::unique_ptr<EventListener> EventListener::Copy() const {
64 std::unique_ptr<DictionaryValue> filter_copy; 78 std::unique_ptr<DictionaryValue> filter_copy;
65 if (filter_) 79 if (filter_)
66 filter_copy.reset(filter_->DeepCopy()); 80 filter_copy.reset(filter_->DeepCopy());
67 return std::unique_ptr<EventListener>( 81 return std::unique_ptr<EventListener>(
68 new EventListener(event_name_, extension_id_, listener_url_, process_, 82 new EventListener(event_name_, extension_id_, listener_url_, process_,
69 std::move(filter_copy))); 83 worker_thread_id_, std::move(filter_copy)));
70 } 84 }
71 85
72 bool EventListener::IsLazy() const { 86 bool EventListener::IsLazy() const {
73 return !process_; 87 return !process_;
74 } 88 }
75 89
90 bool EventListener::IsForServiceWorker() const {
91 return worker_thread_id_ != kNonWorkerThreadId;
92 }
93
76 void EventListener::MakeLazy() { 94 void EventListener::MakeLazy() {
77 process_ = NULL; 95 DCHECK(worker_thread_id_ == kNonWorkerThreadId);
Devlin 2017/05/24 17:58:25 nit: DCHECK_EQ
lazyboy 2017/05/25 01:33:43 Done.
96 process_ = nullptr;
78 } 97 }
79 98
80 content::BrowserContext* EventListener::GetBrowserContext() const { 99 content::BrowserContext* EventListener::GetBrowserContext() const {
81 return process_ ? process_->GetBrowserContext() : NULL; 100 return process_ ? process_->GetBrowserContext() : NULL;
82 } 101 }
83 102
84 EventListener::EventListener(const std::string& event_name, 103 EventListener::EventListener(const std::string& event_name,
85 const std::string& extension_id, 104 const std::string& extension_id,
86 const GURL& listener_url, 105 const GURL& listener_url,
87 content::RenderProcessHost* process, 106 content::RenderProcessHost* process,
107 int worker_thread_id,
88 std::unique_ptr<DictionaryValue> filter) 108 std::unique_ptr<DictionaryValue> filter)
89 : event_name_(event_name), 109 : event_name_(event_name),
90 extension_id_(extension_id), 110 extension_id_(extension_id),
91 listener_url_(listener_url), 111 listener_url_(listener_url),
92 process_(process), 112 process_(process),
113 worker_thread_id_(worker_thread_id),
93 filter_(std::move(filter)), 114 filter_(std::move(filter)),
94 matcher_id_(-1) {} 115 matcher_id_(-1) {}
95 116
96 EventListenerMap::EventListenerMap(Delegate* delegate) 117 EventListenerMap::EventListenerMap(Delegate* delegate)
97 : delegate_(delegate) { 118 : delegate_(delegate) {
98 } 119 }
99 120
100 EventListenerMap::~EventListenerMap() {} 121 EventListenerMap::~EventListenerMap() {}
101 122
102 bool EventListenerMap::AddListener(std::unique_ptr<EventListener> listener) { 123 bool EventListenerMap::AddListener(std::unique_ptr<EventListener> listener) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 for (ListenerList::iterator it2 = it->second.begin(); 189 for (ListenerList::iterator it2 = it->second.begin();
169 it2 != it->second.end(); it2++) { 190 it2 != it->second.end(); it2++) {
170 if ((*it2)->Equals(listener)) { 191 if ((*it2)->Equals(listener)) {
171 return true; 192 return true;
172 } 193 }
173 } 194 }
174 return false; 195 return false;
175 } 196 }
176 197
177 bool EventListenerMap::HasProcessListener(content::RenderProcessHost* process, 198 bool EventListenerMap::HasProcessListener(content::RenderProcessHost* process,
199 int worker_thread_id,
178 const std::string& extension_id) { 200 const std::string& extension_id) {
179 for (ListenerMap::iterator it = listeners_.begin(); it != listeners_.end(); 201 for (ListenerMap::iterator it = listeners_.begin(); it != listeners_.end();
180 it++) { 202 it++) {
181 for (ListenerList::iterator it2 = it->second.begin(); 203 for (ListenerList::iterator it2 = it->second.begin();
182 it2 != it->second.end(); it2++) { 204 it2 != it->second.end(); it2++) {
183 if ((*it2)->process() == process && 205 if ((*it2)->process() == process &&
184 (*it2)->extension_id() == extension_id) 206 (*it2)->extension_id() == extension_id &&
207 (*it2)->worker_thread_id() == worker_thread_id) {
185 return true; 208 return true;
209 }
186 } 210 }
187 } 211 }
188 return false; 212 return false;
189 } 213 }
190 214
191 void EventListenerMap::RemoveListenersForExtension( 215 void EventListenerMap::RemoveListenersForExtension(
192 const std::string& extension_id) { 216 const std::string& extension_id) {
193 for (ListenerMap::iterator it = listeners_.begin(); it != listeners_.end(); 217 for (ListenerMap::iterator it = listeners_.begin(); it != listeners_.end();
194 it++) { 218 it++) {
195 for (ListenerList::iterator it2 = it->second.begin(); 219 for (ListenerList::iterator it2 = it->second.begin();
196 it2 != it->second.end();) { 220 it2 != it->second.end();) {
197 if ((*it2)->extension_id() == extension_id) { 221 if ((*it2)->extension_id() == extension_id) {
198 std::unique_ptr<EventListener> listener_removed = std::move(*it2); 222 std::unique_ptr<EventListener> listener_removed = std::move(*it2);
199 CleanupListener(listener_removed.get()); 223 CleanupListener(listener_removed.get());
200 it2 = it->second.erase(it2); 224 it2 = it->second.erase(it2);
201 delegate_->OnListenerRemoved(listener_removed.get()); 225 delegate_->OnListenerRemoved(listener_removed.get());
202 } else { 226 } else {
203 it2++; 227 it2++;
204 } 228 }
205 } 229 }
206 } 230 }
207 } 231 }
208 232
209 void EventListenerMap::LoadUnfilteredLazyListeners( 233 void EventListenerMap::LoadUnfilteredLazyListeners(
210 const std::string& extension_id, 234 const std::string& extension_id,
211 const std::set<std::string>& event_names) { 235 const std::set<std::string>& event_names) {
212 for (std::set<std::string>::const_iterator it = event_names.begin(); 236 for (std::set<std::string>::const_iterator it = event_names.begin();
213 it != event_names.end(); ++it) { 237 it != event_names.end(); ++it) {
214 AddListener(EventListener::ForExtension( 238 AddListener(EventListener::ForExtension(
215 *it, extension_id, NULL, std::unique_ptr<DictionaryValue>())); 239 *it, extension_id, nullptr, std::unique_ptr<DictionaryValue>()));
216 } 240 }
217 } 241 }
218 242
219 void EventListenerMap::LoadFilteredLazyListeners( 243 void EventListenerMap::LoadFilteredLazyListeners(
220 const std::string& extension_id, 244 const std::string& extension_id,
221 const DictionaryValue& filtered) { 245 const DictionaryValue& filtered) {
222 for (DictionaryValue::Iterator it(filtered); !it.IsAtEnd(); it.Advance()) { 246 for (DictionaryValue::Iterator it(filtered); !it.IsAtEnd(); it.Advance()) {
223 // We skip entries if they are malformed. 247 // We skip entries if they are malformed.
224 const base::ListValue* filter_list = NULL; 248 const base::ListValue* filter_list = NULL;
225 if (!it.value().GetAsList(&filter_list)) 249 if (!it.value().GetAsList(&filter_list))
226 continue; 250 continue;
227 for (size_t i = 0; i < filter_list->GetSize(); i++) { 251 for (size_t i = 0; i < filter_list->GetSize(); i++) {
228 const DictionaryValue* filter = NULL; 252 const DictionaryValue* filter = NULL;
229 if (!filter_list->GetDictionary(i, &filter)) 253 if (!filter_list->GetDictionary(i, &filter))
230 continue; 254 continue;
255 // Currently this is only used for lazy background page events.
256 // TODO(lazyboy): Add extension SW lazy events.
231 AddListener(EventListener::ForExtension( 257 AddListener(EventListener::ForExtension(
232 it.key(), extension_id, NULL, base::WrapUnique(filter->DeepCopy()))); 258 it.key(), extension_id, NULL, base::WrapUnique(filter->DeepCopy())));
233 } 259 }
234 } 260 }
235 } 261 }
236 262
237 std::set<const EventListener*> EventListenerMap::GetEventListeners( 263 std::set<const EventListener*> EventListenerMap::GetEventListeners(
238 const Event& event) { 264 const Event& event) {
239 std::set<const EventListener*> interested_listeners; 265 std::set<const EventListener*> interested_listeners;
240 if (IsFilteredEvent(event)) { 266 if (IsFilteredEvent(event)) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 return; 310 return;
285 event_filter_.RemoveEventMatcher(listener->matcher_id()); 311 event_filter_.RemoveEventMatcher(listener->matcher_id());
286 CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id())); 312 CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id()));
287 } 313 }
288 314
289 bool EventListenerMap::IsFilteredEvent(const Event& event) const { 315 bool EventListenerMap::IsFilteredEvent(const Event& event) const {
290 return filtered_events_.count(event.event_name) > 0u; 316 return filtered_events_.count(event.event_name) > 0u;
291 } 317 }
292 318
293 } // namespace extensions 319 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698