Chromium Code Reviews| 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 | |
| 211 WindowController* | 287 WindowController* |
| 212 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { | 288 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { |
| 213 return nullptr; | 289 return nullptr; |
| 214 } | 290 } |
| 215 | 291 |
| 216 content::WebContents* | 292 content::WebContents* |
| 217 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { | 293 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { |
| 218 return nullptr; | 294 return nullptr; |
| 219 } | 295 } |
| 220 | 296 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 | 376 |
| 301 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( | 377 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
| 302 content::BrowserContext* browser_context) | 378 content::BrowserContext* browser_context) |
| 303 : browser_context_(browser_context), delegate_(nullptr) {} | 379 : browser_context_(browser_context), delegate_(nullptr) {} |
| 304 | 380 |
| 305 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { | 381 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
| 306 } | 382 } |
| 307 | 383 |
| 308 void ExtensionFunctionDispatcher::Dispatch( | 384 void ExtensionFunctionDispatcher::Dispatch( |
| 309 const ExtensionHostMsg_Request_Params& params, | 385 const ExtensionHostMsg_Request_Params& params, |
| 310 content::RenderFrameHost* render_frame_host) { | 386 content::RenderFrameHost* render_frame_host, |
| 311 UIThreadResponseCallbackWrapperMap::const_iterator | 387 int render_process_id) { |
| 312 iter = ui_thread_response_callback_wrappers_.find(render_frame_host); | 388 if (render_frame_host) { |
| 313 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; | 389 // Extension API from a non Service Worker context, e.g. extension page, |
| 314 if (iter == ui_thread_response_callback_wrappers_.end()) { | 390 // background page, content script. |
| 315 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), | 391 UIThreadResponseCallbackWrapperMap::const_iterator iter = |
| 316 render_frame_host); | 392 ui_thread_response_callback_wrappers_.find(render_frame_host); |
| 317 ui_thread_response_callback_wrappers_[render_frame_host] = callback_wrapper; | 393 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; |
| 394 if (iter == ui_thread_response_callback_wrappers_.end()) { | |
| 395 callback_wrapper = | |
| 396 new UIThreadResponseCallbackWrapper(AsWeakPtr(), render_frame_host); | |
| 397 ui_thread_response_callback_wrappers_[render_frame_host] = | |
| 398 callback_wrapper; | |
| 399 } else { | |
| 400 callback_wrapper = iter->second; | |
| 401 } | |
| 402 DispatchWithCallbackInternal( | |
| 403 params, render_frame_host, render_process_id, | |
| 404 callback_wrapper->CreateCallback(params.request_id)); | |
| 318 } else { | 405 } else { |
| 319 callback_wrapper = iter->second; | 406 // Extension API from Service Worker. |
| 407 DCHECK_GE(params.embedded_worker_id, 0); | |
| 408 UIThreadWorkerResponseCallbackWrapperMapKey key = | |
| 409 std::make_pair(render_process_id, params.embedded_worker_id); | |
| 410 UIThreadWorkerResponseCallbackWrapperMap::const_iterator iter = | |
| 411 ui_thread_response_callback_wrappers_for_worker_.find(key); | |
| 412 UIThreadWorkerResponseCallbackWrapper* callback_wrapper = nullptr; | |
| 413 if (iter == ui_thread_response_callback_wrappers_for_worker_.end()) { | |
| 414 callback_wrapper = new UIThreadWorkerResponseCallbackWrapper( | |
| 415 AsWeakPtr(), render_process_id, params.worker_thread_id); | |
| 416 ui_thread_response_callback_wrappers_for_worker_[key] = | |
| 417 std::unique_ptr<UIThreadWorkerResponseCallbackWrapper>( | |
|
dcheng
2016/05/11 23:41:18
base::WrapUnique(callback_wrapper) will save a lot
lazyboy
2016/05/12 00:21:22
Done, thanks.
| |
| 418 callback_wrapper); | |
| 419 } else { | |
| 420 callback_wrapper = iter->second.get(); | |
| 421 } | |
| 422 DispatchWithCallbackInternal( | |
| 423 params, nullptr, render_process_id, | |
| 424 callback_wrapper->CreateCallback(params.request_id)); | |
| 320 } | 425 } |
| 321 | |
| 322 DispatchWithCallbackInternal( | |
| 323 params, render_frame_host, | |
| 324 callback_wrapper->CreateCallback(params.request_id)); | |
| 325 } | 426 } |
| 326 | 427 |
| 327 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( | 428 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( |
| 328 const ExtensionHostMsg_Request_Params& params, | 429 const ExtensionHostMsg_Request_Params& params, |
| 329 content::RenderFrameHost* render_frame_host, | 430 content::RenderFrameHost* render_frame_host, |
| 431 int render_process_id, | |
| 330 const ExtensionFunction::ResponseCallback& callback) { | 432 const ExtensionFunction::ResponseCallback& callback) { |
| 331 DCHECK(render_frame_host); | |
| 332 // TODO(yzshen): There is some shared logic between this method and | 433 // TODO(yzshen): There is some shared logic between this method and |
| 333 // DispatchOnIOThread(). It is nice to deduplicate. | 434 // DispatchOnIOThread(). It is nice to deduplicate. |
| 334 ProcessMap* process_map = ProcessMap::Get(browser_context_); | 435 ProcessMap* process_map = ProcessMap::Get(browser_context_); |
| 335 if (!process_map) | 436 if (!process_map) |
| 336 return; | 437 return; |
| 337 | 438 |
| 338 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | 439 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); |
| 339 const Extension* extension = | 440 const Extension* extension = |
| 340 registry->enabled_extensions().GetByID(params.extension_id); | 441 registry->enabled_extensions().GetByID(params.extension_id); |
| 341 if (!extension) { | 442 if (!extension) { |
| 342 extension = | 443 extension = |
| 343 registry->enabled_extensions().GetHostedAppByURL(params.source_url); | 444 registry->enabled_extensions().GetHostedAppByURL(params.source_url); |
| 344 } | 445 } |
| 345 | 446 |
| 346 int process_id = render_frame_host->GetProcess()->GetID(); | 447 if (render_frame_host) |
| 347 scoped_refptr<ExtensionFunction> function( | 448 DCHECK_EQ(render_process_id, render_frame_host->GetProcess()->GetID()); |
| 348 CreateExtensionFunction(params, | 449 |
| 349 extension, | 450 scoped_refptr<ExtensionFunction> function(CreateExtensionFunction( |
| 350 process_id, | 451 params, extension, render_process_id, *process_map, |
| 351 *process_map, | 452 ExtensionAPI::GetSharedInstance(), browser_context_, callback)); |
| 352 ExtensionAPI::GetSharedInstance(), | |
| 353 browser_context_, | |
| 354 callback)); | |
| 355 if (!function.get()) | 453 if (!function.get()) |
| 356 return; | 454 return; |
| 357 | 455 |
| 358 UIThreadExtensionFunction* function_ui = | 456 UIThreadExtensionFunction* function_ui = |
| 359 function->AsUIThreadExtensionFunction(); | 457 function->AsUIThreadExtensionFunction(); |
| 360 if (!function_ui) { | 458 if (!function_ui) { |
| 361 NOTREACHED(); | 459 NOTREACHED(); |
| 362 return; | 460 return; |
| 363 } | 461 } |
| 364 function_ui->SetRenderFrameHost(render_frame_host); | 462 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. | 509 // if function->Run() ended up closing the tab that owns us. |
| 412 | 510 |
| 413 // Check if extension was uninstalled by management.uninstall. | 511 // Check if extension was uninstalled by management.uninstall. |
| 414 if (!registry->enabled_extensions().GetByID(params.extension_id)) | 512 if (!registry->enabled_extensions().GetByID(params.extension_id)) |
| 415 return; | 513 return; |
| 416 | 514 |
| 417 // We only adjust the keepalive count for UIThreadExtensionFunction for | 515 // We only adjust the keepalive count for UIThreadExtensionFunction for |
| 418 // now, largely for simplicity's sake. This is OK because currently, only | 516 // now, largely for simplicity's sake. This is OK because currently, only |
| 419 // the webRequest API uses IOThreadExtensionFunction, and that API is not | 517 // the webRequest API uses IOThreadExtensionFunction, and that API is not |
| 420 // compatible with lazy background pages. | 518 // compatible with lazy background pages. |
| 519 // TODO(lazyboy): API functions from extension Service Worker will incorrectly | |
| 520 // change keepalive count below. | |
| 421 process_manager->IncrementLazyKeepaliveCount(extension); | 521 process_manager->IncrementLazyKeepaliveCount(extension); |
| 422 } | 522 } |
| 423 | 523 |
| 524 void ExtensionFunctionDispatcher::RemoveWorkerCallbacksForProcess( | |
| 525 int render_process_id) { | |
| 526 UIThreadWorkerResponseCallbackWrapperMap& map = | |
| 527 ui_thread_response_callback_wrappers_for_worker_; | |
| 528 for (UIThreadWorkerResponseCallbackWrapperMap::iterator it = map.begin(); | |
| 529 it != map.end();) { | |
| 530 if (it->first.first == render_process_id) { | |
| 531 it = map.erase(it); | |
| 532 continue; | |
| 533 } | |
| 534 ++it; | |
| 535 } | |
| 536 } | |
| 537 | |
| 424 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( | 538 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( |
| 425 const Extension* extension) { | 539 const Extension* extension) { |
| 540 // TODO(lazyboy): API functions from extension Service Worker will incorrectly | |
| 541 // change keepalive count below. | |
| 426 if (extension) { | 542 if (extension) { |
| 427 ProcessManager::Get(browser_context_) | 543 ProcessManager::Get(browser_context_) |
| 428 ->DecrementLazyKeepaliveCount(extension); | 544 ->DecrementLazyKeepaliveCount(extension); |
| 429 } | 545 } |
| 430 } | 546 } |
| 431 | 547 |
| 432 WindowController* | 548 WindowController* |
| 433 ExtensionFunctionDispatcher::GetExtensionWindowController() const { | 549 ExtensionFunctionDispatcher::GetExtensionWindowController() const { |
| 434 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; | 550 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; |
| 435 } | 551 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 // static | 610 // static |
| 495 void ExtensionFunctionDispatcher::SendAccessDenied( | 611 void ExtensionFunctionDispatcher::SendAccessDenied( |
| 496 const ExtensionFunction::ResponseCallback& callback, | 612 const ExtensionFunction::ResponseCallback& callback, |
| 497 functions::HistogramValue histogram_value) { | 613 functions::HistogramValue histogram_value) { |
| 498 base::ListValue empty_list; | 614 base::ListValue empty_list; |
| 499 callback.Run(ExtensionFunction::FAILED, empty_list, | 615 callback.Run(ExtensionFunction::FAILED, empty_list, |
| 500 "Access to extension API denied.", histogram_value); | 616 "Access to extension API denied.", histogram_value); |
| 501 } | 617 } |
| 502 | 618 |
| 503 } // namespace extensions | 619 } // namespace extensions |
| OLD | NEW |