blob: bd523cb1c24e050a33805095d7f67c1a536bf220 [file] [log] [blame]
[email protected]e0813a392011-02-01 22:57:251// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]bfdffe2b2009-04-24 22:05:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_function_dispatcher.h"
6
[email protected]745feedb2010-08-02 04:08:077#include <map>
8
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/ref_counted.h"
10#include "base/memory/singleton.h"
[email protected]bfdffe2b2009-04-24 22:05:3511#include "base/process_util.h"
[email protected]bfdffe2b2009-04-24 22:05:3512#include "base/values.h"
[email protected]17d40f02010-07-01 01:18:0613#include "build/build_config.h"
[email protected]912256b32009-09-18 09:47:3514#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5615#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]c1d05aa2011-06-28 02:07:3016#include "chrome/browser/extensions/extension_app_api.h"
[email protected]9dd97bc2010-01-14 01:40:0417#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3518#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]ec9ac0df2009-10-01 18:06:4719#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]be9d9c82011-07-13 04:17:3120#include "chrome/browser/extensions/extension_chrome_auth_private_api.h"
[email protected]fa0624262011-06-09 14:17:3821#include "chrome/browser/extensions/extension_content_settings_api.h"
[email protected]2e3b5202010-03-23 06:52:4122#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2923#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2224#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3525#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0126#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2727#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2528#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0829#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2830#include "chrome/browser/extensions/extension_management_api.h"
[email protected]438772df2010-02-26 18:08:4331#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0932#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3333#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3434#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]902fd7b2011-07-27 18:42:3135#include "chrome/browser/extensions/extension_permissions_api.h"
[email protected]598bbcc2011-02-24 10:03:2536#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]381162b2010-01-28 17:29:3537#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4438#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0639#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2540#include "chrome/browser/extensions/extension_service.h"
[email protected]b7f853e282011-08-10 09:24:2041#include "chrome/browser/extensions/extension_settings_api.h"
[email protected]8b8e7c92010-08-19 18:05:5642#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3543#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1444#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2245#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]c63f2b72011-07-07 05:25:0046#include "chrome/browser/extensions/extension_tts_engine_api.h"
[email protected]c6e584c2011-05-18 11:58:4447#include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1048#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]557a51dd2011-07-26 12:17:1149#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]c41fe662011-02-15 01:19:2650#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2351#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0252#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]ed2b1002011-05-25 14:12:1053#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3354#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4255#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]71b73f02011-04-06 15:57:2956#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3357#include "chrome/browser/ui/browser_window.h"
[email protected]44c49c92011-03-28 16:17:2358#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4359#include "chrome/common/url_constants.h"
[email protected]f82d57b52011-04-27 19:13:1760#include "content/browser/child_process_security_policy.h"
[email protected]5de634712011-03-02 00:20:1961#include "content/browser/renderer_host/render_process_host.h"
62#include "content/browser/renderer_host/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1763#include "ipc/ipc_message.h"
64#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0265#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3566
[email protected]8f9d4eb2011-02-05 01:39:1067#if defined(TOOLKIT_VIEWS)
68#include "chrome/browser/extensions/extension_input_api.h"
69#endif
[email protected]d6833852010-08-20 18:00:4570
[email protected]27072cad2011-05-09 19:46:4071#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
72#include "chrome/browser/extensions/extension_input_ui_api.h"
73#endif
74
[email protected]61b55b62011-03-24 09:03:1075#if defined(OS_CHROMEOS)
[email protected]b6b805e92011-04-16 09:24:1476#include "chrome/browser/extensions/extension_file_browser_private_api.h"
[email protected]61b55b62011-03-24 09:03:1077#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
[email protected]14da67b02011-08-02 05:49:1378#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]be5bd5742011-07-14 07:21:0179#include "chrome/browser/extensions/extension_input_method_api.h"
[email protected]ce9802042011-05-27 10:11:4080#include "chrome/browser/extensions/extension_mediaplayer_private_api.h"
[email protected]61b55b62011-03-24 09:03:1081#endif
82
[email protected]bfdffe2b2009-04-24 22:05:3583// FactoryRegistry -------------------------------------------------------------
84
85namespace {
86
[email protected]b83e4602009-05-15 22:58:3387// Template for defining ExtensionFunctionFactory.
88template<class T>
89ExtensionFunction* NewExtensionFunction() {
90 return new T();
91}
[email protected]bfdffe2b2009-04-24 22:05:3592
[email protected]b83e4602009-05-15 22:58:3393// Contains a list of all known extension functions and allows clients to
94// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3595class FactoryRegistry {
96 public:
[email protected]8e8bb6d2010-12-13 08:18:5597 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3398 FactoryRegistry() { ResetFunctions(); }
99
100 // Resets all functions to their default values.
101 void ResetFunctions();
102
103 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:35104 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:33105
106 // Allows overriding of specific functions (e.g. for testing). Functions
107 // must be previously registered. Returns true if successful.
108 bool OverrideFunction(const std::string& name,
109 ExtensionFunctionFactory factory);
110
111 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35112 ExtensionFunction* NewFunction(const std::string& name);
113
114 private:
[email protected]61424c062009-10-14 23:14:59115 template<class T>
116 void RegisterFunction() {
117 factories_[T::function_name()] = &NewExtensionFunction<T>;
118 }
119
[email protected]bfdffe2b2009-04-24 22:05:35120 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
121 FactoryMap factories_;
122};
123
[email protected]8e8bb6d2010-12-13 08:18:55124FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35125 return Singleton<FactoryRegistry>::get();
126}
127
[email protected]b83e4602009-05-15 22:58:33128void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35129 // Register all functions here.
130
[email protected]e515f5d2009-05-05 03:05:00131 // Windows
[email protected]61424c062009-10-14 23:14:59132 RegisterFunction<GetWindowFunction>();
133 RegisterFunction<GetCurrentWindowFunction>();
134 RegisterFunction<GetLastFocusedWindowFunction>();
135 RegisterFunction<GetAllWindowsFunction>();
136 RegisterFunction<CreateWindowFunction>();
137 RegisterFunction<UpdateWindowFunction>();
138 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33139
[email protected]e515f5d2009-05-05 03:05:00140 // Tabs
[email protected]61424c062009-10-14 23:14:59141 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41142 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59143 RegisterFunction<GetSelectedTabFunction>();
144 RegisterFunction<GetAllTabsInWindowFunction>();
145 RegisterFunction<CreateTabFunction>();
146 RegisterFunction<UpdateTabFunction>();
147 RegisterFunction<MoveTabFunction>();
148 RegisterFunction<RemoveTabFunction>();
149 RegisterFunction<DetectTabLanguageFunction>();
150 RegisterFunction<CaptureVisibleTabFunction>();
151 RegisterFunction<TabsExecuteScriptFunction>();
152 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35153
[email protected]f7f3a5f2009-05-01 22:02:34154 // Page Actions.
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<EnablePageActionFunction>();
156 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46157 RegisterFunction<PageActionShowFunction>();
158 RegisterFunction<PageActionHideFunction>();
159 RegisterFunction<PageActionSetIconFunction>();
160 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29161 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34162
[email protected]ec9ac0df2009-10-01 18:06:47163 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59164 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24165 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59166 RegisterFunction<BrowserActionSetBadgeTextFunction>();
167 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50168 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47169
[email protected]f7f3a5f2009-05-01 22:02:34170 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59171 RegisterFunction<GetBookmarksFunction>();
172 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55173 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59174 RegisterFunction<GetBookmarkTreeFunction>();
[email protected]532a8c62011-06-03 21:30:01175 RegisterFunction<GetBookmarkSubTreeFunction>();
[email protected]61424c062009-10-14 23:14:59176 RegisterFunction<SearchBookmarksFunction>();
177 RegisterFunction<RemoveBookmarkFunction>();
178 RegisterFunction<RemoveTreeBookmarkFunction>();
179 RegisterFunction<CreateBookmarkFunction>();
180 RegisterFunction<MoveBookmarkFunction>();
181 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43182
[email protected]f34e79632010-03-17 02:34:08183 // Infobars.
184 RegisterFunction<ShowInfoBarFunction>();
185
[email protected]9dd97bc2010-01-14 01:40:04186 // BookmarkManager
187 RegisterFunction<CopyBookmarkManagerFunction>();
188 RegisterFunction<CutBookmarkManagerFunction>();
189 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57190 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56191 RegisterFunction<ImportBookmarksFunction>();
192 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18193 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04194 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16195 RegisterFunction<StartDragBookmarkManagerFunction>();
196 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31197 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]933ebbe2011-06-30 12:24:01198 RegisterFunction<CanEditBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04199
[email protected]de768a832009-10-30 05:25:01200 // History
201 RegisterFunction<AddUrlHistoryFunction>();
202 RegisterFunction<DeleteAllHistoryFunction>();
203 RegisterFunction<DeleteRangeHistoryFunction>();
204 RegisterFunction<DeleteUrlHistoryFunction>();
205 RegisterFunction<GetVisitsHistoryFunction>();
206 RegisterFunction<SearchHistoryFunction>();
207
[email protected]f5205412010-03-16 00:19:34208 // Idle
209 RegisterFunction<ExtensionIdleQueryStateFunction>();
210
[email protected]198bcfe2009-09-09 22:56:28211 // I18N.
[email protected]61424c062009-10-14 23:14:59212 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28213
[email protected]381162b2010-01-28 17:29:35214 // Processes.
[email protected]8a661f82010-10-19 21:47:11215 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35216
[email protected]438772df2010-02-26 18:08:43217 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03218 RegisterFunction<MetricsGetEnabledFunction>();
219 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34220 RegisterFunction<MetricsRecordUserActionFunction>();
221 RegisterFunction<MetricsRecordValueFunction>();
222 RegisterFunction<MetricsRecordPercentageFunction>();
223 RegisterFunction<MetricsRecordCountFunction>();
224 RegisterFunction<MetricsRecordSmallCountFunction>();
225 RegisterFunction<MetricsRecordMediumCountFunction>();
226 RegisterFunction<MetricsRecordTimeFunction>();
227 RegisterFunction<MetricsRecordMediumTimeFunction>();
228 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43229
[email protected]17d40f02010-07-01 01:18:06230 // RLZ.
231#if defined(OS_WIN)
232 RegisterFunction<RlzRecordProductEventFunction>();
233 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21234 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06235 RegisterFunction<RlzClearProductStateFunction>();
236#endif
237
[email protected]898bbd32010-05-18 18:52:29238 // Cookies.
239 RegisterFunction<GetCookieFunction>();
240 RegisterFunction<GetAllCookiesFunction>();
241 RegisterFunction<SetCookieFunction>();
242 RegisterFunction<RemoveCookieFunction>();
243 RegisterFunction<GetAllCookieStoresFunction>();
244
[email protected]25fd1b2e2009-08-17 20:57:14245 // Test.
[email protected]61424c062009-10-14 23:14:59246 RegisterFunction<ExtensionTestPassFunction>();
247 RegisterFunction<ExtensionTestFailFunction>();
248 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02249 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50250 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07251 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39252 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56253
254 // Accessibility.
255 RegisterFunction<GetFocusedControlFunction>();
256 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58257
[email protected]8abe0a32010-08-12 00:40:22258 // Text-to-speech.
[email protected]c63f2b72011-07-07 05:25:00259 RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>();
260 RegisterFunction<ExtensionTtsGetVoicesFunction>();
261 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22262 RegisterFunction<ExtensionTtsSpeakFunction>();
263 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22264
[email protected]2e3b5202010-03-23 06:52:41265 // Context Menus.
266 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15267 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41268 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15269 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33270
271 // Omnibox.
272 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53273 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44274
[email protected]8b8e7c92010-08-19 18:05:56275 // Sidebar.
276 RegisterFunction<CollapseSidebarFunction>();
277 RegisterFunction<ExpandSidebarFunction>();
278 RegisterFunction<GetStateSidebarFunction>();
279 RegisterFunction<HideSidebarFunction>();
280 RegisterFunction<NavigateSidebarFunction>();
281 RegisterFunction<SetBadgeTextSidebarFunction>();
282 RegisterFunction<SetIconSidebarFunction>();
283 RegisterFunction<SetTitleSidebarFunction>();
284 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45285
286#if defined(TOOLKIT_VIEWS)
287 // Input.
288 RegisterFunction<SendKeyboardEventInputFunction>();
289#endif
[email protected]cffd7892010-08-26 17:43:28290
[email protected]bcde7482011-05-23 19:29:12291#if defined(TOUCH_UI)
292 RegisterFunction<HideKeyboardFunction>();
[email protected]3c8faef2011-06-09 22:56:51293 RegisterFunction<SetKeyboardHeightFunction>();
[email protected]bcde7482011-05-23 19:29:12294#endif
295
[email protected]14da67b02011-08-02 05:49:13296#if defined(OS_CHROMEOS)
[email protected]27072cad2011-05-09 19:46:40297 // IME
[email protected]14da67b02011-08-02 05:49:13298 RegisterFunction<SetCompositionFunction>();
299 RegisterFunction<ClearCompositionFunction>();
300 RegisterFunction<CommitTextFunction>();
301 RegisterFunction<SetCandidateWindowPropertiesFunction>();
302 RegisterFunction<SetCandidatesFunction>();
303 RegisterFunction<SetCursorPositionFunction>();
304 RegisterFunction<SetMenuItemsFunction>();
305 RegisterFunction<UpdateMenuItemsFunction>();
306#if defined(TOUCH_UI)
[email protected]27072cad2011-05-09 19:46:40307 RegisterFunction<CandidateClickedInputUiFunction>();
308 RegisterFunction<CursorUpInputUiFunction>();
309 RegisterFunction<CursorDownInputUiFunction>();
310 RegisterFunction<PageUpInputUiFunction>();
311 RegisterFunction<PageDownInputUiFunction>();
312 RegisterFunction<RegisterInputUiFunction>();
313 RegisterFunction<PageUpInputUiFunction>();
314 RegisterFunction<PageDownInputUiFunction>();
315#endif
[email protected]14da67b02011-08-02 05:49:13316#endif
[email protected]27072cad2011-05-09 19:46:40317
[email protected]cffd7892010-08-26 17:43:28318 // Management.
319 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23320 RegisterFunction<GetExtensionByIdFunction>();
[email protected]925a54d2011-07-29 17:21:00321 RegisterFunction<GetPermissionWarningsByIdFunction>();
322 RegisterFunction<GetPermissionWarningsByManifestFunction>();
[email protected]e558ff82010-09-23 20:19:56323 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28324 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28325 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23326
[email protected]a65882c2010-11-12 15:15:09327 // Extension module.
328 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01329 RegisterFunction<IsAllowedIncognitoAccessFunction>();
330 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09331
[email protected]63cda0c2010-09-01 04:41:23332 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24333 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23334 RegisterFunction<GetStoreLoginFunction>();
335 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24336 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30337 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17338 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30339 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26340
[email protected]557a51dd2011-07-26 12:17:11341 // WebNavigation.
342 RegisterFunction<GetFrameFunction>();
343
[email protected]c41fe662011-02-15 01:19:26344 // WebRequest.
345 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48346 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25347
348 // Preferences.
349 RegisterFunction<GetPreferenceFunction>();
350 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27351 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22352
[email protected]b6b805e92011-04-16 09:24:14353 // ChromeOS-specific part of the API.
354#if defined(OS_CHROMEOS)
355 // Device Customization.
356 RegisterFunction<GetChromeosInfoFunction>();
357
358 // FileBrowserPrivate functions.
[email protected]94bda202011-04-18 23:31:00359 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14360 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00361 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14362 RegisterFunction<GetFileTasksFileBrowserFunction>();
[email protected]61334fa2011-06-16 01:01:40363 RegisterFunction<GetVolumeMetadataFunction>();
[email protected]b6b805e92011-04-16 09:24:14364 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]a6d06642011-06-02 05:55:23365 RegisterFunction<AddFileWatchBrowserFunction>();
366 RegisterFunction<RemoveFileWatchBrowserFunction>();
[email protected]9d4c2c52011-04-07 18:53:10367 RegisterFunction<SelectFileFunction>();
368 RegisterFunction<SelectFilesFunction>();
[email protected]928ae8852011-07-26 01:15:03369 RegisterFunction<AddMountFunction>();
370 RegisterFunction<RemoveMountFunction>();
371 RegisterFunction<GetMountPointsFunction>();
[email protected]94bda202011-04-18 23:31:00372 RegisterFunction<ViewFilesFunction>();
[email protected]f1852b52011-05-17 04:58:08373
[email protected]ce9802042011-05-27 10:11:40374 // Mediaplayer
375 RegisterFunction<PlayAtMediaplayerFunction>();
376 RegisterFunction<SetPlaybackErrorMediaplayerFunction>();
377 RegisterFunction<GetPlaylistMediaplayerFunction>();
378 RegisterFunction<TogglePlaylistPanelMediaplayerFunction>();
379 RegisterFunction<ToggleFullscreenMediaplayerFunction>();
[email protected]be5bd5742011-07-14 07:21:01380
381 // InputMethod
382 RegisterFunction<GetInputMethodFunction>();
383
[email protected]f1852b52011-05-17 04:58:08384#if defined(TOUCH_UI)
385 // Input
386 RegisterFunction<SendHandwritingStrokeFunction>();
387 RegisterFunction<CancelHandwritingStrokesFunction>();
388#endif
[email protected]61b55b62011-03-24 09:03:10389#endif
390
[email protected]c6e584c2011-05-18 11:58:44391 // Websocket to TCP proxy. Currently noop on anything other than ChromeOS.
392 RegisterFunction<WebSocketProxyPrivateGetPassportForTCPFunction>();
393
[email protected]91ba3312011-03-17 20:39:22394 // Debugger
395 RegisterFunction<AttachDebuggerFunction>();
396 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33397 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]fa0624262011-06-09 14:17:38398
[email protected]b7f853e282011-08-10 09:24:20399 // Settings
400 RegisterFunction<GetSettingsFunction>();
401 RegisterFunction<SetSettingsFunction>();
402 RegisterFunction<RemoveSettingsFunction>();
403 RegisterFunction<ClearSettingsFunction>();
404
[email protected]fa0624262011-06-09 14:17:38405 // Content settings.
[email protected]49ba4822011-06-15 18:41:56406 RegisterFunction<GetResourceIdentifiersFunction>();
[email protected]fa0624262011-06-09 14:17:38407 RegisterFunction<ClearContentSettingsFunction>();
408 RegisterFunction<GetContentSettingFunction>();
409 RegisterFunction<SetContentSettingFunction>();
[email protected]c1d05aa2011-06-28 02:07:30410
[email protected]be9d9c82011-07-13 04:17:31411 // ChromeAuth settings.
412 RegisterFunction<SetCloudPrintCredentialsFunction>();
413
[email protected]c1d05aa2011-06-28 02:07:30414 // Experimental App API.
415 RegisterFunction<AppNotifyFunction>();
416 RegisterFunction<AppClearAllNotificationsFunction>();
[email protected]902fd7b2011-07-27 18:42:31417
418 // Permissions
419 RegisterFunction<ContainsPermissionsFunction>();
420 RegisterFunction<GetAllPermissionsFunction>();
421 RegisterFunction<RemovePermissionsFunction>();
422 RegisterFunction<RequestPermissionsFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35423}
424
[email protected]b83e4602009-05-15 22:58:33425void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
426 for (FactoryMap::iterator iter = factories_.begin();
427 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35428 names->push_back(iter->first);
429 }
430}
431
[email protected]b83e4602009-05-15 22:58:33432bool FactoryRegistry::OverrideFunction(const std::string& name,
433 ExtensionFunctionFactory factory) {
434 FactoryMap::iterator iter = factories_.find(name);
435 if (iter == factories_.end()) {
436 return false;
437 } else {
438 iter->second = factory;
439 return true;
440 }
441}
442
[email protected]bfdffe2b2009-04-24 22:05:35443ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
444 FactoryMap::iterator iter = factories_.find(name);
445 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33446 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56447 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33448 return function;
[email protected]bfdffe2b2009-04-24 22:05:35449}
450
[email protected]b83e4602009-05-15 22:58:33451}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35452
453// ExtensionFunctionDispatcher -------------------------------------------------
454
455void ExtensionFunctionDispatcher::GetAllFunctionNames(
456 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55457 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35458}
459
[email protected]b83e4602009-05-15 22:58:33460bool ExtensionFunctionDispatcher::OverrideFunction(
461 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55462 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33463}
464
465void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55466 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33467}
468
[email protected]c357acb42011-06-09 20:52:42469// static
470void ExtensionFunctionDispatcher::DispatchOnIOThread(
471 const ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18472 void* profile,
[email protected]c357acb42011-06-09 20:52:42473 int render_process_id,
474 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
475 int routing_id,
476 const ExtensionHostMsg_Request_Params& params) {
477 const Extension* extension =
478 extension_info_map->extensions().GetByURL(params.source_url);
479
480 scoped_refptr<ExtensionFunction> function(
[email protected]673514522011-07-13 18:17:18481 CreateExtensionFunction(params, extension, profile, render_process_id,
[email protected]c357acb42011-06-09 20:52:42482 ipc_sender, routing_id));
483 if (!function)
484 return;
485
486 IOThreadExtensionFunction* function_io =
487 function->AsIOThreadExtensionFunction();
488 if (!function_io) {
489 NOTREACHED();
490 return;
491 }
492 function_io->set_ipc_sender(ipc_sender, routing_id);
493 function_io->set_extension_info_map(extension_info_map);
494 function->set_include_incognito(
495 extension_info_map->IsIncognitoEnabled(extension->id()));
496 function->Run();
497}
498
[email protected]c5dbef02011-05-13 05:06:09499ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
500 Delegate* delegate)
501 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27502 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35503}
504
[email protected]32dda362009-06-05 19:07:01505ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01506}
507
[email protected]0ec92032010-03-24 19:59:41508Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09509 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41510 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12511
[email protected]bc535ee52010-08-31 18:40:32512 // If the delegate has an associated browser, that is always the right answer.
513 if (browser)
514 return browser;
[email protected]9c45b7182009-08-04 16:44:43515
[email protected]bc535ee52010-08-31 18:40:32516 // Otherwise, try to default to a reasonable browser. If |include_incognito|
517 // is true, we will also search browsers in the incognito version of this
518 // profile. Note that the profile may already be incognito, in which case
519 // we will search the incognito version only, regardless of the value of
520 // |include_incognito|.
[email protected]9b62ecf2011-07-27 20:23:08521 Profile* profile = Profile::FromBrowserContext(
522 render_view_host->process()->browser_context());
[email protected]b35b26b32011-05-05 20:35:14523 browser = BrowserList::FindTabbedBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41524
[email protected]0ec92032010-03-24 19:59:41525 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42526 // a background_page onload chrome.tabs api call can make it into here
527 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41528 // A similar situation may arise during shutdown.
529 // TODO(rafaelw): Delay creation of background_page until the browser
530 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
531 return browser;
[email protected]b27257562009-11-16 23:28:26532}
533
[email protected]c5dbef02011-05-13 05:06:09534void ExtensionFunctionDispatcher::Dispatch(
535 const ExtensionHostMsg_Request_Params& params,
536 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09537 ExtensionService* service = profile()->GetExtensionService();
538 if (!service)
539 return;
540
541 if (!service->ExtensionBindingsAllowed(params.source_url)) {
542 LOG(ERROR) << "Extension bindings not allowed for URL: "
543 << params.source_url.spec();
[email protected]c357acb42011-06-09 20:52:42544 SendAccessDenied(render_view_host, render_view_host->routing_id(),
545 params.request_id);
[email protected]c5dbef02011-05-13 05:06:09546 return;
547 }
548
549 // TODO(aa): When we allow content scripts to call extension APIs, we will
550 // have to pass the extension ID explicitly here, not use the source URL.
551 const Extension* extension = service->GetExtensionByURL(params.source_url);
552 if (!extension)
553 extension = service->GetExtensionByWebExtent(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09554
[email protected]c357acb42011-06-09 20:52:42555 scoped_refptr<ExtensionFunction> function(CreateExtensionFunction(
[email protected]673514522011-07-13 18:17:18556 params, extension, profile_,
[email protected]c357acb42011-06-09 20:52:42557 render_view_host->process()->id(),
558 render_view_host, render_view_host->routing_id()));
559 if (!function)
[email protected]f82d57b52011-04-27 19:13:17560 return;
[email protected]f82d57b52011-04-27 19:13:17561
[email protected]a2aef2e2011-05-26 22:48:12562 UIThreadExtensionFunction* function_ui =
563 function->AsUIThreadExtensionFunction();
564 if (!function_ui) {
565 NOTREACHED();
566 return;
567 }
568 function_ui->SetRenderViewHost(render_view_host);
569 function_ui->set_dispatcher(AsWeakPtr());
570 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05571 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41572
[email protected]d13950e2009-12-04 01:43:02573 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09574 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56575 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26576 // See crbug.com/39178.
577 ExternalProtocolHandler::PermitLaunchUrl();
578
[email protected]d13950e2009-12-04 01:43:02579 function->Run();
580 } else {
[email protected]c5dbef02011-05-13 05:06:09581 render_view_host->Send(new ExtensionMsg_Response(
582 render_view_host->routing_id(), function->request_id(), false,
[email protected]553602e12011-04-05 17:01:18583 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02584 }
[email protected]bfdffe2b2009-04-24 22:05:35585}
586
[email protected]c357acb42011-06-09 20:52:42587// static
588ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
589 const ExtensionHostMsg_Request_Params& params,
590 const Extension* extension,
[email protected]673514522011-07-13 18:17:18591 void* profile,
[email protected]c357acb42011-06-09 20:52:42592 int render_process_id,
593 IPC::Message::Sender* ipc_sender,
594 int routing_id) {
595 // TODO(aa): It would be cool to use ExtensionProcessManager to track which
596 // processes are extension processes rather than ChildProcessSecurityPolicy.
597 // EPM has richer information: it not only knows which processes contain
598 // at least one extension, but it knows which extensions are inside and what
599 // permissions the have. So we would be able to enforce permissions more
600 // granularly.
601 if (!ChildProcessSecurityPolicy::GetInstance()->HasExtensionBindings(
602 render_process_id)) {
603 // TODO(aa): Allow content scripts access to low-threat extension APIs.
604 // See: crbug.com/80308.
605 LOG(ERROR) << "Extension API called from non-extension process.";
606 SendAccessDenied(ipc_sender, routing_id, params.request_id);
607 return NULL;
608 }
609
610 if (!extension) {
611 LOG(ERROR) << "Extension does not exist for URL: "
612 << params.source_url.spec();
613 SendAccessDenied(ipc_sender, routing_id, params.request_id);
614 return NULL;
615 }
616
[email protected]0d3e4a22011-06-23 19:02:52617 if (!extension->HasAPIPermission(params.name)) {
[email protected]c357acb42011-06-09 20:52:42618 LOG(ERROR) << "Extension " << extension->id() << " does not have "
619 << "permission to function: " << params.name;
620 SendAccessDenied(ipc_sender, routing_id, params.request_id);
621 return NULL;
622 }
623
624 ExtensionFunction* function =
625 FactoryRegistry::GetInstance()->NewFunction(params.name);
626 function->SetArgs(&params.arguments);
627 function->set_source_url(params.source_url);
628 function->set_request_id(params.request_id);
629 function->set_has_callback(params.has_callback);
630 function->set_user_gesture(params.user_gesture);
631 function->set_extension(extension);
[email protected]673514522011-07-13 18:17:18632 function->set_profile(profile);
[email protected]c357acb42011-06-09 20:52:42633 return function;
634}
635
636// static
[email protected]c5dbef02011-05-13 05:06:09637void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]c357acb42011-06-09 20:52:42638 IPC::Message::Sender* ipc_sender, int routing_id, int request_id) {
639 ipc_sender->Send(new ExtensionMsg_Response(
640 routing_id, request_id, false, std::string(),
[email protected]c5dbef02011-05-13 05:06:09641 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35642}