blob: 0eb742ae2127b6c97b65f82cecbe4a6629649c27 [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]9dd97bc2010-01-14 01:40:0416#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3517#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]ec9ac0df2009-10-01 18:06:4718#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]fa0624262011-06-09 14:17:3819#include "chrome/browser/extensions/extension_content_settings_api.h"
[email protected]2e3b5202010-03-23 06:52:4120#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2921#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2222#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3523#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0124#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2725#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2526#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0827#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2828#include "chrome/browser/extensions/extension_management_api.h"
[email protected]438772df2010-02-26 18:08:4329#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0930#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3331#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3432#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]598bbcc2011-02-24 10:03:2533#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]381162b2010-01-28 17:29:3534#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4435#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0636#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2537#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5638#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3539#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1440#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2241#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]c6e584c2011-05-18 11:58:4442#include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1043#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2644#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2345#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0246#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]ed2b1002011-05-25 14:12:1047#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3348#include "chrome/browser/profiles/profile.h"
[email protected]71b73f02011-04-06 15:57:2949#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3350#include "chrome/browser/ui/browser_window.h"
[email protected]44c49c92011-03-28 16:17:2351#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4352#include "chrome/common/url_constants.h"
[email protected]f82d57b52011-04-27 19:13:1753#include "content/browser/child_process_security_policy.h"
[email protected]5de634712011-03-02 00:20:1954#include "content/browser/renderer_host/render_process_host.h"
55#include "content/browser/renderer_host/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1756#include "ipc/ipc_message.h"
57#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0258#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3559
[email protected]8f9d4eb2011-02-05 01:39:1060#if defined(TOOLKIT_VIEWS)
61#include "chrome/browser/extensions/extension_input_api.h"
62#endif
[email protected]d6833852010-08-20 18:00:4563
[email protected]27072cad2011-05-09 19:46:4064#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
65#include "chrome/browser/extensions/extension_input_ui_api.h"
66#endif
67
[email protected]61b55b62011-03-24 09:03:1068#if defined(OS_CHROMEOS)
[email protected]b6b805e92011-04-16 09:24:1469#include "chrome/browser/extensions/extension_file_browser_private_api.h"
[email protected]61b55b62011-03-24 09:03:1070#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
[email protected]ce9802042011-05-27 10:11:4071#include "chrome/browser/extensions/extension_mediaplayer_private_api.h"
[email protected]61b55b62011-03-24 09:03:1072#endif
73
[email protected]bfdffe2b2009-04-24 22:05:3574// FactoryRegistry -------------------------------------------------------------
75
76namespace {
77
[email protected]b83e4602009-05-15 22:58:3378// Template for defining ExtensionFunctionFactory.
79template<class T>
80ExtensionFunction* NewExtensionFunction() {
81 return new T();
82}
[email protected]bfdffe2b2009-04-24 22:05:3583
[email protected]b83e4602009-05-15 22:58:3384// Contains a list of all known extension functions and allows clients to
85// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3586class FactoryRegistry {
87 public:
[email protected]8e8bb6d2010-12-13 08:18:5588 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3389 FactoryRegistry() { ResetFunctions(); }
90
91 // Resets all functions to their default values.
92 void ResetFunctions();
93
94 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3595 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3396
97 // Allows overriding of specific functions (e.g. for testing). Functions
98 // must be previously registered. Returns true if successful.
99 bool OverrideFunction(const std::string& name,
100 ExtensionFunctionFactory factory);
101
102 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35103 ExtensionFunction* NewFunction(const std::string& name);
104
105 private:
[email protected]61424c062009-10-14 23:14:59106 template<class T>
107 void RegisterFunction() {
108 factories_[T::function_name()] = &NewExtensionFunction<T>;
109 }
110
[email protected]bfdffe2b2009-04-24 22:05:35111 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
112 FactoryMap factories_;
113};
114
[email protected]8e8bb6d2010-12-13 08:18:55115FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35116 return Singleton<FactoryRegistry>::get();
117}
118
[email protected]b83e4602009-05-15 22:58:33119void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35120 // Register all functions here.
121
[email protected]e515f5d2009-05-05 03:05:00122 // Windows
[email protected]61424c062009-10-14 23:14:59123 RegisterFunction<GetWindowFunction>();
124 RegisterFunction<GetCurrentWindowFunction>();
125 RegisterFunction<GetLastFocusedWindowFunction>();
126 RegisterFunction<GetAllWindowsFunction>();
127 RegisterFunction<CreateWindowFunction>();
128 RegisterFunction<UpdateWindowFunction>();
129 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33130
[email protected]e515f5d2009-05-05 03:05:00131 // Tabs
[email protected]61424c062009-10-14 23:14:59132 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41133 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59134 RegisterFunction<GetSelectedTabFunction>();
135 RegisterFunction<GetAllTabsInWindowFunction>();
136 RegisterFunction<CreateTabFunction>();
137 RegisterFunction<UpdateTabFunction>();
138 RegisterFunction<MoveTabFunction>();
139 RegisterFunction<RemoveTabFunction>();
140 RegisterFunction<DetectTabLanguageFunction>();
141 RegisterFunction<CaptureVisibleTabFunction>();
142 RegisterFunction<TabsExecuteScriptFunction>();
143 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35144
[email protected]f7f3a5f2009-05-01 22:02:34145 // Page Actions.
[email protected]61424c062009-10-14 23:14:59146 RegisterFunction<EnablePageActionFunction>();
147 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46148 RegisterFunction<PageActionShowFunction>();
149 RegisterFunction<PageActionHideFunction>();
150 RegisterFunction<PageActionSetIconFunction>();
151 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29152 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34153
[email protected]ec9ac0df2009-10-01 18:06:47154 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24156 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59157 RegisterFunction<BrowserActionSetBadgeTextFunction>();
158 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50159 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47160
[email protected]f7f3a5f2009-05-01 22:02:34161 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59162 RegisterFunction<GetBookmarksFunction>();
163 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55164 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59165 RegisterFunction<GetBookmarkTreeFunction>();
[email protected]532a8c62011-06-03 21:30:01166 RegisterFunction<GetBookmarkSubTreeFunction>();
[email protected]61424c062009-10-14 23:14:59167 RegisterFunction<SearchBookmarksFunction>();
168 RegisterFunction<RemoveBookmarkFunction>();
169 RegisterFunction<RemoveTreeBookmarkFunction>();
170 RegisterFunction<CreateBookmarkFunction>();
171 RegisterFunction<MoveBookmarkFunction>();
172 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43173
[email protected]f34e79632010-03-17 02:34:08174 // Infobars.
175 RegisterFunction<ShowInfoBarFunction>();
176
[email protected]9dd97bc2010-01-14 01:40:04177 // BookmarkManager
178 RegisterFunction<CopyBookmarkManagerFunction>();
179 RegisterFunction<CutBookmarkManagerFunction>();
180 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57181 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56182 RegisterFunction<ImportBookmarksFunction>();
183 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18184 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04185 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16186 RegisterFunction<StartDragBookmarkManagerFunction>();
187 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31188 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04189
[email protected]de768a832009-10-30 05:25:01190 // History
191 RegisterFunction<AddUrlHistoryFunction>();
192 RegisterFunction<DeleteAllHistoryFunction>();
193 RegisterFunction<DeleteRangeHistoryFunction>();
194 RegisterFunction<DeleteUrlHistoryFunction>();
195 RegisterFunction<GetVisitsHistoryFunction>();
196 RegisterFunction<SearchHistoryFunction>();
197
[email protected]f5205412010-03-16 00:19:34198 // Idle
199 RegisterFunction<ExtensionIdleQueryStateFunction>();
200
[email protected]198bcfe2009-09-09 22:56:28201 // I18N.
[email protected]61424c062009-10-14 23:14:59202 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28203
[email protected]381162b2010-01-28 17:29:35204 // Processes.
[email protected]8a661f82010-10-19 21:47:11205 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35206
[email protected]438772df2010-02-26 18:08:43207 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03208 RegisterFunction<MetricsGetEnabledFunction>();
209 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34210 RegisterFunction<MetricsRecordUserActionFunction>();
211 RegisterFunction<MetricsRecordValueFunction>();
212 RegisterFunction<MetricsRecordPercentageFunction>();
213 RegisterFunction<MetricsRecordCountFunction>();
214 RegisterFunction<MetricsRecordSmallCountFunction>();
215 RegisterFunction<MetricsRecordMediumCountFunction>();
216 RegisterFunction<MetricsRecordTimeFunction>();
217 RegisterFunction<MetricsRecordMediumTimeFunction>();
218 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43219
[email protected]17d40f02010-07-01 01:18:06220 // RLZ.
221#if defined(OS_WIN)
222 RegisterFunction<RlzRecordProductEventFunction>();
223 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21224 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06225 RegisterFunction<RlzClearProductStateFunction>();
226#endif
227
[email protected]898bbd32010-05-18 18:52:29228 // Cookies.
229 RegisterFunction<GetCookieFunction>();
230 RegisterFunction<GetAllCookiesFunction>();
231 RegisterFunction<SetCookieFunction>();
232 RegisterFunction<RemoveCookieFunction>();
233 RegisterFunction<GetAllCookieStoresFunction>();
234
[email protected]25fd1b2e2009-08-17 20:57:14235 // Test.
[email protected]61424c062009-10-14 23:14:59236 RegisterFunction<ExtensionTestPassFunction>();
237 RegisterFunction<ExtensionTestFailFunction>();
238 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02239 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50240 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07241 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39242 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56243
244 // Accessibility.
245 RegisterFunction<GetFocusedControlFunction>();
246 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58247
[email protected]8abe0a32010-08-12 00:40:22248 // Text-to-speech.
249 RegisterFunction<ExtensionTtsSpeakFunction>();
250 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
251 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56252 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22253
[email protected]2e3b5202010-03-23 06:52:41254 // Context Menus.
255 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15256 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41257 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15258 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33259
260 // Omnibox.
261 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53262 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44263
[email protected]8b8e7c92010-08-19 18:05:56264 // Sidebar.
265 RegisterFunction<CollapseSidebarFunction>();
266 RegisterFunction<ExpandSidebarFunction>();
267 RegisterFunction<GetStateSidebarFunction>();
268 RegisterFunction<HideSidebarFunction>();
269 RegisterFunction<NavigateSidebarFunction>();
270 RegisterFunction<SetBadgeTextSidebarFunction>();
271 RegisterFunction<SetIconSidebarFunction>();
272 RegisterFunction<SetTitleSidebarFunction>();
273 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45274
275#if defined(TOOLKIT_VIEWS)
276 // Input.
277 RegisterFunction<SendKeyboardEventInputFunction>();
278#endif
[email protected]cffd7892010-08-26 17:43:28279
[email protected]bcde7482011-05-23 19:29:12280#if defined(TOUCH_UI)
281 RegisterFunction<HideKeyboardFunction>();
282#endif
283
[email protected]27072cad2011-05-09 19:46:40284#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
285 // IME
286 RegisterFunction<CandidateClickedInputUiFunction>();
287 RegisterFunction<CursorUpInputUiFunction>();
288 RegisterFunction<CursorDownInputUiFunction>();
289 RegisterFunction<PageUpInputUiFunction>();
290 RegisterFunction<PageDownInputUiFunction>();
291 RegisterFunction<RegisterInputUiFunction>();
292 RegisterFunction<PageUpInputUiFunction>();
293 RegisterFunction<PageDownInputUiFunction>();
294#endif
295
[email protected]cffd7892010-08-26 17:43:28296 // Management.
297 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23298 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56299 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28300 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28301 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23302
[email protected]a65882c2010-11-12 15:15:09303 // Extension module.
304 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01305 RegisterFunction<IsAllowedIncognitoAccessFunction>();
306 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09307
[email protected]63cda0c2010-09-01 04:41:23308 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24309 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23310 RegisterFunction<GetStoreLoginFunction>();
311 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24312 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30313 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17314 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30315 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26316
317 // WebRequest.
318 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48319 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25320
321 // Preferences.
322 RegisterFunction<GetPreferenceFunction>();
323 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27324 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22325
[email protected]b6b805e92011-04-16 09:24:14326 // ChromeOS-specific part of the API.
327#if defined(OS_CHROMEOS)
328 // Device Customization.
329 RegisterFunction<GetChromeosInfoFunction>();
330
331 // FileBrowserPrivate functions.
[email protected]94bda202011-04-18 23:31:00332 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14333 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00334 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14335 RegisterFunction<GetFileTasksFileBrowserFunction>();
336 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]a6d06642011-06-02 05:55:23337 RegisterFunction<AddFileWatchBrowserFunction>();
338 RegisterFunction<RemoveFileWatchBrowserFunction>();
[email protected]9d4c2c52011-04-07 18:53:10339 RegisterFunction<SelectFileFunction>();
340 RegisterFunction<SelectFilesFunction>();
[email protected]94bda202011-04-18 23:31:00341 RegisterFunction<ViewFilesFunction>();
[email protected]f1852b52011-05-17 04:58:08342
[email protected]ce9802042011-05-27 10:11:40343 // Mediaplayer
344 RegisterFunction<PlayAtMediaplayerFunction>();
345 RegisterFunction<SetPlaybackErrorMediaplayerFunction>();
346 RegisterFunction<GetPlaylistMediaplayerFunction>();
347 RegisterFunction<TogglePlaylistPanelMediaplayerFunction>();
348 RegisterFunction<ToggleFullscreenMediaplayerFunction>();
[email protected]f1852b52011-05-17 04:58:08349#if defined(TOUCH_UI)
350 // Input
351 RegisterFunction<SendHandwritingStrokeFunction>();
352 RegisterFunction<CancelHandwritingStrokesFunction>();
353#endif
[email protected]61b55b62011-03-24 09:03:10354#endif
355
[email protected]c6e584c2011-05-18 11:58:44356 // Websocket to TCP proxy. Currently noop on anything other than ChromeOS.
357 RegisterFunction<WebSocketProxyPrivateGetPassportForTCPFunction>();
358
[email protected]91ba3312011-03-17 20:39:22359 // Debugger
360 RegisterFunction<AttachDebuggerFunction>();
361 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33362 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]fa0624262011-06-09 14:17:38363
364 // Content settings.
365 RegisterFunction<ClearContentSettingsFunction>();
366 RegisterFunction<GetContentSettingFunction>();
367 RegisterFunction<SetContentSettingFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35368}
369
[email protected]b83e4602009-05-15 22:58:33370void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
371 for (FactoryMap::iterator iter = factories_.begin();
372 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35373 names->push_back(iter->first);
374 }
375}
376
[email protected]b83e4602009-05-15 22:58:33377bool FactoryRegistry::OverrideFunction(const std::string& name,
378 ExtensionFunctionFactory factory) {
379 FactoryMap::iterator iter = factories_.find(name);
380 if (iter == factories_.end()) {
381 return false;
382 } else {
383 iter->second = factory;
384 return true;
385 }
386}
387
[email protected]bfdffe2b2009-04-24 22:05:35388ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
389 FactoryMap::iterator iter = factories_.find(name);
390 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33391 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56392 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33393 return function;
[email protected]bfdffe2b2009-04-24 22:05:35394}
395
[email protected]b83e4602009-05-15 22:58:33396}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35397
398// ExtensionFunctionDispatcher -------------------------------------------------
399
400void ExtensionFunctionDispatcher::GetAllFunctionNames(
401 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55402 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35403}
404
[email protected]b83e4602009-05-15 22:58:33405bool ExtensionFunctionDispatcher::OverrideFunction(
406 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55407 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33408}
409
410void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55411 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33412}
413
[email protected]c5dbef02011-05-13 05:06:09414ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
415 Delegate* delegate)
416 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27417 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35418}
419
[email protected]32dda362009-06-05 19:07:01420ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01421}
422
[email protected]0ec92032010-03-24 19:59:41423Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09424 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41425 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12426
[email protected]bc535ee52010-08-31 18:40:32427 // If the delegate has an associated browser, that is always the right answer.
428 if (browser)
429 return browser;
[email protected]9c45b7182009-08-04 16:44:43430
[email protected]bc535ee52010-08-31 18:40:32431 // Otherwise, try to default to a reasonable browser. If |include_incognito|
432 // is true, we will also search browsers in the incognito version of this
433 // profile. Note that the profile may already be incognito, in which case
434 // we will search the incognito version only, regardless of the value of
435 // |include_incognito|.
[email protected]c5dbef02011-05-13 05:06:09436 Profile* profile = render_view_host->process()->profile();
[email protected]b35b26b32011-05-05 20:35:14437 browser = BrowserList::FindTabbedBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41438
[email protected]0ec92032010-03-24 19:59:41439 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42440 // a background_page onload chrome.tabs api call can make it into here
441 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41442 // A similar situation may arise during shutdown.
443 // TODO(rafaelw): Delay creation of background_page until the browser
444 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
445 return browser;
[email protected]b27257562009-11-16 23:28:26446}
447
[email protected]c5dbef02011-05-13 05:06:09448void ExtensionFunctionDispatcher::Dispatch(
449 const ExtensionHostMsg_Request_Params& params,
450 RenderViewHost* render_view_host) {
451 // TODO(aa): It would be cool to use ExtensionProcessManager to track which
452 // processes are extension processes rather than ChildProcessSecurityPolicy.
453 // EPM has richer information: it not only knows which processes contain
454 // at least one extension, but it knows which extensions are inside and what
455 // permissions the have. So we would be able to enforce permissions more
456 // granularly.
457 if (!ChildProcessSecurityPolicy::GetInstance()->HasExtensionBindings(
458 render_view_host->process()->id())) {
459 // TODO(aa): Allow content scripts access to low-threat extension APIs.
460 // See: crbug.com/80308.
461 LOG(ERROR) << "Extension API called from non-extension process.";
462 SendAccessDenied(render_view_host, params.request_id);
463 return;
464 }
[email protected]f82d57b52011-04-27 19:13:17465
[email protected]c5dbef02011-05-13 05:06:09466 ExtensionService* service = profile()->GetExtensionService();
467 if (!service)
468 return;
469
470 if (!service->ExtensionBindingsAllowed(params.source_url)) {
471 LOG(ERROR) << "Extension bindings not allowed for URL: "
472 << params.source_url.spec();
473 SendAccessDenied(render_view_host, params.request_id);
474 return;
475 }
476
477 // TODO(aa): When we allow content scripts to call extension APIs, we will
478 // have to pass the extension ID explicitly here, not use the source URL.
479 const Extension* extension = service->GetExtensionByURL(params.source_url);
480 if (!extension)
481 extension = service->GetExtensionByWebExtent(params.source_url);
482 if (!extension) {
483 LOG(ERROR) << "Extension does not exist for URL: "
484 << params.source_url.spec();
485 SendAccessDenied(render_view_host, params.request_id);
486 return;
487 }
488
489 if (!extension->HasApiPermission(params.name)) {
490 LOG(ERROR) << "Extension " << extension->id() << " does not have "
491 << "permission to function: " << params.name;
492 SendAccessDenied(render_view_host, params.request_id);
[email protected]f82d57b52011-04-27 19:13:17493 return;
494 }
495
[email protected]32dda362009-06-05 19:07:01496 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55497 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]a2aef2e2011-05-26 22:48:12498 UIThreadExtensionFunction* function_ui =
499 function->AsUIThreadExtensionFunction();
500 if (!function_ui) {
501 NOTREACHED();
502 return;
503 }
504 function_ui->SetRenderViewHost(render_view_host);
505 function_ui->set_dispatcher(AsWeakPtr());
506 function_ui->set_profile(profile_);
507
508 function->set_profile_id(profile_->GetRuntimeId());
509 function->set_extension(extension);
[email protected]8b8e7c92010-08-19 18:05:56510 function->SetArgs(&params.arguments);
511 function->set_source_url(params.source_url);
512 function->set_request_id(params.request_id);
513 function->set_has_callback(params.has_callback);
514 function->set_user_gesture(params.user_gesture);
[email protected]2a8f24e2010-11-03 21:37:05515 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41516
[email protected]d13950e2009-12-04 01:43:02517 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09518 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56519 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26520 // See crbug.com/39178.
521 ExternalProtocolHandler::PermitLaunchUrl();
522
[email protected]d13950e2009-12-04 01:43:02523 function->Run();
524 } else {
[email protected]c5dbef02011-05-13 05:06:09525 render_view_host->Send(new ExtensionMsg_Response(
526 render_view_host->routing_id(), function->request_id(), false,
[email protected]553602e12011-04-05 17:01:18527 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02528 }
[email protected]bfdffe2b2009-04-24 22:05:35529}
530
[email protected]c5dbef02011-05-13 05:06:09531void ExtensionFunctionDispatcher::SendAccessDenied(
532 RenderViewHost* render_view_host, int request_id) {
533 render_view_host->Send(new ExtensionMsg_Response(
534 render_view_host->routing_id(), request_id, false, std::string(),
535 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35536}