blob: 4f693618940056ed7b037dd79ad9b6dffe7c517a [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]e0813a392011-02-01 22:57:2516#include "chrome/browser/dom_ui/web_ui_favicon_source.h"
[email protected]912256b32009-09-18 09:47:3517#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5618#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]9dd97bc2010-01-14 01:40:0419#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3520#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]ec9ac0df2009-10-01 18:06:4721#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]446255952010-03-17 20:41:5822#include "chrome/browser/extensions/extension_clipboard_api.h"
[email protected]2e3b5202010-03-23 06:52:4123#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2924#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3525#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0126#include "chrome/browser/extensions/extension_history_api.h"
[email protected]2bb51302011-02-18 22:39:2727#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]2f69b382011-02-19 00:34:2528#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]f34e79632010-03-17 02:34:0829#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]cffd7892010-08-26 17:43:2830#include "chrome/browser/extensions/extension_management_api.h"
[email protected]e916901c2009-05-07 00:14:3131#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4332#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0933#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3334#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3435#include "chrome/browser/extensions/extension_page_actions_module.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]bfdffe2b2009-04-24 22:05:3552#include "chrome/browser/renderer_host/render_process_host.h"
53#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]2f69b382011-02-19 00:34:2554#include "chrome/browser/webui/chrome_url_data_manager.h"
[email protected]9006b1152010-12-15 21:42:2155#include "chrome/common/notification_service.h"
[email protected]35506352009-08-07 18:58:1956#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0257#include "chrome/common/render_messages_params.h"
[email protected]bfdffe2b2009-04-24 22:05:3558#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4359#include "chrome/common/url_constants.h"
[email protected]939856a2010-08-24 20:29:0260#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3561
[email protected]8f9d4eb2011-02-05 01:39:1062#if defined(TOOLKIT_VIEWS)
63#include "chrome/browser/extensions/extension_input_api.h"
64#endif
[email protected]d6833852010-08-20 18:00:4565
[email protected]bfdffe2b2009-04-24 22:05:3566// FactoryRegistry -------------------------------------------------------------
67
68namespace {
69
[email protected]b83e4602009-05-15 22:58:3370// Template for defining ExtensionFunctionFactory.
71template<class T>
72ExtensionFunction* NewExtensionFunction() {
73 return new T();
74}
[email protected]bfdffe2b2009-04-24 22:05:3575
[email protected]b83e4602009-05-15 22:58:3376// Contains a list of all known extension functions and allows clients to
77// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3578class FactoryRegistry {
79 public:
[email protected]8e8bb6d2010-12-13 08:18:5580 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3381 FactoryRegistry() { ResetFunctions(); }
82
83 // Resets all functions to their default values.
84 void ResetFunctions();
85
86 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3587 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3388
89 // Allows overriding of specific functions (e.g. for testing). Functions
90 // must be previously registered. Returns true if successful.
91 bool OverrideFunction(const std::string& name,
92 ExtensionFunctionFactory factory);
93
94 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3595 ExtensionFunction* NewFunction(const std::string& name);
96
97 private:
[email protected]61424c062009-10-14 23:14:5998 template<class T>
99 void RegisterFunction() {
100 factories_[T::function_name()] = &NewExtensionFunction<T>;
101 }
102
[email protected]bfdffe2b2009-04-24 22:05:35103 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
104 FactoryMap factories_;
105};
106
[email protected]8e8bb6d2010-12-13 08:18:55107FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35108 return Singleton<FactoryRegistry>::get();
109}
110
[email protected]b83e4602009-05-15 22:58:33111void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35112 // Register all functions here.
113
[email protected]e515f5d2009-05-05 03:05:00114 // Windows
[email protected]61424c062009-10-14 23:14:59115 RegisterFunction<GetWindowFunction>();
116 RegisterFunction<GetCurrentWindowFunction>();
117 RegisterFunction<GetLastFocusedWindowFunction>();
118 RegisterFunction<GetAllWindowsFunction>();
119 RegisterFunction<CreateWindowFunction>();
120 RegisterFunction<UpdateWindowFunction>();
121 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33122
[email protected]e515f5d2009-05-05 03:05:00123 // Tabs
[email protected]61424c062009-10-14 23:14:59124 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41125 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59126 RegisterFunction<GetSelectedTabFunction>();
127 RegisterFunction<GetAllTabsInWindowFunction>();
128 RegisterFunction<CreateTabFunction>();
129 RegisterFunction<UpdateTabFunction>();
130 RegisterFunction<MoveTabFunction>();
131 RegisterFunction<RemoveTabFunction>();
132 RegisterFunction<DetectTabLanguageFunction>();
133 RegisterFunction<CaptureVisibleTabFunction>();
134 RegisterFunction<TabsExecuteScriptFunction>();
135 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35136
[email protected]f7f3a5f2009-05-01 22:02:34137 // Page Actions.
[email protected]61424c062009-10-14 23:14:59138 RegisterFunction<EnablePageActionFunction>();
139 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46140 RegisterFunction<PageActionShowFunction>();
141 RegisterFunction<PageActionHideFunction>();
142 RegisterFunction<PageActionSetIconFunction>();
143 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29144 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34145
[email protected]ec9ac0df2009-10-01 18:06:47146 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59147 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24148 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59149 RegisterFunction<BrowserActionSetBadgeTextFunction>();
150 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50151 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47152
[email protected]f7f3a5f2009-05-01 22:02:34153 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59154 RegisterFunction<GetBookmarksFunction>();
155 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55156 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59157 RegisterFunction<GetBookmarkTreeFunction>();
158 RegisterFunction<SearchBookmarksFunction>();
159 RegisterFunction<RemoveBookmarkFunction>();
160 RegisterFunction<RemoveTreeBookmarkFunction>();
161 RegisterFunction<CreateBookmarkFunction>();
162 RegisterFunction<MoveBookmarkFunction>();
163 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43164
[email protected]f34e79632010-03-17 02:34:08165 // Infobars.
166 RegisterFunction<ShowInfoBarFunction>();
167
[email protected]9dd97bc2010-01-14 01:40:04168 // BookmarkManager
169 RegisterFunction<CopyBookmarkManagerFunction>();
170 RegisterFunction<CutBookmarkManagerFunction>();
171 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57172 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56173 RegisterFunction<ImportBookmarksFunction>();
174 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18175 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04176 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16177 RegisterFunction<StartDragBookmarkManagerFunction>();
178 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31179 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04180
[email protected]de768a832009-10-30 05:25:01181 // History
182 RegisterFunction<AddUrlHistoryFunction>();
183 RegisterFunction<DeleteAllHistoryFunction>();
184 RegisterFunction<DeleteRangeHistoryFunction>();
185 RegisterFunction<DeleteUrlHistoryFunction>();
186 RegisterFunction<GetVisitsHistoryFunction>();
187 RegisterFunction<SearchHistoryFunction>();
188
[email protected]f5205412010-03-16 00:19:34189 // Idle
190 RegisterFunction<ExtensionIdleQueryStateFunction>();
191
[email protected]198bcfe2009-09-09 22:56:28192 // I18N.
[email protected]61424c062009-10-14 23:14:59193 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28194
[email protected]381162b2010-01-28 17:29:35195 // Processes.
[email protected]8a661f82010-10-19 21:47:11196 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35197
[email protected]438772df2010-02-26 18:08:43198 // Metrics.
[email protected]e93d2d02011-01-12 21:41:03199 RegisterFunction<MetricsGetEnabledFunction>();
200 RegisterFunction<MetricsSetEnabledFunction>();
[email protected]cf25e4d2010-03-12 21:19:34201 RegisterFunction<MetricsRecordUserActionFunction>();
202 RegisterFunction<MetricsRecordValueFunction>();
203 RegisterFunction<MetricsRecordPercentageFunction>();
204 RegisterFunction<MetricsRecordCountFunction>();
205 RegisterFunction<MetricsRecordSmallCountFunction>();
206 RegisterFunction<MetricsRecordMediumCountFunction>();
207 RegisterFunction<MetricsRecordTimeFunction>();
208 RegisterFunction<MetricsRecordMediumTimeFunction>();
209 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43210
[email protected]17d40f02010-07-01 01:18:06211 // RLZ.
212#if defined(OS_WIN)
213 RegisterFunction<RlzRecordProductEventFunction>();
214 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21215 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06216 RegisterFunction<RlzClearProductStateFunction>();
217#endif
218
[email protected]898bbd32010-05-18 18:52:29219 // Cookies.
220 RegisterFunction<GetCookieFunction>();
221 RegisterFunction<GetAllCookiesFunction>();
222 RegisterFunction<SetCookieFunction>();
223 RegisterFunction<RemoveCookieFunction>();
224 RegisterFunction<GetAllCookieStoresFunction>();
225
[email protected]25fd1b2e2009-08-17 20:57:14226 // Test.
[email protected]61424c062009-10-14 23:14:59227 RegisterFunction<ExtensionTestPassFunction>();
228 RegisterFunction<ExtensionTestFailFunction>();
229 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02230 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50231 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07232 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39233 RegisterFunction<ExtensionTestGetConfigFunction>();
[email protected]5cbe1e22010-01-30 01:18:56234
235 // Accessibility.
236 RegisterFunction<GetFocusedControlFunction>();
237 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58238
[email protected]8abe0a32010-08-12 00:40:22239 // Text-to-speech.
240 RegisterFunction<ExtensionTtsSpeakFunction>();
241 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
242 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]a4a38c12010-12-23 16:43:56243 RegisterFunction<ExtensionTtsSpeakCompletedFunction>();
[email protected]8abe0a32010-08-12 00:40:22244
[email protected]446255952010-03-17 20:41:58245 // Clipboard.
246 RegisterFunction<ExecuteCopyClipboardFunction>();
247 RegisterFunction<ExecuteCutClipboardFunction>();
248 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41249
250 // Context Menus.
251 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15252 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41253 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15254 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33255
256 // Omnibox.
257 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53258 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44259
260 // Proxies.
261 RegisterFunction<UseCustomProxySettingsFunction>();
[email protected]77641702011-02-04 12:27:23262 RegisterFunction<RemoveCustomProxySettingsFunction>();
[email protected]18f4ea52011-02-14 12:00:46263 RegisterFunction<GetCurrentProxySettingsFunction>();
[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]bfdffe2b2009-04-24 22:05:35301}
302
[email protected]b83e4602009-05-15 22:58:33303void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
304 for (FactoryMap::iterator iter = factories_.begin();
305 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35306 names->push_back(iter->first);
307 }
308}
309
[email protected]b83e4602009-05-15 22:58:33310bool FactoryRegistry::OverrideFunction(const std::string& name,
311 ExtensionFunctionFactory factory) {
312 FactoryMap::iterator iter = factories_.find(name);
313 if (iter == factories_.end()) {
314 return false;
315 } else {
316 iter->second = factory;
317 return true;
318 }
319}
320
[email protected]bfdffe2b2009-04-24 22:05:35321ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
322 FactoryMap::iterator iter = factories_.find(name);
323 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33324 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56325 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33326 return function;
[email protected]bfdffe2b2009-04-24 22:05:35327}
328
[email protected]b83e4602009-05-15 22:58:33329}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35330
331// ExtensionFunctionDispatcher -------------------------------------------------
332
333void ExtensionFunctionDispatcher::GetAllFunctionNames(
334 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55335 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35336}
337
[email protected]b83e4602009-05-15 22:58:33338bool ExtensionFunctionDispatcher::OverrideFunction(
339 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55340 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33341}
342
343void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55344 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33345}
346
[email protected]a91afcb2010-03-25 21:15:02347ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
348 RenderViewHost* render_view_host,
349 Delegate* delegate,
350 const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:00351 ExtensionService* service =
352 render_view_host->process()->profile()->GetExtensionService();
[email protected]a91afcb2010-03-25 21:15:02353 DCHECK(service);
354
[email protected]583d45c12010-08-31 02:48:12355 if (!service->ExtensionBindingsAllowed(url))
356 return NULL;
357
[email protected]9adb9692010-10-29 23:14:02358 const Extension* extension = service->GetExtensionByURL(url);
[email protected]583d45c12010-08-31 02:48:12359 if (!extension)
360 extension = service->GetExtensionByWebExtent(url);
361
[email protected]a91afcb2010-03-25 21:15:02362 if (extension)
363 return new ExtensionFunctionDispatcher(render_view_host, delegate,
364 extension, url);
365 else
366 return NULL;
367}
368
[email protected]bfdffe2b2009-04-24 22:05:35369ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
370 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12371 Delegate* delegate,
[email protected]9adb9692010-10-29 23:14:02372 const Extension* extension,
[email protected]811bfe32009-07-01 08:46:25373 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30374 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58375 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12376 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25377 url_(url),
[email protected]583d45c12010-08-31 02:48:12378 extension_id_(extension->id()),
[email protected]32dda362009-06-05 19:07:01379 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43380 // TODO(erikkay) should we do something for these errors in Release?
[email protected]35506352009-08-07 18:58:19381 DCHECK(extension);
[email protected]583d45c12010-08-31 02:48:12382 DCHECK(url.SchemeIs(chrome::kExtensionScheme) ||
383 extension->location() == Extension::COMPONENT);
[email protected]9c45b7182009-08-04 16:44:43384
[email protected]45776222009-07-15 20:21:58385 // Notify the ExtensionProcessManager that the view was created.
386 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
387 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45388 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19389
[email protected]584b8e3f2010-04-10 00:23:37390 // If the extension has permission to load chrome://favicon/ resources we need
[email protected]e0813a392011-02-01 22:57:25391 // to make sure that the WebUIFavIconSource is registered with the
[email protected]584b8e3f2010-04-10 00:23:37392 // ChromeURLDataManager.
393 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
[email protected]e0813a392011-02-01 22:57:25394 WebUIFavIconSource* favicon_source = new WebUIFavIconSource(profile_);
[email protected]248ce192011-02-10 15:26:34395 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
[email protected]584b8e3f2010-04-10 00:23:37396 }
397
[email protected]35506352009-08-07 18:58:19398 // Update the extension permissions. Doing this each time we create an EFD
399 // ensures that new processes are informed of permissions for newly installed
400 // extensions.
[email protected]cccf90932009-08-23 17:56:25401 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19402 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25403 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
404 extension->url(), extension->host_permissions()));
[email protected]68f07912010-03-05 18:33:58405
406 NotificationService::current()->Notify(
407 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
408 Source<Profile>(profile_),
409 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35410}
411
[email protected]32dda362009-06-05 19:07:01412ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
413 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58414
415 NotificationService::current()->Notify(
416 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
417 Source<Profile>(profile_),
418 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01419}
420
[email protected]0ec92032010-03-24 19:59:41421Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
422 bool include_incognito) {
423 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12424
[email protected]bc535ee52010-08-31 18:40:32425 // If the delegate has an associated browser, that is always the right answer.
426 if (browser)
427 return browser;
[email protected]9c45b7182009-08-04 16:44:43428
[email protected]bc535ee52010-08-31 18:40:32429 // Otherwise, try to default to a reasonable browser. If |include_incognito|
430 // is true, we will also search browsers in the incognito version of this
431 // profile. Note that the profile may already be incognito, in which case
432 // we will search the incognito version only, regardless of the value of
433 // |include_incognito|.
[email protected]0ec92032010-03-24 19:59:41434 Profile* profile = render_view_host()->process()->profile();
[email protected]aa916d572010-08-13 02:59:31435 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31436 include_incognito);
[email protected]0ec92032010-03-24 19:59:41437
[email protected]0ec92032010-03-24 19:59:41438 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42439 // a background_page onload chrome.tabs api call can make it into here
440 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41441 // A similar situation may arise during shutdown.
442 // TODO(rafaelw): Delay creation of background_page until the browser
443 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
444 return browser;
[email protected]b27257562009-11-16 23:28:26445}
446
[email protected]8b8e7c92010-08-19 18:05:56447void ExtensionFunctionDispatcher::HandleRequest(
448 const ViewHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01449 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55450 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01451 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43452 function->set_profile(profile_);
453 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56454 function->SetArgs(&params.arguments);
455 function->set_source_url(params.source_url);
456 function->set_request_id(params.request_id);
457 function->set_has_callback(params.has_callback);
458 function->set_user_gesture(params.user_gesture);
[email protected]eaa7dd182010-12-14 11:09:00459 ExtensionService* service = profile()->GetExtensionService();
[email protected]d13950e2009-12-04 01:43:02460 DCHECK(service);
[email protected]9adb9692010-10-29 23:14:02461 const Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]cb0ce1e022010-03-10 19:54:41462 DCHECK(extension);
[email protected]2a8f24e2010-11-03 21:37:05463 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41464
[email protected]583d45c12010-08-31 02:48:12465 if (!service->ExtensionBindingsAllowed(function->source_url()) ||
[email protected]246c05f2010-09-10 09:12:11466 !extension->HasApiPermission(function->name())) {
[email protected]583d45c12010-08-31 02:48:12467 render_view_host_->BlockExtensionRequest(function->request_id());
468 return;
469 }
470
[email protected]d13950e2009-12-04 01:43:02471 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56472 if (quota->Assess(extension_id(), function, &params.arguments,
473 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26474 // See crbug.com/39178.
475 ExternalProtocolHandler::PermitLaunchUrl();
476
[email protected]d13950e2009-12-04 01:43:02477 function->Run();
478 } else {
479 render_view_host_->SendExtensionResponse(function->request_id(), false,
480 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
481 }
[email protected]bfdffe2b2009-04-24 22:05:35482}
483
[email protected]c6619182009-05-12 14:59:32484void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
485 bool success) {
[email protected]c6619182009-05-12 14:59:32486 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33487 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35488}
489
490void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14491 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45492 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35493 " : terminating renderer.";
494 if (RenderProcessHost::run_renderer_in_process()) {
495 // In single process mode it is better if we don't suicide but just crash.
496 CHECK(false);
497 } else {
498 NOTREACHED();
[email protected]c66027632010-12-22 21:26:30499 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_EFD"));
[email protected]201b2732009-11-13 18:57:46500 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35501 ResultCodes::KILLED_BAD_MESSAGE, false);
502 }
503}
504
505Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58506 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35507}