blob: 7ee1a19e0f092e1ef08354f1f849d13f3d8d10e5 [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]a692a952011-11-28 08:23:5414#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5315#include "chrome/browser/bookmarks/bookmark_extension_api.h"
16#include "chrome/browser/bookmarks/bookmark_manager_extension_api.h"
[email protected]cdcf60242011-11-04 15:53:4317#include "chrome/browser/download/download_extension_api.h"
[email protected]912256b32009-09-18 09:47:3518#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]c1d05aa2011-06-28 02:07:3019#include "chrome/browser/extensions/extension_app_api.h"
[email protected]ec9ac0df2009-10-01 18:06:4720#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]cdcf60242011-11-04 15:53:4321#include "chrome/browser/extensions/extension_chrome_auth_private_api.h"
[email protected]6093d742011-08-26 16:37:1822#include "chrome/browser/extensions/extension_clear_api.h"
[email protected]fa0624262011-06-09 14:17:3823#include "chrome/browser/extensions/extension_content_settings_api.h"
[email protected]2e3b5202010-03-23 06:52:4124#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2925#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2226#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3527#include "chrome/browser/extensions/extension_function.h"
[email protected]2bb51302011-02-18 22:39:2728#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2529#include "chrome/browser/extensions/extension_idle_api.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]9e4430f2011-11-28 07:43:2935#include "chrome/browser/extensions/extension_page_capture_api.h"
[email protected]902fd7b2011-07-27 18:42:3136#include "chrome/browser/extensions/extension_permissions_api.h"
[email protected]598bbcc2011-02-24 10:03:2537#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]381162b2010-01-28 17:29:3538#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4439#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]2f69b382011-02-19 00:34:2540#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5641#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3542#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1443#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2244#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]c63f2b72011-07-07 05:25:0045#include "chrome/browser/extensions/extension_tts_engine_api.h"
[email protected]c6e584c2011-05-18 11:58:4446#include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1047#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]557a51dd2011-07-26 12:17:1148#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]c41fe662011-02-15 01:19:2649#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2350#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0251#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]83820d42011-11-12 22:03:1152#include "chrome/browser/extensions/process_map.h"
[email protected]ae7f1ba2011-11-28 23:07:3153#include "chrome/browser/extensions/settings/settings_api.h"
54#include "chrome/browser/extensions/socket_api.h"
[email protected]ed2b1002011-05-25 14:12:1055#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]d2e6bd62011-10-24 20:29:0756#include "chrome/browser/history/history_extension_api.h"
[email protected]5eddc3e2011-10-26 04:33:3157#include "chrome/browser/history/top_sites_extension_api.h"
[email protected]cdcf60242011-11-04 15:53:4358#include "chrome/browser/infobars/infobar_extension_api.h"
[email protected]8ecad5e2010-12-02 21:18:3359#include "chrome/browser/profiles/profile.h"
[email protected]c357acb42011-06-09 20:52:4260#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]792556c2011-10-25 05:01:0761#include "chrome/browser/rlz/rlz_extension_api.h"
[email protected]b9971dc2011-11-11 15:50:4362#include "chrome/browser/speech/speech_input_extension_api.h"
[email protected]71b73f02011-04-06 15:57:2963#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3364#include "chrome/browser/ui/browser_window.h"
[email protected]83820d42011-11-12 22:03:1165#include "chrome/common/extensions/api/extension_api.h"
[email protected]44c49c92011-03-28 16:17:2366#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4367#include "chrome/common/url_constants.h"
[email protected]5de634712011-03-02 00:20:1968#include "content/browser/renderer_host/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1769#include "ipc/ipc_message.h"
70#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0271#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3572
[email protected]8f9d4eb2011-02-05 01:39:1073#if defined(TOOLKIT_VIEWS)
74#include "chrome/browser/extensions/extension_input_api.h"
75#endif
[email protected]d6833852010-08-20 18:00:4576
[email protected]27072cad2011-05-09 19:46:4077#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
78#include "chrome/browser/extensions/extension_input_ui_api.h"
79#endif
80
[email protected]61b55b62011-03-24 09:03:1081#if defined(OS_CHROMEOS)
[email protected]8baf0dbb2011-11-27 22:47:0282#include "chrome/browser/extensions/extension_file_browser_private_api.h"
83#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
84#include "chrome/browser/extensions/extension_input_ime_api.h"
85#include "chrome/browser/extensions/extension_input_method_api.h"
86#include "chrome/browser/extensions/extension_mediaplayer_private_api.h"
[email protected]61b55b62011-03-24 09:03:1087#endif
88
[email protected]83820d42011-11-12 22:03:1189using extensions::ExtensionAPI;
90
[email protected]bfdffe2b2009-04-24 22:05:3591// FactoryRegistry -------------------------------------------------------------
92
93namespace {
94
[email protected]b83e4602009-05-15 22:58:3395// Template for defining ExtensionFunctionFactory.
96template<class T>
97ExtensionFunction* NewExtensionFunction() {
98 return new T();
99}
[email protected]bfdffe2b2009-04-24 22:05:35100
[email protected]b83e4602009-05-15 22:58:33101// Contains a list of all known extension functions and allows clients to
102// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:35103class FactoryRegistry {
104 public:
[email protected]8e8bb6d2010-12-13 08:18:55105 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:33106 FactoryRegistry() { ResetFunctions(); }
107
108 // Resets all functions to their default values.
109 void ResetFunctions();
110
111 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:35112 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:33113
114 // Allows overriding of specific functions (e.g. for testing). Functions
115 // must be previously registered. Returns true if successful.
116 bool OverrideFunction(const std::string& name,
117 ExtensionFunctionFactory factory);
118
119 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35120 ExtensionFunction* NewFunction(const std::string& name);
121
122 private:
[email protected]61424c062009-10-14 23:14:59123 template<class T>
124 void RegisterFunction() {
125 factories_[T::function_name()] = &NewExtensionFunction<T>;
126 }
127
[email protected]bfdffe2b2009-04-24 22:05:35128 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
129 FactoryMap factories_;
130};
131
[email protected]8e8bb6d2010-12-13 08:18:55132FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35133 return Singleton<FactoryRegistry>::get();
134}
135
[email protected]b83e4602009-05-15 22:58:33136void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35137 // Register all functions here.
138
[email protected]e515f5d2009-05-05 03:05:00139 // Windows
[email protected]61424c062009-10-14 23:14:59140 RegisterFunction<GetWindowFunction>();
141 RegisterFunction<GetCurrentWindowFunction>();
142 RegisterFunction<GetLastFocusedWindowFunction>();
143 RegisterFunction<GetAllWindowsFunction>();
144 RegisterFunction<CreateWindowFunction>();
145 RegisterFunction<UpdateWindowFunction>();
146 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33147
[email protected]e515f5d2009-05-05 03:05:00148 // Tabs
[email protected]61424c062009-10-14 23:14:59149 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41150 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59151 RegisterFunction<GetSelectedTabFunction>();
152 RegisterFunction<GetAllTabsInWindowFunction>();
[email protected]8c3495c2011-09-28 03:32:30153 RegisterFunction<QueryTabsFunction>();
154 RegisterFunction<HighlightTabsFunction>();
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<CreateTabFunction>();
156 RegisterFunction<UpdateTabFunction>();
[email protected]8c3495c2011-09-28 03:32:30157 RegisterFunction<MoveTabsFunction>();
[email protected]5d935112011-09-07 09:24:54158 RegisterFunction<ReloadTabFunction>();
[email protected]8c3495c2011-09-28 03:32:30159 RegisterFunction<RemoveTabsFunction>();
[email protected]61424c062009-10-14 23:14:59160 RegisterFunction<DetectTabLanguageFunction>();
161 RegisterFunction<CaptureVisibleTabFunction>();
162 RegisterFunction<TabsExecuteScriptFunction>();
163 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35164
[email protected]f7f3a5f2009-05-01 22:02:34165 // Page Actions.
[email protected]61424c062009-10-14 23:14:59166 RegisterFunction<EnablePageActionFunction>();
167 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46168 RegisterFunction<PageActionShowFunction>();
169 RegisterFunction<PageActionHideFunction>();
170 RegisterFunction<PageActionSetIconFunction>();
171 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29172 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34173
[email protected]ec9ac0df2009-10-01 18:06:47174 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59175 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24176 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59177 RegisterFunction<BrowserActionSetBadgeTextFunction>();
178 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50179 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47180
[email protected]6093d742011-08-26 16:37:18181 // Browsing Data.
182 RegisterFunction<ClearBrowsingDataFunction>();
183 RegisterFunction<ClearCacheFunction>();
184 RegisterFunction<ClearCookiesFunction>();
185 RegisterFunction<ClearDownloadsFunction>();
186 RegisterFunction<ClearFormDataFunction>();
187 RegisterFunction<ClearHistoryFunction>();
188 RegisterFunction<ClearPasswordsFunction>();
189
[email protected]f7f3a5f2009-05-01 22:02:34190 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59191 RegisterFunction<GetBookmarksFunction>();
192 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55193 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59194 RegisterFunction<GetBookmarkTreeFunction>();
[email protected]532a8c62011-06-03 21:30:01195 RegisterFunction<GetBookmarkSubTreeFunction>();
[email protected]61424c062009-10-14 23:14:59196 RegisterFunction<SearchBookmarksFunction>();
197 RegisterFunction<RemoveBookmarkFunction>();
198 RegisterFunction<RemoveTreeBookmarkFunction>();
199 RegisterFunction<CreateBookmarkFunction>();
200 RegisterFunction<MoveBookmarkFunction>();
201 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43202
[email protected]f34e79632010-03-17 02:34:08203 // Infobars.
204 RegisterFunction<ShowInfoBarFunction>();
205
[email protected]9dd97bc2010-01-14 01:40:04206 // BookmarkManager
207 RegisterFunction<CopyBookmarkManagerFunction>();
208 RegisterFunction<CutBookmarkManagerFunction>();
209 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57210 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56211 RegisterFunction<ImportBookmarksFunction>();
212 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18213 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04214 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16215 RegisterFunction<StartDragBookmarkManagerFunction>();
216 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31217 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]933ebbe2011-06-30 12:24:01218 RegisterFunction<CanEditBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04219
[email protected]de768a832009-10-30 05:25:01220 // History
221 RegisterFunction<AddUrlHistoryFunction>();
222 RegisterFunction<DeleteAllHistoryFunction>();
223 RegisterFunction<DeleteRangeHistoryFunction>();
224 RegisterFunction<DeleteUrlHistoryFunction>();
225 RegisterFunction<GetVisitsHistoryFunction>();
226 RegisterFunction<SearchHistoryFunction>();
227
[email protected]f5205412010-03-16 00:19:34228 // Idle
229 RegisterFunction<ExtensionIdleQueryStateFunction>();
230
[email protected]198bcfe2009-09-09 22:56:28231 // I18N.
[email protected]61424c062009-10-14 23:14:59232 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28233
[email protected]381162b2010-01-28 17:29:35234 // Processes.
[email protected]8a661f82010-10-19 21:47:11235 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35236
[email protected]438772df2010-02-26 18:08:43237 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34238 RegisterFunction<MetricsRecordUserActionFunction>();
239 RegisterFunction<MetricsRecordValueFunction>();
240 RegisterFunction<MetricsRecordPercentageFunction>();
241 RegisterFunction<MetricsRecordCountFunction>();
242 RegisterFunction<MetricsRecordSmallCountFunction>();
243 RegisterFunction<MetricsRecordMediumCountFunction>();
244 RegisterFunction<MetricsRecordTimeFunction>();
245 RegisterFunction<MetricsRecordMediumTimeFunction>();
246 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43247
[email protected]17d40f02010-07-01 01:18:06248 // RLZ.
249#if defined(OS_WIN)
250 RegisterFunction<RlzRecordProductEventFunction>();
251 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21252 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06253 RegisterFunction<RlzClearProductStateFunction>();
254#endif
255
[email protected]898bbd32010-05-18 18:52:29256 // Cookies.
257 RegisterFunction<GetCookieFunction>();
258 RegisterFunction<GetAllCookiesFunction>();
259 RegisterFunction<SetCookieFunction>();
260 RegisterFunction<RemoveCookieFunction>();
261 RegisterFunction<GetAllCookieStoresFunction>();
262
[email protected]25fd1b2e2009-08-17 20:57:14263 // Test.
[email protected]61424c062009-10-14 23:14:59264 RegisterFunction<ExtensionTestPassFunction>();
265 RegisterFunction<ExtensionTestFailFunction>();
266 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02267 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50268 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07269 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39270 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56271
272 // Accessibility.
273 RegisterFunction<GetFocusedControlFunction>();
274 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58275
[email protected]8abe0a32010-08-12 00:40:22276 // Text-to-speech.
[email protected]c63f2b72011-07-07 05:25:00277 RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>();
278 RegisterFunction<ExtensionTtsGetVoicesFunction>();
279 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22280 RegisterFunction<ExtensionTtsSpeakFunction>();
281 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22282
[email protected]2e3b5202010-03-23 06:52:41283 // Context Menus.
284 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15285 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41286 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15287 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33288
289 // Omnibox.
290 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53291 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44292
[email protected]8b8e7c92010-08-19 18:05:56293 // Sidebar.
294 RegisterFunction<CollapseSidebarFunction>();
295 RegisterFunction<ExpandSidebarFunction>();
296 RegisterFunction<GetStateSidebarFunction>();
297 RegisterFunction<HideSidebarFunction>();
298 RegisterFunction<NavigateSidebarFunction>();
299 RegisterFunction<SetBadgeTextSidebarFunction>();
300 RegisterFunction<SetIconSidebarFunction>();
301 RegisterFunction<SetTitleSidebarFunction>();
302 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45303
[email protected]e06a1fe2011-09-29 21:20:44304 // Speech input.
305 RegisterFunction<StartSpeechInputFunction>();
306 RegisterFunction<StopSpeechInputFunction>();
307 RegisterFunction<IsRecordingSpeechInputFunction>();
308
[email protected]d6833852010-08-20 18:00:45309#if defined(TOOLKIT_VIEWS)
310 // Input.
311 RegisterFunction<SendKeyboardEventInputFunction>();
312#endif
[email protected]cffd7892010-08-26 17:43:28313
[email protected]2cc81d32011-10-04 17:03:18314#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]bcde7482011-05-23 19:29:12315 RegisterFunction<HideKeyboardFunction>();
[email protected]3c8faef2011-06-09 22:56:51316 RegisterFunction<SetKeyboardHeightFunction>();
[email protected]bcde7482011-05-23 19:29:12317#endif
318
[email protected]14da67b02011-08-02 05:49:13319#if defined(OS_CHROMEOS)
[email protected]27072cad2011-05-09 19:46:40320 // IME
[email protected]14da67b02011-08-02 05:49:13321 RegisterFunction<SetCompositionFunction>();
322 RegisterFunction<ClearCompositionFunction>();
323 RegisterFunction<CommitTextFunction>();
324 RegisterFunction<SetCandidateWindowPropertiesFunction>();
325 RegisterFunction<SetCandidatesFunction>();
326 RegisterFunction<SetCursorPositionFunction>();
327 RegisterFunction<SetMenuItemsFunction>();
328 RegisterFunction<UpdateMenuItemsFunction>();
[email protected]cd0b04c92011-09-08 05:18:06329
330 RegisterFunction<InputEventHandled>();
[email protected]14da67b02011-08-02 05:49:13331#if defined(TOUCH_UI)
[email protected]27072cad2011-05-09 19:46:40332 RegisterFunction<CandidateClickedInputUiFunction>();
333 RegisterFunction<CursorUpInputUiFunction>();
334 RegisterFunction<CursorDownInputUiFunction>();
335 RegisterFunction<PageUpInputUiFunction>();
336 RegisterFunction<PageDownInputUiFunction>();
337 RegisterFunction<RegisterInputUiFunction>();
338 RegisterFunction<PageUpInputUiFunction>();
339 RegisterFunction<PageDownInputUiFunction>();
340#endif
[email protected]14da67b02011-08-02 05:49:13341#endif
[email protected]27072cad2011-05-09 19:46:40342
[email protected]cffd7892010-08-26 17:43:28343 // Management.
344 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23345 RegisterFunction<GetExtensionByIdFunction>();
[email protected]925a54d2011-07-29 17:21:00346 RegisterFunction<GetPermissionWarningsByIdFunction>();
347 RegisterFunction<GetPermissionWarningsByManifestFunction>();
[email protected]e558ff82010-09-23 20:19:56348 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28349 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28350 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23351
[email protected]a65882c2010-11-12 15:15:09352 // Extension module.
353 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01354 RegisterFunction<IsAllowedIncognitoAccessFunction>();
355 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09356
[email protected]63cda0c2010-09-01 04:41:23357 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24358 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23359 RegisterFunction<GetStoreLoginFunction>();
360 RegisterFunction<SetStoreLoginFunction>();
[email protected]5349ac6d2011-04-05 22:20:17361 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30362 RegisterFunction<CompleteInstallFunction>();
[email protected]f66a50a2011-11-02 23:53:46363 RegisterFunction<SilentlyInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26364
[email protected]557a51dd2011-07-26 12:17:11365 // WebNavigation.
366 RegisterFunction<GetFrameFunction>();
[email protected]5a130672011-09-09 08:42:29367 RegisterFunction<GetAllFramesFunction>();
[email protected]557a51dd2011-07-26 12:17:11368
[email protected]c41fe662011-02-15 01:19:26369 // WebRequest.
370 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48371 RegisterFunction<WebRequestEventHandled>();
[email protected]e81d4d72011-09-29 16:54:31372 RegisterFunction<WebRequestHandlerBehaviorChanged>();
[email protected]598bbcc2011-02-24 10:03:25373
374 // Preferences.
375 RegisterFunction<GetPreferenceFunction>();
376 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27377 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22378
[email protected]b6b805e92011-04-16 09:24:14379 // ChromeOS-specific part of the API.
380#if defined(OS_CHROMEOS)
381 // Device Customization.
382 RegisterFunction<GetChromeosInfoFunction>();
383
384 // FileBrowserPrivate functions.
[email protected]04ef5152011-11-18 04:05:25385 // TODO(jamescook): Expose these on non-ChromeOS platforms so we can use
386 // the extension-based file picker on Aura. crbug.com/97424
[email protected]94bda202011-04-18 23:31:00387 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14388 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00389 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14390 RegisterFunction<GetFileTasksFileBrowserFunction>();
[email protected]61334fa2011-06-16 01:01:40391 RegisterFunction<GetVolumeMetadataFunction>();
[email protected]b6b805e92011-04-16 09:24:14392 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]a6d06642011-06-02 05:55:23393 RegisterFunction<AddFileWatchBrowserFunction>();
394 RegisterFunction<RemoveFileWatchBrowserFunction>();
[email protected]9d4c2c52011-04-07 18:53:10395 RegisterFunction<SelectFileFunction>();
396 RegisterFunction<SelectFilesFunction>();
[email protected]928ae8852011-07-26 01:15:03397 RegisterFunction<AddMountFunction>();
398 RegisterFunction<RemoveMountFunction>();
399 RegisterFunction<GetMountPointsFunction>();
[email protected]694cafac2011-09-21 17:41:53400 RegisterFunction<GetSizeStatsFunction>();
[email protected]3fd9004c2011-08-12 00:58:46401 RegisterFunction<FormatDeviceFunction>();
[email protected]94bda202011-04-18 23:31:00402 RegisterFunction<ViewFilesFunction>();
[email protected]f1852b52011-05-17 04:58:08403
[email protected]ce9802042011-05-27 10:11:40404 // Mediaplayer
405 RegisterFunction<PlayAtMediaplayerFunction>();
406 RegisterFunction<SetPlaybackErrorMediaplayerFunction>();
407 RegisterFunction<GetPlaylistMediaplayerFunction>();
408 RegisterFunction<TogglePlaylistPanelMediaplayerFunction>();
409 RegisterFunction<ToggleFullscreenMediaplayerFunction>();
[email protected]be5bd5742011-07-14 07:21:01410
411 // InputMethod
412 RegisterFunction<GetInputMethodFunction>();
413
[email protected]f1852b52011-05-17 04:58:08414#if defined(TOUCH_UI)
415 // Input
416 RegisterFunction<SendHandwritingStrokeFunction>();
417 RegisterFunction<CancelHandwritingStrokesFunction>();
418#endif
[email protected]61b55b62011-03-24 09:03:10419#endif
420
[email protected]c6e584c2011-05-18 11:58:44421 // Websocket to TCP proxy. Currently noop on anything other than ChromeOS.
422 RegisterFunction<WebSocketProxyPrivateGetPassportForTCPFunction>();
[email protected]5ddffb82011-10-14 17:48:07423 RegisterFunction<WebSocketProxyPrivateGetURLForTCPFunction>();
[email protected]c6e584c2011-05-18 11:58:44424
[email protected]91ba3312011-03-17 20:39:22425 // Debugger
426 RegisterFunction<AttachDebuggerFunction>();
427 RegisterFunction<DetachDebuggerFunction>();
[email protected]c7580b62011-10-26 08:46:30428 RegisterFunction<SendCommandDebuggerFunction>();
[email protected]fa0624262011-06-09 14:17:38429
[email protected]b7f853e282011-08-10 09:24:20430 // Settings
[email protected]0d9a2202011-11-09 13:48:41431 RegisterFunction<extensions::GetSettingsFunction>();
432 RegisterFunction<extensions::SetSettingsFunction>();
433 RegisterFunction<extensions::RemoveSettingsFunction>();
434 RegisterFunction<extensions::ClearSettingsFunction>();
[email protected]b7f853e282011-08-10 09:24:20435
[email protected]fa0624262011-06-09 14:17:38436 // Content settings.
[email protected]49ba4822011-06-15 18:41:56437 RegisterFunction<GetResourceIdentifiersFunction>();
[email protected]fa0624262011-06-09 14:17:38438 RegisterFunction<ClearContentSettingsFunction>();
439 RegisterFunction<GetContentSettingFunction>();
440 RegisterFunction<SetContentSettingFunction>();
[email protected]c1d05aa2011-06-28 02:07:30441
[email protected]be9d9c82011-07-13 04:17:31442 // ChromeAuth settings.
443 RegisterFunction<SetCloudPrintCredentialsFunction>();
444
[email protected]c1d05aa2011-06-28 02:07:30445 // Experimental App API.
446 RegisterFunction<AppNotifyFunction>();
447 RegisterFunction<AppClearAllNotificationsFunction>();
[email protected]902fd7b2011-07-27 18:42:31448
449 // Permissions
450 RegisterFunction<ContainsPermissionsFunction>();
451 RegisterFunction<GetAllPermissionsFunction>();
452 RegisterFunction<RemovePermissionsFunction>();
453 RegisterFunction<RequestPermissionsFunction>();
[email protected]21d7a4e302011-08-15 16:17:12454
455 // Downloads
456 RegisterFunction<DownloadsDownloadFunction>();
457 RegisterFunction<DownloadsSearchFunction>();
458 RegisterFunction<DownloadsPauseFunction>();
459 RegisterFunction<DownloadsResumeFunction>();
460 RegisterFunction<DownloadsCancelFunction>();
461 RegisterFunction<DownloadsEraseFunction>();
462 RegisterFunction<DownloadsSetDestinationFunction>();
463 RegisterFunction<DownloadsAcceptDangerFunction>();
464 RegisterFunction<DownloadsShowFunction>();
465 RegisterFunction<DownloadsDragFunction>();
[email protected]3a605e82011-09-26 17:26:16466
[email protected]9e4430f2011-11-28 07:43:29467 // PageCapture
468 RegisterFunction<PageCaptureSaveAsMHTMLFunction>();
[email protected]6a16dc9b2011-10-17 18:28:16469
[email protected]5eddc3e2011-10-26 04:33:31470 // TopSites
471 RegisterFunction<GetTopSitesFunction>();
[email protected]ae7f1ba2011-11-28 23:07:31472
473 // Sockets
474 RegisterFunction<extensions::SocketCreateFunction>();
475 RegisterFunction<extensions::SocketConnectFunction>();
476 RegisterFunction<extensions::SocketDisconnectFunction>();
477 RegisterFunction<extensions::SocketSendFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35478}
479
[email protected]b83e4602009-05-15 22:58:33480void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
481 for (FactoryMap::iterator iter = factories_.begin();
482 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35483 names->push_back(iter->first);
484 }
485}
486
[email protected]b83e4602009-05-15 22:58:33487bool FactoryRegistry::OverrideFunction(const std::string& name,
488 ExtensionFunctionFactory factory) {
489 FactoryMap::iterator iter = factories_.find(name);
490 if (iter == factories_.end()) {
491 return false;
492 } else {
493 iter->second = factory;
494 return true;
495 }
496}
497
[email protected]bfdffe2b2009-04-24 22:05:35498ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
499 FactoryMap::iterator iter = factories_.find(name);
500 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33501 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56502 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33503 return function;
[email protected]bfdffe2b2009-04-24 22:05:35504}
505
[email protected]b83e4602009-05-15 22:58:33506}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35507
508// ExtensionFunctionDispatcher -------------------------------------------------
509
510void ExtensionFunctionDispatcher::GetAllFunctionNames(
511 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55512 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35513}
514
[email protected]b83e4602009-05-15 22:58:33515bool ExtensionFunctionDispatcher::OverrideFunction(
516 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55517 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33518}
519
520void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55521 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33522}
523
[email protected]c357acb42011-06-09 20:52:42524// static
525void ExtensionFunctionDispatcher::DispatchOnIOThread(
[email protected]fd50e7b2011-11-03 09:20:25526 ExtensionInfoMap* extension_info_map,
[email protected]673514522011-07-13 18:17:18527 void* profile,
[email protected]c357acb42011-06-09 20:52:42528 int render_process_id,
529 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender,
530 int routing_id,
531 const ExtensionHostMsg_Request_Params& params) {
532 const Extension* extension =
[email protected]83820d42011-11-12 22:03:11533 extension_info_map->extensions().GetByID(params.extension_id);
[email protected]c357acb42011-06-09 20:52:42534
[email protected]6f371442011-11-09 06:45:46535 scoped_refptr<ExtensionFunction> function(
536 CreateExtensionFunction(params, extension, render_process_id,
537 extension_info_map->process_map(), profile,
538 ipc_sender, routing_id));
[email protected]c357acb42011-06-09 20:52:42539 if (!function)
540 return;
541
542 IOThreadExtensionFunction* function_io =
543 function->AsIOThreadExtensionFunction();
544 if (!function_io) {
545 NOTREACHED();
546 return;
547 }
548 function_io->set_ipc_sender(ipc_sender, routing_id);
549 function_io->set_extension_info_map(extension_info_map);
550 function->set_include_incognito(
551 extension_info_map->IsIncognitoEnabled(extension->id()));
[email protected]fd50e7b2011-11-03 09:20:25552
553 ExtensionsQuotaService* quota = extension_info_map->quota_service();
554 if (quota->Assess(extension->id(), function, &params.arguments,
555 base::TimeTicks::Now())) {
556 function->Run();
557 } else {
558 function->OnQuotaExceeded();
559 }
[email protected]c357acb42011-06-09 20:52:42560}
561
[email protected]c5dbef02011-05-13 05:06:09562ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
563 Delegate* delegate)
564 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27565 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35566}
567
[email protected]32dda362009-06-05 19:07:01568ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01569}
570
[email protected]0ec92032010-03-24 19:59:41571Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09572 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41573 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12574
[email protected]bc535ee52010-08-31 18:40:32575 // If the delegate has an associated browser, that is always the right answer.
576 if (browser)
577 return browser;
[email protected]9c45b7182009-08-04 16:44:43578
[email protected]bc535ee52010-08-31 18:40:32579 // Otherwise, try to default to a reasonable browser. If |include_incognito|
580 // is true, we will also search browsers in the incognito version of this
581 // profile. Note that the profile may already be incognito, in which case
582 // we will search the incognito version only, regardless of the value of
583 // |include_incognito|.
[email protected]9b62ecf2011-07-27 20:23:08584 Profile* profile = Profile::FromBrowserContext(
[email protected]f3b1a082011-11-18 00:34:30585 render_view_host->process()->GetBrowserContext());
[email protected]b35b26b32011-05-05 20:35:14586 browser = BrowserList::FindTabbedBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41587
[email protected]0ec92032010-03-24 19:59:41588 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42589 // a background_page onload chrome.tabs api call can make it into here
590 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41591 // A similar situation may arise during shutdown.
592 // TODO(rafaelw): Delay creation of background_page until the browser
593 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
594 return browser;
[email protected]b27257562009-11-16 23:28:26595}
596
[email protected]c5dbef02011-05-13 05:06:09597void ExtensionFunctionDispatcher::Dispatch(
598 const ExtensionHostMsg_Request_Params& params,
599 RenderViewHost* render_view_host) {
[email protected]c5dbef02011-05-13 05:06:09600 ExtensionService* service = profile()->GetExtensionService();
[email protected]6f371442011-11-09 06:45:46601 extensions::ProcessMap* process_map = service->process_map();
602 if (!service || !process_map)
[email protected]c5dbef02011-05-13 05:06:09603 return;
604
[email protected]83820d42011-11-12 22:03:11605 const Extension* extension = service->GetExtensionById(
606 params.extension_id, false);
[email protected]c5dbef02011-05-13 05:06:09607 if (!extension)
608 extension = service->GetExtensionByWebExtent(params.source_url);
[email protected]c5dbef02011-05-13 05:06:09609
[email protected]8add5412011-10-01 21:02:14610 scoped_refptr<ExtensionFunction> function(
[email protected]6f371442011-11-09 06:45:46611 CreateExtensionFunction(params, extension,
[email protected]f3b1a082011-11-18 00:34:30612 render_view_host->process()->GetID(),
[email protected]6f371442011-11-09 06:45:46613 *(service->process_map()),
614 profile(), render_view_host,
[email protected]8add5412011-10-01 21:02:14615 render_view_host->routing_id()));
[email protected]c357acb42011-06-09 20:52:42616 if (!function)
[email protected]f82d57b52011-04-27 19:13:17617 return;
[email protected]f82d57b52011-04-27 19:13:17618
[email protected]a2aef2e2011-05-26 22:48:12619 UIThreadExtensionFunction* function_ui =
620 function->AsUIThreadExtensionFunction();
621 if (!function_ui) {
622 NOTREACHED();
623 return;
624 }
625 function_ui->SetRenderViewHost(render_view_host);
626 function_ui->set_dispatcher(AsWeakPtr());
627 function_ui->set_profile(profile_);
[email protected]2a8f24e2010-11-03 21:37:05628 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41629
[email protected]d13950e2009-12-04 01:43:02630 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09631 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56632 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26633 // See crbug.com/39178.
634 ExternalProtocolHandler::PermitLaunchUrl();
635
[email protected]d13950e2009-12-04 01:43:02636 function->Run();
637 } else {
[email protected]fd50e7b2011-11-03 09:20:25638 function->OnQuotaExceeded();
[email protected]d13950e2009-12-04 01:43:02639 }
[email protected]bfdffe2b2009-04-24 22:05:35640}
641
[email protected]c357acb42011-06-09 20:52:42642// static
643ExtensionFunction* ExtensionFunctionDispatcher::CreateExtensionFunction(
644 const ExtensionHostMsg_Request_Params& params,
645 const Extension* extension,
[email protected]6f371442011-11-09 06:45:46646 int requesting_process_id,
647 const extensions::ProcessMap& process_map,
[email protected]673514522011-07-13 18:17:18648 void* profile,
[email protected]c357acb42011-06-09 20:52:42649 IPC::Message::Sender* ipc_sender,
650 int routing_id) {
[email protected]c357acb42011-06-09 20:52:42651 if (!extension) {
[email protected]6f371442011-11-09 06:45:46652 LOG(ERROR) << "Specified extension does not exist.";
653 SendAccessDenied(ipc_sender, routing_id, params.request_id);
654 return NULL;
655 }
656
[email protected]83820d42011-11-12 22:03:11657 if (ExtensionAPI::GetInstance()->IsPrivileged(params.name) &&
658 !process_map.Contains(extension->id(), requesting_process_id)) {
[email protected]6f371442011-11-09 06:45:46659 LOG(ERROR) << "Extension API called from incorrect process "
660 << requesting_process_id
661 << " from URL " << params.source_url.spec();
[email protected]c357acb42011-06-09 20:52:42662 SendAccessDenied(ipc_sender, routing_id, params.request_id);
663 return NULL;
664 }
665
[email protected]0d3e4a22011-06-23 19:02:52666 if (!extension->HasAPIPermission(params.name)) {
[email protected]c357acb42011-06-09 20:52:42667 LOG(ERROR) << "Extension " << extension->id() << " does not have "
668 << "permission to function: " << params.name;
669 SendAccessDenied(ipc_sender, routing_id, params.request_id);
670 return NULL;
671 }
672
673 ExtensionFunction* function =
674 FactoryRegistry::GetInstance()->NewFunction(params.name);
675 function->SetArgs(&params.arguments);
676 function->set_source_url(params.source_url);
677 function->set_request_id(params.request_id);
678 function->set_has_callback(params.has_callback);
679 function->set_user_gesture(params.user_gesture);
680 function->set_extension(extension);
[email protected]637bf322011-10-01 20:46:32681 function->set_profile_id(profile);
[email protected]c357acb42011-06-09 20:52:42682 return function;
683}
684
685// static
[email protected]c5dbef02011-05-13 05:06:09686void ExtensionFunctionDispatcher::SendAccessDenied(
[email protected]c357acb42011-06-09 20:52:42687 IPC::Message::Sender* ipc_sender, int routing_id, int request_id) {
688 ipc_sender->Send(new ExtensionMsg_Response(
689 routing_id, request_id, false, std::string(),
[email protected]c5dbef02011-05-13 05:06:09690 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35691}