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

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

Issue 2943583002: [extension SW] Support lazy events from extension service workers. (Closed)
Patch Set: sync @tott Created 3 years, 5 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
« no previous file with comments | « extensions/browser/event_listener_map.h ('k') | extensions/browser/event_router.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 12 matching lines...) Expand all
23 23
24 typedef EventFilter::MatcherID MatcherID; 24 typedef EventFilter::MatcherID MatcherID;
25 25
26 // static 26 // static
27 std::unique_ptr<EventListener> EventListener::ForExtension( 27 std::unique_ptr<EventListener> EventListener::ForExtension(
28 const std::string& event_name, 28 const std::string& event_name,
29 const std::string& extension_id, 29 const std::string& extension_id,
30 content::RenderProcessHost* process, 30 content::RenderProcessHost* process,
31 std::unique_ptr<base::DictionaryValue> filter) { 31 std::unique_ptr<base::DictionaryValue> filter) {
32 return base::WrapUnique(new EventListener(event_name, extension_id, GURL(), 32 return base::WrapUnique(new EventListener(event_name, extension_id, GURL(),
33 process, kNonWorkerThreadId, 33 process, false, kNonWorkerThreadId,
34 std::move(filter))); 34 std::move(filter)));
35 } 35 }
36 36
37 // static 37 // static
38 std::unique_ptr<EventListener> EventListener::ForURL( 38 std::unique_ptr<EventListener> EventListener::ForURL(
39 const std::string& event_name, 39 const std::string& event_name,
40 const GURL& listener_url, 40 const GURL& listener_url,
41 content::RenderProcessHost* process, 41 content::RenderProcessHost* process,
42 std::unique_ptr<base::DictionaryValue> filter) { 42 std::unique_ptr<base::DictionaryValue> filter) {
43 // 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
44 // for chrome://settings/accounts, to avoid multiple events being triggered 44 // for chrome://settings/accounts, to avoid multiple events being triggered
45 // 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
46 // we dispatched events to processes more intelligently this could be avoided. 46 // we dispatched events to processes more intelligently this could be avoided.
47 return base::WrapUnique(new EventListener( 47 return base::WrapUnique(new EventListener(
48 event_name, ExtensionId(), url::Origin(listener_url).GetURL(), process, 48 event_name, ExtensionId(), url::Origin(listener_url).GetURL(), process,
49 kNonWorkerThreadId, std::move(filter))); 49 false, kNonWorkerThreadId, std::move(filter)));
50 } 50 }
51 51
52 std::unique_ptr<EventListener> EventListener::ForExtensionServiceWorker( 52 std::unique_ptr<EventListener> EventListener::ForExtensionServiceWorker(
53 const std::string& event_name, 53 const std::string& event_name,
54 const std::string& extension_id, 54 const std::string& extension_id,
55 content::RenderProcessHost* process, 55 content::RenderProcessHost* process,
56 const GURL& service_worker_scope,
56 int worker_thread_id, 57 int worker_thread_id,
57 std::unique_ptr<base::DictionaryValue> filter) { 58 std::unique_ptr<base::DictionaryValue> filter) {
58 return base::WrapUnique(new EventListener(event_name, extension_id, GURL(), 59 return base::WrapUnique(
59 process, worker_thread_id, 60 new EventListener(event_name, extension_id, service_worker_scope, process,
60 std::move(filter))); 61 true, worker_thread_id, std::move(filter)));
61 } 62 }
62 63
63 EventListener::~EventListener() {} 64 EventListener::~EventListener() {}
64 65
65 bool EventListener::Equals(const EventListener* other) const { 66 bool EventListener::Equals(const EventListener* other) const {
66 // We don't check matcher_id equality because we want a listener with a 67 // We don't check matcher_id equality because we want a listener with a
67 // filter that hasn't been added to EventFilter to match one that is 68 // filter that hasn't been added to EventFilter to match one that is
68 // equivalent but has. 69 // equivalent but has.
69 return event_name_ == other->event_name_ && 70 return event_name_ == other->event_name_ &&
70 extension_id_ == other->extension_id_ && 71 extension_id_ == other->extension_id_ &&
71 listener_url_ == other->listener_url_ && process_ == other->process_ && 72 listener_url_ == other->listener_url_ && process_ == other->process_ &&
73 is_for_service_worker_ == other->is_for_service_worker_ &&
72 worker_thread_id_ == other->worker_thread_id_ && 74 worker_thread_id_ == other->worker_thread_id_ &&
73 ((!!filter_.get()) == (!!other->filter_.get())) && 75 ((!!filter_.get()) == (!!other->filter_.get())) &&
74 (!filter_.get() || filter_->Equals(other->filter_.get())); 76 (!filter_.get() || filter_->Equals(other->filter_.get()));
75 } 77 }
76 78
77 std::unique_ptr<EventListener> EventListener::Copy() const { 79 std::unique_ptr<EventListener> EventListener::Copy() const {
78 std::unique_ptr<DictionaryValue> filter_copy; 80 std::unique_ptr<DictionaryValue> filter_copy;
79 if (filter_) 81 if (filter_)
80 filter_copy = filter_->CreateDeepCopy(); 82 filter_copy = filter_->CreateDeepCopy();
81 return base::WrapUnique( 83 return base::WrapUnique(new EventListener(
82 new EventListener(event_name_, extension_id_, listener_url_, process_, 84 event_name_, extension_id_, listener_url_, process_,
83 worker_thread_id_, std::move(filter_copy))); 85 is_for_service_worker_, worker_thread_id_, std::move(filter_copy)));
84 } 86 }
85 87
86 bool EventListener::IsLazy() const { 88 bool EventListener::IsLazy() const {
87 return !process_; 89 return !process_;
88 } 90 }
89 91
90 bool EventListener::IsForServiceWorker() const {
91 return worker_thread_id_ != kNonWorkerThreadId;
92 }
93
94 void EventListener::MakeLazy() { 92 void EventListener::MakeLazy() {
95 DCHECK_EQ(worker_thread_id_, kNonWorkerThreadId); 93 DCHECK_EQ(worker_thread_id_, kNonWorkerThreadId);
96 process_ = nullptr; 94 process_ = nullptr;
97 } 95 }
98 96
99 content::BrowserContext* EventListener::GetBrowserContext() const { 97 content::BrowserContext* EventListener::GetBrowserContext() const {
100 return process_ ? process_->GetBrowserContext() : nullptr; 98 return process_ ? process_->GetBrowserContext() : nullptr;
101 } 99 }
102 100
103 EventListener::EventListener(const std::string& event_name, 101 EventListener::EventListener(const std::string& event_name,
104 const std::string& extension_id, 102 const std::string& extension_id,
105 const GURL& listener_url, 103 const GURL& listener_url,
106 content::RenderProcessHost* process, 104 content::RenderProcessHost* process,
105 bool is_for_service_worker,
107 int worker_thread_id, 106 int worker_thread_id,
108 std::unique_ptr<DictionaryValue> filter) 107 std::unique_ptr<DictionaryValue> filter)
109 : event_name_(event_name), 108 : event_name_(event_name),
110 extension_id_(extension_id), 109 extension_id_(extension_id),
111 listener_url_(listener_url), 110 listener_url_(listener_url),
112 process_(process), 111 process_(process),
112 is_for_service_worker_(is_for_service_worker),
113 worker_thread_id_(worker_thread_id), 113 worker_thread_id_(worker_thread_id),
114 filter_(std::move(filter)), 114 filter_(std::move(filter)),
115 matcher_id_(-1) {} 115 matcher_id_(-1) {}
116 116
117 EventListenerMap::EventListenerMap(Delegate* delegate) 117 EventListenerMap::EventListenerMap(Delegate* delegate)
118 : delegate_(delegate) { 118 : delegate_(delegate) {
119 } 119 }
120 120
121 EventListenerMap::~EventListenerMap() {} 121 EventListenerMap::~EventListenerMap() {}
122 122
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 return; 303 return;
304 event_filter_.RemoveEventMatcher(listener->matcher_id()); 304 event_filter_.RemoveEventMatcher(listener->matcher_id());
305 CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id())); 305 CHECK_EQ(1u, listeners_by_matcher_id_.erase(listener->matcher_id()));
306 } 306 }
307 307
308 bool EventListenerMap::IsFilteredEvent(const Event& event) const { 308 bool EventListenerMap::IsFilteredEvent(const Event& event) const {
309 return base::ContainsKey(filtered_events_, event.event_name); 309 return base::ContainsKey(filtered_events_, event.event_name);
310 } 310 }
311 311
312 } // namespace extensions 312 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/event_listener_map.h ('k') | extensions/browser/event_router.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698