blob: efca9b68ef724ed1b54061dee40474a3661f3a90 [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]438772df2010-02-26 18:08:4329#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0930#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3331#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3432#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]598bbcc2011-02-24 10:03:2533#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]45776222009-07-15 20:21:5834#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3535#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4436#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0637#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2538#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5639#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3540#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1441#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2242#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1043#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2644#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2345#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0246#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]2f69b382011-02-19 00:34:2547#include "chrome/browser/external_protocol_handler.h"
[email protected]c66027632010-12-22 21:26:3048#include "chrome/browser/metrics/user_metrics.h"
[email protected]8ecad5e2010-12-02 21:18:3349#include "chrome/browser/profiles/profile.h"
[email protected]71b73f02011-04-06 15:57:2950#include "chrome/browser/ui/browser_list.h"
[email protected]00070c732011-04-09 15:31:3351#include "chrome/browser/ui/browser_window.h"
[email protected]fd42ac30f2011-02-27 19:33:3652#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
53#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]44c49c92011-03-28 16:17:2354#include "chrome/common/extensions/extension_messages.h"
[email protected]9c45b7182009-08-04 16:44:4355#include "chrome/common/url_constants.h"
[email protected]f82d57b52011-04-27 19:13:1756#include "content/browser/child_process_security_policy.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]f82d57b52011-04-27 19:13:1761#include "ipc/ipc_message.h"
62#include "ipc/ipc_message_macros.h"
[email protected]939856a2010-08-24 20:29:0263#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3564
[email protected]8f9d4eb2011-02-05 01:39:1065#if defined(TOOLKIT_VIEWS)
66#include "chrome/browser/extensions/extension_input_api.h"
67#endif
[email protected]d6833852010-08-20 18:00:4568
[email protected]61b55b62011-03-24 09:03:1069#if defined(OS_CHROMEOS)
[email protected]b6b805e92011-04-16 09:24:1470#include "chrome/browser/extensions/extension_file_browser_private_api.h"
[email protected]61b55b62011-03-24 09:03:1071#include "chrome/browser/extensions/extension_info_private_api_chromeos.h"
72#endif
73
[email protected]bfdffe2b2009-04-24 22:05:3574// FactoryRegistry -------------------------------------------------------------
75
76namespace {
77
[email protected]b83e4602009-05-15 22:58:3378// Template for defining ExtensionFunctionFactory.
79template<class T>
80ExtensionFunction* NewExtensionFunction() {
81 return new T();
82}
[email protected]bfdffe2b2009-04-24 22:05:3583
[email protected]b83e4602009-05-15 22:58:3384// Contains a list of all known extension functions and allows clients to
85// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3586class FactoryRegistry {
87 public:
[email protected]8e8bb6d2010-12-13 08:18:5588 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3389 FactoryRegistry() { ResetFunctions(); }
90
91 // Resets all functions to their default values.
92 void ResetFunctions();
93
94 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3595 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3396
97 // Allows overriding of specific functions (e.g. for testing). Functions
98 // must be previously registered. Returns true if successful.
99 bool OverrideFunction(const std::string& name,
100 ExtensionFunctionFactory factory);
101
102 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:35103 ExtensionFunction* NewFunction(const std::string& name);
104
105 private:
[email protected]61424c062009-10-14 23:14:59106 template<class T>
107 void RegisterFunction() {
108 factories_[T::function_name()] = &NewExtensionFunction<T>;
109 }
110
[email protected]bfdffe2b2009-04-24 22:05:35111 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
112 FactoryMap factories_;
113};
114
[email protected]8e8bb6d2010-12-13 08:18:55115FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35116 return Singleton<FactoryRegistry>::get();
117}
118
[email protected]b83e4602009-05-15 22:58:33119void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35120 // Register all functions here.
121
[email protected]e515f5d2009-05-05 03:05:00122 // Windows
[email protected]61424c062009-10-14 23:14:59123 RegisterFunction<GetWindowFunction>();
124 RegisterFunction<GetCurrentWindowFunction>();
125 RegisterFunction<GetLastFocusedWindowFunction>();
126 RegisterFunction<GetAllWindowsFunction>();
127 RegisterFunction<CreateWindowFunction>();
128 RegisterFunction<UpdateWindowFunction>();
129 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33130
[email protected]e515f5d2009-05-05 03:05:00131 // Tabs
[email protected]61424c062009-10-14 23:14:59132 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41133 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59134 RegisterFunction<GetSelectedTabFunction>();
135 RegisterFunction<GetAllTabsInWindowFunction>();
136 RegisterFunction<CreateTabFunction>();
137 RegisterFunction<UpdateTabFunction>();
138 RegisterFunction<MoveTabFunction>();
139 RegisterFunction<RemoveTabFunction>();
140 RegisterFunction<DetectTabLanguageFunction>();
141 RegisterFunction<CaptureVisibleTabFunction>();
142 RegisterFunction<TabsExecuteScriptFunction>();
143 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35144
[email protected]f7f3a5f2009-05-01 22:02:34145 // Page Actions.
[email protected]61424c062009-10-14 23:14:59146 RegisterFunction<EnablePageActionFunction>();
147 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46148 RegisterFunction<PageActionShowFunction>();
149 RegisterFunction<PageActionHideFunction>();
150 RegisterFunction<PageActionSetIconFunction>();
151 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29152 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34153
[email protected]ec9ac0df2009-10-01 18:06:47154 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24156 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59157 RegisterFunction<BrowserActionSetBadgeTextFunction>();
158 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50159 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47160
[email protected]f7f3a5f2009-05-01 22:02:34161 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59162 RegisterFunction<GetBookmarksFunction>();
163 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55164 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59165 RegisterFunction<GetBookmarkTreeFunction>();
166 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]446255952010-03-17 20:41:58253 // Clipboard.
254 RegisterFunction<ExecuteCopyClipboardFunction>();
255 RegisterFunction<ExecuteCutClipboardFunction>();
256 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41257
258 // Context Menus.
259 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15260 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41261 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15262 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33263
264 // Omnibox.
265 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53266 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44267
[email protected]8b8e7c92010-08-19 18:05:56268 // Sidebar.
269 RegisterFunction<CollapseSidebarFunction>();
270 RegisterFunction<ExpandSidebarFunction>();
271 RegisterFunction<GetStateSidebarFunction>();
272 RegisterFunction<HideSidebarFunction>();
273 RegisterFunction<NavigateSidebarFunction>();
274 RegisterFunction<SetBadgeTextSidebarFunction>();
275 RegisterFunction<SetIconSidebarFunction>();
276 RegisterFunction<SetTitleSidebarFunction>();
277 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45278
279#if defined(TOOLKIT_VIEWS)
280 // Input.
281 RegisterFunction<SendKeyboardEventInputFunction>();
282#endif
[email protected]cffd7892010-08-26 17:43:28283
284 // Management.
285 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23286 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56287 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28288 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28289 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23290
[email protected]a65882c2010-11-12 15:15:09291 // Extension module.
292 RegisterFunction<SetUpdateUrlDataFunction>();
[email protected]c7c401d2011-03-16 10:20:01293 RegisterFunction<IsAllowedIncognitoAccessFunction>();
294 RegisterFunction<IsAllowedFileSchemeAccessFunction>();
[email protected]a65882c2010-11-12 15:15:09295
[email protected]63cda0c2010-09-01 04:41:23296 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24297 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23298 RegisterFunction<GetStoreLoginFunction>();
299 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24300 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30301 RegisterFunction<BeginInstallFunction>();
[email protected]5349ac6d2011-04-05 22:20:17302 RegisterFunction<BeginInstallWithManifestFunction>();
[email protected]b1f04cc2010-11-10 22:59:30303 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26304
305 // WebRequest.
306 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48307 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25308
309 // Preferences.
310 RegisterFunction<GetPreferenceFunction>();
311 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27312 RegisterFunction<ClearPreferenceFunction>();
[email protected]91ba3312011-03-17 20:39:22313
[email protected]b6b805e92011-04-16 09:24:14314 // ChromeOS-specific part of the API.
315#if defined(OS_CHROMEOS)
316 // Device Customization.
317 RegisterFunction<GetChromeosInfoFunction>();
318
319 // FileBrowserPrivate functions.
[email protected]94bda202011-04-18 23:31:00320 RegisterFunction<CancelFileDialogFunction>();
[email protected]b6b805e92011-04-16 09:24:14321 RegisterFunction<ExecuteTasksFileBrowserFunction>();
[email protected]94bda202011-04-18 23:31:00322 RegisterFunction<FileDialogStringsFunction>();
[email protected]b6b805e92011-04-16 09:24:14323 RegisterFunction<GetFileTasksFileBrowserFunction>();
324 RegisterFunction<RequestLocalFileSystemFunction>();
[email protected]9d4c2c52011-04-07 18:53:10325 RegisterFunction<SelectFileFunction>();
326 RegisterFunction<SelectFilesFunction>();
[email protected]94bda202011-04-18 23:31:00327 RegisterFunction<ViewFilesFunction>();
[email protected]61b55b62011-03-24 09:03:10328#endif
329
[email protected]91ba3312011-03-17 20:39:22330 // Debugger
331 RegisterFunction<AttachDebuggerFunction>();
332 RegisterFunction<DetachDebuggerFunction>();
[email protected]ac310102011-04-08 14:08:33333 RegisterFunction<SendRequestDebuggerFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35334}
335
[email protected]b83e4602009-05-15 22:58:33336void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
337 for (FactoryMap::iterator iter = factories_.begin();
338 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35339 names->push_back(iter->first);
340 }
341}
342
[email protected]b83e4602009-05-15 22:58:33343bool FactoryRegistry::OverrideFunction(const std::string& name,
344 ExtensionFunctionFactory factory) {
345 FactoryMap::iterator iter = factories_.find(name);
346 if (iter == factories_.end()) {
347 return false;
348 } else {
349 iter->second = factory;
350 return true;
351 }
352}
353
[email protected]bfdffe2b2009-04-24 22:05:35354ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
355 FactoryMap::iterator iter = factories_.find(name);
356 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33357 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56358 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33359 return function;
[email protected]bfdffe2b2009-04-24 22:05:35360}
361
[email protected]b83e4602009-05-15 22:58:33362}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35363
364// ExtensionFunctionDispatcher -------------------------------------------------
365
366void ExtensionFunctionDispatcher::GetAllFunctionNames(
367 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55368 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35369}
370
[email protected]b83e4602009-05-15 22:58:33371bool ExtensionFunctionDispatcher::OverrideFunction(
372 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55373 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33374}
375
376void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55377 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33378}
379
[email protected]a91afcb2010-03-25 21:15:02380ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
381 RenderViewHost* render_view_host,
382 Delegate* delegate,
383 const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:00384 ExtensionService* service =
385 render_view_host->process()->profile()->GetExtensionService();
[email protected]a91afcb2010-03-25 21:15:02386 DCHECK(service);
387
[email protected]583d45c12010-08-31 02:48:12388 if (!service->ExtensionBindingsAllowed(url))
389 return NULL;
390
[email protected]9adb9692010-10-29 23:14:02391 const Extension* extension = service->GetExtensionByURL(url);
[email protected]583d45c12010-08-31 02:48:12392 if (!extension)
393 extension = service->GetExtensionByWebExtent(url);
394
[email protected]a91afcb2010-03-25 21:15:02395 if (extension)
396 return new ExtensionFunctionDispatcher(render_view_host, delegate,
397 extension, url);
398 else
399 return NULL;
400}
401
[email protected]bfdffe2b2009-04-24 22:05:35402ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
403 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12404 Delegate* delegate,
[email protected]9adb9692010-10-29 23:14:02405 const Extension* extension,
[email protected]811bfe32009-07-01 08:46:25406 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30407 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58408 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12409 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25410 url_(url),
[email protected]583d45c12010-08-31 02:48:12411 extension_id_(extension->id()),
[email protected]32dda362009-06-05 19:07:01412 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43413 // TODO(erikkay) should we do something for these errors in Release?
[email protected]35506352009-08-07 18:58:19414 DCHECK(extension);
[email protected]583d45c12010-08-31 02:48:12415 DCHECK(url.SchemeIs(chrome::kExtensionScheme) ||
416 extension->location() == Extension::COMPONENT);
[email protected]9c45b7182009-08-04 16:44:43417
[email protected]45776222009-07-15 20:21:58418 // Notify the ExtensionProcessManager that the view was created.
419 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
420 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45421 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19422
[email protected]584b8e3f2010-04-10 00:23:37423 // If the extension has permission to load chrome://favicon/ resources we need
[email protected]16835e62011-03-16 14:13:08424 // to make sure that the FaviconSource is registered with the
[email protected]584b8e3f2010-04-10 00:23:37425 // ChromeURLDataManager.
[email protected]f16039d22011-03-16 18:54:05426 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]7c158cca2011-04-20 17:57:06427 FaviconSource* favicon_source = new FaviconSource(profile_,
428 FaviconSource::FAVICON);
[email protected]248ce192011-02-10 15:26:34429 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
[email protected]584b8e3f2010-04-10 00:23:37430 }
431
[email protected]912aee1e02011-04-25 23:46:59432 // Activate this extension in the renderer. This must be done before any
433 // extension JavaScript code runs because it controls some privileges the
434 // extension code has in the renderer.
435 render_view_host->Send(new ExtensionMsg_ActivateExtension(extension->id()));
[email protected]68f07912010-03-05 18:33:58436
437 NotificationService::current()->Notify(
438 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
439 Source<Profile>(profile_),
440 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35441}
442
[email protected]32dda362009-06-05 19:07:01443ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
444 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58445
446 NotificationService::current()->Notify(
447 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
448 Source<Profile>(profile_),
449 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01450}
451
[email protected]0ec92032010-03-24 19:59:41452Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
453 bool include_incognito) {
454 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12455
[email protected]bc535ee52010-08-31 18:40:32456 // If the delegate has an associated browser, that is always the right answer.
457 if (browser)
458 return browser;
[email protected]9c45b7182009-08-04 16:44:43459
[email protected]bc535ee52010-08-31 18:40:32460 // Otherwise, try to default to a reasonable browser. If |include_incognito|
461 // is true, we will also search browsers in the incognito version of this
462 // profile. Note that the profile may already be incognito, in which case
463 // we will search the incognito version only, regardless of the value of
464 // |include_incognito|.
[email protected]0ec92032010-03-24 19:59:41465 Profile* profile = render_view_host()->process()->profile();
[email protected]aa916d572010-08-13 02:59:31466 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31467 include_incognito);
[email protected]0ec92032010-03-24 19:59:41468
[email protected]0ec92032010-03-24 19:59:41469 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42470 // a background_page onload chrome.tabs api call can make it into here
471 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41472 // A similar situation may arise during shutdown.
473 // TODO(rafaelw): Delay creation of background_page until the browser
474 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
475 return browser;
[email protected]b27257562009-11-16 23:28:26476}
477
[email protected]f82d57b52011-04-27 19:13:17478bool ExtensionFunctionDispatcher::OnMessageReceived(
479 const IPC::Message& message) {
480 bool handled = true;
481 IPC_BEGIN_MESSAGE_MAP(ExtensionFunctionDispatcher, message)
482 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
483 IPC_MESSAGE_UNHANDLED(handled = false)
484 IPC_END_MESSAGE_MAP()
485 return handled;
486}
487
488void ExtensionFunctionDispatcher::OnRequest(
489 const ExtensionHostMsg_Request_Params& params) {
490 if (!ChildProcessSecurityPolicy::GetInstance()->
491 HasExtensionBindings(render_view_host_->process()->id())) {
492 // This can happen if someone uses window.open() to open an extension URL
493 // from a non-extension context.
494 render_view_host_->Send(new ExtensionMsg_Response(
495 render_view_host_->routing_id(), params.request_id, false,
496 std::string(), "Access to extension API denied."));
497 return;
498 }
499
[email protected]32dda362009-06-05 19:07:01500 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55501 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01502 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43503 function->set_profile(profile_);
504 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56505 function->SetArgs(&params.arguments);
506 function->set_source_url(params.source_url);
507 function->set_request_id(params.request_id);
508 function->set_has_callback(params.has_callback);
509 function->set_user_gesture(params.user_gesture);
[email protected]eaa7dd182010-12-14 11:09:00510 ExtensionService* service = profile()->GetExtensionService();
[email protected]d13950e2009-12-04 01:43:02511 DCHECK(service);
[email protected]9adb9692010-10-29 23:14:02512 const Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]cb0ce1e022010-03-10 19:54:41513 DCHECK(extension);
[email protected]2a8f24e2010-11-03 21:37:05514 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41515
[email protected]583d45c12010-08-31 02:48:12516 if (!service->ExtensionBindingsAllowed(function->source_url()) ||
[email protected]246c05f2010-09-10 09:12:11517 !extension->HasApiPermission(function->name())) {
[email protected]553602e12011-04-05 17:01:18518 render_view_host_->Send(new ExtensionMsg_Response(
519 render_view_host_->routing_id(), function->request_id(), false,
520 std::string(), "Access to extension API denied."));
[email protected]583d45c12010-08-31 02:48:12521 return;
522 }
523
[email protected]d13950e2009-12-04 01:43:02524 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56525 if (quota->Assess(extension_id(), function, &params.arguments,
526 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26527 // See crbug.com/39178.
528 ExternalProtocolHandler::PermitLaunchUrl();
529
[email protected]d13950e2009-12-04 01:43:02530 function->Run();
531 } else {
[email protected]553602e12011-04-05 17:01:18532 render_view_host_->Send(new ExtensionMsg_Response(
533 render_view_host_->routing_id(), function->request_id(), false,
534 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError));
[email protected]d13950e2009-12-04 01:43:02535 }
[email protected]bfdffe2b2009-04-24 22:05:35536}
537
[email protected]c6619182009-05-12 14:59:32538void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
539 bool success) {
[email protected]553602e12011-04-05 17:01:18540 render_view_host_->Send(new ExtensionMsg_Response(
541 render_view_host_->routing_id(), function->request_id(), success,
542 function->GetResult(), function->GetError()));
[email protected]bfdffe2b2009-04-24 22:05:35543}
544
545void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14546 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45547 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35548 " : terminating renderer.";
549 if (RenderProcessHost::run_renderer_in_process()) {
550 // In single process mode it is better if we don't suicide but just crash.
551 CHECK(false);
552 } else {
553 NOTREACHED();
[email protected]c66027632010-12-22 21:26:30554 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]201b2732009-11-13 18:57:46555 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35556 ResultCodes::KILLED_BAD_MESSAGE, false);
557 }
558}
559
560Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58561 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35562}