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

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

Issue 2886923002: [extension SW]: Support event listener registration and event dispatching. (Closed)
Patch Set: address comments Created 3 years, 6 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(),
Devlin 2017/06/01 04:54:49 while we're here, prefer std::string()
lazyboy 2017/06/01 23:33:29 Done.
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 = filter_->CreateDeepCopy(); 80 filter_copy = filter_->CreateDeepCopy();
67 return base::WrapUnique(new EventListener(event_name_, extension_id_, 81 return base::WrapUnique(
68 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() {
95 DCHECK_NE(worker_thread_id_, kNonWorkerThreadId);
77 process_ = nullptr; 96 process_ = nullptr;
78 } 97 }
79 98
80 content::BrowserContext* EventListener::GetBrowserContext() const { 99 content::BrowserContext* EventListener::GetBrowserContext() const {
81 return process_ ? process_->GetBrowserContext() : nullptr; 100 return process_ ? process_->GetBrowserContext() : nullptr;
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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 188
168 for (const auto& listener_to_search : it->second) { 189 for (const auto& listener_to_search : it->second) {
169 if (listener_to_search->Equals(listener)) 190 if (listener_to_search->Equals(listener))
170 return true; 191 return true;
171 } 192 }
172 return false; 193 return false;
173 } 194 }
174 195
175 bool EventListenerMap::HasProcessListener( 196 bool EventListenerMap::HasProcessListener(
176 content::RenderProcessHost* process, 197 content::RenderProcessHost* process,
198 int worker_thread_id,
177 const std::string& extension_id) const { 199 const std::string& extension_id) const {
178 for (const auto& it : listeners_) { 200 for (const auto& it : listeners_) {
179 for (const auto& listener : it.second) { 201 for (const auto& listener : it.second) {
180 if (listener->process() == process && 202 if (listener->process() == process &&
181 listener->extension_id() == extension_id) 203 listener->extension_id() == extension_id &&
204 listener->worker_thread_id() == worker_thread_id) {
182 return true; 205 return true;
206 }
183 } 207 }
184 } 208 }
185 return false; 209 return false;
186 } 210 }
187 211
188 void EventListenerMap::RemoveListenersForExtension( 212 void EventListenerMap::RemoveListenersForExtension(
189 const std::string& extension_id) { 213 const std::string& extension_id) {
190 for (auto& it : listeners_) { 214 for (auto& it : listeners_) {
191 auto& listener_map = it.second; 215 auto& listener_map = it.second;
192 for (ListenerList::iterator it2 = listener_map.begin(); 216 for (ListenerList::iterator it2 = listener_map.begin();
(...skipping 24 matching lines...) Expand all
217 const DictionaryValue& filtered) { 241 const DictionaryValue& filtered) {
218 for (DictionaryValue::Iterator it(filtered); !it.IsAtEnd(); it.Advance()) { 242 for (DictionaryValue::Iterator it(filtered); !it.IsAtEnd(); it.Advance()) {
219 // We skip entries if they are malformed. 243 // We skip entries if they are malformed.
220 const base::ListValue* filter_list = nullptr; 244 const base::ListValue* filter_list = nullptr;
221 if (!it.value().GetAsList(&filter_list)) 245 if (!it.value().GetAsList(&filter_list))
222 continue; 246 continue;
223 for (size_t i = 0; i < filter_list->GetSize(); i++) { 247 for (size_t i = 0; i < filter_list->GetSize(); i++) {
224 const DictionaryValue* filter = nullptr; 248 const DictionaryValue* filter = nullptr;
225 if (!filter_list->GetDictionary(i, &filter)) 249 if (!filter_list->GetDictionary(i, &filter))
226 continue; 250 continue;
251 // Currently this is only used for lazy background page events.
252 // TODO(lazyboy): Add extension SW lazy events.
227 AddListener( 253 AddListener(
228 EventListener::ForExtension(it.key(), extension_id, nullptr, 254 EventListener::ForExtension(it.key(), extension_id, nullptr,
229 base::WrapUnique(filter->DeepCopy()))); 255 base::WrapUnique(filter->DeepCopy())));
230 } 256 }
231 } 257 }
232 } 258 }
233 259
234 std::set<const EventListener*> EventListenerMap::GetEventListeners( 260 std::set<const EventListener*> EventListenerMap::GetEventListeners(
235 const Event& event) { 261 const Event& event) {
236 std::set<const EventListener*> interested_listeners; 262 std::set<const EventListener*> interested_listeners;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 return; 303 return;
278 event_filter_.RemoveEventMatcher(listener->matcher_id()); 304 event_filter_.RemoveEventMatcher(listener->matcher_id());
279 CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id())); 305 CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id()));
280 } 306 }
281 307
282 bool EventListenerMap::IsFilteredEvent(const Event& event) const { 308 bool EventListenerMap::IsFilteredEvent(const Event& event) const {
283 return base::ContainsKey(filtered_events_, event.event_name); 309 return base::ContainsKey(filtered_events_, event.event_name);
284 } 310 }
285 311
286 } // namespace extensions 312 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698