blob: 9a6b372214baaed763f7f8fdafc4df406e7efef2 [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]8f9d4eb2011-02-05 01:39:1041#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2642#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2343#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0244#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]2f69b382011-02-19 00:34:2545#include "chrome/browser/external_protocol_handler.h"
[email protected]8ecad5e2010-12-02 21:18:3346#include "chrome/browser/profiles/profile.h"
[email protected]71b73f02011-04-06 15:57:2947#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3348#include "chrome/browser/ui/browser_window.h"
[email protected]44c49c92011-03-28 16:17:2349#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4350#include "chrome/common/url_constants.h"
[email protected]f82d57b52011-04-27 19:13:1751#include "content/browser/child_process_security_policy.h"
[email protected]5de634712011-03-02 00:20:1952#include "content/browser/renderer_host/render_process_host.h"
53#include "content/browser/renderer_host/render_view_host.h"
[email protected]f82d57b52011-04-27 19:13:1754#include "ipc/ipc_message.h"
55#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0256#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3557
[email protected]8f9d4eb2011-02-05 01:39:1058#if defined(TOOLKIT_VIEWS)
59#include "chrome/browser/extensions/extension_input_api.h"
60#endif
[email protected]d6833852010-08-20 18:00:4561
[email protected]27072cad2011-05-09 19:46:4062#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
63#include "chrome/browser/extensions/extension_input_ui_api.h"
64#endif
65
[email protected]61b55b62011-03-24 09:03:1066#if defined(OS_CHROMEOS)
[email protected]b6b805e92011-04-16 09:24:1467#include "chrome/browser/extensions/extension_file_browser_private_api.h"
[email protected]61b55b62011-03-24 09:03:1068#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
69#endif
70
[email protected]bfdffe2b2009-04-24 22:05:3571// FactoryRegistry -------------------------------------------------------------
72
73namespace {
74
[email protected]b83e4602009-05-15 22:58:3375// Template for defining ExtensionFunctionFactory.
76template<class T>
77ExtensionFunction* NewExtensionFunction() {
78 return new T();
79}
[email protected]bfdffe2b2009-04-24 22:05:3580
[email protected]b83e4602009-05-15 22:58:3381// Contains a list of all known extension functions and allows clients to
82// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3583class FactoryRegistry {
84 public:
[email protected]8e8bb6d2010-12-13 08:18:5585 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3386 FactoryRegistry() { ResetFunctions(); }
87
88 // Resets all functions to their default values.
89 void ResetFunctions();
90
91 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3592 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3393
94 // Allows overriding of specific functions (e.g. for testing). Functions
95 // must be previously registered. Returns true if successful.
96 bool OverrideFunction(const std::string& name,
97 ExtensionFunctionFactory factory);
98
99 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35100 ExtensionFunction* NewFunction(const std::string& name);
101
102 private:
[email protected]61424c062009-10-14 23:14:59103 template<class T>
104 void RegisterFunction() {
105 factories_[T::function_name()] = &NewExtensionFunction<T>;
106 }
107
[email protected]bfdffe2b2009-04-24 22:05:35108 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
109 FactoryMap factories_;
110};
111
[email protected]8e8bb6d2010-12-13 08:18:55112FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35113 return Singleton<FactoryRegistry>::get();
114}
115
[email protected]b83e4602009-05-15 22:58:33116void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35117 // Register all functions here.
118
[email protected]e515f5d2009-05-05 03:05:00119 // Windows
[email protected]61424c062009-10-14 23:14:59120 RegisterFunction<GetWindowFunction>();
121 RegisterFunction<GetCurrentWindowFunction>();
122 RegisterFunction<GetLastFocusedWindowFunction>();
123 RegisterFunction<GetAllWindowsFunction>();
124 RegisterFunction<CreateWindowFunction>();
125 RegisterFunction<UpdateWindowFunction>();
126 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33127
[email protected]e515f5d2009-05-05 03:05:00128 // Tabs
[email protected]61424c062009-10-14 23:14:59129 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41130 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59131 RegisterFunction<GetSelectedTabFunction>();
132 RegisterFunction<GetAllTabsInWindowFunction>();
133 RegisterFunction<CreateTabFunction>();
134 RegisterFunction<UpdateTabFunction>();
135 RegisterFunction<MoveTabFunction>();
136 RegisterFunction<RemoveTabFunction>();
137 RegisterFunction<DetectTabLanguageFunction>();
138 RegisterFunction<CaptureVisibleTabFunction>();
139 RegisterFunction<TabsExecuteScriptFunction>();
140 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35141
[email protected]f7f3a5f2009-05-01 22:02:34142 // Page Actions.
[email protected]61424c062009-10-14 23:14:59143 RegisterFunction<EnablePageActionFunction>();
144 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46145 RegisterFunction<PageActionShowFunction>();
146 RegisterFunction<PageActionHideFunction>();
147 RegisterFunction<PageActionSetIconFunction>();
148 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29149 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34150
[email protected]ec9ac0df2009-10-01 18:06:47151 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59152 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24153 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59154 RegisterFunction<BrowserActionSetBadgeTextFunction>();
155 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50156 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47157
[email protected]f7f3a5f2009-05-01 22:02:34158 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59159 RegisterFunction<GetBookmarksFunction>();
160 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55161 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59162 RegisterFunction<GetBookmarkTreeFunction>();
163 RegisterFunction<SearchBookmarksFunction>();
164 RegisterFunction<RemoveBookmarkFunction>();
165 RegisterFunction<RemoveTreeBookmarkFunction>();
166 RegisterFunction<CreateBookmarkFunction>();
167 RegisterFunction<MoveBookmarkFunction>();
168 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43169
[email protected]f34e79632010-03-17 02:34:08170 // Infobars.
171 RegisterFunction<ShowInfoBarFunction>();
172
[email protected]9dd97bc2010-01-14 01:40:04173 // BookmarkManager
174 RegisterFunction<CopyBookmarkManagerFunction>();
175 RegisterFunction<CutBookmarkManagerFunction>();
176 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57177 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56178 RegisterFunction<ImportBookmarksFunction>();
179 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18180 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04181 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16182 RegisterFunction<StartDragBookmarkManagerFunction>();
183 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31184 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04185
[email protected]de768a832009-10-30 05:25:01186 // History
187 RegisterFunction<AddUrlHistoryFunction>();
188 RegisterFunction<DeleteAllHistoryFunction>();
189 RegisterFunction<DeleteRangeHistoryFunction>();
190 RegisterFunction<DeleteUrlHistoryFunction>();
191 RegisterFunction<GetVisitsHistoryFunction>();
192 RegisterFunction<SearchHistoryFunction>();
193
[email protected]f5205412010-03-16 00:19:34194 // Idle
195 RegisterFunction<ExtensionIdleQueryStateFunction>();
196
[email protected]198bcfe2009-09-09 22:56:28197 // I18N.
[email protected]61424c062009-10-14 23:14:59198 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28199
[email protected]381162b2010-01-28 17:29:35200 // Processes.
[email protected]8a661f82010-10-19 21:47:11201 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35202
[email protected]438772df2010-02-26 18:08:43203 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03204 RegisterFunction<MetricsGetEnabledFunction>();
205 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34206 RegisterFunction<MetricsRecordUserActionFunction>();
207 RegisterFunction<MetricsRecordValueFunction>();
208 RegisterFunction<MetricsRecordPercentageFunction>();
209 RegisterFunction<MetricsRecordCountFunction>();
210 RegisterFunction<MetricsRecordSmallCountFunction>();
211 RegisterFunction<MetricsRecordMediumCountFunction>();
212 RegisterFunction<MetricsRecordTimeFunction>();
213 RegisterFunction<MetricsRecordMediumTimeFunction>();
214 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43215
[email protected]17d40f02010-07-01 01:18:06216 // RLZ.
217#if defined(OS_WIN)
218 RegisterFunction<RlzRecordProductEventFunction>();
219 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21220 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06221 RegisterFunction<RlzClearProductStateFunction>();
222#endif
223
[email protected]898bbd32010-05-18 18:52:29224 // Cookies.
225 RegisterFunction<GetCookieFunction>();
226 RegisterFunction<GetAllCookiesFunction>();
227 RegisterFunction<SetCookieFunction>();
228 RegisterFunction<RemoveCookieFunction>();
229 RegisterFunction<GetAllCookieStoresFunction>();
230
[email protected]25fd1b2e2009-08-17 20:57:14231 // Test.
[email protected]61424c062009-10-14 23:14:59232 RegisterFunction<ExtensionTestPassFunction>();
233 RegisterFunction<ExtensionTestFailFunction>();
234 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02235 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50236 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07237 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39238 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56239
240 // Accessibility.
241 RegisterFunction<GetFocusedControlFunction>();
242 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58243
[email protected]8abe0a32010-08-12 00:40:22244 // Text-to-speech.
245 RegisterFunction<ExtensionTtsSpeakFunction>();
246 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
247 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56248 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22249
[email protected]2e3b5202010-03-23 06:52:41250 // Context Menus.
251 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15252 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41253 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15254 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33255
256 // Omnibox.
257 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53258 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44259
[email protected]8b8e7c92010-08-19 18:05:56260 // Sidebar.
261 RegisterFunction<CollapseSidebarFunction>();
262 RegisterFunction<ExpandSidebarFunction>();
263 RegisterFunction<GetStateSidebarFunction>();
264 RegisterFunction<HideSidebarFunction>();
265 RegisterFunction<NavigateSidebarFunction>();
266 RegisterFunction<SetBadgeTextSidebarFunction>();
267 RegisterFunction<SetIconSidebarFunction>();
268 RegisterFunction<SetTitleSidebarFunction>();
269 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45270
271#if defined(TOOLKIT_VIEWS)
272 // Input.
273 RegisterFunction<SendKeyboardEventInputFunction>();
274#endif
[email protected]cffd7892010-08-26 17:43:28275
[email protected]27072cad2011-05-09 19:46:40276#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
277 // IME
278 RegisterFunction<CandidateClickedInputUiFunction>();
279 RegisterFunction<CursorUpInputUiFunction>();
280 RegisterFunction<CursorDownInputUiFunction>();
281 RegisterFunction<PageUpInputUiFunction>();
282 RegisterFunction<PageDownInputUiFunction>();
283 RegisterFunction<RegisterInputUiFunction>();
284 RegisterFunction<PageUpInputUiFunction>();
285 RegisterFunction<PageDownInputUiFunction>();
286#endif
287
[email protected]cffd7892010-08-26 17:43:28288 // Management.
289 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23290 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56291 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28292 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28293 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23294
[email protected]a65882c2010-11-12 15:15:09295 // Extension module.
296 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01297 RegisterFunction<IsAllowedIncognitoAccessFunction>();
298 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09299
[email protected]63cda0c2010-09-01 04:41:23300 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24301 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23302 RegisterFunction<GetStoreLoginFunction>();
303 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24304 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30305 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17306 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30307 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26308
309 // WebRequest.
310 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48311 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25312
313 // Preferences.
314 RegisterFunction<GetPreferenceFunction>();
315 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27316 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22317
[email protected]b6b805e92011-04-16 09:24:14318 // ChromeOS-specific part of the API.
319#if defined(OS_CHROMEOS)
320 // Device Customization.
321 RegisterFunction<GetChromeosInfoFunction>();
322
323 // FileBrowserPrivate functions.
[email protected]94bda202011-04-18 23:31:00324 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14325 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00326 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14327 RegisterFunction<GetFileTasksFileBrowserFunction>();
328 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]9d4c2c52011-04-07 18:53:10329 RegisterFunction<SelectFileFunction>();
330 RegisterFunction<SelectFilesFunction>();
[email protected]94bda202011-04-18 23:31:00331 RegisterFunction<ViewFilesFunction>();
[email protected]f1852b52011-05-17 04:58:08332
333#if defined(TOUCH_UI)
334 // Input
335 RegisterFunction<SendHandwritingStrokeFunction>();
336 RegisterFunction<CancelHandwritingStrokesFunction>();
337#endif
[email protected]61b55b62011-03-24 09:03:10338#endif
339
[email protected]91ba3312011-03-17 20:39:22340 // Debugger
341 RegisterFunction<AttachDebuggerFunction>();
342 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33343 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35344}
345
[email protected]b83e4602009-05-15 22:58:33346void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
347 for (FactoryMap::iterator iter = factories_.begin();
348 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35349 names->push_back(iter->first);
350 }
351}
352
[email protected]b83e4602009-05-15 22:58:33353bool FactoryRegistry::OverrideFunction(const std::string& name,
354 ExtensionFunctionFactory factory) {
355 FactoryMap::iterator iter = factories_.find(name);
356 if (iter == factories_.end()) {
357 return false;
358 } else {
359 iter->second = factory;
360 return true;
361 }
362}
363
[email protected]bfdffe2b2009-04-24 22:05:35364ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
365 FactoryMap::iterator iter = factories_.find(name);
366 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33367 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56368 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33369 return function;
[email protected]bfdffe2b2009-04-24 22:05:35370}
371
[email protected]b83e4602009-05-15 22:58:33372}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35373
374// ExtensionFunctionDispatcher -------------------------------------------------
375
376void ExtensionFunctionDispatcher::GetAllFunctionNames(
377 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55378 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35379}
380
[email protected]b83e4602009-05-15 22:58:33381bool ExtensionFunctionDispatcher::OverrideFunction(
382 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55383 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33384}
385
386void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55387 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33388}
389
[email protected]c5dbef02011-05-13 05:06:09390ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile,
391 Delegate* delegate)
392 : profile_(profile),
[email protected]7eecaed52009-05-07 21:44:12393 delegate_(delegate),
[email protected]32dda362009-06-05 19:07:01394 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]bfdffe2b2009-04-24 22:05:35395}
396
[email protected]32dda362009-06-05 19:07:01397ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
398 peer_->dispatcher_ = NULL;
[email protected]32dda362009-06-05 19:07:01399}
400
[email protected]0ec92032010-03-24 19:59:41401Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
[email protected]c5dbef02011-05-13 05:06:09402 RenderViewHost* render_view_host, bool include_incognito) {
[email protected]0ec92032010-03-24 19:59:41403 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12404
[email protected]bc535ee52010-08-31 18:40:32405 // If the delegate has an associated browser, that is always the right answer.
406 if (browser)
407 return browser;
[email protected]9c45b7182009-08-04 16:44:43408
[email protected]bc535ee52010-08-31 18:40:32409 // Otherwise, try to default to a reasonable browser. If |include_incognito|
410 // is true, we will also search browsers in the incognito version of this
411 // profile. Note that the profile may already be incognito, in which case
412 // we will search the incognito version only, regardless of the value of
413 // |include_incognito|.
[email protected]c5dbef02011-05-13 05:06:09414 Profile* profile = render_view_host->process()->profile();
[email protected]b35b26b32011-05-05 20:35:14415 browser = BrowserList::FindTabbedBrowser(profile, include_incognito);
[email protected]0ec92032010-03-24 19:59:41416
[email protected]0ec92032010-03-24 19:59:41417 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42418 // a background_page onload chrome.tabs api call can make it into here
419 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41420 // A similar situation may arise during shutdown.
421 // TODO(rafaelw): Delay creation of background_page until the browser
422 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
423 return browser;
[email protected]b27257562009-11-16 23:28:26424}
425
[email protected]c5dbef02011-05-13 05:06:09426void ExtensionFunctionDispatcher::Dispatch(
427 const ExtensionHostMsg_Request_Params& params,
428 RenderViewHost* render_view_host) {
429 // TODO(aa): It would be cool to use ExtensionProcessManager to track which
430 // processes are extension processes rather than ChildProcessSecurityPolicy.
431 // EPM has richer information: it not only knows which processes contain
432 // at least one extension, but it knows which extensions are inside and what
433 // permissions the have. So we would be able to enforce permissions more
434 // granularly.
435 if (!ChildProcessSecurityPolicy::GetInstance()->HasExtensionBindings(
436 render_view_host->process()->id())) {
437 // TODO(aa): Allow content scripts access to low-threat extension APIs.
438 // See: crbug.com/80308.
439 LOG(ERROR) << "Extension API called from non-extension process.";
440 SendAccessDenied(render_view_host, params.request_id);
441 return;
442 }
[email protected]f82d57b52011-04-27 19:13:17443
[email protected]c5dbef02011-05-13 05:06:09444 ExtensionService* service = profile()->GetExtensionService();
445 if (!service)
446 return;
447
448 if (!service->ExtensionBindingsAllowed(params.source_url)) {
449 LOG(ERROR) << "Extension bindings not allowed for URL: "
450 << params.source_url.spec();
451 SendAccessDenied(render_view_host, params.request_id);
452 return;
453 }
454
455 // TODO(aa): When we allow content scripts to call extension APIs, we will
456 // have to pass the extension ID explicitly here, not use the source URL.
457 const Extension* extension = service->GetExtensionByURL(params.source_url);
458 if (!extension)
459 extension = service->GetExtensionByWebExtent(params.source_url);
460 if (!extension) {
461 LOG(ERROR) << "Extension does not exist for URL: "
462 << params.source_url.spec();
463 SendAccessDenied(render_view_host, params.request_id);
464 return;
465 }
466
467 if (!extension->HasApiPermission(params.name)) {
468 LOG(ERROR) << "Extension " << extension->id() << " does not have "
469 << "permission to function: " << params.name;
470 SendAccessDenied(render_view_host, params.request_id);
[email protected]f82d57b52011-04-27 19:13:17471 return;
472 }
473
[email protected]32dda362009-06-05 19:07:01474 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55475 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]c5dbef02011-05-13 05:06:09476 function->SetRenderViewHost(render_view_host);
[email protected]32dda362009-06-05 19:07:01477 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43478 function->set_profile(profile_);
[email protected]c5dbef02011-05-13 05:06:09479 function->set_extension_id(extension->id());
[email protected]8b8e7c92010-08-19 18:05:56480 function->SetArgs(&params.arguments);
481 function->set_source_url(params.source_url);
482 function->set_request_id(params.request_id);
483 function->set_has_callback(params.has_callback);
484 function->set_user_gesture(params.user_gesture);
[email protected]2a8f24e2010-11-03 21:37:05485 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41486
[email protected]d13950e2009-12-04 01:43:02487 ExtensionsQuotaService* quota = service->quota_service();
[email protected]c5dbef02011-05-13 05:06:09488 if (quota->Assess(extension->id(), function, &params.arguments,
[email protected]8b8e7c92010-08-19 18:05:56489 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26490 // See crbug.com/39178.
491 ExternalProtocolHandler::PermitLaunchUrl();
492
[email protected]d13950e2009-12-04 01:43:02493 function->Run();
494 } else {
[email protected]c5dbef02011-05-13 05:06:09495 render_view_host->Send(new ExtensionMsg_Response(
496 render_view_host->routing_id(), function->request_id(), false,
[email protected]553602e12011-04-05 17:01:18497 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02498 }
[email protected]bfdffe2b2009-04-24 22:05:35499}
500
[email protected]c5dbef02011-05-13 05:06:09501void ExtensionFunctionDispatcher::SendAccessDenied(
502 RenderViewHost* render_view_host, int request_id) {
503 render_view_host->Send(new ExtensionMsg_Response(
504 render_view_host->routing_id(), request_id, false, std::string(),
505 "Access to extension API denied."));
[email protected]bfdffe2b2009-04-24 22:05:35506}