blob: d326e6219e180754a508b1d25c0f1a63b2081db5 [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]2e3b5202010-03-23 06:52:4119#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2920#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2221#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3522#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0123#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2724#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2525#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0826#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2827#include "chrome/browser/extensions/extension_management_api.h"
[email protected]438772df2010-02-26 18:08:4328#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0929#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3330#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3431#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]598bbcc2011-02-24 10:03:2532#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]381162b2010-01-28 17:29:3533#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4434#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0635#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2536#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5637#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3538#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1439#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2240#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]c6e584c2011-05-18 11:58:4441#include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1042#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2643#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2344#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0245#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]ed2b1002011-05-25 14:12:1046#include "chrome/browser/external_protocol/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3347#include "chrome/browser/profiles/profile.h"
[email protected]71b73f02011-04-06 15:57:2948#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3349#include "chrome/browser/ui/browser_window.h"
[email protected]44c49c92011-03-28 16:17:2350#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4351#include "chrome/common/url_constants.h"
[email protected]f82d57b52011-04-27 19:13:1752#include "content/browser/child_process_security_policy.h"
[email protected]5de634712011-03-02 00:20:1953#include "content/browser/renderer_host/render_process_host.h"
54#include "content/browser/renderer_host/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1755#include "ipc/ipc_message.h"
56#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0257#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3558
[email protected]8f9d4eb2011-02-05 01:39:1059#if defined(TOOLKIT_VIEWS)
60#include "chrome/browser/extensions/extension_input_api.h"
61#endif
[email protected]d6833852010-08-20 18:00:4562
[email protected]27072cad2011-05-09 19:46:4063#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
64#include "chrome/browser/extensions/extension_input_ui_api.h"
65#endif
66
[email protected]61b55b62011-03-24 09:03:1067#if defined(OS_CHROMEOS)
[email protected]b6b805e92011-04-16 09:24:1468#include "chrome/browser/extensions/extension_file_browser_private_api.h"
[email protected]61b55b62011-03-24 09:03:1069#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
[email protected]ce9802042011-05-27 10:11:4070#include "chrome/browser/extensions/extension_mediaplayer_private_api.h"
[email protected]61b55b62011-03-24 09:03:1071#endif
72
[email protected]bfdffe2b2009-04-24 22:05:3573// FactoryRegistry -------------------------------------------------------------
74
75namespace {
76
[email protected]b83e4602009-05-15 22:58:3377// Template for defining ExtensionFunctionFactory.
78template<class T>
79ExtensionFunction* NewExtensionFunction() {
80 return new T();
81}
[email protected]bfdffe2b2009-04-24 22:05:3582
[email protected]b83e4602009-05-15 22:58:3383// Contains a list of all known extension functions and allows clients to
84// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3585class FactoryRegistry {
86 public:
[email protected]8e8bb6d2010-12-13 08:18:5587 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3388 FactoryRegistry() { ResetFunctions(); }
89
90 // Resets all functions to their default values.
91 void ResetFunctions();
92
93 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3594 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3395
96 // Allows overriding of specific functions (e.g. for testing). Functions
97 // must be previously registered. Returns true if successful.
98 bool OverrideFunction(const std::string& name,
99 ExtensionFunctionFactory factory);
100
101 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35102 ExtensionFunction* NewFunction(const std::string& name);
103
104 private:
[email protected]61424c062009-10-14 23:14:59105 template<class T>
106 void RegisterFunction() {
107 factories_[T::function_name()] = &NewExtensionFunction<T>;
108 }
109
[email protected]bfdffe2b2009-04-24 22:05:35110 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
111 FactoryMap factories_;
112};
113
[email protected]8e8bb6d2010-12-13 08:18:55114FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35115 return Singleton<FactoryRegistry>::get();
116}
117
[email protected]b83e4602009-05-15 22:58:33118void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35119 // Register all functions here.
120
[email protected]e515f5d2009-05-05 03:05:00121 // Windows
[email protected]61424c062009-10-14 23:14:59122 RegisterFunction<GetWindowFunction>();
123 RegisterFunction<GetCurrentWindowFunction>();
124 RegisterFunction<GetLastFocusedWindowFunction>();
125 RegisterFunction<GetAllWindowsFunction>();
126 RegisterFunction<CreateWindowFunction>();
127 RegisterFunction<UpdateWindowFunction>();
128 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33129
[email protected]e515f5d2009-05-05 03:05:00130 // Tabs
[email protected]61424c062009-10-14 23:14:59131 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41132 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59133 RegisterFunction<GetSelectedTabFunction>();
134 RegisterFunction<GetAllTabsInWindowFunction>();
135 RegisterFunction<CreateTabFunction>();
136 RegisterFunction<UpdateTabFunction>();
137 RegisterFunction<MoveTabFunction>();
138 RegisterFunction<RemoveTabFunction>();
139 RegisterFunction<DetectTabLanguageFunction>();
140 RegisterFunction<CaptureVisibleTabFunction>();
141 RegisterFunction<TabsExecuteScriptFunction>();
142 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35143
[email protected]f7f3a5f2009-05-01 22:02:34144 // Page Actions.
[email protected]61424c062009-10-14 23:14:59145 RegisterFunction<EnablePageActionFunction>();
146 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46147 RegisterFunction<PageActionShowFunction>();
148 RegisterFunction<PageActionHideFunction>();
149 RegisterFunction<PageActionSetIconFunction>();
150 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29151 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34152
[email protected]ec9ac0df2009-10-01 18:06:47153 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59154 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24155 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59156 RegisterFunction<BrowserActionSetBadgeTextFunction>();
157 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50158 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47159
[email protected]f7f3a5f2009-05-01 22:02:34160 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59161 RegisterFunction<GetBookmarksFunction>();
162 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55163 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59164 RegisterFunction<GetBookmarkTreeFunction>();
[email protected]532a8c62011-06-03 21:30:01165 RegisterFunction<GetBookmarkSubTreeFunction>();
[email protected]61424c062009-10-14 23:14:59166 RegisterFunction<SearchBookmarksFunction>();
167 RegisterFunction<RemoveBookmarkFunction>();
168 RegisterFunction<RemoveTreeBookmarkFunction>();
169 RegisterFunction<CreateBookmarkFunction>();
170 RegisterFunction<MoveBookmarkFunction>();
171 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43172
[email protected]f34e79632010-03-17 02:34:08173 // Infobars.
174 RegisterFunction<ShowInfoBarFunction>();
175
[email protected]9dd97bc2010-01-14 01:40:04176 // BookmarkManager
177 RegisterFunction<CopyBookmarkManagerFunction>();
178 RegisterFunction<CutBookmarkManagerFunction>();
179 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57180 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56181 RegisterFunction<ImportBookmarksFunction>();
182 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18183 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04184 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16185 RegisterFunction<StartDragBookmarkManagerFunction>();
186 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31187 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04188
[email protected]de768a832009-10-30 05:25:01189 // History
190 RegisterFunction<AddUrlHistoryFunction>();
191 RegisterFunction<DeleteAllHistoryFunction>();
192 RegisterFunction<DeleteRangeHistoryFunction>();
193 RegisterFunction<DeleteUrlHistoryFunction>();
194 RegisterFunction<GetVisitsHistoryFunction>();
195 RegisterFunction<SearchHistoryFunction>();
196
[email protected]f5205412010-03-16 00:19:34197 // Idle
198 RegisterFunction<ExtensionIdleQueryStateFunction>();
199
[email protected]198bcfe2009-09-09 22:56:28200 // I18N.
[email protected]61424c062009-10-14 23:14:59201 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28202
[email protected]381162b2010-01-28 17:29:35203 // Processes.
[email protected]8a661f82010-10-19 21:47:11204 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35205
[email protected]438772df2010-02-26 18:08:43206 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03207 RegisterFunction<MetricsGetEnabledFunction>();
208 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34209 RegisterFunction<MetricsRecordUserActionFunction>();
210 RegisterFunction<MetricsRecordValueFunction>();
211 RegisterFunction<MetricsRecordPercentageFunction>();
212 RegisterFunction<MetricsRecordCountFunction>();
213 RegisterFunction<MetricsRecordSmallCountFunction>();
214 RegisterFunction<MetricsRecordMediumCountFunction>();
215 RegisterFunction<MetricsRecordTimeFunction>();
216 RegisterFunction<MetricsRecordMediumTimeFunction>();
217 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43218
[email protected]17d40f02010-07-01 01:18:06219 // RLZ.
220#if defined(OS_WIN)
221 RegisterFunction<RlzRecordProductEventFunction>();
222 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21223 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06224 RegisterFunction<RlzClearProductStateFunction>();
225#endif
226
[email protected]898bbd32010-05-18 18:52:29227 // Cookies.
228 RegisterFunction<GetCookieFunction>();
229 RegisterFunction<GetAllCookiesFunction>();
230 RegisterFunction<SetCookieFunction>();
231 RegisterFunction<RemoveCookieFunction>();
232 RegisterFunction<GetAllCookieStoresFunction>();
233
[email protected]25fd1b2e2009-08-17 20:57:14234 // Test.
[email protected]61424c062009-10-14 23:14:59235 RegisterFunction<ExtensionTestPassFunction>();
236 RegisterFunction<ExtensionTestFailFunction>();
237 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02238 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50239 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07240 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39241 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56242
243 // Accessibility.
244 RegisterFunction<GetFocusedControlFunction>();
245 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58246
[email protected]8abe0a32010-08-12 00:40:22247 // Text-to-speech.
248 RegisterFunction<ExtensionTtsSpeakFunction>();
249 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
250 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56251 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22252
[email protected]2e3b5202010-03-23 06:52:41253 // Context Menus.
254 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15255 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41256 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15257 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33258
259 // Omnibox.
260 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53261 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44262
[email protected]8b8e7c92010-08-19 18:05:56263 // Sidebar.
264 RegisterFunction<CollapseSidebarFunction>();
265 RegisterFunction<ExpandSidebarFunction>();
266 RegisterFunction<GetStateSidebarFunction>();
267 RegisterFunction<HideSidebarFunction>();
268 RegisterFunction<NavigateSidebarFunction>();
269 RegisterFunction<SetBadgeTextSidebarFunction>();
270 RegisterFunction<SetIconSidebarFunction>();
271 RegisterFunction<SetTitleSidebarFunction>();
272 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45273
274#if defined(TOOLKIT_VIEWS)
275 // Input.
276 RegisterFunction<SendKeyboardEventInputFunction>();
277#endif
[email protected]cffd7892010-08-26 17:43:28278
[email protected]bcde7482011-05-23 19:29:12279#if defined(TOUCH_UI)
280 RegisterFunction<HideKeyboardFunction>();
281#endif
282
[email protected]27072cad2011-05-09 19:46:40283#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
284 // IME
285 RegisterFunction<CandidateClickedInputUiFunction>();
286 RegisterFunction<CursorUpInputUiFunction>();
287 RegisterFunction<CursorDownInputUiFunction>();
288 RegisterFunction<PageUpInputUiFunction>();
289 RegisterFunction<PageDownInputUiFunction>();
290 RegisterFunction<RegisterInputUiFunction>();
291 RegisterFunction<PageUpInputUiFunction>();
292 RegisterFunction<PageDownInputUiFunction>();
293#endif
294
[email protected]cffd7892010-08-26 17:43:28295 // Management.
296 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23297 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56298 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28299 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28300 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23301
[email protected]a65882c2010-11-12 15:15:09302 // Extension module.
303 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01304 RegisterFunction<IsAllowedIncognitoAccessFunction>();
305 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09306
[email protected]63cda0c2010-09-01 04:41:23307 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24308 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23309 RegisterFunction<GetStoreLoginFunction>();
310 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24311 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30312 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17313 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30314 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26315
316 // WebRequest.
317 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48318 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25319
320 // Preferences.
321 RegisterFunction<GetPreferenceFunction>();
322 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27323 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22324
[email protected]b6b805e92011-04-16 09:24:14325 // ChromeOS-specific part of the API.
326#if defined(OS_CHROMEOS)
327 // Device Customization.
328 RegisterFunction<GetChromeosInfoFunction>();
329
330 // FileBrowserPrivate functions.
[email protected]94bda202011-04-18 23:31:00331 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14332 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00333 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14334 RegisterFunction<GetFileTasksFileBrowserFunction>();
335 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]a6d06642011-06-02 05:55:23336 RegisterFunction<AddFileWatchBrowserFunction>();
337 RegisterFunction<RemoveFileWatchBrowserFunction>();
[email protected]9d4c2c52011-04-07 18:53:10338 RegisterFunction<SelectFileFunction>();
339 RegisterFunction<SelectFilesFunction>();
[email protected]94bda202011-04-18 23:31:00340 RegisterFunction<ViewFilesFunction>();
[email protected]f1852b52011-05-17 04:58:08341
[email protected]ce9802042011-05-27 10:11:40342 // Mediaplayer
343 RegisterFunction<PlayAtMediaplayerFunction>();
344 RegisterFunction<SetPlaybackErrorMediaplayerFunction>();
345 RegisterFunction<GetPlaylistMediaplayerFunction>();
346 RegisterFunction<TogglePlaylistPanelMediaplayerFunction>();
347 RegisterFunction<ToggleFullscreenMediaplayerFunction>();
[email protected]f1852b52011-05-17 04:58:08348#if defined(TOUCH_UI)
349 // Input
350 RegisterFunction<SendHandwritingStrokeFunction>();
351 RegisterFunction<CancelHandwritingStrokesFunction>();
352#endif
[email protected]61b55b62011-03-24 09:03:10353#endif
354
[email protected]c6e584c2011-05-18 11:58:44355 // Websocket to TCP proxy. Currently noop on anything other than ChromeOS.
356 RegisterFunction<WebSocketProxyPrivateGetPassportForTCPFunction>();
357
[email protected]91ba3312011-03-17 20:39:22358 // Debugger
359 RegisterFunction<AttachDebuggerFunction>();
360 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33361 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35362}
363
[email protected]b83e4602009-05-15 22:58:33364void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
365 for (FactoryMap::iterator iter = factories_.begin();
366 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35367 names->push_back(iter->first);
368 }
369}
370
[email protected]b83e4602009-05-15 22:58:33371bool FactoryRegistry::OverrideFunction(const std::string& name,
372 ExtensionFunctionFactory factory) {
373 FactoryMap::iterator iter = factories_.find(name);
374 if (iter == factories_.end()) {
375 return false;
376 } else {
377 iter->second = factory;
378 return true;
379 }
380}
381
[email protected]bfdffe2b2009-04-24 22:05:35382ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
383 FactoryMap::iterator iter = factories_.find(name);
384 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33385 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56386 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33387 return function;
[email protected]bfdffe2b2009-04-24 22:05:35388}
389
[email protected]b83e4602009-05-15 22:58:33390}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35391
392// ExtensionFunctionDispatcher -------------------------------------------------
393
394void ExtensionFunctionDispatcher::GetAllFunctionNames(
395 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55396 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35397}
398
[email protected]b83e4602009-05-15 22:58:33399bool ExtensionFunctionDispatcher::OverrideFunction(
400 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55401 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33402}
403
404void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55405 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33406}
407
[email protected]c5dbef02011-05-13 05:06:09408ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
409 Delegate* delegate)
410 : profile_(profile),
[email protected]55ce330712011-05-24 19:04:27411 delegate_(delegate) {
[email protected]bfdffe2b2009-04-24 22:05:35412}
413
[email protected]32dda362009-06-05 19:07:01414ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01415}
416
[email protected]0ec92032010-03-24 19:59:41417Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09418 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41419 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12420
[email protected]bc535ee52010-08-31 18:40:32421 // If the delegate has an associated browser, that is always the right answer.
422 if (browser)
423 return browser;
[email protected]9c45b7182009-08-04 16:44:43424
[email protected]bc535ee52010-08-31 18:40:32425 // Otherwise, try to default to a reasonable browser. If |include_incognito|
426 // is true, we will also search browsers in the incognito version of this
427 // profile. Note that the profile may already be incognito, in which case
428 // we will search the incognito version only, regardless of the value of
429 // |include_incognito|.
[email protected]c5dbef02011-05-13 05:06:09430 Profile* profile = render_view_host->process()->profile();
[email protected]b35b26b32011-05-05 20:35:14431 browser = BrowserList::FindTabbedBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41432
[email protected]0ec92032010-03-24 19:59:41433 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42434 // a background_page onload chrome.tabs api call can make it into here
435 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41436 // A similar situation may arise during shutdown.
437 // TODO(rafaelw): Delay creation of background_page until the browser
438 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
439 return browser;
[email protected]b27257562009-11-16 23:28:26440}
441
[email protected]c5dbef02011-05-13 05:06:09442void ExtensionFunctionDispatcher::Dispatch(
443 const ExtensionHostMsg_Request_Params& params,
444 RenderViewHost* render_view_host) {
445 // TODO(aa): It would be cool to use ExtensionProcessManager to track which
446 // processes are extension processes rather than ChildProcessSecurityPolicy.
447 // EPM has richer information: it not only knows which processes contain
448 // at least one extension, but it knows which extensions are inside and what
449 // permissions the have. So we would be able to enforce permissions more
450 // granularly.
451 if (!ChildProcessSecurityPolicy::GetInstance()->HasExtensionBindings(
452 render_view_host->process()->id())) {
453 // TODO(aa): Allow content scripts access to low-threat extension APIs.
454 // See: crbug.com/80308.
455 LOG(ERROR) << "Extension API called from non-extension process.";
456 SendAccessDenied(render_view_host, params.request_id);
457 return;
458 }
[email protected]f82d57b52011-04-27 19:13:17459
[email protected]c5dbef02011-05-13 05:06:09460 ExtensionService* service = profile()->GetExtensionService();
461 if (!service)
462 return;
463
464 if (!service->ExtensionBindingsAllowed(params.source_url)) {
465 LOG(ERROR) << "Extension bindings not allowed for URL: "
466 << params.source_url.spec();
467 SendAccessDenied(render_view_host, params.request_id);
468 return;
469 }
470
471 // TODO(aa): When we allow content scripts to call extension APIs, we will
472 // have to pass the extension ID explicitly here, not use the source URL.
473 const Extension* extension = service->GetExtensionByURL(params.source_url);
474 if (!extension)
475 extension = service->GetExtensionByWebExtent(params.source_url);
476 if (!extension) {
477 LOG(ERROR) << "Extension does not exist for URL: "
478 << params.source_url.spec();
479 SendAccessDenied(render_view_host, params.request_id);
480 return;
481 }
482
483 if (!extension->HasApiPermission(params.name)) {
484 LOG(ERROR) << "Extension " << extension->id() << " does not have "
485 << "permission to function: " << params.name;
486 SendAccessDenied(render_view_host, params.request_id);
[email protected]f82d57b52011-04-27 19:13:17487 return;
488 }
489
[email protected]32dda362009-06-05 19:07:01490 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55491 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]a2aef2e2011-05-26 22:48:12492 UIThreadExtensionFunction* function_ui =
493 function->AsUIThreadExtensionFunction();
494 if (!function_ui) {
495 NOTREACHED();
496 return;
497 }
498 function_ui->SetRenderViewHost(render_view_host);
499 function_ui->set_dispatcher(AsWeakPtr());
500 function_ui->set_profile(profile_);
501
502 function->set_profile_id(profile_->GetRuntimeId());
503 function->set_extension(extension);
[email protected]8b8e7c92010-08-19 18:05:56504 function->SetArgs(&params.arguments);
505 function->set_source_url(params.source_url);
506 function->set_request_id(params.request_id);
507 function->set_has_callback(params.has_callback);
508 function->set_user_gesture(params.user_gesture);
[email protected]2a8f24e2010-11-03 21:37:05509 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41510
[email protected]d13950e2009-12-04 01:43:02511 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09512 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56513 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26514 // See crbug.com/39178.
515 ExternalProtocolHandler::PermitLaunchUrl();
516
[email protected]d13950e2009-12-04 01:43:02517 function->Run();
518 } else {
[email protected]c5dbef02011-05-13 05:06:09519 render_view_host->Send(new ExtensionMsg_Response(
520 render_view_host->routing_id(), function->request_id(), false,
[email protected]553602e12011-04-05 17:01:18521 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02522 }
[email protected]bfdffe2b2009-04-24 22:05:35523}
524
[email protected]c5dbef02011-05-13 05:06:09525void ExtensionFunctionDispatcher::SendAccessDenied(
526 RenderViewHost* render_view_host, int request_id) {
527 render_view_host->Send(new ExtensionMsg_Response(
528 render_view_host->routing_id(), request_id, false, std::string(),
529 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35530}