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" |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 histogram_value); | 202 histogram_value); |
| 202 } | 203 } |
| 203 | 204 |
| 204 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; | 205 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; |
| 205 content::RenderFrameHost* render_frame_host_; | 206 content::RenderFrameHost* render_frame_host_; |
| 206 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; | 207 base::WeakPtrFactory<UIThreadResponseCallbackWrapper> weak_ptr_factory_; |
| 207 | 208 |
| 208 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); | 209 DISALLOW_COPY_AND_ASSIGN(UIThreadResponseCallbackWrapper); |
| 209 }; | 210 }; |
| 210 | 211 |
| 212 class ExtensionFunctionDispatcher::UIThreadWorkerResponseCallbackWrapper | |
| 213 : public content::RenderProcessHostObserver { | |
| 214 public: | |
| 215 UIThreadWorkerResponseCallbackWrapper( | |
| 216 const base::WeakPtr<ExtensionFunctionDispatcher>& dispatcher, | |
| 217 int render_process_id, | |
| 218 int worker_thread_id) | |
| 219 : dispatcher_(dispatcher), | |
| 220 render_process_id_(render_process_id), | |
| 221 worker_thread_id_(worker_thread_id), | |
| 222 weak_ptr_factory_(this) { | |
| 223 content::RenderProcessHost::FromID(render_process_id_)->AddObserver(this); | |
| 224 } | |
| 225 | |
| 226 ~UIThreadWorkerResponseCallbackWrapper() override {} | |
| 227 | |
| 228 // content::RenderProcessHostObserver override. | |
| 229 void RenderProcessExited(content::RenderProcessHost* process_host, | |
| 230 base::TerminationStatus status, | |
| 231 int exit_code) override { | |
| 232 process_host->RemoveObserver(this); | |
| 233 Cleanup(process_host->GetID()); | |
| 234 } | |
| 235 | |
| 236 // content::RenderProcessHostObserver override. | |
| 237 void RenderProcessHostDestroyed( | |
| 238 content::RenderProcessHost* process_host) override { | |
| 239 process_host->RemoveObserver(this); | |
| 240 Cleanup(process_host->GetID()); | |
| 241 } | |
| 242 | |
| 243 ExtensionFunction::ResponseCallback CreateCallback(int request_id) { | |
| 244 return base::Bind( | |
| 245 &UIThreadWorkerResponseCallbackWrapper::OnExtensionFunctionCompleted, | |
| 246 weak_ptr_factory_.GetWeakPtr(), request_id); | |
| 247 } | |
| 248 | |
| 249 private: | |
| 250 void Cleanup(int render_process_id) { | |
| 251 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 252 if (render_process_id != render_process_id_) | |
| 253 return; | |
| 254 | |
| 255 if (dispatcher_.get()) { | |
| 256 UIThreadWorkerResponseCallbackWrapperMap& map = | |
|
Devlin
2016/04/13 19:46:30
This could potentially remove multiple CallbackWra
lazyboy
2016/04/14 02:07:52
Ah, RenderProcessHost would still keep a reference
| |
| 257 dispatcher_->ui_thread_response_callback_wrappers_for_worker_; | |
| 258 for (UIThreadWorkerResponseCallbackWrapperMap::iterator it = map.begin(); | |
| 259 it != map.end();) { | |
| 260 if (it->first.first == render_process_id) { | |
| 261 it = map.erase(it); | |
| 262 continue; | |
| 263 } | |
| 264 ++it; | |
| 265 } | |
| 266 } | |
| 267 // Note: we might be deleted here! | |
|
Devlin
2016/04/13 19:46:30
Technically, this object might even be deleted *be
lazyboy
2016/04/14 02:07:52
Done.
| |
| 268 } | |
| 269 | |
| 270 void OnExtensionFunctionCompleted(int request_id, | |
| 271 ExtensionFunction::ResponseType type, | |
| 272 const base::ListValue& results, | |
| 273 const std::string& error, | |
| 274 functions::HistogramValue histogram_value) { | |
| 275 if (type == ExtensionFunction::BAD_MESSAGE) { | |
| 276 // TODO(lazyboy): Kill the offending process. | |
| 277 return; | |
| 278 } | |
| 279 content::RenderProcessHost* sender = | |
| 280 content::RenderProcessHost::FromID(render_process_id_); | |
| 281 DCHECK(sender); | |
| 282 sender->Send(new ExtensionMsg_ResponseWorker( | |
| 283 worker_thread_id_, request_id, type == ExtensionFunction::SUCCEEDED, | |
| 284 results, error)); | |
| 285 } | |
| 286 | |
| 287 base::WeakPtr<ExtensionFunctionDispatcher> dispatcher_; | |
| 288 const int render_process_id_; | |
| 289 const int worker_thread_id_; | |
| 290 base::WeakPtrFactory<UIThreadWorkerResponseCallbackWrapper> weak_ptr_factory_; | |
| 291 | |
| 292 DISALLOW_COPY_AND_ASSIGN(UIThreadWorkerResponseCallbackWrapper); | |
| 293 }; | |
| 294 | |
| 211 WindowController* | 295 WindowController* |
| 212 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { | 296 ExtensionFunctionDispatcher::Delegate::GetExtensionWindowController() const { |
| 213 return nullptr; | 297 return nullptr; |
| 214 } | 298 } |
| 215 | 299 |
| 216 content::WebContents* | 300 content::WebContents* |
| 217 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { | 301 ExtensionFunctionDispatcher::Delegate::GetAssociatedWebContents() const { |
| 218 return nullptr; | 302 return nullptr; |
| 219 } | 303 } |
| 220 | 304 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 FROM_HERE_WITH_EXPLICIT_FUNCTION(function->name()), | 377 FROM_HERE_WITH_EXPLICIT_FUNCTION(function->name()), |
| 294 tracked_objects::ScopedProfile::ENABLED); | 378 tracked_objects::ScopedProfile::ENABLED); |
| 295 function->Run()->Execute(); | 379 function->Run()->Execute(); |
| 296 } else { | 380 } else { |
| 297 function->OnQuotaExceeded(violation_error); | 381 function->OnQuotaExceeded(violation_error); |
| 298 } | 382 } |
| 299 } | 383 } |
| 300 | 384 |
| 301 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( | 385 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( |
| 302 content::BrowserContext* browser_context) | 386 content::BrowserContext* browser_context) |
| 303 : browser_context_(browser_context) { | 387 : browser_context_(browser_context), delegate_(nullptr) {} |
| 304 } | |
| 305 | 388 |
| 306 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { | 389 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
| 307 } | 390 } |
| 308 | 391 |
| 309 void ExtensionFunctionDispatcher::Dispatch( | 392 void ExtensionFunctionDispatcher::Dispatch( |
| 310 const ExtensionHostMsg_Request_Params& params, | 393 const ExtensionHostMsg_Request_Params& params, |
| 311 content::RenderFrameHost* render_frame_host) { | 394 content::RenderFrameHost* render_frame_host, |
| 312 UIThreadResponseCallbackWrapperMap::const_iterator | 395 int render_process_id) { |
| 313 iter = ui_thread_response_callback_wrappers_.find(render_frame_host); | 396 if (render_frame_host) { |
| 314 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; | 397 UIThreadResponseCallbackWrapperMap::const_iterator iter = |
| 315 if (iter == ui_thread_response_callback_wrappers_.end()) { | 398 ui_thread_response_callback_wrappers_.find(render_frame_host); |
| 316 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), | 399 UIThreadResponseCallbackWrapper* callback_wrapper = nullptr; |
| 317 render_frame_host); | 400 if (iter == ui_thread_response_callback_wrappers_.end()) { |
| 318 ui_thread_response_callback_wrappers_[render_frame_host] = callback_wrapper; | 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)); | |
| 319 } else { | 411 } else { |
| 320 callback_wrapper = iter->second; | 412 // Extension API from Service Worker. |
|
Devlin
2016/04/13 19:46:31
Add an analogous comment in the other if clause on
lazyboy
2016/04/14 02:07:52
Done.
| |
| 413 DCHECK(params.embedded_worker_id >= 0); | |
|
Devlin
2016/04/13 19:46:31
DCHECK_GE?
lazyboy
2016/04/14 02:07:52
Done.
| |
| 414 const int& id = params.embedded_worker_id; | |
|
Devlin
2016/04/13 19:46:31
no need to make this a const & - it's no cheaper t
lazyboy
2016/04/14 02:07:52
Done.
| |
| 415 UIThreadWorkerResponseCallbackWrapperMapKey key = | |
| 416 std::make_pair(render_process_id, id); | |
| 417 UIThreadWorkerResponseCallbackWrapperMap::const_iterator iter = | |
| 418 ui_thread_response_callback_wrappers_for_worker_.find(key); | |
| 419 UIThreadWorkerResponseCallbackWrapper* callback_wrapper = nullptr; | |
| 420 if (iter == ui_thread_response_callback_wrappers_for_worker_.end()) { | |
| 421 callback_wrapper = new UIThreadWorkerResponseCallbackWrapper( | |
| 422 AsWeakPtr(), render_process_id, params.worker_thread_id); | |
| 423 ui_thread_response_callback_wrappers_for_worker_[key] = | |
| 424 std::unique_ptr<UIThreadWorkerResponseCallbackWrapper>( | |
| 425 callback_wrapper); | |
| 426 } else { | |
| 427 callback_wrapper = iter->second.get(); | |
| 428 } | |
| 429 DispatchWithCallbackInternal( | |
|
Devlin
2016/04/13 19:46:31
We can lump these (this and line 408) together if
lazyboy
2016/04/14 02:07:52
I had that in the beginning, but later the code di
| |
| 430 params, nullptr, render_process_id, | |
| 431 callback_wrapper->CreateCallback(params.request_id)); | |
| 321 } | 432 } |
| 322 | |
| 323 DispatchWithCallbackInternal( | |
| 324 params, render_frame_host, | |
| 325 callback_wrapper->CreateCallback(params.request_id)); | |
| 326 } | 433 } |
| 327 | 434 |
| 328 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( | 435 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( |
| 329 const ExtensionHostMsg_Request_Params& params, | 436 const ExtensionHostMsg_Request_Params& params, |
| 330 content::RenderFrameHost* render_frame_host, | 437 content::RenderFrameHost* render_frame_host, |
| 438 int render_process_id, | |
| 331 const ExtensionFunction::ResponseCallback& callback) { | 439 const ExtensionFunction::ResponseCallback& callback) { |
| 332 DCHECK(render_frame_host); | |
| 333 // TODO(yzshen): There is some shared logic between this method and | 440 // TODO(yzshen): There is some shared logic between this method and |
| 334 // DispatchOnIOThread(). It is nice to deduplicate. | 441 // DispatchOnIOThread(). It is nice to deduplicate. |
| 335 ProcessMap* process_map = ProcessMap::Get(browser_context_); | 442 ProcessMap* process_map = ProcessMap::Get(browser_context_); |
| 336 if (!process_map) | 443 if (!process_map) |
| 337 return; | 444 return; |
| 338 | 445 |
| 339 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | 446 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); |
| 340 const Extension* extension = | 447 const Extension* extension = |
| 341 registry->enabled_extensions().GetByID(params.extension_id); | 448 registry->enabled_extensions().GetByID(params.extension_id); |
| 342 if (!extension) { | 449 if (!extension) { |
| 343 extension = | 450 extension = |
| 344 registry->enabled_extensions().GetHostedAppByURL(params.source_url); | 451 registry->enabled_extensions().GetHostedAppByURL(params.source_url); |
| 345 } | 452 } |
| 346 | 453 |
| 347 int process_id = render_frame_host->GetProcess()->GetID(); | 454 if (render_frame_host) |
| 348 scoped_refptr<ExtensionFunction> function( | 455 DCHECK_EQ(render_process_id, render_frame_host->GetProcess()->GetID()); |
| 349 CreateExtensionFunction(params, | 456 |
| 350 extension, | 457 scoped_refptr<ExtensionFunction> function(CreateExtensionFunction( |
| 351 process_id, | 458 params, extension, render_process_id, *process_map, |
| 352 *process_map, | 459 ExtensionAPI::GetSharedInstance(), browser_context_, callback)); |
| 353 ExtensionAPI::GetSharedInstance(), | |
| 354 browser_context_, | |
| 355 callback)); | |
| 356 if (!function.get()) | 460 if (!function.get()) |
| 357 return; | 461 return; |
| 358 | 462 |
| 359 UIThreadExtensionFunction* function_ui = | 463 UIThreadExtensionFunction* function_ui = |
| 360 function->AsUIThreadExtensionFunction(); | 464 function->AsUIThreadExtensionFunction(); |
| 361 if (!function_ui) { | 465 if (!function_ui) { |
| 362 NOTREACHED(); | 466 NOTREACHED(); |
| 363 return; | 467 return; |
| 364 } | 468 } |
| 365 function_ui->SetRenderFrameHost(render_frame_host); | 469 function_ui->SetRenderFrameHost(render_frame_host); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 // if function->Run() ended up closing the tab that owns us. | 516 // if function->Run() ended up closing the tab that owns us. |
| 413 | 517 |
| 414 // Check if extension was uninstalled by management.uninstall. | 518 // Check if extension was uninstalled by management.uninstall. |
| 415 if (!registry->enabled_extensions().GetByID(params.extension_id)) | 519 if (!registry->enabled_extensions().GetByID(params.extension_id)) |
| 416 return; | 520 return; |
| 417 | 521 |
| 418 // We only adjust the keepalive count for UIThreadExtensionFunction for | 522 // We only adjust the keepalive count for UIThreadExtensionFunction for |
| 419 // now, largely for simplicity's sake. This is OK because currently, only | 523 // now, largely for simplicity's sake. This is OK because currently, only |
| 420 // the webRequest API uses IOThreadExtensionFunction, and that API is not | 524 // the webRequest API uses IOThreadExtensionFunction, and that API is not |
| 421 // compatible with lazy background pages. | 525 // compatible with lazy background pages. |
| 526 // TODO(lazyboy): API functions from extension Service Worker will incorrectly | |
| 527 // change keepalive count below. | |
| 422 process_manager->IncrementLazyKeepaliveCount(extension); | 528 process_manager->IncrementLazyKeepaliveCount(extension); |
| 423 } | 529 } |
| 424 | 530 |
| 425 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( | 531 void ExtensionFunctionDispatcher::OnExtensionFunctionCompleted( |
| 426 const Extension* extension) { | 532 const Extension* extension) { |
| 533 // TODO(lazyboy): API functions from extension Service Worker will incorrectly | |
| 534 // change keepalive count below. | |
| 427 if (extension) { | 535 if (extension) { |
| 428 ProcessManager::Get(browser_context_) | 536 ProcessManager::Get(browser_context_) |
| 429 ->DecrementLazyKeepaliveCount(extension); | 537 ->DecrementLazyKeepaliveCount(extension); |
| 430 } | 538 } |
| 431 } | 539 } |
| 432 | 540 |
| 433 WindowController* | 541 WindowController* |
| 434 ExtensionFunctionDispatcher::GetExtensionWindowController() const { | 542 ExtensionFunctionDispatcher::GetExtensionWindowController() const { |
| 435 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; | 543 return delegate_ ? delegate_->GetExtensionWindowController() : nullptr; |
| 436 } | 544 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 // static | 603 // static |
| 496 void ExtensionFunctionDispatcher::SendAccessDenied( | 604 void ExtensionFunctionDispatcher::SendAccessDenied( |
| 497 const ExtensionFunction::ResponseCallback& callback, | 605 const ExtensionFunction::ResponseCallback& callback, |
| 498 functions::HistogramValue histogram_value) { | 606 functions::HistogramValue histogram_value) { |
| 499 base::ListValue empty_list; | 607 base::ListValue empty_list; |
| 500 callback.Run(ExtensionFunction::FAILED, empty_list, | 608 callback.Run(ExtensionFunction::FAILED, empty_list, |
| 501 "Access to extension API denied.", histogram_value); | 609 "Access to extension API denied.", histogram_value); |
| 502 } | 610 } |
| 503 | 611 |
| 504 } // namespace extensions | 612 } // namespace extensions |
| OLD | NEW |