blob: cd6714e3da2f79bb4474dfa3e908e1eb1dc8c346 [file] [log] [blame]
[email protected]f34e79632010-03-17 02:34:081// Copyright (c) 2010 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"
10#include "base/singleton.h"
[email protected]584b8e3f2010-04-10 00:23:3711#include "base/ref_counted.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]584b8e3f2010-04-10 00:23:3716#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
17#include "chrome/browser/dom_ui/dom_ui_favicon_source.h"
[email protected]d070ec62010-07-27 21:28:2618#include "chrome/browser/external_protocol_handler.h"
[email protected]912256b32009-09-18 09:47:3519#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5620#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]9dd97bc2010-01-14 01:40:0421#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3522#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]ec9ac0df2009-10-01 18:06:4723#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]446255952010-03-17 20:41:5824#include "chrome/browser/extensions/extension_clipboard_api.h"
[email protected]2e3b5202010-03-23 06:52:4125#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]898bbd32010-05-18 18:52:2926#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]b27257562009-11-16 23:28:2627#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]bfdffe2b2009-04-24 22:05:3528#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0129#include "chrome/browser/extensions/extension_history_api.h"
[email protected]f5205412010-03-16 00:19:3430#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]198bcfe2009-09-09 22:56:2831#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]f34e79632010-03-17 02:34:0832#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]d6833852010-08-20 18:00:4533#if defined(TOOLKIT_VIEWS)
34#include "chrome/browser/extensions/extension_input_api.h"
35#endif
[email protected]cffd7892010-08-26 17:43:2836#include "chrome/browser/extensions/extension_management_api.h"
[email protected]e916901c2009-05-07 00:14:3137#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4338#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]a65882c2010-11-12 15:15:0939#include "chrome/browser/extensions/extension_module.h"
[email protected]56ad3792010-05-28 17:45:3340#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3441#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]1c1c77a52009-11-03 00:37:3142#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5843#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3544#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4445#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0646#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]8b8e7c92010-08-19 18:05:5647#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3548#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1449#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2250#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]63cda0c2010-09-01 04:41:2351#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0252#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]eaa7dd182010-12-14 11:09:0053#include "chrome/browser/extensions/extension_service.h"
[email protected]8ecad5e2010-12-02 21:18:3354#include "chrome/browser/profiles/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3555#include "chrome/browser/renderer_host/render_process_host.h"
56#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]9006b1152010-12-15 21:42:2157#include "chrome/common/notification_service.h"
[email protected]35506352009-08-07 18:58:1958#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0259#include "chrome/common/render_messages_params.h"
[email protected]bfdffe2b2009-04-24 22:05:3560#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4361#include "chrome/common/url_constants.h"
[email protected]939856a2010-08-24 20:29:0262#include "third_party/skia/include/core/SkBitmap.h"
[email protected]bfdffe2b2009-04-24 22:05:3563
[email protected]d6833852010-08-20 18:00:4564
[email protected]bfdffe2b2009-04-24 22:05:3565// FactoryRegistry -------------------------------------------------------------
66
67namespace {
68
[email protected]b83e4602009-05-15 22:58:3369// Template for defining ExtensionFunctionFactory.
70template<class T>
71ExtensionFunction* NewExtensionFunction() {
72 return new T();
73}
[email protected]bfdffe2b2009-04-24 22:05:3574
[email protected]b83e4602009-05-15 22:58:3375// Contains a list of all known extension functions and allows clients to
76// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3577class FactoryRegistry {
78 public:
[email protected]8e8bb6d2010-12-13 08:18:5579 static FactoryRegistry* GetInstance();
[email protected]b83e4602009-05-15 22:58:3380 FactoryRegistry() { ResetFunctions(); }
81
82 // Resets all functions to their default values.
83 void ResetFunctions();
84
85 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3586 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3387
88 // Allows overriding of specific functions (e.g. for testing). Functions
89 // must be previously registered. Returns true if successful.
90 bool OverrideFunction(const std::string& name,
91 ExtensionFunctionFactory factory);
92
93 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3594 ExtensionFunction* NewFunction(const std::string& name);
95
96 private:
[email protected]61424c062009-10-14 23:14:5997 template<class T>
98 void RegisterFunction() {
99 factories_[T::function_name()] = &NewExtensionFunction<T>;
100 }
101
[email protected]bfdffe2b2009-04-24 22:05:35102 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
103 FactoryMap factories_;
104};
105
[email protected]8e8bb6d2010-12-13 08:18:55106FactoryRegistry* FactoryRegistry::GetInstance() {
[email protected]bfdffe2b2009-04-24 22:05:35107 return Singleton<FactoryRegistry>::get();
108}
109
[email protected]b83e4602009-05-15 22:58:33110void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35111 // Register all functions here.
112
[email protected]e515f5d2009-05-05 03:05:00113 // Windows
[email protected]61424c062009-10-14 23:14:59114 RegisterFunction<GetWindowFunction>();
115 RegisterFunction<GetCurrentWindowFunction>();
116 RegisterFunction<GetLastFocusedWindowFunction>();
117 RegisterFunction<GetAllWindowsFunction>();
118 RegisterFunction<CreateWindowFunction>();
119 RegisterFunction<UpdateWindowFunction>();
120 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33121
[email protected]e515f5d2009-05-05 03:05:00122 // Tabs
[email protected]61424c062009-10-14 23:14:59123 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41124 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59125 RegisterFunction<GetSelectedTabFunction>();
126 RegisterFunction<GetAllTabsInWindowFunction>();
127 RegisterFunction<CreateTabFunction>();
128 RegisterFunction<UpdateTabFunction>();
129 RegisterFunction<MoveTabFunction>();
130 RegisterFunction<RemoveTabFunction>();
131 RegisterFunction<DetectTabLanguageFunction>();
132 RegisterFunction<CaptureVisibleTabFunction>();
133 RegisterFunction<TabsExecuteScriptFunction>();
134 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35135
[email protected]f7f3a5f2009-05-01 22:02:34136 // Page Actions.
[email protected]61424c062009-10-14 23:14:59137 RegisterFunction<EnablePageActionFunction>();
138 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46139 RegisterFunction<PageActionShowFunction>();
140 RegisterFunction<PageActionHideFunction>();
141 RegisterFunction<PageActionSetIconFunction>();
142 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29143 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34144
[email protected]ec9ac0df2009-10-01 18:06:47145 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59146 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24147 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59148 RegisterFunction<BrowserActionSetBadgeTextFunction>();
149 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50150 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47151
[email protected]f7f3a5f2009-05-01 22:02:34152 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59153 RegisterFunction<GetBookmarksFunction>();
154 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55155 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59156 RegisterFunction<GetBookmarkTreeFunction>();
157 RegisterFunction<SearchBookmarksFunction>();
158 RegisterFunction<RemoveBookmarkFunction>();
159 RegisterFunction<RemoveTreeBookmarkFunction>();
160 RegisterFunction<CreateBookmarkFunction>();
161 RegisterFunction<MoveBookmarkFunction>();
162 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43163
[email protected]f34e79632010-03-17 02:34:08164 // Infobars.
165 RegisterFunction<ShowInfoBarFunction>();
166
[email protected]9dd97bc2010-01-14 01:40:04167 // BookmarkManager
168 RegisterFunction<CopyBookmarkManagerFunction>();
169 RegisterFunction<CutBookmarkManagerFunction>();
170 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57171 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56172 RegisterFunction<ImportBookmarksFunction>();
173 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18174 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04175 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16176 RegisterFunction<StartDragBookmarkManagerFunction>();
177 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31178 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04179
[email protected]de768a832009-10-30 05:25:01180 // History
181 RegisterFunction<AddUrlHistoryFunction>();
182 RegisterFunction<DeleteAllHistoryFunction>();
183 RegisterFunction<DeleteRangeHistoryFunction>();
184 RegisterFunction<DeleteUrlHistoryFunction>();
185 RegisterFunction<GetVisitsHistoryFunction>();
186 RegisterFunction<SearchHistoryFunction>();
187
[email protected]f5205412010-03-16 00:19:34188 // Idle
189 RegisterFunction<ExtensionIdleQueryStateFunction>();
190
[email protected]198bcfe2009-09-09 22:56:28191 // I18N.
[email protected]61424c062009-10-14 23:14:59192 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28193
[email protected]1c1c77a52009-11-03 00:37:31194 // Popup API.
195 RegisterFunction<PopupShowFunction>();
196
[email protected]381162b2010-01-28 17:29:35197 // Processes.
[email protected]8a661f82010-10-19 21:47:11198 RegisterFunction<GetProcessIdForTabFunction>();
[email protected]381162b2010-01-28 17:29:35199
[email protected]438772df2010-02-26 18:08:43200 // Metrics.
[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]8abe0a32010-08-12 00:40:22243
[email protected]446255952010-03-17 20:41:58244 // Clipboard.
245 RegisterFunction<ExecuteCopyClipboardFunction>();
246 RegisterFunction<ExecuteCutClipboardFunction>();
247 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41248
249 // Context Menus.
250 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15251 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41252 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15253 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33254
255 // Omnibox.
256 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]1bead0712010-11-27 17:41:53257 RegisterFunction<OmniboxSetDefaultSuggestionFunction>();
[email protected]a9c23a52010-08-04 09:13:44258
259 // Proxies.
260 RegisterFunction<UseCustomProxySettingsFunction>();
[email protected]8b8e7c92010-08-19 18:05:56261
262 // Sidebar.
263 RegisterFunction<CollapseSidebarFunction>();
264 RegisterFunction<ExpandSidebarFunction>();
265 RegisterFunction<GetStateSidebarFunction>();
266 RegisterFunction<HideSidebarFunction>();
267 RegisterFunction<NavigateSidebarFunction>();
268 RegisterFunction<SetBadgeTextSidebarFunction>();
269 RegisterFunction<SetIconSidebarFunction>();
270 RegisterFunction<SetTitleSidebarFunction>();
271 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45272
273#if defined(TOOLKIT_VIEWS)
274 // Input.
275 RegisterFunction<SendKeyboardEventInputFunction>();
276#endif
[email protected]cffd7892010-08-26 17:43:28277
278 // Management.
279 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e78543c2010-11-08 22:05:23280 RegisterFunction<GetExtensionByIdFunction>();
[email protected]e558ff82010-09-23 20:19:56281 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28282 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28283 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23284
[email protected]a65882c2010-11-12 15:15:09285 // Extension module.
286 RegisterFunction<SetUpdateUrlDataFunction>();
287
[email protected]63cda0c2010-09-01 04:41:23288 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24289 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23290 RegisterFunction<GetStoreLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23291 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24292 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]b1f04cc2010-11-10 22:59:30293 RegisterFunction<BeginInstallFunction>();
294 RegisterFunction<CompleteInstallFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35295}
296
[email protected]b83e4602009-05-15 22:58:33297void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
298 for (FactoryMap::iterator iter = factories_.begin();
299 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35300 names->push_back(iter->first);
301 }
302}
303
[email protected]b83e4602009-05-15 22:58:33304bool FactoryRegistry::OverrideFunction(const std::string& name,
305 ExtensionFunctionFactory factory) {
306 FactoryMap::iterator iter = factories_.find(name);
307 if (iter == factories_.end()) {
308 return false;
309 } else {
310 iter->second = factory;
311 return true;
312 }
313}
314
[email protected]bfdffe2b2009-04-24 22:05:35315ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
316 FactoryMap::iterator iter = factories_.find(name);
317 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33318 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56319 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33320 return function;
[email protected]bfdffe2b2009-04-24 22:05:35321}
322
[email protected]b83e4602009-05-15 22:58:33323}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35324
325// ExtensionFunctionDispatcher -------------------------------------------------
326
327void ExtensionFunctionDispatcher::GetAllFunctionNames(
328 std::vector<std::string>* names) {
[email protected]8e8bb6d2010-12-13 08:18:55329 FactoryRegistry::GetInstance()->GetAllNames(names);
[email protected]bfdffe2b2009-04-24 22:05:35330}
331
[email protected]b83e4602009-05-15 22:58:33332bool ExtensionFunctionDispatcher::OverrideFunction(
333 const std::string& name, ExtensionFunctionFactory factory) {
[email protected]8e8bb6d2010-12-13 08:18:55334 return FactoryRegistry::GetInstance()->OverrideFunction(name, factory);
[email protected]b83e4602009-05-15 22:58:33335}
336
337void ExtensionFunctionDispatcher::ResetFunctions() {
[email protected]8e8bb6d2010-12-13 08:18:55338 FactoryRegistry::GetInstance()->ResetFunctions();
[email protected]b83e4602009-05-15 22:58:33339}
340
[email protected]a91afcb2010-03-25 21:15:02341ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
342 RenderViewHost* render_view_host,
343 Delegate* delegate,
344 const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:00345 ExtensionService* service =
346 render_view_host->process()->profile()->GetExtensionService();
[email protected]a91afcb2010-03-25 21:15:02347 DCHECK(service);
348
[email protected]583d45c12010-08-31 02:48:12349 if (!service->ExtensionBindingsAllowed(url))
350 return NULL;
351
[email protected]9adb9692010-10-29 23:14:02352 const Extension* extension = service->GetExtensionByURL(url);
[email protected]583d45c12010-08-31 02:48:12353 if (!extension)
354 extension = service->GetExtensionByWebExtent(url);
355
[email protected]a91afcb2010-03-25 21:15:02356 if (extension)
357 return new ExtensionFunctionDispatcher(render_view_host, delegate,
358 extension, url);
359 else
360 return NULL;
361}
362
[email protected]bfdffe2b2009-04-24 22:05:35363ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
364 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12365 Delegate* delegate,
[email protected]9adb9692010-10-29 23:14:02366 const Extension* extension,
[email protected]811bfe32009-07-01 08:46:25367 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30368 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58369 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12370 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25371 url_(url),
[email protected]583d45c12010-08-31 02:48:12372 extension_id_(extension->id()),
[email protected]32dda362009-06-05 19:07:01373 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43374 // TODO(erikkay) should we do something for these errors in Release?
[email protected]35506352009-08-07 18:58:19375 DCHECK(extension);
[email protected]583d45c12010-08-31 02:48:12376 DCHECK(url.SchemeIs(chrome::kExtensionScheme) ||
377 extension->location() == Extension::COMPONENT);
[email protected]9c45b7182009-08-04 16:44:43378
[email protected]45776222009-07-15 20:21:58379 // Notify the ExtensionProcessManager that the view was created.
380 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
381 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45382 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19383
[email protected]584b8e3f2010-04-10 00:23:37384 // If the extension has permission to load chrome://favicon/ resources we need
385 // to make sure that the DOMUIFavIconSource is registered with the
386 // ChromeURLDataManager.
387 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
388 DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_);
[email protected]ca4b5fa32010-10-09 12:42:18389 BrowserThread::PostTask(
390 BrowserThread::IO, FROM_HERE,
[email protected]d3c6c0d72010-12-09 08:15:04391 NewRunnableMethod(ChromeURLDataManager::GetInstance(),
[email protected]584b8e3f2010-04-10 00:23:37392 &ChromeURLDataManager::AddDataSource,
393 make_scoped_refptr(favicon_source)));
394 }
395
[email protected]35506352009-08-07 18:58:19396 // Update the extension permissions. Doing this each time we create an EFD
397 // ensures that new processes are informed of permissions for newly installed
398 // extensions.
[email protected]cccf90932009-08-23 17:56:25399 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19400 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25401 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
402 extension->url(), extension->host_permissions()));
[email protected]68f07912010-03-05 18:33:58403
404 NotificationService::current()->Notify(
405 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
406 Source<Profile>(profile_),
407 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35408}
409
[email protected]32dda362009-06-05 19:07:01410ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
411 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58412
413 NotificationService::current()->Notify(
414 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
415 Source<Profile>(profile_),
416 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01417}
418
[email protected]0ec92032010-03-24 19:59:41419Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
420 bool include_incognito) {
421 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12422
[email protected]bc535ee52010-08-31 18:40:32423 // If the delegate has an associated browser, that is always the right answer.
424 if (browser)
425 return browser;
[email protected]9c45b7182009-08-04 16:44:43426
[email protected]bc535ee52010-08-31 18:40:32427 // Otherwise, try to default to a reasonable browser. If |include_incognito|
428 // is true, we will also search browsers in the incognito version of this
429 // profile. Note that the profile may already be incognito, in which case
430 // we will search the incognito version only, regardless of the value of
431 // |include_incognito|.
[email protected]0ec92032010-03-24 19:59:41432 Profile* profile = render_view_host()->process()->profile();
[email protected]aa916d572010-08-13 02:59:31433 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31434 include_incognito);
[email protected]0ec92032010-03-24 19:59:41435
[email protected]0ec92032010-03-24 19:59:41436 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42437 // a background_page onload chrome.tabs api call can make it into here
438 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41439 // A similar situation may arise during shutdown.
440 // TODO(rafaelw): Delay creation of background_page until the browser
441 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
442 return browser;
[email protected]b27257562009-11-16 23:28:26443}
444
[email protected]8b8e7c92010-08-19 18:05:56445void ExtensionFunctionDispatcher::HandleRequest(
446 const ViewHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01447 scoped_refptr<ExtensionFunction> function(
[email protected]8e8bb6d2010-12-13 08:18:55448 FactoryRegistry::GetInstance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01449 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43450 function->set_profile(profile_);
451 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56452 function->SetArgs(&params.arguments);
453 function->set_source_url(params.source_url);
454 function->set_request_id(params.request_id);
455 function->set_has_callback(params.has_callback);
456 function->set_user_gesture(params.user_gesture);
[email protected]eaa7dd182010-12-14 11:09:00457 ExtensionService* service = profile()->GetExtensionService();
[email protected]d13950e2009-12-04 01:43:02458 DCHECK(service);
[email protected]9adb9692010-10-29 23:14:02459 const Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]cb0ce1e022010-03-10 19:54:41460 DCHECK(extension);
[email protected]2a8f24e2010-11-03 21:37:05461 function->set_include_incognito(service->CanCrossIncognito(extension));
[email protected]cb0ce1e022010-03-10 19:54:41462
[email protected]583d45c12010-08-31 02:48:12463 if (!service->ExtensionBindingsAllowed(function->source_url()) ||
[email protected]246c05f2010-09-10 09:12:11464 !extension->HasApiPermission(function->name())) {
[email protected]583d45c12010-08-31 02:48:12465 render_view_host_->BlockExtensionRequest(function->request_id());
466 return;
467 }
468
[email protected]d13950e2009-12-04 01:43:02469 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56470 if (quota->Assess(extension_id(), function, &params.arguments,
471 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26472 // See crbug.com/39178.
473 ExternalProtocolHandler::PermitLaunchUrl();
474
[email protected]d13950e2009-12-04 01:43:02475 function->Run();
476 } else {
477 render_view_host_->SendExtensionResponse(function->request_id(), false,
478 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
479 }
[email protected]bfdffe2b2009-04-24 22:05:35480}
481
[email protected]c6619182009-05-12 14:59:32482void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
483 bool success) {
[email protected]c6619182009-05-12 14:59:32484 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33485 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35486}
487
488void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14489 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45490 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35491 " : terminating renderer.";
492 if (RenderProcessHost::run_renderer_in_process()) {
493 // In single process mode it is better if we don't suicide but just crash.
494 CHECK(false);
495 } else {
496 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46497 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35498 ResultCodes::KILLED_BAD_MESSAGE, false);
499 }
500}
501
502Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58503 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35504}