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