blob: 4fb7af38474e1d47edde00c3b212a239a97e4232 [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]bfdffe2b2009-04-24 22:05:359#include "base/process_util.h"
[email protected]2bb51302011-02-18 22:39:2710#include "base/ref_counted.h"
[email protected]2f69b382011-02-19 00:34:2511#include "base/singleton.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]0ec92032010-03-24 19:59:4114#include "chrome/browser/browser_list.h"
[email protected]a95631cb2009-12-10 01:59:1115#include "chrome/browser/browser_window.h"
[email protected]912256b32009-09-18 09:47:3516#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5617#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]9dd97bc2010-01-14 01:40:0418#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3519#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]ec9ac0df2009-10-01 18:06:4720#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]446255952010-03-17 20:41:5821#include "chrome/browser/extensions/extension_clipboard_api.h"
[email protected]2e3b5202010-03-23 06:52:4122#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2923#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3524#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0125#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2726#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2527#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0828#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2829#include "chrome/browser/extensions/extension_management_api.h"
[email protected]e916901c2009-05-07 00:14:3130#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4331#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0932#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3333#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3434#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]598bbcc2011-02-24 10:03:2535#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]45776222009-07-15 20:21:5836#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3537#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4438#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0639#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]2f69b382011-02-19 00:34:2540#include "chrome/browser/extensions/extension_service.h"
[email protected]8b8e7c92010-08-19 18:05:5641#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3542#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1443#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2244#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]8f9d4eb2011-02-05 01:39:1045#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]c41fe662011-02-15 01:19:2646#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]63cda0c2010-09-01 04:41:2347#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0248#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]2f69b382011-02-19 00:34:2549#include "chrome/browser/external_protocol_handler.h"
[email protected]c66027632010-12-22 21:26:3050#include "chrome/browser/metrics/user_metrics.h"
[email protected]8ecad5e2010-12-02 21:18:3351#include "chrome/browser/profiles/profile.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]35506352009-08-07 18:58:1954#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0255#include "chrome/common/render_messages_params.h"
[email protected]bfdffe2b2009-04-24 22:05:3556#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4357#include "chrome/common/url_constants.h"
[email protected]5de634712011-03-02 00:20:1958#include "content/browser/renderer_host/render_process_host.h"
59#include "content/browser/renderer_host/render_view_host.h"
[email protected]f20d7332011-03-08 21:11:5360#include "content/common/notification_service.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]bfdffe2b2009-04-24 22:05:3567// FactoryRegistry -------------------------------------------------------------
68
69namespace {
70
[email protected]b83e4602009-05-15 22:58:3371// Template for defining ExtensionFunctionFactory.
72template<class T>
73ExtensionFunction* NewExtensionFunction() {
74 return new T();
75}
[email protected]bfdffe2b2009-04-24 22:05:3576
[email protected]b83e4602009-05-15 22:58:3377// Contains a list of all known extension functions and allows clients to
78// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3579class FactoryRegistry {
80 public:
[email protected]8e8bb6d2010-12-13 08:18:5581 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3382 FactoryRegistry() { ResetFunctions(); }
83
84 // Resets all functions to their default values.
85 void ResetFunctions();
86
87 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3588 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3389
90 // Allows overriding of specific functions (e.g. for testing). Functions
91 // must be previously registered. Returns true if successful.
92 bool OverrideFunction(const std::string& name,
93 ExtensionFunctionFactory factory);
94
95 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3596 ExtensionFunction* NewFunction(const std::string& name);
97
98 private:
[email protected]61424c062009-10-14 23:14:5999 template<class T>
100 void RegisterFunction() {
101 factories_[T::function_name()] = &NewExtensionFunction<T>;
102 }
103
[email protected]bfdffe2b2009-04-24 22:05:35104 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
105 FactoryMap factories_;
106};
107
[email protected]8e8bb6d2010-12-13 08:18:55108FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35109 return Singleton<FactoryRegistry>::get();
110}
111
[email protected]b83e4602009-05-15 22:58:33112void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35113 // Register all functions here.
114
[email protected]e515f5d2009-05-05 03:05:00115 // Windows
[email protected]61424c062009-10-14 23:14:59116 RegisterFunction<GetWindowFunction>();
117 RegisterFunction<GetCurrentWindowFunction>();
118 RegisterFunction<GetLastFocusedWindowFunction>();
119 RegisterFunction<GetAllWindowsFunction>();
120 RegisterFunction<CreateWindowFunction>();
121 RegisterFunction<UpdateWindowFunction>();
122 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33123
[email protected]e515f5d2009-05-05 03:05:00124 // Tabs
[email protected]61424c062009-10-14 23:14:59125 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41126 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59127 RegisterFunction<GetSelectedTabFunction>();
128 RegisterFunction<GetAllTabsInWindowFunction>();
129 RegisterFunction<CreateTabFunction>();
130 RegisterFunction<UpdateTabFunction>();
131 RegisterFunction<MoveTabFunction>();
132 RegisterFunction<RemoveTabFunction>();
133 RegisterFunction<DetectTabLanguageFunction>();
134 RegisterFunction<CaptureVisibleTabFunction>();
135 RegisterFunction<TabsExecuteScriptFunction>();
136 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35137
[email protected]f7f3a5f2009-05-01 22:02:34138 // Page Actions.
[email protected]61424c062009-10-14 23:14:59139 RegisterFunction<EnablePageActionFunction>();
140 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46141 RegisterFunction<PageActionShowFunction>();
142 RegisterFunction<PageActionHideFunction>();
143 RegisterFunction<PageActionSetIconFunction>();
144 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29145 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34146
[email protected]ec9ac0df2009-10-01 18:06:47147 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59148 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24149 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59150 RegisterFunction<BrowserActionSetBadgeTextFunction>();
151 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50152 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47153
[email protected]f7f3a5f2009-05-01 22:02:34154 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<GetBookmarksFunction>();
156 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55157 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59158 RegisterFunction<GetBookmarkTreeFunction>();
159 RegisterFunction<SearchBookmarksFunction>();
160 RegisterFunction<RemoveBookmarkFunction>();
161 RegisterFunction<RemoveTreeBookmarkFunction>();
162 RegisterFunction<CreateBookmarkFunction>();
163 RegisterFunction<MoveBookmarkFunction>();
164 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43165
[email protected]f34e79632010-03-17 02:34:08166 // Infobars.
167 RegisterFunction<ShowInfoBarFunction>();
168
[email protected]9dd97bc2010-01-14 01:40:04169 // BookmarkManager
170 RegisterFunction<CopyBookmarkManagerFunction>();
171 RegisterFunction<CutBookmarkManagerFunction>();
172 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57173 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56174 RegisterFunction<ImportBookmarksFunction>();
175 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18176 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04177 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16178 RegisterFunction<StartDragBookmarkManagerFunction>();
179 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31180 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04181
[email protected]de768a832009-10-30 05:25:01182 // History
183 RegisterFunction<AddUrlHistoryFunction>();
184 RegisterFunction<DeleteAllHistoryFunction>();
185 RegisterFunction<DeleteRangeHistoryFunction>();
186 RegisterFunction<DeleteUrlHistoryFunction>();
187 RegisterFunction<GetVisitsHistoryFunction>();
188 RegisterFunction<SearchHistoryFunction>();
189
[email protected]f5205412010-03-16 00:19:34190 // Idle
191 RegisterFunction<ExtensionIdleQueryStateFunction>();
192
[email protected]198bcfe2009-09-09 22:56:28193 // I18N.
[email protected]61424c062009-10-14 23:14:59194 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28195
[email protected]381162b2010-01-28 17:29:35196 // Processes.
[email protected]8a661f82010-10-19 21:47:11197 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35198
[email protected]438772df2010-02-26 18:08:43199 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03200 RegisterFunction<MetricsGetEnabledFunction>();
201 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34202 RegisterFunction<MetricsRecordUserActionFunction>();
203 RegisterFunction<MetricsRecordValueFunction>();
204 RegisterFunction<MetricsRecordPercentageFunction>();
205 RegisterFunction<MetricsRecordCountFunction>();
206 RegisterFunction<MetricsRecordSmallCountFunction>();
207 RegisterFunction<MetricsRecordMediumCountFunction>();
208 RegisterFunction<MetricsRecordTimeFunction>();
209 RegisterFunction<MetricsRecordMediumTimeFunction>();
210 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43211
[email protected]17d40f02010-07-01 01:18:06212 // RLZ.
213#if defined(OS_WIN)
214 RegisterFunction<RlzRecordProductEventFunction>();
215 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21216 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06217 RegisterFunction<RlzClearProductStateFunction>();
218#endif
219
[email protected]898bbd32010-05-18 18:52:29220 // Cookies.
221 RegisterFunction<GetCookieFunction>();
222 RegisterFunction<GetAllCookiesFunction>();
223 RegisterFunction<SetCookieFunction>();
224 RegisterFunction<RemoveCookieFunction>();
225 RegisterFunction<GetAllCookieStoresFunction>();
226
[email protected]25fd1b2e2009-08-17 20:57:14227 // Test.
[email protected]61424c062009-10-14 23:14:59228 RegisterFunction<ExtensionTestPassFunction>();
229 RegisterFunction<ExtensionTestFailFunction>();
230 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02231 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50232 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07233 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39234 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56235
236 // Accessibility.
237 RegisterFunction<GetFocusedControlFunction>();
238 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58239
[email protected]8abe0a32010-08-12 00:40:22240 // Text-to-speech.
241 RegisterFunction<ExtensionTtsSpeakFunction>();
242 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
243 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56244 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22245
[email protected]446255952010-03-17 20:41:58246 // Clipboard.
247 RegisterFunction<ExecuteCopyClipboardFunction>();
248 RegisterFunction<ExecuteCutClipboardFunction>();
249 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41250
251 // Context Menus.
252 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15253 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41254 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15255 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33256
257 // Omnibox.
258 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53259 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44260
261 // Proxies.
[email protected]ccfdfbdf2011-02-24 12:41:46262 RegisterFunction<SetProxySettingsFunction>();
[email protected]ccfdfbdf2011-02-24 12:41:46263 RegisterFunction<GetProxySettingsFunction>();
[email protected]8b8e7c92010-08-19 18:05:56264
265 // Sidebar.
266 RegisterFunction<CollapseSidebarFunction>();
267 RegisterFunction<ExpandSidebarFunction>();
268 RegisterFunction<GetStateSidebarFunction>();
269 RegisterFunction<HideSidebarFunction>();
270 RegisterFunction<NavigateSidebarFunction>();
271 RegisterFunction<SetBadgeTextSidebarFunction>();
272 RegisterFunction<SetIconSidebarFunction>();
273 RegisterFunction<SetTitleSidebarFunction>();
274 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45275
276#if defined(TOOLKIT_VIEWS)
277 // Input.
278 RegisterFunction<SendKeyboardEventInputFunction>();
279#endif
[email protected]cffd7892010-08-26 17:43:28280
281 // Management.
282 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23283 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56284 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28285 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28286 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23287
[email protected]a65882c2010-11-12 15:15:09288 // Extension module.
289 RegisterFunction<SetUpdateUrlDataFunction>();
290
[email protected]63cda0c2010-09-01 04:41:23291 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24292 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23293 RegisterFunction<GetStoreLoginFunction>();
294 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24295 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30296 RegisterFunction<BeginInstallFunction>();
297 RegisterFunction<CompleteInstallFunction>();
[email protected]c41fe662011-02-15 01:19:26298
299 // WebRequest.
300 RegisterFunction<WebRequestAddEventListener>();
[email protected]05cc4e72011-03-08 21:29:48301 RegisterFunction<WebRequestEventHandled>();
[email protected]598bbcc2011-02-24 10:03:25302
303 // Preferences.
304 RegisterFunction<GetPreferenceFunction>();
305 RegisterFunction<SetPreferenceFunction>();
[email protected]c433bcb2011-02-24 13:10:27306 RegisterFunction<ClearPreferenceFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35307}
308
[email protected]b83e4602009-05-15 22:58:33309void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
310 for (FactoryMap::iterator iter = factories_.begin();
311 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35312 names->push_back(iter->first);
313 }
314}
315
[email protected]b83e4602009-05-15 22:58:33316bool FactoryRegistry::OverrideFunction(const std::string& name,
317 ExtensionFunctionFactory factory) {
318 FactoryMap::iterator iter = factories_.find(name);
319 if (iter == factories_.end()) {
320 return false;
321 } else {
322 iter->second = factory;
323 return true;
324 }
325}
326
[email protected]bfdffe2b2009-04-24 22:05:35327ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
328 FactoryMap::iterator iter = factories_.find(name);
329 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33330 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56331 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33332 return function;
[email protected]bfdffe2b2009-04-24 22:05:35333}
334
[email protected]b83e4602009-05-15 22:58:33335}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35336
337// ExtensionFunctionDispatcher -------------------------------------------------
338
339void ExtensionFunctionDispatcher::GetAllFunctionNames(
340 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55341 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35342}
343
[email protected]b83e4602009-05-15 22:58:33344bool ExtensionFunctionDispatcher::OverrideFunction(
345 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55346 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33347}
348
349void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55350 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33351}
352
[email protected]a91afcb2010-03-25 21:15:02353ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
354 RenderViewHost* render_view_host,
355 Delegate* delegate,
356 const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:00357 ExtensionService* service =
358 render_view_host->process()->profile()->GetExtensionService();
[email protected]a91afcb2010-03-25 21:15:02359 DCHECK(service);
360
[email protected]583d45c12010-08-31 02:48:12361 if (!service->ExtensionBindingsAllowed(url))
362 return NULL;
363
[email protected]9adb9692010-10-29 23:14:02364 const Extension* extension = service->GetExtensionByURL(url);
[email protected]583d45c12010-08-31 02:48:12365 if (!extension)
366 extension = service->GetExtensionByWebExtent(url);
367
[email protected]a91afcb2010-03-25 21:15:02368 if (extension)
369 return new ExtensionFunctionDispatcher(render_view_host, delegate,
370 extension, url);
371 else
372 return NULL;
373}
374
[email protected]bfdffe2b2009-04-24 22:05:35375ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
376 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12377 Delegate* delegate,
[email protected]9adb9692010-10-29 23:14:02378 const Extension* extension,
[email protected]811bfe32009-07-01 08:46:25379 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30380 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58381 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12382 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25383 url_(url),
[email protected]583d45c12010-08-31 02:48:12384 extension_id_(extension->id()),
[email protected]32dda362009-06-05 19:07:01385 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43386 // TODO(erikkay) should we do something for these errors in Release?
[email protected]35506352009-08-07 18:58:19387 DCHECK(extension);
[email protected]583d45c12010-08-31 02:48:12388 DCHECK(url.SchemeIs(chrome::kExtensionScheme) ||
389 extension->location() == Extension::COMPONENT);
[email protected]9c45b7182009-08-04 16:44:43390
[email protected]45776222009-07-15 20:21:58391 // Notify the ExtensionProcessManager that the view was created.
392 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
393 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45394 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19395
[email protected]584b8e3f2010-04-10 00:23:37396 // If the extension has permission to load chrome://favicon/ resources we need
[email protected]65992592011-02-24 02:25:15397 // to make sure that the FavIconSource is registered with the
[email protected]584b8e3f2010-04-10 00:23:37398 // ChromeURLDataManager.
399 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
[email protected]65992592011-02-24 02:25:15400 FavIconSource* favicon_source = new FavIconSource(profile_);
[email protected]248ce192011-02-10 15:26:34401 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
[email protected]584b8e3f2010-04-10 00:23:37402 }
403
[email protected]35506352009-08-07 18:58:19404 // Update the extension permissions. Doing this each time we create an EFD
405 // ensures that new processes are informed of permissions for newly installed
406 // extensions.
[email protected]cccf90932009-08-23 17:56:25407 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19408 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25409 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
410 extension->url(), extension->host_permissions()));
[email protected]68f07912010-03-05 18:33:58411
412 NotificationService::current()->Notify(
413 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
414 Source<Profile>(profile_),
415 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35416}
417
[email protected]32dda362009-06-05 19:07:01418ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
419 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58420
421 NotificationService::current()->Notify(
422 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
423 Source<Profile>(profile_),
424 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01425}
426
[email protected]0ec92032010-03-24 19:59:41427Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
428 bool include_incognito) {
429 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12430
[email protected]bc535ee52010-08-31 18:40:32431 // If the delegate has an associated browser, that is always the right answer.
432 if (browser)
433 return browser;
[email protected]9c45b7182009-08-04 16:44:43434
[email protected]bc535ee52010-08-31 18:40:32435 // Otherwise, try to default to a reasonable browser. If |include_incognito|
436 // is true, we will also search browsers in the incognito version of this
437 // profile. Note that the profile may already be incognito, in which case
438 // we will search the incognito version only, regardless of the value of
439 // |include_incognito|.
[email protected]0ec92032010-03-24 19:59:41440 Profile* profile = render_view_host()->process()->profile();
[email protected]aa916d572010-08-13 02:59:31441 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31442 include_incognito);
[email protected]0ec92032010-03-24 19:59:41443
[email protected]0ec92032010-03-24 19:59:41444 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42445 // a background_page onload chrome.tabs api call can make it into here
446 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41447 // A similar situation may arise during shutdown.
448 // TODO(rafaelw): Delay creation of background_page until the browser
449 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
450 return browser;
[email protected]b27257562009-11-16 23:28:26451}
452
[email protected]8b8e7c92010-08-19 18:05:56453void ExtensionFunctionDispatcher::HandleRequest(
454 const ViewHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01455 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55456 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01457 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43458 function->set_profile(profile_);
459 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56460 function->SetArgs(&params.arguments);
461 function->set_source_url(params.source_url);
462 function->set_request_id(params.request_id);
463 function->set_has_callback(params.has_callback);
464 function->set_user_gesture(params.user_gesture);
[email protected]eaa7dd182010-12-14 11:09:00465 ExtensionService* service = profile()->GetExtensionService();
[email protected]d13950e2009-12-04 01:43:02466 DCHECK(service);
[email protected]9adb9692010-10-29 23:14:02467 const Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]cb0ce1e022010-03-10 19:54:41468 DCHECK(extension);
[email protected]2a8f24e2010-11-03 21:37:05469 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41470
[email protected]583d45c12010-08-31 02:48:12471 if (!service->ExtensionBindingsAllowed(function->source_url()) ||
[email protected]246c05f2010-09-10 09:12:11472 !extension->HasApiPermission(function->name())) {
[email protected]583d45c12010-08-31 02:48:12473 render_view_host_->BlockExtensionRequest(function->request_id());
474 return;
475 }
476
[email protected]d13950e2009-12-04 01:43:02477 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56478 if (quota->Assess(extension_id(), function, &params.arguments,
479 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26480 // See crbug.com/39178.
481 ExternalProtocolHandler::PermitLaunchUrl();
482
[email protected]d13950e2009-12-04 01:43:02483 function->Run();
484 } else {
485 render_view_host_->SendExtensionResponse(function->request_id(), false,
486 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
487 }
[email protected]bfdffe2b2009-04-24 22:05:35488}
489
[email protected]c6619182009-05-12 14:59:32490void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
491 bool success) {
[email protected]c6619182009-05-12 14:59:32492 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33493 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35494}
495
496void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14497 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45498 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35499 " : terminating renderer.";
500 if (RenderProcessHost::run_renderer_in_process()) {
501 // In single process mode it is better if we don't suicide but just crash.
502 CHECK(false);
503 } else {
504 NOTREACHED();
[email protected]c66027632010-12-22 21:26:30505 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]201b2732009-11-13 18:57:46506 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35507 ResultCodes::KILLED_BAD_MESSAGE, false);
508 }
509}
510
511Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58512 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35513}