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/scoped_observer.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/render_frame_host.h" | 23 #include "content/public/browser/render_frame_host.h" |
23 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 25 #include "content/public/browser/render_process_host_observer.h" |
24 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
25 #include "content/public/browser/user_metrics.h" | 27 #include "content/public/browser/user_metrics.h" |
26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_observer.h" | 29 #include "content/public/browser/web_contents_observer.h" |
28 #include "content/public/common/result_codes.h" | 30 #include "content/public/common/result_codes.h" |
29 #include "extensions/browser/api_activity_monitor.h" | 31 #include "extensions/browser/api_activity_monitor.h" |
30 #include "extensions/browser/extension_function_registry.h" | 32 #include "extensions/browser/extension_function_registry.h" |
31 #include "extensions/browser/extension_registry.h" | 33 #include "extensions/browser/extension_registry.h" |
32 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
33 #include "extensions/browser/extensions_browser_client.h" | 35 #include "extensions/browser/extensions_browser_client.h" |
34 #include "extensions/browser/io_thread_extension_message_filter.h" | 36 #include "extensions/browser/io_thread_extension_message_filter.h" |
35 #include "extensions/browser/process_manager.h" | 37 #include "extensions/browser/process_manager.h" |
36 #include "extensions/browser/process_map.h" | 38 #include "extensions/browser/process_map.h" |
37 #include "extensions/browser/quota_service.h" | 39 #include "extensions/browser/quota_service.h" |
38 #include "extensions/common/extension_api.h" | 40 #include "extensions/common/extension_api.h" |
39 #include "extensions/common/extension_messages.h" | 41 #include "extensions/common/extension_messages.h" |
40 #include "extensions/common/extension_set.h" | 42 #include "extensions/common/extension_set.h" |
| 43 #include "extensions/common/extensions_client.h" |
41 #include "ipc/ipc_message.h" | 44 #include "ipc/ipc_message.h" |
42 #include "ipc/ipc_message_macros.h" | 45 #include "ipc/ipc_message_macros.h" |
43 | 46 |
44 using content::BrowserThread; | 47 using content::BrowserThread; |
45 using content::RenderViewHost; | 48 using content::RenderViewHost; |
46 | 49 |
47 namespace extensions { | 50 namespace extensions { |
48 namespace { | 51 namespace { |
49 | 52 |
50 // Notifies the ApiActivityMonitor that an extension API function has been | 53 // 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); | 204 histogram_value); |
202 } | 205 } |
203 | 206 |
204 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; | 207 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; |
205 content::RenderFrameHost* render_frame_host_; | 208 content::RenderFrameHost* render_frame_host_; |
206 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; | 209 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; |
207 | 210 |
208 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); | 211 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); |
209 }; | 212 }; |
210 | 213 |
| 214 class ExtensionFunctionDispatcher::UIThreadWorkerResponseCallbackWrapper |
| 215 : public content::RenderProcessHostObserver { |
| 216 public: |
| 217 UIThreadWorkerResponseCallbackWrapper( |
| 218 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher, |
| 219 int render_process_id, |
| 220 int worker_thread_id) |
| 221 : dispatcher_(dispatcher), |
| 222 observer_(this), |
| 223 render_process_id_(render_process_id), |
| 224 worker_thread_id_(worker_thread_id), |
| 225 weak_ptr_factory_(this) { |
| 226 observer_.Add(content::RenderProcessHost::FromID(render_process_id_)); |
| 227 DCHECK(ExtensionsClient::Get() |
| 228 ->ExtensionAPIEnabledInExtensionServiceWorkers()); |
| 229 } |
| 230 |
| 231 ~UIThreadWorkerResponseCallbackWrapper() override {} |
| 232 |
| 233 // content::RenderProcessHostObserver override. |
| 234 void RenderProcessExited(content::RenderProcessHost* rph, |
| 235 base::TerminationStatus status, |
| 236 int exit_code) override { |
| 237 CleanUp(); |
| 238 } |
| 239 |
| 240 // content::RenderProcessHostObserver override. |
| 241 void RenderProcessHostDestroyed(content::RenderProcessHost* rph) override { |
| 242 CleanUp(); |
| 243 } |
| 244 |
| 245 ExtensionFunction::ResponseCallback CreateCallback(int request_id) { |
| 246 return base::Bind( |
| 247 &UIThreadWorkerResponseCallbackWrapper::OnExtensionFunctionCompleted, |
| 248 weak_ptr_factory_.GetWeakPtr(), request_id); |
| 249 } |
| 250 |
| 251 private: |
| 252 void CleanUp() { |
| 253 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 254 if (dispatcher_) |
| 255 dispatcher_->RemoveWorkerCallbacksForProcess(render_process_id_); |
| 256 // Note: we are deleted here! |
| 257 } |
| 258 |
| 259 void OnExtensionFunctionCompleted(int request_id, |
| 260 ExtensionFunction::ResponseType type, |
| 261 const base::ListValue& results, |
| 262 const std::string& error, |
| 263 functions::HistogramValue histogram_value) { |
| 264 if (type == ExtensionFunction::BAD_MESSAGE) { |
| 265 // TODO(lazyboy): Kill the offending process. |
| 266 return; |
| 267 } |
| 268 content::RenderProcessHost* sender = |
| 269 content::RenderProcessHost::FromID(render_process_id_); |
| 270 DCHECK(sender); |
| 271 sender->Send(new ExtensionMsg_ResponseWorker( |
| 272 worker_thread_id_, request_id, type == ExtensionFunction::SUCCEEDED, |
| 273 results, error)); |
| 274 } |
| 275 |
| 276 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; |
| 277 ScopedObserver<content::RenderProcessHost, |
| 278 UIThreadWorkerResponseCallbackWrapper> |
| 279 observer_; |
| 280 const int render_process_id_; |
| 281 const int worker_thread_id_; |
| 282 base::WeakPtrFactory<UIThreadWorkerResponseCallbackWrapper> weak_ptr_factory_; |
| 283 |
| 284 DISALLOW_COPY_AND_ASSIGN(UIThreadWorkerResponseCallbackWrapper); |
| 285 }; |
| 286 |
| 287 struct ExtensionFunctionDispatcher::WorkerResponseCallbackMapKey { |
| 288 WorkerResponseCallbackMapKey(int render_process_id, int embedded_worker_id) |
| 289 : render_process_id(render_process_id), |
| 290 embedded_worker_id(embedded_worker_id) {} |
| 291 |
| 292 bool operator<(const WorkerResponseCallbackMapKey& other) const { |
| 293 return std::tie(render_process_id, embedded_worker_id) < |
| 294 std::tie(other.render_process_id, other.embedded_worker_id); |
| 295 } |
| 296 |
| 297 int render_process_id; |
| 298 int embedded_worker_id; |
| 299 }; |
| 300 |
211 WindowController* | 301 WindowController* |
212 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { | 302 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { |
213 return nullptr; | 303 return nullptr; |
214 } | 304 } |
215 | 305 |
216 content::WebContents* | 306 content::WebContents* |
217 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { | 307 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { |
218 return nullptr; | 308 return nullptr; |
219 } | 309 } |
220 | 310 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 | 399 |
310 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( | 400 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
311 content::BrowserContext* browser_context) | 401 content::BrowserContext* browser_context) |
312 : browser_context_(browser_context), delegate_(nullptr) {} | 402 : browser_context_(browser_context), delegate_(nullptr) {} |
313 | 403 |
314 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { | 404 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
315 } | 405 } |
316 | 406 |
317 void ExtensionFunctionDispatcher::Dispatch( | 407 void ExtensionFunctionDispatcher::Dispatch( |
318 const ExtensionHostMsg_Request_Params& params, | 408 const ExtensionHostMsg_Request_Params& params, |
319 content::RenderFrameHost* render_frame_host) { | 409 content::RenderFrameHost* render_frame_host, |
320 UIThreadResponseCallbackWrapperMap::const_iterator | 410 int render_process_id) { |
321 iter = ui_thread_response_callback_wrappers_.find(render_frame_host); | 411 if (render_frame_host) { |
322 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; | 412 // Extension API from a non Service Worker context, e.g. extension page, |
323 if (iter == ui_thread_response_callback_wrappers_.end()) { | 413 // background page, content script. |
324 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), | 414 UIThreadResponseCallbackWrapperMap::const_iterator iter = |
325 render_frame_host); | 415 ui_thread_response_callback_wrappers_.find(render_frame_host); |
326 ui_thread_response_callback_wrappers_[render_frame_host] = callback_wrapper; | 416 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; |
| 417 if (iter == ui_thread_response_callback_wrappers_.end()) { |
| 418 callback_wrapper = |
| 419 new UIThreadResponseCallbackWrapper(AsWeakPtr(), render_frame_host); |
| 420 ui_thread_response_callback_wrappers_[render_frame_host] = |
| 421 callback_wrapper; |
| 422 } else { |
| 423 callback_wrapper = iter->second; |
| 424 } |
| 425 DispatchWithCallbackInternal( |
| 426 params, render_frame_host, render_process_id, |
| 427 callback_wrapper->CreateCallback(params.request_id)); |
327 } else { | 428 } else { |
328 callback_wrapper = iter->second; | 429 // Extension API from Service Worker. |
| 430 DCHECK_GE(params.embedded_worker_id, 0); |
| 431 WorkerResponseCallbackMapKey key(render_process_id, |
| 432 params.embedded_worker_id); |
| 433 UIThreadWorkerResponseCallbackWrapperMap::const_iterator iter = |
| 434 ui_thread_response_callback_wrappers_for_worker_.find(key); |
| 435 UIThreadWorkerResponseCallbackWrapper* callback_wrapper = nullptr; |
| 436 if (iter == ui_thread_response_callback_wrappers_for_worker_.end()) { |
| 437 callback_wrapper = new UIThreadWorkerResponseCallbackWrapper( |
| 438 AsWeakPtr(), render_process_id, params.worker_thread_id); |
| 439 ui_thread_response_callback_wrappers_for_worker_[key] = |
| 440 base::WrapUnique(callback_wrapper); |
| 441 } else { |
| 442 callback_wrapper = iter->second.get(); |
| 443 } |
| 444 DispatchWithCallbackInternal( |
| 445 params, nullptr, render_process_id, |
| 446 callback_wrapper->CreateCallback(params.request_id)); |
329 } | 447 } |
330 | |
331 DispatchWithCallbackInternal( | |
332 params, render_frame_host, | |
333 callback_wrapper->CreateCallback(params.request_id)); | |
334 } | 448 } |
335 | 449 |
336 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( | 450 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( |
337 const ExtensionHostMsg_Request_Params& params, | 451 const ExtensionHostMsg_Request_Params& params, |
338 content::RenderFrameHost* render_frame_host, | 452 content::RenderFrameHost* render_frame_host, |
| 453 int render_process_id, |
339 const ExtensionFunction::ResponseCallback& callback) { | 454 const ExtensionFunction::ResponseCallback& callback) { |
340 DCHECK(render_frame_host); | |
341 // TODO(yzshen): There is some shared logic between this method and | 455 // TODO(yzshen): There is some shared logic between this method and |
342 // DispatchOnIOThread(). It is nice to deduplicate. | 456 // DispatchOnIOThread(). It is nice to deduplicate. |
343 ProcessMap* process_map = ProcessMap::Get(browser_context_); | 457 ProcessMap* process_map = ProcessMap::Get(browser_context_); |
344 if (!process_map) | 458 if (!process_map) |
345 return; | 459 return; |
346 | 460 |
347 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | 461 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); |
348 const Extension* extension = | 462 const Extension* extension = |
349 registry->enabled_extensions().GetByID(params.extension_id); | 463 registry->enabled_extensions().GetByID(params.extension_id); |
350 if (!extension) { | 464 if (!extension) { |
351 extension = | 465 extension = |
352 registry->enabled_extensions().GetHostedAppByURL(params.source_url); | 466 registry->enabled_extensions().GetHostedAppByURL(params.source_url); |
353 } | 467 } |
354 | 468 |
355 int process_id = render_frame_host->GetProcess()->GetID(); | 469 if (render_frame_host) |
356 scoped_refptr<ExtensionFunction> function( | 470 DCHECK_EQ(render_process_id, render_frame_host->GetProcess()->GetID()); |
357 CreateExtensionFunction(params, | 471 |
358 extension, | 472 scoped_refptr<ExtensionFunction> function(CreateExtensionFunction( |
359 process_id, | 473 params, extension, render_process_id, *process_map, |
360 *process_map, | 474 ExtensionAPI::GetSharedInstance(), browser_context_, callback)); |
361 ExtensionAPI::GetSharedInstance(), | |
362 browser_context_, | |
363 callback)); | |
364 if (!function.get()) | 475 if (!function.get()) |
365 return; | 476 return; |
366 | 477 |
367 UIThreadExtensionFunction* function_ui = | 478 UIThreadExtensionFunction* function_ui = |
368 function->AsUIThreadExtensionFunction(); | 479 function->AsUIThreadExtensionFunction(); |
369 if (!function_ui) { | 480 if (!function_ui) { |
370 NOTREACHED(); | 481 NOTREACHED(); |
371 return; | 482 return; |
372 } | 483 } |
373 function_ui->SetRenderFrameHost(render_frame_host); | 484 function_ui->SetRenderFrameHost(render_frame_host); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 // if function->Run() ended up closing the tab that owns us. | 540 // if function->Run() ended up closing the tab that owns us. |
430 | 541 |
431 // Check if extension was uninstalled by management.uninstall. | 542 // Check if extension was uninstalled by management.uninstall. |
432 if (!registry->enabled_extensions().GetByID(params.extension_id)) | 543 if (!registry->enabled_extensions().GetByID(params.extension_id)) |
433 return; | 544 return; |
434 | 545 |
435 // We only adjust the keepalive count for UIThreadExtensionFunction for | 546 // We only adjust the keepalive count for UIThreadExtensionFunction for |
436 // now, largely for simplicity's sake. This is OK because currently, only | 547 // now, largely for simplicity's sake. This is OK because currently, only |
437 // the webRequest API uses IOThreadExtensionFunction, and that API is not | 548 // the webRequest API uses IOThreadExtensionFunction, and that API is not |
438 // compatible with lazy background pages. | 549 // compatible with lazy background pages. |
| 550 // TODO(lazyboy): API functions from extension Service Worker will incorrectly |
| 551 // change keepalive count below. |
439 process_manager->IncrementLazyKeepaliveCount(extension); | 552 process_manager->IncrementLazyKeepaliveCount(extension); |
440 } | 553 } |
441 | 554 |
| 555 void ExtensionFunctionDispatcher::RemoveWorkerCallbacksForProcess( |
| 556 int render_process_id) { |
| 557 UIThreadWorkerResponseCallbackWrapperMap& map = |
| 558 ui_thread_response_callback_wrappers_for_worker_; |
| 559 for (UIThreadWorkerResponseCallbackWrapperMap::iterator it = map.begin(); |
| 560 it != map.end();) { |
| 561 if (it->first.render_process_id == render_process_id) { |
| 562 it = map.erase(it); |
| 563 continue; |
| 564 } |
| 565 ++it; |
| 566 } |
| 567 } |
| 568 |
442 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( | 569 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( |
443 const Extension* extension) { | 570 const Extension* extension) { |
| 571 // TODO(lazyboy): API functions from extension Service Worker will incorrectly |
| 572 // change keepalive count below. |
444 if (extension) { | 573 if (extension) { |
445 ProcessManager::Get(browser_context_) | 574 ProcessManager::Get(browser_context_) |
446 ->DecrementLazyKeepaliveCount(extension); | 575 ->DecrementLazyKeepaliveCount(extension); |
447 } | 576 } |
448 } | 577 } |
449 | 578 |
450 WindowController* | 579 WindowController* |
451 ExtensionFunctionDispatcher::GetExtensionWindowController() const { | 580 ExtensionFunctionDispatcher::GetExtensionWindowController() const { |
452 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; | 581 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; |
453 } | 582 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 // static | 641 // static |
513 void ExtensionFunctionDispatcher::SendAccessDenied( | 642 void ExtensionFunctionDispatcher::SendAccessDenied( |
514 const ExtensionFunction::ResponseCallback& callback, | 643 const ExtensionFunction::ResponseCallback& callback, |
515 functions::HistogramValue histogram_value) { | 644 functions::HistogramValue histogram_value) { |
516 base::ListValue empty_list; | 645 base::ListValue empty_list; |
517 callback.Run(ExtensionFunction::FAILED, empty_list, | 646 callback.Run(ExtensionFunction::FAILED, empty_list, |
518 "Access to extension API denied.", histogram_value); | 647 "Access to extension API denied.", histogram_value); |
519 } | 648 } |
520 | 649 |
521 } // namespace extensions | 650 } // namespace extensions |
OLD | NEW |