| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_function_dispatcher.h" | 5 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/process/process.h" | 12 #include "base/process/process.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" | 15 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" |
| 16 #include "chrome/browser/extensions/activity_log/activity_log.h" | 16 #include "chrome/browser/extensions/activity_log/activity_log.h" |
| 17 #include "chrome/browser/extensions/api/activity_log_private/activity_log_privat
e_api.h" | 17 #include "chrome/browser/extensions/api/activity_log_private/activity_log_privat
e_api.h" |
| 18 #include "chrome/browser/extensions/extension_function_registry.h" | 18 #include "chrome/browser/extensions/extension_function_registry.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
| 21 #include "chrome/browser/extensions/extension_util.h" | 21 #include "chrome/browser/extensions/extension_util.h" |
| 22 #include "chrome/browser/extensions/extension_web_ui.h" | 22 #include "chrome/browser/extensions/extension_web_ui.h" |
| 23 #include "chrome/browser/external_protocol/external_protocol_handler.h" | 23 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| 24 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 24 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 25 #include "chrome/common/extensions/extension_messages.h" | 25 #include "chrome/common/extensions/extension_messages.h" |
| 26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
| 27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/render_frame_host.h" |
| 28 #include "content/public/browser/render_process_host.h" | 29 #include "content/public/browser/render_process_host.h" |
| 29 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
| 30 #include "content/public/browser/user_metrics.h" | 31 #include "content/public/browser/user_metrics.h" |
| 31 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_contents_observer.h" | 33 #include "content/public/browser/web_contents_observer.h" |
| 33 #include "content/public/common/result_codes.h" | 34 #include "content/public/common/result_codes.h" |
| 34 #include "extensions/browser/process_map.h" | 35 #include "extensions/browser/process_map.h" |
| 35 #include "extensions/browser/quota_service.h" | 36 #include "extensions/browser/quota_service.h" |
| 36 #include "extensions/common/extension_api.h" | 37 #include "extensions/common/extension_api.h" |
| 37 #include "extensions/common/extension_set.h" | 38 #include "extensions/common/extension_set.h" |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 iter = ui_thread_response_callback_wrappers_.find(render_view_host); | 305 iter = ui_thread_response_callback_wrappers_.find(render_view_host); |
| 305 UIThreadResponseCallbackWrapper* callback_wrapper = NULL; | 306 UIThreadResponseCallbackWrapper* callback_wrapper = NULL; |
| 306 if (iter == ui_thread_response_callback_wrappers_.end()) { | 307 if (iter == ui_thread_response_callback_wrappers_.end()) { |
| 307 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), | 308 callback_wrapper = new UIThreadResponseCallbackWrapper(AsWeakPtr(), |
| 308 render_view_host); | 309 render_view_host); |
| 309 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper; | 310 ui_thread_response_callback_wrappers_[render_view_host] = callback_wrapper; |
| 310 } else { | 311 } else { |
| 311 callback_wrapper = iter->second; | 312 callback_wrapper = iter->second; |
| 312 } | 313 } |
| 313 | 314 |
| 314 DispatchWithCallback(params, render_view_host, | 315 DispatchWithCallbackInternal( |
| 315 callback_wrapper->CreateCallback(params.request_id)); | 316 params, render_view_host, NULL, |
| 317 callback_wrapper->CreateCallback(params.request_id)); |
| 316 } | 318 } |
| 317 | 319 |
| 318 void ExtensionFunctionDispatcher::DispatchWithCallback( | 320 void ExtensionFunctionDispatcher::DispatchWithCallback( |
| 319 const ExtensionHostMsg_Request_Params& params, | 321 const ExtensionHostMsg_Request_Params& params, |
| 322 content::RenderFrameHost* render_frame_host, |
| 323 const ExtensionFunction::ResponseCallback& callback) { |
| 324 DispatchWithCallbackInternal(params, NULL, render_frame_host, callback); |
| 325 } |
| 326 |
| 327 void ExtensionFunctionDispatcher::DispatchWithCallbackInternal( |
| 328 const ExtensionHostMsg_Request_Params& params, |
| 320 RenderViewHost* render_view_host, | 329 RenderViewHost* render_view_host, |
| 330 content::RenderFrameHost* render_frame_host, |
| 321 const ExtensionFunction::ResponseCallback& callback) { | 331 const ExtensionFunction::ResponseCallback& callback) { |
| 332 DCHECK(render_view_host || render_frame_host); |
| 322 // TODO(yzshen): There is some shared logic between this method and | 333 // TODO(yzshen): There is some shared logic between this method and |
| 323 // DispatchOnIOThread(). It is nice to deduplicate. | 334 // DispatchOnIOThread(). It is nice to deduplicate. |
| 324 ExtensionSystem* extension_system = | 335 ExtensionSystem* extension_system = |
| 325 ExtensionSystem::GetForBrowserContext(browser_context_); | 336 ExtensionSystem::GetForBrowserContext(browser_context_); |
| 326 ExtensionService* service = extension_system->extension_service(); | 337 ExtensionService* service = extension_system->extension_service(); |
| 327 extensions::ProcessMap* process_map = service->process_map(); | 338 extensions::ProcessMap* process_map = service->process_map(); |
| 328 if (!process_map) | 339 if (!process_map) |
| 329 return; | 340 return; |
| 330 | 341 |
| 331 const Extension* extension = service->extensions()->GetByID( | 342 const Extension* extension = service->extensions()->GetByID( |
| 332 params.extension_id); | 343 params.extension_id); |
| 333 if (!extension) | 344 if (!extension) |
| 334 extension = service->extensions()->GetHostedAppByURL(params.source_url); | 345 extension = service->extensions()->GetHostedAppByURL(params.source_url); |
| 335 | 346 |
| 347 int process_id = render_view_host ? render_view_host->GetProcess()->GetID() : |
| 348 render_frame_host->GetProcess()->GetID(); |
| 336 scoped_refptr<ExtensionFunction> function( | 349 scoped_refptr<ExtensionFunction> function( |
| 337 CreateExtensionFunction(params, | 350 CreateExtensionFunction(params, |
| 338 extension, | 351 extension, |
| 339 render_view_host->GetProcess()->GetID(), | 352 process_id, |
| 340 *process_map, | 353 *process_map, |
| 341 extensions::ExtensionAPI::GetSharedInstance(), | 354 extensions::ExtensionAPI::GetSharedInstance(), |
| 342 browser_context_, | 355 browser_context_, |
| 343 callback)); | 356 callback)); |
| 344 scoped_ptr<ListValue> args(params.arguments.DeepCopy()); | 357 scoped_ptr<ListValue> args(params.arguments.DeepCopy()); |
| 345 | 358 |
| 346 if (!function.get()) | 359 if (!function.get()) |
| 347 return; | 360 return; |
| 348 | 361 |
| 349 UIThreadExtensionFunction* function_ui = | 362 UIThreadExtensionFunction* function_ui = |
| 350 function->AsUIThreadExtensionFunction(); | 363 function->AsUIThreadExtensionFunction(); |
| 351 if (!function_ui) { | 364 if (!function_ui) { |
| 352 NOTREACHED(); | 365 NOTREACHED(); |
| 353 return; | 366 return; |
| 354 } | 367 } |
| 355 function_ui->SetRenderViewHost(render_view_host); | 368 if (render_view_host) { |
| 369 function_ui->SetRenderViewHost(render_view_host); |
| 370 } else { |
| 371 function_ui->SetRenderFrameHost(render_frame_host); |
| 372 } |
| 356 function_ui->set_dispatcher(AsWeakPtr()); | 373 function_ui->set_dispatcher(AsWeakPtr()); |
| 357 function_ui->set_context(browser_context_); | 374 function_ui->set_context(browser_context_); |
| 358 function->set_include_incognito(extension_util::CanCrossIncognito(extension, | 375 function->set_include_incognito(extension_util::CanCrossIncognito(extension, |
| 359 service)); | 376 service)); |
| 360 | 377 |
| 361 if (!CheckPermissions(function.get(), extension, params, callback)) | 378 if (!CheckPermissions(function.get(), extension, params, callback)) |
| 362 return; | 379 return; |
| 363 | 380 |
| 364 extensions::QuotaService* quota = service->quota_service(); | 381 extensions::QuotaService* quota = service->quota_service(); |
| 365 std::string violation_error = quota->Assess(extension->id(), | 382 std::string violation_error = quota->Assess(extension->id(), |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 return function; | 507 return function; |
| 491 } | 508 } |
| 492 | 509 |
| 493 // static | 510 // static |
| 494 void ExtensionFunctionDispatcher::SendAccessDenied( | 511 void ExtensionFunctionDispatcher::SendAccessDenied( |
| 495 const ExtensionFunction::ResponseCallback& callback) { | 512 const ExtensionFunction::ResponseCallback& callback) { |
| 496 ListValue empty_list; | 513 ListValue empty_list; |
| 497 callback.Run(ExtensionFunction::FAILED, empty_list, | 514 callback.Run(ExtensionFunction::FAILED, empty_list, |
| 498 "Access to extension API denied."); | 515 "Access to extension API denied."); |
| 499 } | 516 } |
| OLD | NEW |