// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/extensions/extension_function_dispatcher.h" #include #include "base/json/json_value_serializer.h" #include "base/memory/ref_counted.h" #include "base/memory/singleton.h" #include "base/process_util.h" #include "base/values.h" #include "build/build_config.h" #include "chrome/browser/accessibility/accessibility_extension_api.h" #include "chrome/browser/bookmarks/bookmark_extension_api.h" #include "chrome/browser/bookmarks/bookmark_manager_extension_api.h" #include "chrome/browser/download/download_extension_api.h" #include "chrome/browser/extensions/api/app/app_api.h" #include "chrome/browser/extensions/api/dns/dns_api.h" #include "chrome/browser/extensions/api/permissions/permissions_api.h" #include "chrome/browser/extensions/api/serial/serial_api.h" #include "chrome/browser/extensions/api/socket/socket_api.h" #include "chrome/browser/extensions/api/webrequest/webrequest_api.h" #include "chrome/browser/extensions/execute_code_in_tab_function.h" #include "chrome/browser/extensions/extension_activity_log.h" #include "chrome/browser/extensions/extension_browser_actions_api.h" #include "chrome/browser/extensions/extension_chrome_auth_private_api.h" #include "chrome/browser/extensions/extension_clear_api.h" #include "chrome/browser/extensions/extension_content_settings_api.h" #include "chrome/browser/extensions/extension_context_menu_api.h" #include "chrome/browser/extensions/extension_cookies_api.h" #include "chrome/browser/extensions/extension_debugger_api.h" #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/extensions/extension_i18n_api.h" #include "chrome/browser/extensions/extension_idle_api.h" #include "chrome/browser/extensions/extension_management_api.h" #include "chrome/browser/extensions/extension_metrics_module.h" #include "chrome/browser/extensions/extension_module.h" #include "chrome/browser/extensions/extension_omnibox_api.h" #include "chrome/browser/extensions/extension_page_actions_module.h" #include "chrome/browser/extensions/extension_page_capture_api.h" #include "chrome/browser/extensions/extension_preference_api.h" #include "chrome/browser/extensions/extension_processes_api.h" #include "chrome/browser/extensions/extension_proxy_api.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/extensions/extension_test_api.h" #include "chrome/browser/extensions/extension_tts_api.h" #include "chrome/browser/extensions/extension_tts_engine_api.h" #include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h" #include "chrome/browser/extensions/extension_web_ui.h" #include "chrome/browser/extensions/extension_webnavigation_api.h" #include "chrome/browser/extensions/extension_webstore_private_api.h" #include "chrome/browser/extensions/extensions_quota_service.h" #include "chrome/browser/extensions/system/system_api.h" #include "chrome/browser/extensions/process_map.h" #include "chrome/browser/extensions/settings/settings_api.h" #include "chrome/browser/external_protocol/external_protocol_handler.h" #include "chrome/browser/history/history_extension_api.h" #include "chrome/browser/history/top_sites_extension_api.h" #include "chrome/browser/infobars/infobar_extension_api.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/renderer_host/chrome_render_message_filter.h" #include "chrome/browser/rlz/rlz_extension_api.h" #include "chrome/browser/speech/speech_input_extension_api.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/common/extensions/api/extension_api.h" #include "chrome/common/extensions/extension_messages.h" #include "chrome/common/extensions/extension_set.h" #include "chrome/common/url_constants.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/public/browser/render_process_host.h" #include "ipc/ipc_message.h" #include "ipc/ipc_message_macros.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" #if defined(TOOLKIT_VIEWS) #include "chrome/browser/extensions/extension_input_api.h" #endif #if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD) #include "chrome/browser/extensions/extension_input_ui_api.h" #endif #if defined(OS_CHROMEOS) #include "chrome/browser/extensions/api/terminal/terminal_private_api.h" #include "chrome/browser/extensions/extension_file_browser_private_api.h" #include "chrome/browser/extensions/extension_info_private_api_chromeos.h" #include "chrome/browser/extensions/extension_input_ime_api.h" #include "chrome/browser/extensions/extension_input_method_api.h" #include "chrome/browser/extensions/extension_mediaplayer_private_api.h" #endif using extensions::ExtensionAPI; using WebKit::WebSecurityOrigin; // FactoryRegistry ------------------------------------------------------------- namespace { // Template for defining ExtensionFunctionFactory. template ExtensionFunction* NewExtensionFunction() { return new T(); } // Contains a list of all known extension functions and allows clients to // create instances of them. class FactoryRegistry { public: static FactoryRegistry* GetInstance(); FactoryRegistry() { ResetFunctions(); } // Resets all functions to their default values. void ResetFunctions(); // Adds all function names to 'names'. void GetAllNames(std::vector* names); // Allows overriding of specific functions (e.g. for testing). Functions // must be previously registered. Returns true if successful. bool OverrideFunction(const std::string& name, ExtensionFunctionFactory factory); // Factory method for the ExtensionFunction registered as 'name'. ExtensionFunction* NewFunction(const std::string& name); private: template void RegisterFunction() { factories_[T::function_name()] = &NewExtensionFunction; } typedef std::map FactoryMap; FactoryMap factories_; }; FactoryRegistry* FactoryRegistry::GetInstance() { return Singleton::get(); } void FactoryRegistry::ResetFunctions() { // Register all functions here. // Windows RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Tabs RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Page Actions. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Browser Actions. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Browsing Data. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Bookmarks. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Infobars. RegisterFunction(); // BookmarkManager RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // History RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Idle RegisterFunction(); // I18N. RegisterFunction(); // Processes. RegisterFunction(); // Metrics. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // RLZ. #if defined(OS_WIN) RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); #endif // Cookies. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Test. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Accessibility. RegisterFunction(); RegisterFunction(); RegisterFunction(); // Text-to-speech. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Context Menus. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Omnibox. RegisterFunction(); RegisterFunction(); // Speech input. RegisterFunction(); RegisterFunction(); RegisterFunction(); #if defined(TOOLKIT_VIEWS) // Input. RegisterFunction(); #endif #if defined(USE_VIRTUAL_KEYBOARD) RegisterFunction(); RegisterFunction(); #endif #if defined(OS_CHROMEOS) // IME RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); #if defined(USE_VIRTUAL_KEYBOARD) RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); #endif #endif // Management. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Extension module. RegisterFunction(); RegisterFunction(); RegisterFunction(); // WebstorePrivate. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // WebNavigation. RegisterFunction(); RegisterFunction(); // WebRequest. RegisterFunction(); RegisterFunction(); RegisterFunction(); // Preferences. RegisterFunction(); RegisterFunction(); RegisterFunction(); // ChromeOS-specific part of the API. #if defined(OS_CHROMEOS) // Device Customization. RegisterFunction(); // FileBrowserPrivate functions. // TODO(jamescook): Expose these on non-ChromeOS platforms so we can use // the extension-based file picker on Aura. crbug.com/97424 RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Mediaplayer RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // InputMethod RegisterFunction(); // Terminal RegisterFunction(); RegisterFunction(); RegisterFunction(); #if defined(USE_VIRTUAL_KEYBOARD) // Input RegisterFunction(); RegisterFunction(); #endif #endif // Websocket to TCP proxy. Currently noop on anything other than ChromeOS. RegisterFunction(); RegisterFunction(); // Debugger RegisterFunction(); RegisterFunction(); RegisterFunction(); // Settings RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Content settings. RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // ChromeAuth settings. RegisterFunction(); // Experimental App API. RegisterFunction(); RegisterFunction(); // Permissions RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // Downloads RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // PageCapture RegisterFunction(); // TopSites RegisterFunction(); // Serial RegisterFunction(); RegisterFunction(); // Sockets RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); RegisterFunction(); // DNS RegisterFunction(); // System RegisterFunction(); RegisterFunction(); } void FactoryRegistry::GetAllNames(std::vector* names) { for (FactoryMap::iterator iter = factories_.begin(); iter != factories_.end(); ++iter) { names->push_back(iter->first); } } bool FactoryRegistry::OverrideFunction(const std::string& name, ExtensionFunctionFactory factory) { FactoryMap::iterator iter = factories_.find(name); if (iter == factories_.end()) { return false; } else { iter->second = factory; return true; } } ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { FactoryMap::iterator iter = factories_.find(name); DCHECK(iter != factories_.end()); ExtensionFunction* function = iter->second(); function->set_name(name); return function; } const char kAccessDenied[] = "access denied"; const char kQuotaExceeded[] = "quota exceeded"; void LogSuccess(const Extension* extension, const ExtensionHostMsg_Request_Params& params) { ExtensionActivityLog* extension_activity_log = ExtensionActivityLog::GetInstance(); if (extension_activity_log->HasObservers(extension)) { std::string call_signature = params.name + "("; ListValue::const_iterator it = params.arguments.begin(); for (; it != params.arguments.end(); ++it) { std::string arg; JSONStringValueSerializer serializer(&arg); if (serializer.SerializeAndOmitBinaryValues(**it)) { if (it != params.arguments.begin()) call_signature += ", "; call_signature += arg; } } call_signature += ")"; extension_activity_log->Log( extension, ExtensionActivityLog::ACTIVITY_EXTENSION_API_CALL, call_signature); } } void LogFailure(const Extension* extension, const std::string& func_name, const char* reason) { ExtensionActivityLog* extension_activity_log = ExtensionActivityLog::GetInstance(); if (extension_activity_log->HasObservers(extension)) { extension_activity_log->Log( extension, ExtensionActivityLog::ACTIVITY_EXTENSION_API_BLOCK, func_name + ": " + reason); } } }; // namespace // ExtensionFunctionDispatcher ------------------------------------------------- void ExtensionFunctionDispatcher::GetAllFunctionNames( std::vector* names) { FactoryRegistry::GetInstance()->GetAllNames(names); } bool ExtensionFunctionDispatcher::OverrideFunction( const std::string& name, ExtensionFunctionFactory factory) { return FactoryRegistry::GetInstance()->OverrideFunction(name, factory); } void ExtensionFunctionDispatcher::ResetFunctions() { FactoryRegistry::GetInstance()->ResetFunctions(); } // static void ExtensionFunctionDispatcher::DispatchOnIOThread( ExtensionInfoMap* extension_info_map, void* profile, int render_process_id, base::WeakPtr ipc_sender, int routing_id, const ExtensionHostMsg_Request_Params& params) { const Extension* extension = extension_info_map->extensions().GetByID(params.extension_id); scoped_refptr function( CreateExtensionFunction(params, extension, render_process_id, extension_info_map->process_map(), profile, ipc_sender, routing_id)); if (!function) { LogFailure(extension, params.name, kAccessDenied); return; } IOThreadExtensionFunction* function_io = function->AsIOThreadExtensionFunction(); if (!function_io) { NOTREACHED(); return; } function_io->set_ipc_sender(ipc_sender, routing_id); function_io->set_extension_info_map(extension_info_map); function->set_include_incognito( extension_info_map->IsIncognitoEnabled(extension->id())); ExtensionsQuotaService* quota = extension_info_map->quota_service(); if (quota->Assess(extension->id(), function, ¶ms.arguments, base::TimeTicks::Now())) { function->Run(); LogSuccess(extension, params); } else { function->OnQuotaExceeded(); LogFailure(extension, params.name, kQuotaExceeded); } } ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile, Delegate* delegate) : profile_(profile), delegate_(delegate) { } ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { } Browser* ExtensionFunctionDispatcher::GetCurrentBrowser( RenderViewHost* render_view_host, bool include_incognito) { Browser* browser = delegate_->GetBrowser(); // If the delegate has an associated browser, that is always the right answer. if (browser) return browser; // Otherwise, try to default to a reasonable browser. If |include_incognito| // is true, we will also search browsers in the incognito version of this // profile. Note that the profile may already be incognito, in which case // we will search the incognito version only, regardless of the value of // |include_incognito|. Profile* profile = Profile::FromBrowserContext( render_view_host->process()->GetBrowserContext()); browser = BrowserList::FindAnyBrowser(profile, include_incognito); // NOTE(rafaelw): This can return NULL in some circumstances. In particular, // a background_page onload chrome.tabs api call can make it into here // before the browser is sufficiently initialized to return here. // A similar situation may arise during shutdown. // TODO(rafaelw): Delay creation of background_page until the browser // is available. http://code.google.com/p/chromium/issues/detail?id=13284 return browser; } void ExtensionFunctionDispatcher::Dispatch( const ExtensionHostMsg_Request_Params& params, RenderViewHost* render_view_host) { ExtensionService* service = profile()->GetExtensionService(); extensions::ProcessMap* process_map = service->process_map(); if (!service || !process_map) return; const Extension* extension = service->extensions()->GetByID( params.extension_id); if (!extension) extension = service->extensions()->GetHostedAppByURL(ExtensionURLInfo( WebSecurityOrigin::createFromString(params.source_origin), params.source_url)); scoped_refptr function( CreateExtensionFunction(params, extension, render_view_host->process()->GetID(), *(service->process_map()), profile(), render_view_host, render_view_host->routing_id())); if (!function) { LogFailure(extension, params.name, kAccessDenied); return; } UIThreadExtensionFunction* function_ui = function->AsUIThreadExtensionFunction(); if (!function_ui) { NOTREACHED(); return; } function_ui->SetRenderViewHost(render_view_host); function_ui->set_dispatcher(AsWeakPtr()); function_ui->set_profile(profile_); function->set_include_incognito(service->CanCrossIncognito(extension)); ExtensionsQuotaService* quota = service->quota_service(); if (quota->Assess(extension->id(), function, ¶ms.arguments, base::TimeTicks::Now())) { // See crbug.com/39178. ExternalProtocolHandler::PermitLaunchUrl(); function->Run(); LogSuccess(extension, params); } else { function->OnQuotaExceeded(); LogFailure(extension, params.name, kQuotaExceeded); } } // static ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction( const ExtensionHostMsg_Request_Params& params, const Extension* extension, int requesting_process_id, const extensions::ProcessMap& process_map, void* profile, IPC::Message::Sender* ipc_sender, int routing_id) { if (!extension) { LOG(ERROR) << "Specified extension does not exist."; SendAccessDenied(ipc_sender, routing_id, params.request_id); return NULL; } if (ExtensionAPI::GetInstance()->IsPrivileged(params.name) && !process_map.Contains(extension->id(), requesting_process_id)) { LOG(ERROR) << "Extension API called from incorrect process " << requesting_process_id << " from URL " << params.source_url.spec(); SendAccessDenied(ipc_sender, routing_id, params.request_id); return NULL; } if (!extension->HasAPIPermission(params.name)) { LOG(ERROR) << "Extension " << extension->id() << " does not have " << "permission to function: " << params.name; SendAccessDenied(ipc_sender, routing_id, params.request_id); return NULL; } ExtensionFunction* function = FactoryRegistry::GetInstance()->NewFunction(params.name); function->SetArgs(¶ms.arguments); function->set_source_url(params.source_url); function->set_request_id(params.request_id); function->set_has_callback(params.has_callback); function->set_user_gesture(params.user_gesture); function->set_extension(extension); function->set_profile_id(profile); return function; } // static void ExtensionFunctionDispatcher::SendAccessDenied( IPC::Message::Sender* ipc_sender, int routing_id, int request_id) { ipc_sender->Send(new ExtensionMsg_Response( routing_id, request_id, false, std::string(), "Access to extension API denied.")); }