blob: 1fd1ccf1a713d6fecd12afc1314d02887249b31b [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]446255952010-03-17 20:41:5819#include "chrome/browser/extensions/extension_clipboard_api.h"
[email protected]2e3b5202010-03-23 06:52:4120#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2921#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]91ba3312011-03-17 20:39:2222#include "chrome/browser/extensions/extension_debugger_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3523#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0124#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2725#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2526#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0827#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2828#include "chrome/browser/extensions/extension_management_api.h"
[email protected]e916901c2009-05-07 00:14:3129#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4330#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0931#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3332#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3433#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]598bbcc2011-02-24 10:03:2534#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]45776222009-07-15 20:21:5835#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3536#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4437#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0638#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2539#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5640#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3541#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1442#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2243#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1044#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2645#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2346#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0247#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]2f69b382011-02-19 00:34:2548#include "chrome/browser/external_protocol_handler.h"
[email protected]c66027632010-12-22 21:26:3049#include "chrome/browser/metrics/user_metrics.h"
[email protected]8ecad5e2010-12-02 21:18:3350#include "chrome/browser/profiles/profile.h"
[email protected]71b73f02011-04-06 15:57:2951#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3352#include "chrome/browser/ui/browser_window.h"
[email protected]fd42ac30f2011-02-27 19:33:3653#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
54#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]44c49c92011-03-28 16:17:2355#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4356#include "chrome/common/url_constants.h"
[email protected]5de634712011-03-02 00:20:1957#include "content/browser/renderer_host/render_process_host.h"
58#include "content/browser/renderer_host/render_view_host.h"
[email protected]f20d7332011-03-08 21:11:5359#include "content/common/notification_service.h"
[email protected]4dd57932011-03-17 06:06:1260#include "content/common/result_codes.h"
[email protected]939856a2010-08-24 20:29:0261#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3562
[email protected]8f9d4eb2011-02-05 01:39:1063#if defined(TOOLKIT_VIEWS)
64#include "chrome/browser/extensions/extension_input_api.h"
65#endif
[email protected]d6833852010-08-20 18:00:4566
[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"
70#endif
71
[email protected]bfdffe2b2009-04-24 22:05:3572// FactoryRegistry -------------------------------------------------------------
73
74namespace {
75
[email protected]b83e4602009-05-15 22:58:3376// Template for defining ExtensionFunctionFactory.
77template<class T>
78ExtensionFunction* NewExtensionFunction() {
79 return new T();
80}
[email protected]bfdffe2b2009-04-24 22:05:3581
[email protected]b83e4602009-05-15 22:58:3382// Contains a list of all known extension functions and allows clients to
83// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3584class FactoryRegistry {
85 public:
[email protected]8e8bb6d2010-12-13 08:18:5586 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3387 FactoryRegistry() { ResetFunctions(); }
88
89 // Resets all functions to their default values.
90 void ResetFunctions();
91
92 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3593 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3394
95 // Allows overriding of specific functions (e.g. for testing). Functions
96 // must be previously registered. Returns true if successful.
97 bool OverrideFunction(const std::string& name,
98 ExtensionFunctionFactory factory);
99
100 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35101 ExtensionFunction* NewFunction(const std::string& name);
102
103 private:
[email protected]61424c062009-10-14 23:14:59104 template<class T>
105 void RegisterFunction() {
106 factories_[T::function_name()] = &NewExtensionFunction<T>;
107 }
108
[email protected]bfdffe2b2009-04-24 22:05:35109 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
110 FactoryMap factories_;
111};
112
[email protected]8e8bb6d2010-12-13 08:18:55113FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35114 return Singleton<FactoryRegistry>::get();
115}
116
[email protected]b83e4602009-05-15 22:58:33117void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35118 // Register all functions here.
119
[email protected]e515f5d2009-05-05 03:05:00120 // Windows
[email protected]61424c062009-10-14 23:14:59121 RegisterFunction<GetWindowFunction>();
122 RegisterFunction<GetCurrentWindowFunction>();
123 RegisterFunction<GetLastFocusedWindowFunction>();
124 RegisterFunction<GetAllWindowsFunction>();
125 RegisterFunction<CreateWindowFunction>();
126 RegisterFunction<UpdateWindowFunction>();
127 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33128
[email protected]e515f5d2009-05-05 03:05:00129 // Tabs
[email protected]61424c062009-10-14 23:14:59130 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41131 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59132 RegisterFunction<GetSelectedTabFunction>();
133 RegisterFunction<GetAllTabsInWindowFunction>();
134 RegisterFunction<CreateTabFunction>();
135 RegisterFunction<UpdateTabFunction>();
136 RegisterFunction<MoveTabFunction>();
137 RegisterFunction<RemoveTabFunction>();
138 RegisterFunction<DetectTabLanguageFunction>();
139 RegisterFunction<CaptureVisibleTabFunction>();
140 RegisterFunction<TabsExecuteScriptFunction>();
141 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35142
[email protected]f7f3a5f2009-05-01 22:02:34143 // Page Actions.
[email protected]61424c062009-10-14 23:14:59144 RegisterFunction<EnablePageActionFunction>();
145 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46146 RegisterFunction<PageActionShowFunction>();
147 RegisterFunction<PageActionHideFunction>();
148 RegisterFunction<PageActionSetIconFunction>();
149 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29150 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34151
[email protected]ec9ac0df2009-10-01 18:06:47152 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59153 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24154 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<BrowserActionSetBadgeTextFunction>();
156 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50157 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47158
[email protected]f7f3a5f2009-05-01 22:02:34159 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59160 RegisterFunction<GetBookmarksFunction>();
161 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55162 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59163 RegisterFunction<GetBookmarkTreeFunction>();
164 RegisterFunction<SearchBookmarksFunction>();
165 RegisterFunction<RemoveBookmarkFunction>();
166 RegisterFunction<RemoveTreeBookmarkFunction>();
167 RegisterFunction<CreateBookmarkFunction>();
168 RegisterFunction<MoveBookmarkFunction>();
169 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43170
[email protected]f34e79632010-03-17 02:34:08171 // Infobars.
172 RegisterFunction<ShowInfoBarFunction>();
173
[email protected]9dd97bc2010-01-14 01:40:04174 // BookmarkManager
175 RegisterFunction<CopyBookmarkManagerFunction>();
176 RegisterFunction<CutBookmarkManagerFunction>();
177 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57178 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56179 RegisterFunction<ImportBookmarksFunction>();
180 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18181 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04182 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16183 RegisterFunction<StartDragBookmarkManagerFunction>();
184 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31185 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04186
[email protected]de768a832009-10-30 05:25:01187 // History
188 RegisterFunction<AddUrlHistoryFunction>();
189 RegisterFunction<DeleteAllHistoryFunction>();
190 RegisterFunction<DeleteRangeHistoryFunction>();
191 RegisterFunction<DeleteUrlHistoryFunction>();
192 RegisterFunction<GetVisitsHistoryFunction>();
193 RegisterFunction<SearchHistoryFunction>();
194
[email protected]f5205412010-03-16 00:19:34195 // Idle
196 RegisterFunction<ExtensionIdleQueryStateFunction>();
197
[email protected]198bcfe2009-09-09 22:56:28198 // I18N.
[email protected]61424c062009-10-14 23:14:59199 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28200
[email protected]381162b2010-01-28 17:29:35201 // Processes.
[email protected]8a661f82010-10-19 21:47:11202 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35203
[email protected]438772df2010-02-26 18:08:43204 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03205 RegisterFunction<MetricsGetEnabledFunction>();
206 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34207 RegisterFunction<MetricsRecordUserActionFunction>();
208 RegisterFunction<MetricsRecordValueFunction>();
209 RegisterFunction<MetricsRecordPercentageFunction>();
210 RegisterFunction<MetricsRecordCountFunction>();
211 RegisterFunction<MetricsRecordSmallCountFunction>();
212 RegisterFunction<MetricsRecordMediumCountFunction>();
213 RegisterFunction<MetricsRecordTimeFunction>();
214 RegisterFunction<MetricsRecordMediumTimeFunction>();
215 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43216
[email protected]17d40f02010-07-01 01:18:06217 // RLZ.
218#if defined(OS_WIN)
219 RegisterFunction<RlzRecordProductEventFunction>();
220 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21221 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06222 RegisterFunction<RlzClearProductStateFunction>();
223#endif
224
[email protected]898bbd32010-05-18 18:52:29225 // Cookies.
226 RegisterFunction<GetCookieFunction>();
227 RegisterFunction<GetAllCookiesFunction>();
228 RegisterFunction<SetCookieFunction>();
229 RegisterFunction<RemoveCookieFunction>();
230 RegisterFunction<GetAllCookieStoresFunction>();
231
[email protected]25fd1b2e2009-08-17 20:57:14232 // Test.
[email protected]61424c062009-10-14 23:14:59233 RegisterFunction<ExtensionTestPassFunction>();
234 RegisterFunction<ExtensionTestFailFunction>();
235 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02236 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50237 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07238 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39239 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56240
241 // Accessibility.
242 RegisterFunction<GetFocusedControlFunction>();
243 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58244
[email protected]8abe0a32010-08-12 00:40:22245 // Text-to-speech.
246 RegisterFunction<ExtensionTtsSpeakFunction>();
247 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
248 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56249 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22250
[email protected]446255952010-03-17 20:41:58251 // Clipboard.
252 RegisterFunction<ExecuteCopyClipboardFunction>();
253 RegisterFunction<ExecuteCutClipboardFunction>();
254 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41255
256 // Context Menus.
257 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15258 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41259 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15260 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33261
262 // Omnibox.
263 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53264 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44265
[email protected]8b8e7c92010-08-19 18:05:56266 // Sidebar.
267 RegisterFunction<CollapseSidebarFunction>();
268 RegisterFunction<ExpandSidebarFunction>();
269 RegisterFunction<GetStateSidebarFunction>();
270 RegisterFunction<HideSidebarFunction>();
271 RegisterFunction<NavigateSidebarFunction>();
272 RegisterFunction<SetBadgeTextSidebarFunction>();
273 RegisterFunction<SetIconSidebarFunction>();
274 RegisterFunction<SetTitleSidebarFunction>();
275 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45276
277#if defined(TOOLKIT_VIEWS)
278 // Input.
279 RegisterFunction<SendKeyboardEventInputFunction>();
280#endif
[email protected]cffd7892010-08-26 17:43:28281
282 // Management.
283 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23284 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56285 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28286 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28287 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23288
[email protected]a65882c2010-11-12 15:15:09289 // Extension module.
290 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01291 RegisterFunction<IsAllowedIncognitoAccessFunction>();
292 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09293
[email protected]63cda0c2010-09-01 04:41:23294 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24295 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23296 RegisterFunction<GetStoreLoginFunction>();
297 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24298 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30299 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17300 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30301 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26302
303 // WebRequest.
304 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48305 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25306
307 // Preferences.
308 RegisterFunction<GetPreferenceFunction>();
309 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27310 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22311
[email protected]b6b805e92011-04-16 09:24:14312 // ChromeOS-specific part of the API.
313#if defined(OS_CHROMEOS)
314 // Device Customization.
315 RegisterFunction<GetChromeosInfoFunction>();
316
317 // FileBrowserPrivate functions.
318 RegisterFunction<ExecuteTasksFileBrowserFunction>();
319 RegisterFunction<GetFileTasksFileBrowserFunction>();
320 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]9d4c2c52011-04-07 18:53:10321 RegisterFunction<SelectFileFunction>();
322 RegisterFunction<SelectFilesFunction>();
323 RegisterFunction<CancelFileDialogFunction>();
324 RegisterFunction<FileDialogStringsFunction>();
[email protected]61b55b62011-03-24 09:03:10325#endif
326
[email protected]91ba3312011-03-17 20:39:22327 // Debugger
328 RegisterFunction<AttachDebuggerFunction>();
329 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33330 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35331}
332
[email protected]b83e4602009-05-15 22:58:33333void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
334 for (FactoryMap::iterator iter = factories_.begin();
335 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35336 names->push_back(iter->first);
337 }
338}
339
[email protected]b83e4602009-05-15 22:58:33340bool FactoryRegistry::OverrideFunction(const std::string& name,
341 ExtensionFunctionFactory factory) {
342 FactoryMap::iterator iter = factories_.find(name);
343 if (iter == factories_.end()) {
344 return false;
345 } else {
346 iter->second = factory;
347 return true;
348 }
349}
350
[email protected]bfdffe2b2009-04-24 22:05:35351ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
352 FactoryMap::iterator iter = factories_.find(name);
353 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33354 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56355 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33356 return function;
[email protected]bfdffe2b2009-04-24 22:05:35357}
358
[email protected]b83e4602009-05-15 22:58:33359}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35360
361// ExtensionFunctionDispatcher -------------------------------------------------
362
363void ExtensionFunctionDispatcher::GetAllFunctionNames(
364 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55365 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35366}
367
[email protected]b83e4602009-05-15 22:58:33368bool ExtensionFunctionDispatcher::OverrideFunction(
369 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55370 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33371}
372
373void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55374 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33375}
376
[email protected]a91afcb2010-03-25 21:15:02377ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
378 RenderViewHost* render_view_host,
379 Delegate* delegate,
380 const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:00381 ExtensionService* service =
382 render_view_host->process()->profile()->GetExtensionService();
[email protected]a91afcb2010-03-25 21:15:02383 DCHECK(service);
384
[email protected]583d45c12010-08-31 02:48:12385 if (!service->ExtensionBindingsAllowed(url))
386 return NULL;
387
[email protected]9adb9692010-10-29 23:14:02388 const Extension* extension = service->GetExtensionByURL(url);
[email protected]583d45c12010-08-31 02:48:12389 if (!extension)
390 extension = service->GetExtensionByWebExtent(url);
391
[email protected]a91afcb2010-03-25 21:15:02392 if (extension)
393 return new ExtensionFunctionDispatcher(render_view_host, delegate,
394 extension, url);
395 else
396 return NULL;
397}
398
[email protected]bfdffe2b2009-04-24 22:05:35399ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
400 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12401 Delegate* delegate,
[email protected]9adb9692010-10-29 23:14:02402 const Extension* extension,
[email protected]811bfe32009-07-01 08:46:25403 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30404 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58405 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12406 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25407 url_(url),
[email protected]583d45c12010-08-31 02:48:12408 extension_id_(extension->id()),
[email protected]32dda362009-06-05 19:07:01409 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43410 // TODO(erikkay) should we do something for these errors in Release?
[email protected]35506352009-08-07 18:58:19411 DCHECK(extension);
[email protected]583d45c12010-08-31 02:48:12412 DCHECK(url.SchemeIs(chrome::kExtensionScheme) ||
413 extension->location() == Extension::COMPONENT);
[email protected]9c45b7182009-08-04 16:44:43414
[email protected]45776222009-07-15 20:21:58415 // Notify the ExtensionProcessManager that the view was created.
416 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
417 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45418 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19419
[email protected]584b8e3f2010-04-10 00:23:37420 // If the extension has permission to load chrome://favicon/ resources we need
[email protected]16835e62011-03-16 14:13:08421 // to make sure that the FaviconSource is registered with the
[email protected]584b8e3f2010-04-10 00:23:37422 // ChromeURLDataManager.
[email protected]f16039d22011-03-16 18:54:05423 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]16835e62011-03-16 14:13:08424 FaviconSource* favicon_source = new FaviconSource(profile_);
[email protected]248ce192011-02-10 15:26:34425 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
[email protected]584b8e3f2010-04-10 00:23:37426 }
427
[email protected]35506352009-08-07 18:58:19428 // Update the extension permissions. Doing this each time we create an EFD
429 // ensures that new processes are informed of permissions for newly installed
430 // extensions.
[email protected]44c49c92011-03-28 16:17:23431 render_view_host->Send(new ExtensionMsg_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19432 extension->id(), extension->api_permissions()));
[email protected]44c49c92011-03-28 16:17:23433 render_view_host->Send(new ExtensionMsg_SetHostPermissions(
[email protected]cccf90932009-08-23 17:56:25434 extension->url(), extension->host_permissions()));
[email protected]68f07912010-03-05 18:33:58435
436 NotificationService::current()->Notify(
437 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
438 Source<Profile>(profile_),
439 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35440}
441
[email protected]32dda362009-06-05 19:07:01442ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
443 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58444
445 NotificationService::current()->Notify(
446 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
447 Source<Profile>(profile_),
448 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01449}
450
[email protected]0ec92032010-03-24 19:59:41451Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
452 bool include_incognito) {
453 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12454
[email protected]bc535ee52010-08-31 18:40:32455 // If the delegate has an associated browser, that is always the right answer.
456 if (browser)
457 return browser;
[email protected]9c45b7182009-08-04 16:44:43458
[email protected]bc535ee52010-08-31 18:40:32459 // Otherwise, try to default to a reasonable browser. If |include_incognito|
460 // is true, we will also search browsers in the incognito version of this
461 // profile. Note that the profile may already be incognito, in which case
462 // we will search the incognito version only, regardless of the value of
463 // |include_incognito|.
[email protected]0ec92032010-03-24 19:59:41464 Profile* profile = render_view_host()->process()->profile();
[email protected]aa916d572010-08-13 02:59:31465 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31466 include_incognito);
[email protected]0ec92032010-03-24 19:59:41467
[email protected]0ec92032010-03-24 19:59:41468 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42469 // a background_page onload chrome.tabs api call can make it into here
470 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41471 // A similar situation may arise during shutdown.
472 // TODO(rafaelw): Delay creation of background_page until the browser
473 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
474 return browser;
[email protected]b27257562009-11-16 23:28:26475}
476
[email protected]8b8e7c92010-08-19 18:05:56477void ExtensionFunctionDispatcher::HandleRequest(
[email protected]44c49c92011-03-28 16:17:23478 const ExtensionHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01479 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55480 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01481 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43482 function->set_profile(profile_);
483 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56484 function->SetArgs(&params.arguments);
485 function->set_source_url(params.source_url);
486 function->set_request_id(params.request_id);
487 function->set_has_callback(params.has_callback);
488 function->set_user_gesture(params.user_gesture);
[email protected]eaa7dd182010-12-14 11:09:00489 ExtensionService* service = profile()->GetExtensionService();
[email protected]d13950e2009-12-04 01:43:02490 DCHECK(service);
[email protected]9adb9692010-10-29 23:14:02491 const Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]cb0ce1e022010-03-10 19:54:41492 DCHECK(extension);
[email protected]2a8f24e2010-11-03 21:37:05493 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41494
[email protected]583d45c12010-08-31 02:48:12495 if (!service->ExtensionBindingsAllowed(function->source_url()) ||
[email protected]246c05f2010-09-10 09:12:11496 !extension->HasApiPermission(function->name())) {
[email protected]553602e12011-04-05 17:01:18497 render_view_host_->Send(new ExtensionMsg_Response(
498 render_view_host_->routing_id(), function->request_id(), false,
499 std::string(), "Access to extension API denied."));
[email protected]583d45c12010-08-31 02:48:12500 return;
501 }
502
[email protected]d13950e2009-12-04 01:43:02503 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56504 if (quota->Assess(extension_id(), function, &params.arguments,
505 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26506 // See crbug.com/39178.
507 ExternalProtocolHandler::PermitLaunchUrl();
508
[email protected]d13950e2009-12-04 01:43:02509 function->Run();
510 } else {
[email protected]553602e12011-04-05 17:01:18511 render_view_host_->Send(new ExtensionMsg_Response(
512 render_view_host_->routing_id(), function->request_id(), false,
513 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02514 }
[email protected]bfdffe2b2009-04-24 22:05:35515}
516
[email protected]c6619182009-05-12 14:59:32517void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
518 bool success) {
[email protected]553602e12011-04-05 17:01:18519 render_view_host_->Send(new ExtensionMsg_Response(
520 render_view_host_->routing_id(), function->request_id(), success,
521 function->GetResult(), function->GetError()));
[email protected]bfdffe2b2009-04-24 22:05:35522}
523
524void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14525 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45526 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35527 " : terminating renderer.";
528 if (RenderProcessHost::run_renderer_in_process()) {
529 // In single process mode it is better if we don't suicide but just crash.
530 CHECK(false);
531 } else {
532 NOTREACHED();
[email protected]c66027632010-12-22 21:26:30533 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]201b2732009-11-13 18:57:46534 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35535 ResultCodes::KILLED_BAD_MESSAGE, false);
536 }
537}
538
539Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58540 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35541}