OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/extension_message_filter.h" | 5 #include "extensions/browser/extension_message_filter.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
9 #include "components/crx_file/id_util.h" | 9 #include "components/crx_file/id_util.h" |
10 #include "components/keyed_service/content/browser_context_keyed_service_shutdow
n_notifier_factory.h" | 10 #include "components/keyed_service/content/browser_context_keyed_service_shutdow
n_notifier_factory.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } | 83 } |
84 | 84 |
85 void ExtensionMessageFilter::OverrideThreadForMessage( | 85 void ExtensionMessageFilter::OverrideThreadForMessage( |
86 const IPC::Message& message, | 86 const IPC::Message& message, |
87 BrowserThread::ID* thread) { | 87 BrowserThread::ID* thread) { |
88 switch (message.type()) { | 88 switch (message.type()) { |
89 case ExtensionHostMsg_AddListener::ID: | 89 case ExtensionHostMsg_AddListener::ID: |
90 case ExtensionHostMsg_RemoveListener::ID: | 90 case ExtensionHostMsg_RemoveListener::ID: |
91 case ExtensionHostMsg_AddLazyListener::ID: | 91 case ExtensionHostMsg_AddLazyListener::ID: |
92 case ExtensionHostMsg_RemoveLazyListener::ID: | 92 case ExtensionHostMsg_RemoveLazyListener::ID: |
| 93 case ExtensionHostMsg_AddLazyServiceWorkerListener::ID: |
| 94 case ExtensionHostMsg_RemoveLazyServiceWorkerListener::ID: |
93 case ExtensionHostMsg_AddFilteredListener::ID: | 95 case ExtensionHostMsg_AddFilteredListener::ID: |
94 case ExtensionHostMsg_RemoveFilteredListener::ID: | 96 case ExtensionHostMsg_RemoveFilteredListener::ID: |
95 case ExtensionHostMsg_ShouldSuspendAck::ID: | 97 case ExtensionHostMsg_ShouldSuspendAck::ID: |
96 case ExtensionHostMsg_SuspendAck::ID: | 98 case ExtensionHostMsg_SuspendAck::ID: |
97 case ExtensionHostMsg_TransferBlobsAck::ID: | 99 case ExtensionHostMsg_TransferBlobsAck::ID: |
98 case ExtensionHostMsg_WakeEventPage::ID: | 100 case ExtensionHostMsg_WakeEventPage::ID: |
99 *thread = BrowserThread::UI; | 101 *thread = BrowserThread::UI; |
100 break; | 102 break; |
101 default: | 103 default: |
102 break; | 104 break; |
(...skipping 12 matching lines...) Expand all Loading... |
115 bool handled = true; | 117 bool handled = true; |
116 IPC_BEGIN_MESSAGE_MAP(ExtensionMessageFilter, message) | 118 IPC_BEGIN_MESSAGE_MAP(ExtensionMessageFilter, message) |
117 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddListener, | 119 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddListener, |
118 OnExtensionAddListener) | 120 OnExtensionAddListener) |
119 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveListener, | 121 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveListener, |
120 OnExtensionRemoveListener) | 122 OnExtensionRemoveListener) |
121 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddLazyListener, | 123 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddLazyListener, |
122 OnExtensionAddLazyListener) | 124 OnExtensionAddLazyListener) |
123 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveLazyListener, | 125 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveLazyListener, |
124 OnExtensionRemoveLazyListener) | 126 OnExtensionRemoveLazyListener) |
| 127 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddLazyServiceWorkerListener, |
| 128 OnExtensionAddLazyServiceWorkerListener); |
| 129 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveLazyServiceWorkerListener, |
| 130 OnExtensionRemoveLazyServiceWorkerListener); |
125 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddFilteredListener, | 131 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AddFilteredListener, |
126 OnExtensionAddFilteredListener) | 132 OnExtensionAddFilteredListener) |
127 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveFilteredListener, | 133 IPC_MESSAGE_HANDLER(ExtensionHostMsg_RemoveFilteredListener, |
128 OnExtensionRemoveFilteredListener) | 134 OnExtensionRemoveFilteredListener) |
129 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ShouldSuspendAck, | 135 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ShouldSuspendAck, |
130 OnExtensionShouldSuspendAck) | 136 OnExtensionShouldSuspendAck) |
131 IPC_MESSAGE_HANDLER(ExtensionHostMsg_SuspendAck, | 137 IPC_MESSAGE_HANDLER(ExtensionHostMsg_SuspendAck, |
132 OnExtensionSuspendAck) | 138 OnExtensionSuspendAck) |
133 IPC_MESSAGE_HANDLER(ExtensionHostMsg_TransferBlobsAck, | 139 IPC_MESSAGE_HANDLER(ExtensionHostMsg_TransferBlobsAck, |
134 OnExtensionTransferBlobsAck) | 140 OnExtensionTransferBlobsAck) |
135 IPC_MESSAGE_HANDLER(ExtensionHostMsg_WakeEventPage, | 141 IPC_MESSAGE_HANDLER(ExtensionHostMsg_WakeEventPage, |
136 OnExtensionWakeEventPage) | 142 OnExtensionWakeEventPage) |
137 IPC_MESSAGE_UNHANDLED(handled = false) | 143 IPC_MESSAGE_UNHANDLED(handled = false) |
138 IPC_END_MESSAGE_MAP() | 144 IPC_END_MESSAGE_MAP() |
139 return handled; | 145 return handled; |
140 } | 146 } |
141 | 147 |
142 void ExtensionMessageFilter::OnExtensionAddListener( | 148 void ExtensionMessageFilter::OnExtensionAddListener( |
143 const std::string& extension_id, | 149 const std::string& extension_id, |
144 const GURL& listener_url, | 150 const GURL& listener_or_worker_scope_url, |
145 const std::string& event_name, | 151 const std::string& event_name, |
146 int worker_thread_id) { | 152 int worker_thread_id) { |
147 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
148 if (!browser_context_) | 154 if (!browser_context_) |
149 return; | 155 return; |
150 | 156 |
151 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); | 157 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); |
152 if (!process) | 158 if (!process) |
153 return; | 159 return; |
154 | 160 |
155 EventRouter* event_router = GetEventRouter(); | 161 EventRouter* event_router = GetEventRouter(); |
156 if (crx_file::id_util::IdIsValid(extension_id)) { | 162 if (crx_file::id_util::IdIsValid(extension_id)) { |
157 if (worker_thread_id != kNonWorkerThreadId) { | 163 const bool is_service_worker_context = |
| 164 worker_thread_id != kNonWorkerThreadId; |
| 165 if (is_service_worker_context) { |
| 166 DCHECK(listener_or_worker_scope_url.is_valid()); |
158 event_router->AddServiceWorkerEventListener( | 167 event_router->AddServiceWorkerEventListener( |
159 event_name, process, extension_id, worker_thread_id); | 168 event_name, process, extension_id, listener_or_worker_scope_url, |
| 169 worker_thread_id); |
160 } else { | 170 } else { |
161 event_router->AddEventListener(event_name, process, extension_id); | 171 event_router->AddEventListener(event_name, process, extension_id); |
162 } | 172 } |
163 } else if (listener_url.is_valid()) { | 173 } else if (listener_or_worker_scope_url.is_valid()) { |
164 event_router->AddEventListenerForURL(event_name, process, listener_url); | 174 event_router->AddEventListenerForURL(event_name, process, |
| 175 listener_or_worker_scope_url); |
165 } else { | 176 } else { |
166 NOTREACHED() << "Tried to add an event listener without a valid " | 177 NOTREACHED() << "Tried to add an event listener without a valid " |
167 << "extension ID nor listener URL"; | 178 << "extension ID nor listener URL"; |
168 } | 179 } |
169 } | 180 } |
170 | 181 |
171 void ExtensionMessageFilter::OnExtensionRemoveListener( | 182 void ExtensionMessageFilter::OnExtensionRemoveListener( |
172 const std::string& extension_id, | 183 const std::string& extension_id, |
173 const GURL& listener_url, | 184 const GURL& listener_or_worker_scope_url, |
174 const std::string& event_name, | 185 const std::string& event_name, |
175 int worker_thread_id) { | 186 int worker_thread_id) { |
176 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 187 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
177 if (!browser_context_) | 188 if (!browser_context_) |
178 return; | 189 return; |
179 | 190 |
180 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); | 191 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id_); |
181 if (!process) | 192 if (!process) |
182 return; | 193 return; |
183 | 194 |
184 if (crx_file::id_util::IdIsValid(extension_id)) { | 195 if (crx_file::id_util::IdIsValid(extension_id)) { |
185 if (worker_thread_id != kNonWorkerThreadId) { | 196 const bool is_service_worker_context = |
| 197 worker_thread_id != kNonWorkerThreadId; |
| 198 if (is_service_worker_context) { |
| 199 DCHECK(listener_or_worker_scope_url.is_valid()); |
186 GetEventRouter()->RemoveServiceWorkerEventListener( | 200 GetEventRouter()->RemoveServiceWorkerEventListener( |
187 event_name, process, extension_id, worker_thread_id); | 201 event_name, process, extension_id, listener_or_worker_scope_url, |
| 202 worker_thread_id); |
188 } else { | 203 } else { |
189 GetEventRouter()->RemoveEventListener(event_name, process, extension_id); | 204 GetEventRouter()->RemoveEventListener(event_name, process, extension_id); |
190 } | 205 } |
191 } else if (listener_url.is_valid()) { | 206 } else if (listener_or_worker_scope_url.is_valid()) { |
192 GetEventRouter()->RemoveEventListenerForURL(event_name, process, | 207 GetEventRouter()->RemoveEventListenerForURL(event_name, process, |
193 listener_url); | 208 listener_or_worker_scope_url); |
194 } else { | 209 } else { |
195 NOTREACHED() << "Tried to remove an event listener without a valid " | 210 NOTREACHED() << "Tried to remove an event listener without a valid " |
196 << "extension ID nor listener URL"; | 211 << "extension ID nor listener URL"; |
197 } | 212 } |
198 } | 213 } |
199 | 214 |
200 void ExtensionMessageFilter::OnExtensionAddLazyListener( | 215 void ExtensionMessageFilter::OnExtensionAddLazyListener( |
201 const std::string& extension_id, | 216 const std::string& extension_id, |
| 217 const std::string& event_name) { |
| 218 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 219 if (!browser_context_) |
| 220 return; |
| 221 GetEventRouter()->AddLazyEventListener(event_name, extension_id); |
| 222 } |
| 223 |
| 224 void ExtensionMessageFilter::OnExtensionAddLazyServiceWorkerListener( |
| 225 const std::string& extension_id, |
202 const std::string& event_name, | 226 const std::string& event_name, |
203 int worker_thread_id) { | 227 const GURL& service_worker_scope) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 228 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
205 if (!browser_context_) | 229 if (!browser_context_) |
206 return; | 230 return; |
207 | 231 |
208 if (worker_thread_id == kNonWorkerThreadId) { | 232 GetEventRouter()->AddLazyServiceWorkerEventListener(event_name, extension_id, |
209 GetEventRouter()->AddLazyEventListener(event_name, extension_id); | 233 service_worker_scope); |
210 } else { | |
211 GetEventRouter()->AddLazyServiceWorkerEventListener( | |
212 event_name, extension_id, worker_thread_id); | |
213 } | |
214 } | 234 } |
215 | 235 |
216 void ExtensionMessageFilter::OnExtensionRemoveLazyListener( | 236 void ExtensionMessageFilter::OnExtensionRemoveLazyListener( |
217 const std::string& extension_id, | 237 const std::string& extension_id, |
218 const std::string& event_name, | 238 const std::string& event_name) { |
219 int worker_thread_id) { | |
220 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 239 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
221 if (!browser_context_) | 240 if (!browser_context_) |
222 return; | 241 return; |
223 | 242 |
224 if (worker_thread_id == kNonWorkerThreadId) { | 243 GetEventRouter()->RemoveLazyEventListener(event_name, extension_id); |
225 GetEventRouter()->RemoveLazyEventListener(event_name, extension_id); | 244 } |
226 } else { | 245 |
227 GetEventRouter()->RemoveLazyServiceWorkerEventListener( | 246 void ExtensionMessageFilter::OnExtensionRemoveLazyServiceWorkerListener( |
228 event_name, extension_id, worker_thread_id); | 247 const std::string& extension_id, |
229 } | 248 const std::string& event_name, |
| 249 const GURL& worker_scope_url) { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 251 if (!browser_context_) |
| 252 return; |
| 253 |
| 254 GetEventRouter()->RemoveLazyServiceWorkerEventListener( |
| 255 event_name, extension_id, worker_scope_url); |
230 } | 256 } |
231 | 257 |
232 void ExtensionMessageFilter::OnExtensionAddFilteredListener( | 258 void ExtensionMessageFilter::OnExtensionAddFilteredListener( |
233 const std::string& extension_id, | 259 const std::string& extension_id, |
234 const std::string& event_name, | 260 const std::string& event_name, |
235 const base::DictionaryValue& filter, | 261 const base::DictionaryValue& filter, |
236 bool lazy) { | 262 bool lazy) { |
237 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 263 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
238 if (!browser_context_) | 264 if (!browser_context_) |
239 return; | 265 return; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 // The extension has no background page, so there is nothing to wake. | 365 // The extension has no background page, so there is nothing to wake. |
340 SendWakeEventPageResponse(request_id, false); | 366 SendWakeEventPageResponse(request_id, false); |
341 } | 367 } |
342 | 368 |
343 void ExtensionMessageFilter::SendWakeEventPageResponse(int request_id, | 369 void ExtensionMessageFilter::SendWakeEventPageResponse(int request_id, |
344 bool success) { | 370 bool success) { |
345 Send(new ExtensionMsg_WakeEventPageResponse(request_id, success)); | 371 Send(new ExtensionMsg_WakeEventPageResponse(request_id, success)); |
346 } | 372 } |
347 | 373 |
348 } // namespace extensions | 374 } // namespace extensions |
OLD | NEW |