OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/events/lazy_event_dispatcher.h" | 5 #include "extensions/browser/events/lazy_event_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "extensions/browser/event_router.h" | 9 #include "extensions/browser/event_router.h" |
10 #include "extensions/browser/extension_registry.h" | 10 #include "extensions/browser/extension_registry.h" |
11 #include "extensions/browser/extensions_browser_client.h" | 11 #include "extensions/browser/extensions_browser_client.h" |
12 #include "extensions/browser/lazy_background_task_queue.h" | 12 #include "extensions/browser/lazy_background_task_queue.h" |
13 #include "extensions/browser/lazy_context_id.h" | 13 #include "extensions/browser/lazy_context_id.h" |
| 14 #include "extensions/browser/service_worker_task_queue.h" |
14 #include "extensions/common/manifest_handlers/incognito_info.h" | 15 #include "extensions/common/manifest_handlers/incognito_info.h" |
15 | 16 |
16 using content::BrowserContext; | 17 using content::BrowserContext; |
17 | 18 |
18 namespace extensions { | 19 namespace extensions { |
19 | 20 |
20 LazyEventDispatcher::LazyEventDispatcher( | 21 LazyEventDispatcher::LazyEventDispatcher( |
21 BrowserContext* browser_context, | 22 BrowserContext* browser_context, |
22 const linked_ptr<Event>& event, | 23 const linked_ptr<Event>& event, |
23 const DispatchFunction& dispatch_function) | 24 const DispatchFunction& dispatch_function) |
24 : browser_context_(browser_context), | 25 : browser_context_(browser_context), |
25 event_(event), | 26 event_(event), |
26 dispatch_function_(dispatch_function) {} | 27 dispatch_function_(dispatch_function) {} |
27 | 28 |
28 LazyEventDispatcher::~LazyEventDispatcher() {} | 29 LazyEventDispatcher::~LazyEventDispatcher() {} |
29 | 30 |
30 void LazyEventDispatcher::DispatchToEventPage( | 31 void LazyEventDispatcher::DispatchToEventPage( |
31 const ExtensionId& extension_id, | 32 const ExtensionId& extension_id, |
32 const base::DictionaryValue* listener_filter) { | 33 const base::DictionaryValue* listener_filter) { |
33 LazyContextId dispatch_context(browser_context_, extension_id); | 34 LazyContextId dispatch_context(browser_context_, extension_id); |
34 DispatchToLazyContext(&dispatch_context, listener_filter); | 35 DispatchToLazyContext(&dispatch_context, listener_filter); |
35 } | 36 } |
36 | 37 |
| 38 void LazyEventDispatcher::DispatchToServiceWorker( |
| 39 const ExtensionId& extension_id, |
| 40 const GURL& service_worker_scope, |
| 41 const base::DictionaryValue* listener_filter) { |
| 42 LazyContextId dispatch_context(browser_context_, extension_id, |
| 43 service_worker_scope); |
| 44 DispatchToLazyContext(&dispatch_context, listener_filter); |
| 45 } |
| 46 |
37 bool LazyEventDispatcher::HasAlreadyDispatched( | 47 bool LazyEventDispatcher::HasAlreadyDispatched( |
38 BrowserContext* context, | 48 BrowserContext* context, |
39 const EventListener* listener) const { | 49 const EventListener* listener) const { |
40 auto dispatch_context = | 50 std::unique_ptr<LazyContextId> dispatch_context; |
41 base::MakeUnique<LazyContextId>(context, listener->extension_id()); | 51 if (listener->is_for_service_worker()) { |
| 52 dispatch_context = base::MakeUnique<LazyContextId>( |
| 53 context, listener->extension_id(), listener->listener_url()); |
| 54 } else { |
| 55 dispatch_context = |
| 56 base::MakeUnique<LazyContextId>(context, listener->extension_id()); |
| 57 } |
| 58 |
42 return HasAlreadyDispatchedImpl(dispatch_context.get()); | 59 return HasAlreadyDispatchedImpl(dispatch_context.get()); |
43 } | 60 } |
44 | 61 |
45 void LazyEventDispatcher::DispatchToLazyContext( | 62 void LazyEventDispatcher::DispatchToLazyContext( |
46 LazyContextId* dispatch_context, | 63 LazyContextId* dispatch_context, |
47 const base::DictionaryValue* listener_filter) { | 64 const base::DictionaryValue* listener_filter) { |
48 const Extension* extension = ExtensionRegistry::Get(browser_context_) | 65 const Extension* extension = ExtensionRegistry::Get(browser_context_) |
49 ->enabled_extensions() | 66 ->enabled_extensions() |
50 .GetByID(dispatch_context->extension_id()); | 67 .GetByID(dispatch_context->extension_id()); |
51 if (!extension) | 68 if (!extension) |
(...skipping 20 matching lines...) Expand all Loading... |
72 const Extension* extension, | 89 const Extension* extension, |
73 const base::DictionaryValue* listener_filter) { | 90 const base::DictionaryValue* listener_filter) { |
74 if (!EventRouter::CanDispatchEventToBrowserContext( | 91 if (!EventRouter::CanDispatchEventToBrowserContext( |
75 dispatch_context->browser_context(), extension, *event_)) { | 92 dispatch_context->browser_context(), extension, *event_)) { |
76 return false; | 93 return false; |
77 } | 94 } |
78 | 95 |
79 if (HasAlreadyDispatchedImpl(dispatch_context)) | 96 if (HasAlreadyDispatchedImpl(dispatch_context)) |
80 return false; | 97 return false; |
81 | 98 |
82 LazyBackgroundTaskQueue* queue = dispatch_context->GetTaskQueue(); | 99 LazyContextTaskQueue* queue = dispatch_context->GetTaskQueue(); |
83 if (!queue->ShouldEnqueueTask(dispatch_context->browser_context(), | 100 if (!queue->ShouldEnqueueTask(dispatch_context->browser_context(), |
84 extension)) { | 101 extension)) { |
85 return false; | 102 return false; |
86 } | 103 } |
87 | 104 |
88 linked_ptr<Event> dispatched_event(event_); | 105 linked_ptr<Event> dispatched_event(event_); |
89 | 106 |
90 // If there's a dispatch callback, call it now (rather than dispatch time) | 107 // If there's a dispatch callback, call it now (rather than dispatch time) |
91 // to avoid lifetime issues. Use a separate copy of the event args, so they | 108 // to avoid lifetime issues. Use a separate copy of the event args, so they |
92 // last until the event is dispatched. | 109 // last until the event is dispatched. |
93 if (!event_->will_dispatch_callback.is_null()) { | 110 if (!event_->will_dispatch_callback.is_null()) { |
94 dispatched_event.reset(event_->DeepCopy()); | 111 dispatched_event.reset(event_->DeepCopy()); |
95 if (!dispatched_event->will_dispatch_callback.Run( | 112 if (!dispatched_event->will_dispatch_callback.Run( |
96 dispatch_context->browser_context(), extension, | 113 dispatch_context->browser_context(), extension, |
97 dispatched_event.get(), listener_filter)) { | 114 dispatched_event.get(), listener_filter)) { |
98 // The event has been canceled. | 115 // The event has been canceled. |
99 return true; | 116 return true; |
100 } | 117 } |
101 // Ensure we don't call it again at dispatch time. | 118 // Ensure we don't call it again at dispatch time. |
102 dispatched_event->will_dispatch_callback.Reset(); | 119 dispatched_event->will_dispatch_callback.Reset(); |
103 } | 120 } |
104 | 121 |
105 queue->AddPendingTask(dispatch_context->browser_context(), | 122 queue->AddPendingTaskToDispatchEvent( |
106 dispatch_context->extension_id(), | 123 dispatch_context, base::Bind(dispatch_function_, dispatched_event)); |
107 base::Bind(dispatch_function_, dispatched_event)); | |
108 | 124 |
109 return true; | 125 return true; |
110 } | 126 } |
111 | 127 |
112 bool LazyEventDispatcher::HasAlreadyDispatchedImpl( | 128 bool LazyEventDispatcher::HasAlreadyDispatchedImpl( |
113 const LazyContextId* dispatch_context) const { | 129 const LazyContextId* dispatch_context) const { |
| 130 if (dispatch_context->is_for_service_worker()) { |
| 131 ServiceWorkerDispatchIdentifier dispatch_id( |
| 132 dispatch_context->browser_context(), |
| 133 dispatch_context->service_worker_scope()); |
| 134 return base::ContainsKey(dispatched_ids_for_service_worker_, dispatch_id); |
| 135 } |
114 DCHECK(dispatch_context->is_for_event_page()); | 136 DCHECK(dispatch_context->is_for_event_page()); |
115 EventPageDispatchIdentifier dispatch_id(dispatch_context->browser_context(), | 137 EventPageDispatchIdentifier dispatch_id(dispatch_context->browser_context(), |
116 dispatch_context->extension_id()); | 138 dispatch_context->extension_id()); |
117 return base::ContainsKey(dispatched_ids_for_event_page_, dispatch_id); | 139 return base::ContainsKey(dispatched_ids_for_event_page_, dispatch_id); |
118 } | 140 } |
119 | 141 |
120 void LazyEventDispatcher::RecordAlreadyDispatched( | 142 void LazyEventDispatcher::RecordAlreadyDispatched( |
121 LazyContextId* dispatch_context) { | 143 LazyContextId* dispatch_context) { |
| 144 if (dispatch_context->is_for_service_worker()) { |
| 145 dispatched_ids_for_service_worker_.insert( |
| 146 std::make_pair(dispatch_context->browser_context(), |
| 147 dispatch_context->service_worker_scope())); |
| 148 return; |
| 149 } |
122 DCHECK(dispatch_context->is_for_event_page()); | 150 DCHECK(dispatch_context->is_for_event_page()); |
123 dispatched_ids_for_event_page_.insert(std::make_pair( | 151 dispatched_ids_for_event_page_.insert(std::make_pair( |
124 dispatch_context->browser_context(), dispatch_context->extension_id())); | 152 dispatch_context->browser_context(), dispatch_context->extension_id())); |
125 } | 153 } |
126 | 154 |
127 BrowserContext* LazyEventDispatcher::GetIncognitoContext( | 155 BrowserContext* LazyEventDispatcher::GetIncognitoContext( |
128 const Extension* extension) { | 156 const Extension* extension) { |
129 if (!IncognitoInfo::IsSplitMode(extension)) | 157 if (!IncognitoInfo::IsSplitMode(extension)) |
130 return nullptr; | 158 return nullptr; |
131 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get(); | 159 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get(); |
132 if (!browser_client->HasOffTheRecordContext(browser_context_)) | 160 if (!browser_client->HasOffTheRecordContext(browser_context_)) |
133 return nullptr; | 161 return nullptr; |
134 return browser_client->GetOffTheRecordContext(browser_context_); | 162 return browser_client->GetOffTheRecordContext(browser_context_); |
135 } | 163 } |
136 | 164 |
137 } // namespace extensions | 165 } // namespace extensions |
OLD | NEW |