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_function_dispatcher.h" | 5 #include "extensions/browser/extension_function_dispatcher.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
17 #include "base/process/process.h" | 17 #include "base/process/process.h" |
18 #include "base/profiler/scoped_profile.h" | 18 #include "base/profiler/scoped_profile.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
23 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
24 #include "content/public/browser/render_process_host_observer.h" | |
24 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
25 #include "content/public/browser/user_metrics.h" | 26 #include "content/public/browser/user_metrics.h" |
26 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_observer.h" | 28 #include "content/public/browser/web_contents_observer.h" |
28 #include "content/public/common/result_codes.h" | 29 #include "content/public/common/result_codes.h" |
29 #include "extensions/browser/api_activity_monitor.h" | 30 #include "extensions/browser/api_activity_monitor.h" |
30 #include "extensions/browser/extension_function_registry.h" | 31 #include "extensions/browser/extension_function_registry.h" |
31 #include "extensions/browser/extension_registry.h" | 32 #include "extensions/browser/extension_registry.h" |
32 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
33 #include "extensions/browser/extensions_browser_client.h" | 34 #include "extensions/browser/extensions_browser_client.h" |
34 #include "extensions/browser/io_thread_extension_message_filter.h" | 35 #include "extensions/browser/io_thread_extension_message_filter.h" |
35 #include "extensions/browser/process_manager.h" | 36 #include "extensions/browser/process_manager.h" |
36 #include "extensions/browser/process_map.h" | 37 #include "extensions/browser/process_map.h" |
37 #include "extensions/browser/quota_service.h" | 38 #include "extensions/browser/quota_service.h" |
38 #include "extensions/common/extension_api.h" | 39 #include "extensions/common/extension_api.h" |
39 #include "extensions/common/extension_messages.h" | 40 #include "extensions/common/extension_messages.h" |
40 #include "extensions/common/extension_set.h" | 41 #include "extensions/common/extension_set.h" |
42 #include "extensions/common/extensions_client.h" | |
41 #include "ipc/ipc_message.h" | 43 #include "ipc/ipc_message.h" |
42 #include "ipc/ipc_message_macros.h" | 44 #include "ipc/ipc_message_macros.h" |
43 | 45 |
44 using content::BrowserThread; | 46 using content::BrowserThread; |
45 using content::RenderViewHost; | 47 using content::RenderViewHost; |
46 | 48 |
47 namespace extensions { | 49 namespace extensions { |
48 namespace { | 50 namespace { |
49 | 51 |
50 // Notifies the ApiActivityMonitor that an extension API function has been | 52 // Notifies the ApiActivityMonitor that an extension API function has been |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 histogram_value); | 203 histogram_value); |
202 } | 204 } |
203 | 205 |
204 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; | 206 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; |
205 content::RenderFrameHost* render_frame_host_; | 207 content::RenderFrameHost* render_frame_host_; |
206 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; | 208 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; |
207 | 209 |
208 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); | 210 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); |
209 }; | 211 }; |
210 | 212 |
213 class ExtensionFunctionDispatcher::UIThreadWorkerResponseCallbackWrapper | |
214 : public content::RenderProcessHostObserver { | |
215 public: | |
216 UIThreadWorkerResponseCallbackWrapper( | |
217 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher, | |
218 int render_process_id, | |
219 int worker_thread_id) | |
220 : dispatcher_(dispatcher), | |
221 render_process_id_(render_process_id), | |
222 worker_thread_id_(worker_thread_id), | |
223 weak_ptr_factory_(this) { | |
224 content::RenderProcessHost::FromID(render_process_id_)->AddObserver(this); | |
225 DCHECK(ExtensionsClient::Get() | |
226 ->ExtensionAPIEnabledInExtensionServiceWorkers()); | |
227 } | |
228 | |
229 ~UIThreadWorkerResponseCallbackWrapper() override { | |
230 content::RenderProcessHost* rph = | |
231 content::RenderProcessHost::FromID(render_process_id_); | |
232 // We can be deleted from | |
233 // ExtensionFunctionDispatcher::RemoveWorkerCallbacksForProcess(), | |
234 // drop our reference from RenderProcessHost in that case. | |
235 if (rph) | |
236 rph->RemoveObserver(this); | |
Devlin
2016/04/15 21:06:19
Hmm... is this right? If this is the object that
lazyboy
2016/04/18 18:42:14
Done.
| |
237 } | |
238 | |
239 // content::RenderProcessHostObserver override. | |
240 void RenderProcessExited(content::RenderProcessHost* rph, | |
241 base::TerminationStatus status, | |
242 int exit_code) override { | |
243 rph->RemoveObserver(this); | |
244 CleanUp(); | |
245 } | |
246 | |
247 // content::RenderProcessHostObserver override. | |
248 void RenderProcessHostDestroyed(content::RenderProcessHost* rph) override { | |
249 rph->RemoveObserver(this); | |
250 CleanUp(); | |
251 } | |
252 | |
253 ExtensionFunction::ResponseCallback CreateCallback(int request_id) { | |
254 return base::Bind( | |
255 &UIThreadWorkerResponseCallbackWrapper::OnExtensionFunctionCompleted, | |
256 weak_ptr_factory_.GetWeakPtr(), request_id); | |
257 } | |
258 | |
259 private: | |
260 void CleanUp() { | |
261 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
262 if (dispatcher_) | |
263 dispatcher_->RemoveWorkerCallbacksForProcess(render_process_id_); | |
264 // Note: we are deleted here! | |
265 } | |
266 | |
267 void OnExtensionFunctionCompleted(int request_id, | |
268 ExtensionFunction::ResponseType type, | |
269 const base::ListValue& results, | |
270 const std::string& error, | |
271 functions::HistogramValue histogram_value) { | |
272 if (type == ExtensionFunction::BAD_MESSAGE) { | |
273 // TODO(lazyboy): Kill the offending process. | |
274 return; | |
275 } | |
276 content::RenderProcessHost* sender = | |
277 content::RenderProcessHost::FromID(render_process_id_); | |
278 DCHECK(sender); | |
279 sender->Send(new ExtensionMsg_ResponseWorker( | |
280 worker_thread_id_, request_id, type == ExtensionFunction::SUCCEEDED, | |
281 results, error)); | |
282 } | |
283 | |
284 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; | |
285 const int render_process_id_; | |
286 const int worker_thread_id_; | |
287 base::WeakPtrFactory<UIThreadWorkerResponseCallbackWrapper> weak_ptr_factory_; | |
288 | |
289 DISALLOW_COPY_AND_ASSIGN(UIThreadWorkerResponseCallbackWrapper); | |
290 }; | |
291 | |
211 WindowController* | 292 WindowController* |
212 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { | 293 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { |
213 return nullptr; | 294 return nullptr; |
214 } | 295 } |
215 | 296 |
216 content::WebContents* | 297 content::WebContents* |
217 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { | 298 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { |
218 return nullptr; | 299 return nullptr; |
219 } | 300 } |
220 | 301 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 | 381 |
301 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( | 382 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
302 content::BrowserContext* browser_context) | 383 content::BrowserContext* browser_context) |
303 : browser_context_(browser_context), delegate_(nullptr) {} | 384 : browser_context_(browser_context), delegate_(nullptr) {} |
304 | 385 |
305 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { | 386 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
306 } | 387 } |
307 | 388 |
308 void ExtensionFunctionDispatcher::Dispatch( | 389 void ExtensionFunctionDispatcher::Dispatch( |
309 const ExtensionHostMsg_Request_Params& params, | 390 const ExtensionHostMsg_Request_Params& params, |
310 content::RenderFrameHost* render_frame_host) { | 391 content::RenderFrameHost* render_frame_host, |
311 UIThreadResponseCallbackWrapperMap::const_iterator | 392 int render_process_id) { |
312 iter = ui_thread_response_callback_wrappers_.find(render_frame_host); | 393 if (render_frame_host) { |
313 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; | 394 // Extension API from a non Service Worker context, e.g. extension page, |
314 if (iter == ui_thread_response_callback_wrappers_.end()) { | 395 // background page, content script. |
315 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), | 396 UIThreadResponseCallbackWrapperMap::const_iterator iter = |
316 render_frame_host); | 397 ui_thread_response_callback_wrappers_.find(render_frame_host); |
317 ui_thread_response_callback_wrappers_[render_frame_host] = callback_wrapper; | 398 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; |
399 if (iter == ui_thread_response_callback_wrappers_.end()) { | |
400 callback_wrapper = | |
401 new UIThreadResponseCallbackWrapper(AsWeakPtr(), render_frame_host); | |
402 ui_thread_response_callback_wrappers_[render_frame_host] = | |
403 callback_wrapper; | |
404 } else { | |
405 callback_wrapper = iter->second; | |
406 } | |
407 DispatchWithCallbackInternal( | |
408 params, render_frame_host, render_process_id, | |
409 callback_wrapper->CreateCallback(params.request_id)); | |
318 } else { | 410 } else { |
319 callback_wrapper = iter->second; | 411 // Extension API from Service Worker. |
412 DCHECK_GE(params.embedded_worker_id, 0); | |
413 UIThreadWorkerResponseCallbackWrapperMapKey key = | |
414 std::make_pair(render_process_id, params.embedded_worker_id); | |
415 UIThreadWorkerResponseCallbackWrapperMap::const_iterator iter = | |
416 ui_thread_response_callback_wrappers_for_worker_.find(key); | |
417 UIThreadWorkerResponseCallbackWrapper* callback_wrapper = nullptr; | |
418 if (iter == ui_thread_response_callback_wrappers_for_worker_.end()) { | |
419 callback_wrapper = new UIThreadWorkerResponseCallbackWrapper( | |
420 AsWeakPtr(), render_process_id, params.worker_thread_id); | |
421 ui_thread_response_callback_wrappers_for_worker_[key] = | |
422 std::unique_ptr<UIThreadWorkerResponseCallbackWrapper>( | |
423 callback_wrapper); | |
424 } else { | |
425 callback_wrapper = iter->second.get(); | |
426 } | |
427 DispatchWithCallbackInternal( | |
428 params, nullptr, render_process_id, | |
429 callback_wrapper->CreateCallback(params.request_id)); | |
320 } | 430 } |
321 | |
322 DispatchWithCallbackInternal( | |
323 params, render_frame_host, | |
324 callback_wrapper->CreateCallback(params.request_id)); | |
325 } | 431 } |
326 | 432 |
327 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( | 433 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( |
328 const ExtensionHostMsg_Request_Params& params, | 434 const ExtensionHostMsg_Request_Params& params, |
329 content::RenderFrameHost* render_frame_host, | 435 content::RenderFrameHost* render_frame_host, |
436 int render_process_id, | |
330 const ExtensionFunction::ResponseCallback& callback) { | 437 const ExtensionFunction::ResponseCallback& callback) { |
331 DCHECK(render_frame_host); | |
332 // TODO(yzshen): There is some shared logic between this method and | 438 // TODO(yzshen): There is some shared logic between this method and |
333 // DispatchOnIOThread(). It is nice to deduplicate. | 439 // DispatchOnIOThread(). It is nice to deduplicate. |
334 ProcessMap* process_map = ProcessMap::Get(browser_context_); | 440 ProcessMap* process_map = ProcessMap::Get(browser_context_); |
335 if (!process_map) | 441 if (!process_map) |
336 return; | 442 return; |
337 | 443 |
338 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | 444 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); |
339 const Extension* extension = | 445 const Extension* extension = |
340 registry->enabled_extensions().GetByID(params.extension_id); | 446 registry->enabled_extensions().GetByID(params.extension_id); |
341 if (!extension) { | 447 if (!extension) { |
342 extension = | 448 extension = |
343 registry->enabled_extensions().GetHostedAppByURL(params.source_url); | 449 registry->enabled_extensions().GetHostedAppByURL(params.source_url); |
344 } | 450 } |
345 | 451 |
346 int process_id = render_frame_host->GetProcess()->GetID(); | 452 if (render_frame_host) |
347 scoped_refptr<ExtensionFunction> function( | 453 DCHECK_EQ(render_process_id, render_frame_host->GetProcess()->GetID()); |
348 CreateExtensionFunction(params, | 454 |
349 extension, | 455 scoped_refptr<ExtensionFunction> function(CreateExtensionFunction( |
350 process_id, | 456 params, extension, render_process_id, *process_map, |
351 *process_map, | 457 ExtensionAPI::GetSharedInstance(), browser_context_, callback)); |
352 ExtensionAPI::GetSharedInstance(), | |
353 browser_context_, | |
354 callback)); | |
355 if (!function.get()) | 458 if (!function.get()) |
356 return; | 459 return; |
357 | 460 |
358 UIThreadExtensionFunction* function_ui = | 461 UIThreadExtensionFunction* function_ui = |
359 function->AsUIThreadExtensionFunction(); | 462 function->AsUIThreadExtensionFunction(); |
360 if (!function_ui) { | 463 if (!function_ui) { |
361 NOTREACHED(); | 464 NOTREACHED(); |
362 return; | 465 return; |
363 } | 466 } |
364 function_ui->SetRenderFrameHost(render_frame_host); | 467 function_ui->SetRenderFrameHost(render_frame_host); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 // if function->Run() ended up closing the tab that owns us. | 514 // if function->Run() ended up closing the tab that owns us. |
412 | 515 |
413 // Check if extension was uninstalled by management.uninstall. | 516 // Check if extension was uninstalled by management.uninstall. |
414 if (!registry->enabled_extensions().GetByID(params.extension_id)) | 517 if (!registry->enabled_extensions().GetByID(params.extension_id)) |
415 return; | 518 return; |
416 | 519 |
417 // We only adjust the keepalive count for UIThreadExtensionFunction for | 520 // We only adjust the keepalive count for UIThreadExtensionFunction for |
418 // now, largely for simplicity's sake. This is OK because currently, only | 521 // now, largely for simplicity's sake. This is OK because currently, only |
419 // the webRequest API uses IOThreadExtensionFunction, and that API is not | 522 // the webRequest API uses IOThreadExtensionFunction, and that API is not |
420 // compatible with lazy background pages. | 523 // compatible with lazy background pages. |
524 // TODO(lazyboy): API functions from extension Service Worker will incorrectly | |
525 // change keepalive count below. | |
421 process_manager->IncrementLazyKeepaliveCount(extension); | 526 process_manager->IncrementLazyKeepaliveCount(extension); |
422 } | 527 } |
423 | 528 |
529 void ExtensionFunctionDispatcher::RemoveWorkerCallbacksForProcess( | |
530 int render_process_id) { | |
531 UIThreadWorkerResponseCallbackWrapperMap& map = | |
532 ui_thread_response_callback_wrappers_for_worker_; | |
533 for (UIThreadWorkerResponseCallbackWrapperMap::iterator it = map.begin(); | |
534 it != map.end();) { | |
535 if (it->first.first == render_process_id) { | |
536 it = map.erase(it); | |
537 continue; | |
538 } | |
539 ++it; | |
540 } | |
541 } | |
542 | |
424 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( | 543 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( |
425 const Extension* extension) { | 544 const Extension* extension) { |
545 // TODO(lazyboy): API functions from extension Service Worker will incorrectly | |
546 // change keepalive count below. | |
426 if (extension) { | 547 if (extension) { |
427 ProcessManager::Get(browser_context_) | 548 ProcessManager::Get(browser_context_) |
428 ->DecrementLazyKeepaliveCount(extension); | 549 ->DecrementLazyKeepaliveCount(extension); |
429 } | 550 } |
430 } | 551 } |
431 | 552 |
432 WindowController* | 553 WindowController* |
433 ExtensionFunctionDispatcher::GetExtensionWindowController() const { | 554 ExtensionFunctionDispatcher::GetExtensionWindowController() const { |
434 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; | 555 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; |
435 } | 556 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 // static | 615 // static |
495 void ExtensionFunctionDispatcher::SendAccessDenied( | 616 void ExtensionFunctionDispatcher::SendAccessDenied( |
496 const ExtensionFunction::ResponseCallback& callback, | 617 const ExtensionFunction::ResponseCallback& callback, |
497 functions::HistogramValue histogram_value) { | 618 functions::HistogramValue histogram_value) { |
498 base::ListValue empty_list; | 619 base::ListValue empty_list; |
499 callback.Run(ExtensionFunction::FAILED, empty_list, | 620 callback.Run(ExtensionFunction::FAILED, empty_list, |
500 "Access to extension API denied.", histogram_value); | 621 "Access to extension API denied.", histogram_value); |
501 } | 622 } |
502 | 623 |
503 } // namespace extensions | 624 } // namespace extensions |
OLD | NEW |