blob: ec7be576bf24b9121fe3beacc71837ab5ff48695 [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]fa0624262011-06-09 14:17:3820#include "chrome/browser/extensions/extension_content_settings_api.h"
[email protected]2e3b5202010-03-23 06:52:4121#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2922#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2223#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3524#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0125#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2726#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2527#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0828#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2829#include "chrome/browser/extensions/extension_management_api.h"
[email protected]438772df2010-02-26 18:08:4330#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0931#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3332#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3433#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]598bbcc2011-02-24 10:03:2534#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]381162b2010-01-28 17:29:3535#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4436#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0637#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2538#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5639#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3540#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1441#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2242#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]c6e584c2011-05-18 11:58:4443#include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1044#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2645#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2346#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0247#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]ed2b1002011-05-25 14:12:1048#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3349#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4250#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]71b73f02011-04-06 15:57:2951#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3352#include "chrome/browser/ui/browser_window.h"
[email protected]44c49c92011-03-28 16:17:2353#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4354#include "chrome/common/url_constants.h"
[email protected]f82d57b52011-04-27 19:13:1755#include "content/browser/child_process_security_policy.h"
[email protected]5de634712011-03-02 00:20:1956#include "content/browser/renderer_host/render_process_host.h"
57#include "content/browser/renderer_host/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1758#include "ipc/ipc_message.h"
59#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0260#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3561
[email protected]8f9d4eb2011-02-05 01:39:1062#if defined(TOOLKIT_VIEWS)
63#include "chrome/browser/extensions/extension_input_api.h"
64#endif
[email protected]d6833852010-08-20 18:00:4565
[email protected]27072cad2011-05-09 19:46:4066#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
67#include "chrome/browser/extensions/extension_input_ui_api.h"
68#endif
69
[email protected]61b55b62011-03-24 09:03:1070#if defined(OS_CHROMEOS)
[email protected]b6b805e92011-04-16 09:24:1471#include "chrome/browser/extensions/extension_file_browser_private_api.h"
[email protected]61b55b62011-03-24 09:03:1072#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
[email protected]ce9802042011-05-27 10:11:4073#include "chrome/browser/extensions/extension_mediaplayer_private_api.h"
[email protected]61b55b62011-03-24 09:03:1074#endif
75
[email protected]bfdffe2b2009-04-24 22:05:3576// FactoryRegistry -------------------------------------------------------------
77
78namespace {
79
[email protected]b83e4602009-05-15 22:58:3380// Template for defining ExtensionFunctionFactory.
81template<class T>
82ExtensionFunction* NewExtensionFunction() {
83 return new T();
84}
[email protected]bfdffe2b2009-04-24 22:05:3585
[email protected]b83e4602009-05-15 22:58:3386// Contains a list of all known extension functions and allows clients to
87// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3588class FactoryRegistry {
89 public:
[email protected]8e8bb6d2010-12-13 08:18:5590 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3391 FactoryRegistry() { ResetFunctions(); }
92
93 // Resets all functions to their default values.
94 void ResetFunctions();
95
96 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3597 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3398
99 // Allows overriding of specific functions (e.g. for testing). Functions
100 // must be previously registered. Returns true if successful.
101 bool OverrideFunction(const std::string& name,
102 ExtensionFunctionFactory factory);
103
104 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35105 ExtensionFunction* NewFunction(const std::string& name);
106
107 private:
[email protected]61424c062009-10-14 23:14:59108 template<class T>
109 void RegisterFunction() {
110 factories_[T::function_name()] = &NewExtensionFunction<T>;
111 }
112
[email protected]bfdffe2b2009-04-24 22:05:35113 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
114 FactoryMap factories_;
115};
116
[email protected]8e8bb6d2010-12-13 08:18:55117FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35118 return Singleton<FactoryRegistry>::get();
119}
120
[email protected]b83e4602009-05-15 22:58:33121void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35122 // Register all functions here.
123
[email protected]e515f5d2009-05-05 03:05:00124 // Windows
[email protected]61424c062009-10-14 23:14:59125 RegisterFunction<GetWindowFunction>();
126 RegisterFunction<GetCurrentWindowFunction>();
127 RegisterFunction<GetLastFocusedWindowFunction>();
128 RegisterFunction<GetAllWindowsFunction>();
129 RegisterFunction<CreateWindowFunction>();
130 RegisterFunction<UpdateWindowFunction>();
131 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33132
[email protected]e515f5d2009-05-05 03:05:00133 // Tabs
[email protected]61424c062009-10-14 23:14:59134 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41135 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59136 RegisterFunction<GetSelectedTabFunction>();
137 RegisterFunction<GetAllTabsInWindowFunction>();
138 RegisterFunction<CreateTabFunction>();
139 RegisterFunction<UpdateTabFunction>();
140 RegisterFunction<MoveTabFunction>();
141 RegisterFunction<RemoveTabFunction>();
142 RegisterFunction<DetectTabLanguageFunction>();
143 RegisterFunction<CaptureVisibleTabFunction>();
144 RegisterFunction<TabsExecuteScriptFunction>();
145 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35146
[email protected]f7f3a5f2009-05-01 22:02:34147 // Page Actions.
[email protected]61424c062009-10-14 23:14:59148 RegisterFunction<EnablePageActionFunction>();
149 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46150 RegisterFunction<PageActionShowFunction>();
151 RegisterFunction<PageActionHideFunction>();
152 RegisterFunction<PageActionSetIconFunction>();
153 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29154 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34155
[email protected]ec9ac0df2009-10-01 18:06:47156 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59157 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24158 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59159 RegisterFunction<BrowserActionSetBadgeTextFunction>();
160 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50161 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47162
[email protected]f7f3a5f2009-05-01 22:02:34163 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59164 RegisterFunction<GetBookmarksFunction>();
165 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55166 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59167 RegisterFunction<GetBookmarkTreeFunction>();
[email protected]532a8c62011-06-03 21:30:01168 RegisterFunction<GetBookmarkSubTreeFunction>();
[email protected]61424c062009-10-14 23:14:59169 RegisterFunction<SearchBookmarksFunction>();
170 RegisterFunction<RemoveBookmarkFunction>();
171 RegisterFunction<RemoveTreeBookmarkFunction>();
172 RegisterFunction<CreateBookmarkFunction>();
173 RegisterFunction<MoveBookmarkFunction>();
174 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43175
[email protected]f34e79632010-03-17 02:34:08176 // Infobars.
177 RegisterFunction<ShowInfoBarFunction>();
178
[email protected]9dd97bc2010-01-14 01:40:04179 // BookmarkManager
180 RegisterFunction<CopyBookmarkManagerFunction>();
181 RegisterFunction<CutBookmarkManagerFunction>();
182 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57183 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56184 RegisterFunction<ImportBookmarksFunction>();
185 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18186 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04187 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16188 RegisterFunction<StartDragBookmarkManagerFunction>();
189 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31190 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04191
[email protected]de768a832009-10-30 05:25:01192 // History
193 RegisterFunction<AddUrlHistoryFunction>();
194 RegisterFunction<DeleteAllHistoryFunction>();
195 RegisterFunction<DeleteRangeHistoryFunction>();
196 RegisterFunction<DeleteUrlHistoryFunction>();
197 RegisterFunction<GetVisitsHistoryFunction>();
198 RegisterFunction<SearchHistoryFunction>();
199
[email protected]f5205412010-03-16 00:19:34200 // Idle
201 RegisterFunction<ExtensionIdleQueryStateFunction>();
202
[email protected]198bcfe2009-09-09 22:56:28203 // I18N.
[email protected]61424c062009-10-14 23:14:59204 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28205
[email protected]381162b2010-01-28 17:29:35206 // Processes.
[email protected]8a661f82010-10-19 21:47:11207 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35208
[email protected]438772df2010-02-26 18:08:43209 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03210 RegisterFunction<MetricsGetEnabledFunction>();
211 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34212 RegisterFunction<MetricsRecordUserActionFunction>();
213 RegisterFunction<MetricsRecordValueFunction>();
214 RegisterFunction<MetricsRecordPercentageFunction>();
215 RegisterFunction<MetricsRecordCountFunction>();
216 RegisterFunction<MetricsRecordSmallCountFunction>();
217 RegisterFunction<MetricsRecordMediumCountFunction>();
218 RegisterFunction<MetricsRecordTimeFunction>();
219 RegisterFunction<MetricsRecordMediumTimeFunction>();
220 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43221
[email protected]17d40f02010-07-01 01:18:06222 // RLZ.
223#if defined(OS_WIN)
224 RegisterFunction<RlzRecordProductEventFunction>();
225 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21226 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06227 RegisterFunction<RlzClearProductStateFunction>();
228#endif
229
[email protected]898bbd32010-05-18 18:52:29230 // Cookies.
231 RegisterFunction<GetCookieFunction>();
232 RegisterFunction<GetAllCookiesFunction>();
233 RegisterFunction<SetCookieFunction>();
234 RegisterFunction<RemoveCookieFunction>();
235 RegisterFunction<GetAllCookieStoresFunction>();
236
[email protected]25fd1b2e2009-08-17 20:57:14237 // Test.
[email protected]61424c062009-10-14 23:14:59238 RegisterFunction<ExtensionTestPassFunction>();
239 RegisterFunction<ExtensionTestFailFunction>();
240 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02241 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50242 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07243 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39244 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56245
246 // Accessibility.
247 RegisterFunction<GetFocusedControlFunction>();
248 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58249
[email protected]8abe0a32010-08-12 00:40:22250 // Text-to-speech.
251 RegisterFunction<ExtensionTtsSpeakFunction>();
252 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
253 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56254 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22255
[email protected]2e3b5202010-03-23 06:52:41256 // Context Menus.
257 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15258 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41259 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15260 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33261
262 // Omnibox.
263 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53264 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44265
[email protected]8b8e7c92010-08-19 18:05:56266 // Sidebar.
267 RegisterFunction<CollapseSidebarFunction>();
268 RegisterFunction<ExpandSidebarFunction>();
269 RegisterFunction<GetStateSidebarFunction>();
270 RegisterFunction<HideSidebarFunction>();
271 RegisterFunction<NavigateSidebarFunction>();
272 RegisterFunction<SetBadgeTextSidebarFunction>();
273 RegisterFunction<SetIconSidebarFunction>();
274 RegisterFunction<SetTitleSidebarFunction>();
275 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45276
277#if defined(TOOLKIT_VIEWS)
278 // Input.
279 RegisterFunction<SendKeyboardEventInputFunction>();
280#endif
[email protected]cffd7892010-08-26 17:43:28281
[email protected]bcde7482011-05-23 19:29:12282#if defined(TOUCH_UI)
283 RegisterFunction<HideKeyboardFunction>();
[email protected]3c8faef2011-06-09 22:56:51284 RegisterFunction<SetKeyboardHeightFunction>();
[email protected]bcde7482011-05-23 19:29:12285#endif
286
[email protected]27072cad2011-05-09 19:46:40287#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
288 // IME
289 RegisterFunction<CandidateClickedInputUiFunction>();
290 RegisterFunction<CursorUpInputUiFunction>();
291 RegisterFunction<CursorDownInputUiFunction>();
292 RegisterFunction<PageUpInputUiFunction>();
293 RegisterFunction<PageDownInputUiFunction>();
294 RegisterFunction<RegisterInputUiFunction>();
295 RegisterFunction<PageUpInputUiFunction>();
296 RegisterFunction<PageDownInputUiFunction>();
297#endif
298
[email protected]cffd7892010-08-26 17:43:28299 // Management.
300 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23301 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56302 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28303 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28304 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23305
[email protected]a65882c2010-11-12 15:15:09306 // Extension module.
307 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01308 RegisterFunction<IsAllowedIncognitoAccessFunction>();
309 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09310
[email protected]63cda0c2010-09-01 04:41:23311 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24312 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23313 RegisterFunction<GetStoreLoginFunction>();
314 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24315 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30316 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17317 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30318 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26319
320 // WebRequest.
321 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48322 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25323
324 // Preferences.
325 RegisterFunction<GetPreferenceFunction>();
326 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27327 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22328
[email protected]b6b805e92011-04-16 09:24:14329 // ChromeOS-specific part of the API.
330#if defined(OS_CHROMEOS)
331 // Device Customization.
332 RegisterFunction<GetChromeosInfoFunction>();
333
334 // FileBrowserPrivate functions.
[email protected]94bda202011-04-18 23:31:00335 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14336 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00337 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14338 RegisterFunction<GetFileTasksFileBrowserFunction>();
[email protected]61334fa2011-06-16 01:01:40339 RegisterFunction<GetVolumeMetadataFunction>();
[email protected]b6b805e92011-04-16 09:24:14340 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]a6d06642011-06-02 05:55:23341 RegisterFunction<AddFileWatchBrowserFunction>();
342 RegisterFunction<RemoveFileWatchBrowserFunction>();
[email protected]9d4c2c52011-04-07 18:53:10343 RegisterFunction<SelectFileFunction>();
344 RegisterFunction<SelectFilesFunction>();
[email protected]61334fa2011-06-16 01:01:40345 RegisterFunction<UnmountVolumeFunction>();
[email protected]94bda202011-04-18 23:31:00346 RegisterFunction<ViewFilesFunction>();
[email protected]f1852b52011-05-17 04:58:08347
[email protected]ce9802042011-05-27 10:11:40348 // Mediaplayer
349 RegisterFunction<PlayAtMediaplayerFunction>();
350 RegisterFunction<SetPlaybackErrorMediaplayerFunction>();
351 RegisterFunction<GetPlaylistMediaplayerFunction>();
352 RegisterFunction<TogglePlaylistPanelMediaplayerFunction>();
353 RegisterFunction<ToggleFullscreenMediaplayerFunction>();
[email protected]f1852b52011-05-17 04:58:08354#if defined(TOUCH_UI)
355 // Input
356 RegisterFunction<SendHandwritingStrokeFunction>();
357 RegisterFunction<CancelHandwritingStrokesFunction>();
358#endif
[email protected]61b55b62011-03-24 09:03:10359#endif
360
[email protected]c6e584c2011-05-18 11:58:44361 // Websocket to TCP proxy. Currently noop on anything other than ChromeOS.
362 RegisterFunction<WebSocketProxyPrivateGetPassportForTCPFunction>();
363
[email protected]91ba3312011-03-17 20:39:22364 // Debugger
365 RegisterFunction<AttachDebuggerFunction>();
366 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33367 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]fa0624262011-06-09 14:17:38368
369 // Content settings.
[email protected]49ba4822011-06-15 18:41:56370 RegisterFunction<GetResourceIdentifiersFunction>();
[email protected]fa0624262011-06-09 14:17:38371 RegisterFunction<ClearContentSettingsFunction>();
372 RegisterFunction<GetContentSettingFunction>();
373 RegisterFunction<SetContentSettingFunction>();
[email protected]c1d05aa2011-06-28 02:07:30374
375 // Experimental App API.
376 RegisterFunction<AppNotifyFunction>();
377 RegisterFunction<AppClearAllNotificationsFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35378}
379
[email protected]b83e4602009-05-15 22:58:33380void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
381 for (FactoryMap::iterator iter = factories_.begin();
382 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35383 names->push_back(iter->first);
384 }
385}
386
[email protected]b83e4602009-05-15 22:58:33387bool FactoryRegistry::OverrideFunction(const std::string& name,
388 ExtensionFunctionFactory factory) {
389 FactoryMap::iterator iter = factories_.find(name);
390 if (iter == factories_.end()) {
391 return false;
392 } else {
393 iter->second = factory;
394 return true;
395 }
396}
397
[email protected]bfdffe2b2009-04-24 22:05:35398ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
399 FactoryMap::iterator iter = factories_.find(name);
400 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33401 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56402 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33403 return function;
[email protected]bfdffe2b2009-04-24 22:05:35404}
405
[email protected]b83e4602009-05-15 22:58:33406}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35407
408// ExtensionFunctionDispatcher -------------------------------------------------
409
410void ExtensionFunctionDispatcher::GetAllFunctionNames(
411 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55412 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35413}
414
[email protected]b83e4602009-05-15 22:58:33415bool ExtensionFunctionDispatcher::OverrideFunction(
416 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55417 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33418}
419
420void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55421 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33422}
423
[email protected]c357acb42011-06-09 20:52:42424// static
425void ExtensionFunctionDispatcher::DispatchOnIOThread(
426 const ExtensionInfoMap* extension_info_map,
427 ProfileId profile_id,
428 int render_process_id,
429 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
430 int routing_id,
431 const ExtensionHostMsg_Request_Params& params) {
432 const Extension* extension =
433 extension_info_map->extensions().GetByURL(params.source_url);
434
435 scoped_refptr<ExtensionFunction> function(
436 CreateExtensionFunction(params, extension, profile_id, render_process_id,
437 ipc_sender, routing_id));
438 if (!function)
439 return;
440
441 IOThreadExtensionFunction* function_io =
442 function->AsIOThreadExtensionFunction();
443 if (!function_io) {
444 NOTREACHED();
445 return;
446 }
447 function_io->set_ipc_sender(ipc_sender, routing_id);
448 function_io->set_extension_info_map(extension_info_map);
449 function->set_include_incognito(
450 extension_info_map->IsIncognitoEnabled(extension->id()));
451 function->Run();
452}
453
[email protected]c5dbef02011-05-13 05:06:09454ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
455 Delegate* delegate)
456 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27457 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35458}
459
[email protected]32dda362009-06-05 19:07:01460ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01461}
462
[email protected]0ec92032010-03-24 19:59:41463Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09464 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41465 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12466
[email protected]bc535ee52010-08-31 18:40:32467 // If the delegate has an associated browser, that is always the right answer.
468 if (browser)
469 return browser;
[email protected]9c45b7182009-08-04 16:44:43470
[email protected]bc535ee52010-08-31 18:40:32471 // Otherwise, try to default to a reasonable browser. If |include_incognito|
472 // is true, we will also search browsers in the incognito version of this
473 // profile. Note that the profile may already be incognito, in which case
474 // we will search the incognito version only, regardless of the value of
475 // |include_incognito|.
[email protected]c5dbef02011-05-13 05:06:09476 Profile* profile = render_view_host->process()->profile();
[email protected]b35b26b32011-05-05 20:35:14477 browser = BrowserList::FindTabbedBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41478
[email protected]0ec92032010-03-24 19:59:41479 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42480 // a background_page onload chrome.tabs api call can make it into here
481 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41482 // A similar situation may arise during shutdown.
483 // TODO(rafaelw): Delay creation of background_page until the browser
484 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
485 return browser;
[email protected]b27257562009-11-16 23:28:26486}
487
[email protected]c5dbef02011-05-13 05:06:09488void ExtensionFunctionDispatcher::Dispatch(
489 const ExtensionHostMsg_Request_Params& params,
490 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09491 ExtensionService* service = profile()->GetExtensionService();
492 if (!service)
493 return;
494
495 if (!service->ExtensionBindingsAllowed(params.source_url)) {
496 LOG(ERROR) << "Extension bindings not allowed for URL: "
497 << params.source_url.spec();
[email protected]c357acb42011-06-09 20:52:42498 SendAccessDenied(render_view_host, render_view_host->routing_id(),
499 params.request_id);
[email protected]c5dbef02011-05-13 05:06:09500 return;
501 }
502
503 // TODO(aa): When we allow content scripts to call extension APIs, we will
504 // have to pass the extension ID explicitly here, not use the source URL.
505 const Extension* extension = service->GetExtensionByURL(params.source_url);
506 if (!extension)
507 extension = service->GetExtensionByWebExtent(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09508
[email protected]c357acb42011-06-09 20:52:42509 scoped_refptr<ExtensionFunction> function(CreateExtensionFunction(
510 params, extension, profile_->GetRuntimeId(),
511 render_view_host->process()->id(),
512 render_view_host, render_view_host->routing_id()));
513 if (!function)
[email protected]f82d57b52011-04-27 19:13:17514 return;
[email protected]f82d57b52011-04-27 19:13:17515
[email protected]a2aef2e2011-05-26 22:48:12516 UIThreadExtensionFunction* function_ui =
517 function->AsUIThreadExtensionFunction();
518 if (!function_ui) {
519 NOTREACHED();
520 return;
521 }
522 function_ui->SetRenderViewHost(render_view_host);
523 function_ui->set_dispatcher(AsWeakPtr());
524 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05525 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41526
[email protected]d13950e2009-12-04 01:43:02527 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09528 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56529 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26530 // See crbug.com/39178.
531 ExternalProtocolHandler::PermitLaunchUrl();
532
[email protected]d13950e2009-12-04 01:43:02533 function->Run();
534 } else {
[email protected]c5dbef02011-05-13 05:06:09535 render_view_host->Send(new ExtensionMsg_Response(
536 render_view_host->routing_id(), function->request_id(), false,
[email protected]553602e12011-04-05 17:01:18537 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02538 }
[email protected]bfdffe2b2009-04-24 22:05:35539}
540
[email protected]c357acb42011-06-09 20:52:42541// static
542ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
543 const ExtensionHostMsg_Request_Params& params,
544 const Extension* extension,
545 ProfileId profile_id,
546 int render_process_id,
547 IPC::Message::Sender* ipc_sender,
548 int routing_id) {
549 // TODO(aa): It would be cool to use ExtensionProcessManager to track which
550 // processes are extension processes rather than ChildProcessSecurityPolicy.
551 // EPM has richer information: it not only knows which processes contain
552 // at least one extension, but it knows which extensions are inside and what
553 // permissions the have. So we would be able to enforce permissions more
554 // granularly.
555 if (!ChildProcessSecurityPolicy::GetInstance()->HasExtensionBindings(
556 render_process_id)) {
557 // TODO(aa): Allow content scripts access to low-threat extension APIs.
558 // See: crbug.com/80308.
559 LOG(ERROR) << "Extension API called from non-extension process.";
560 SendAccessDenied(ipc_sender, routing_id, params.request_id);
561 return NULL;
562 }
563
564 if (!extension) {
565 LOG(ERROR) << "Extension does not exist for URL: "
566 << params.source_url.spec();
567 SendAccessDenied(ipc_sender, routing_id, params.request_id);
568 return NULL;
569 }
570
[email protected]0d3e4a22011-06-23 19:02:52571 if (!extension->HasAPIPermission(params.name)) {
[email protected]c357acb42011-06-09 20:52:42572 LOG(ERROR) << "Extension " << extension->id() << " does not have "
573 << "permission to function: " << params.name;
574 SendAccessDenied(ipc_sender, routing_id, params.request_id);
575 return NULL;
576 }
577
578 ExtensionFunction* function =
579 FactoryRegistry::GetInstance()->NewFunction(params.name);
580 function->SetArgs(&params.arguments);
581 function->set_source_url(params.source_url);
582 function->set_request_id(params.request_id);
583 function->set_has_callback(params.has_callback);
584 function->set_user_gesture(params.user_gesture);
585 function->set_extension(extension);
586 function->set_profile_id(profile_id);
587 return function;
588}
589
590// static
[email protected]c5dbef02011-05-13 05:06:09591void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]c357acb42011-06-09 20:52:42592 IPC::Message::Sender* ipc_sender, int routing_id, int request_id) {
593 ipc_sender->Send(new ExtensionMsg_Response(
594 routing_id, request_id, false, std::string(),
[email protected]c5dbef02011-05-13 05:06:09595 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35596}