blob: a2b16ee6376d2e4aefbbbd5ffeace0d430a83b1a [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]e916901c2009-05-07 00:14:3133#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4334#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]56ad3792010-05-28 17:45:3335#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3436#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]1c1c77a52009-11-03 00:37:3137#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5838#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3539#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4440#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0641#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]8b8e7c92010-08-19 18:05:5642#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3543#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1444#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2245#if defined(OS_CHROMEOS)
46#include "chrome/browser/extensions/extension_tts_api.h"
47#endif
[email protected]d13950e2009-12-04 01:43:0248#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5649#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3150#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3551#include "chrome/browser/renderer_host/render_process_host.h"
52#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1953#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3554#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4355#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3556
57// FactoryRegistry -------------------------------------------------------------
58
59namespace {
60
[email protected]b83e4602009-05-15 22:58:3361// Template for defining ExtensionFunctionFactory.
62template<class T>
63ExtensionFunction* NewExtensionFunction() {
64 return new T();
65}
[email protected]bfdffe2b2009-04-24 22:05:3566
[email protected]b83e4602009-05-15 22:58:3367// Contains a list of all known extension functions and allows clients to
68// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3569class FactoryRegistry {
70 public:
71 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3372 FactoryRegistry() { ResetFunctions(); }
73
74 // Resets all functions to their default values.
75 void ResetFunctions();
76
77 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3578 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3379
80 // Allows overriding of specific functions (e.g. for testing). Functions
81 // must be previously registered. Returns true if successful.
82 bool OverrideFunction(const std::string& name,
83 ExtensionFunctionFactory factory);
84
85 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3586 ExtensionFunction* NewFunction(const std::string& name);
87
88 private:
[email protected]61424c062009-10-14 23:14:5989 template<class T>
90 void RegisterFunction() {
91 factories_[T::function_name()] = &NewExtensionFunction<T>;
92 }
93
[email protected]bfdffe2b2009-04-24 22:05:3594 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
95 FactoryMap factories_;
96};
97
[email protected]bfdffe2b2009-04-24 22:05:3598FactoryRegistry* FactoryRegistry::instance() {
99 return Singleton<FactoryRegistry>::get();
100}
101
[email protected]b83e4602009-05-15 22:58:33102void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35103 // Register all functions here.
104
[email protected]e515f5d2009-05-05 03:05:00105 // Windows
[email protected]61424c062009-10-14 23:14:59106 RegisterFunction<GetWindowFunction>();
107 RegisterFunction<GetCurrentWindowFunction>();
108 RegisterFunction<GetLastFocusedWindowFunction>();
109 RegisterFunction<GetAllWindowsFunction>();
110 RegisterFunction<CreateWindowFunction>();
111 RegisterFunction<UpdateWindowFunction>();
112 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33113
[email protected]e515f5d2009-05-05 03:05:00114 // Tabs
[email protected]61424c062009-10-14 23:14:59115 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41116 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59117 RegisterFunction<GetSelectedTabFunction>();
118 RegisterFunction<GetAllTabsInWindowFunction>();
119 RegisterFunction<CreateTabFunction>();
120 RegisterFunction<UpdateTabFunction>();
121 RegisterFunction<MoveTabFunction>();
122 RegisterFunction<RemoveTabFunction>();
123 RegisterFunction<DetectTabLanguageFunction>();
124 RegisterFunction<CaptureVisibleTabFunction>();
125 RegisterFunction<TabsExecuteScriptFunction>();
126 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35127
[email protected]f7f3a5f2009-05-01 22:02:34128 // Page Actions.
[email protected]61424c062009-10-14 23:14:59129 RegisterFunction<EnablePageActionFunction>();
130 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46131 RegisterFunction<PageActionShowFunction>();
132 RegisterFunction<PageActionHideFunction>();
133 RegisterFunction<PageActionSetIconFunction>();
134 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29135 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34136
[email protected]ec9ac0df2009-10-01 18:06:47137 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59138 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24139 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59140 RegisterFunction<BrowserActionSetBadgeTextFunction>();
141 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50142 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47143
[email protected]f7f3a5f2009-05-01 22:02:34144 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59145 RegisterFunction<GetBookmarksFunction>();
146 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55147 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59148 RegisterFunction<GetBookmarkTreeFunction>();
149 RegisterFunction<SearchBookmarksFunction>();
150 RegisterFunction<RemoveBookmarkFunction>();
151 RegisterFunction<RemoveTreeBookmarkFunction>();
152 RegisterFunction<CreateBookmarkFunction>();
153 RegisterFunction<MoveBookmarkFunction>();
154 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43155
[email protected]f34e79632010-03-17 02:34:08156 // Infobars.
157 RegisterFunction<ShowInfoBarFunction>();
158
[email protected]9dd97bc2010-01-14 01:40:04159 // BookmarkManager
160 RegisterFunction<CopyBookmarkManagerFunction>();
161 RegisterFunction<CutBookmarkManagerFunction>();
162 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57163 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56164 RegisterFunction<ImportBookmarksFunction>();
165 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18166 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04167 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16168 RegisterFunction<StartDragBookmarkManagerFunction>();
169 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31170 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04171
[email protected]de768a832009-10-30 05:25:01172 // History
173 RegisterFunction<AddUrlHistoryFunction>();
174 RegisterFunction<DeleteAllHistoryFunction>();
175 RegisterFunction<DeleteRangeHistoryFunction>();
176 RegisterFunction<DeleteUrlHistoryFunction>();
177 RegisterFunction<GetVisitsHistoryFunction>();
178 RegisterFunction<SearchHistoryFunction>();
179
[email protected]f5205412010-03-16 00:19:34180 // Idle
181 RegisterFunction<ExtensionIdleQueryStateFunction>();
182
[email protected]198bcfe2009-09-09 22:56:28183 // I18N.
[email protected]61424c062009-10-14 23:14:59184 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28185
[email protected]1c1c77a52009-11-03 00:37:31186 // Popup API.
187 RegisterFunction<PopupShowFunction>();
188
[email protected]381162b2010-01-28 17:29:35189 // Processes.
190 RegisterFunction<GetProcessForTabFunction>();
191
[email protected]438772df2010-02-26 18:08:43192 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34193 RegisterFunction<MetricsRecordUserActionFunction>();
194 RegisterFunction<MetricsRecordValueFunction>();
195 RegisterFunction<MetricsRecordPercentageFunction>();
196 RegisterFunction<MetricsRecordCountFunction>();
197 RegisterFunction<MetricsRecordSmallCountFunction>();
198 RegisterFunction<MetricsRecordMediumCountFunction>();
199 RegisterFunction<MetricsRecordTimeFunction>();
200 RegisterFunction<MetricsRecordMediumTimeFunction>();
201 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43202
[email protected]17d40f02010-07-01 01:18:06203 // RLZ.
204#if defined(OS_WIN)
205 RegisterFunction<RlzRecordProductEventFunction>();
206 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21207 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06208 RegisterFunction<RlzClearProductStateFunction>();
209#endif
210
[email protected]898bbd32010-05-18 18:52:29211 // Cookies.
212 RegisterFunction<GetCookieFunction>();
213 RegisterFunction<GetAllCookiesFunction>();
214 RegisterFunction<SetCookieFunction>();
215 RegisterFunction<RemoveCookieFunction>();
216 RegisterFunction<GetAllCookieStoresFunction>();
217
[email protected]25fd1b2e2009-08-17 20:57:14218 // Test.
[email protected]61424c062009-10-14 23:14:59219 RegisterFunction<ExtensionTestPassFunction>();
220 RegisterFunction<ExtensionTestFailFunction>();
221 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02222 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50223 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07224 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]5cbe1e22010-01-30 01:18:56225
226 // Accessibility.
227 RegisterFunction<GetFocusedControlFunction>();
228 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58229
[email protected]8abe0a32010-08-12 00:40:22230#if defined(OS_CHROMEOS)
231 // Text-to-speech.
232 RegisterFunction<ExtensionTtsSpeakFunction>();
233 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
234 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
235#endif
236
[email protected]446255952010-03-17 20:41:58237 // Clipboard.
238 RegisterFunction<ExecuteCopyClipboardFunction>();
239 RegisterFunction<ExecuteCutClipboardFunction>();
240 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41241
242 // Context Menus.
243 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15244 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41245 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15246 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33247
248 // Omnibox.
249 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]a9c23a52010-08-04 09:13:44250
251 // Proxies.
252 RegisterFunction<UseCustomProxySettingsFunction>();
[email protected]8b8e7c92010-08-19 18:05:56253
254 // Sidebar.
255 RegisterFunction<CollapseSidebarFunction>();
256 RegisterFunction<ExpandSidebarFunction>();
257 RegisterFunction<GetStateSidebarFunction>();
258 RegisterFunction<HideSidebarFunction>();
259 RegisterFunction<NavigateSidebarFunction>();
260 RegisterFunction<SetBadgeTextSidebarFunction>();
261 RegisterFunction<SetIconSidebarFunction>();
262 RegisterFunction<SetTitleSidebarFunction>();
263 RegisterFunction<ShowSidebarFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35264}
265
[email protected]b83e4602009-05-15 22:58:33266void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
267 for (FactoryMap::iterator iter = factories_.begin();
268 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35269 names->push_back(iter->first);
270 }
271}
272
[email protected]b83e4602009-05-15 22:58:33273bool FactoryRegistry::OverrideFunction(const std::string& name,
274 ExtensionFunctionFactory factory) {
275 FactoryMap::iterator iter = factories_.find(name);
276 if (iter == factories_.end()) {
277 return false;
278 } else {
279 iter->second = factory;
280 return true;
281 }
282}
283
[email protected]bfdffe2b2009-04-24 22:05:35284ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
285 FactoryMap::iterator iter = factories_.find(name);
286 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33287 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56288 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33289 return function;
[email protected]bfdffe2b2009-04-24 22:05:35290}
291
[email protected]b83e4602009-05-15 22:58:33292}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35293
[email protected]bfdffe2b2009-04-24 22:05:35294// ExtensionFunctionDispatcher -------------------------------------------------
295
296void ExtensionFunctionDispatcher::GetAllFunctionNames(
297 std::vector<std::string>* names) {
298 FactoryRegistry::instance()->GetAllNames(names);
299}
300
[email protected]b83e4602009-05-15 22:58:33301bool ExtensionFunctionDispatcher::OverrideFunction(
302 const std::string& name, ExtensionFunctionFactory factory) {
303 return FactoryRegistry::instance()->OverrideFunction(name, factory);
304}
305
306void ExtensionFunctionDispatcher::ResetFunctions() {
307 FactoryRegistry::instance()->ResetFunctions();
308}
309
[email protected]a91afcb2010-03-25 21:15:02310ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
311 RenderViewHost* render_view_host,
312 Delegate* delegate,
313 const GURL& url) {
314 ExtensionsService* service =
315 render_view_host->process()->profile()->GetExtensionsService();
316 DCHECK(service);
317
318 Extension* extension = service->GetExtensionByURL(url);
319 if (extension)
320 return new ExtensionFunctionDispatcher(render_view_host, delegate,
321 extension, url);
322 else
323 return NULL;
324}
325
[email protected]bfdffe2b2009-04-24 22:05:35326ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
327 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12328 Delegate* delegate,
[email protected]a91afcb2010-03-25 21:15:02329 Extension* extension,
[email protected]811bfe32009-07-01 08:46:25330 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30331 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58332 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12333 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25334 url_(url),
[email protected]32dda362009-06-05 19:07:01335 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43336 // TODO(erikkay) should we do something for these errors in Release?
337 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19338 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43339
[email protected]45776222009-07-15 20:21:58340 // Notify the ExtensionProcessManager that the view was created.
341 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
342 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45343 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19344
[email protected]db7331a2010-02-25 22:10:50345 bool incognito_enabled =
[email protected]cb0ce1e022010-03-10 19:54:41346 profile()->GetExtensionsService()->IsIncognitoEnabled(extension);
[email protected]db7331a2010-02-25 22:10:50347
[email protected]584b8e3f2010-04-10 00:23:37348 // If the extension has permission to load chrome://favicon/ resources we need
349 // to make sure that the DOMUIFavIconSource is registered with the
350 // ChromeURLDataManager.
351 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
352 DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_);
353 ChromeThread::PostTask(
354 ChromeThread::IO, FROM_HERE,
355 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(),
356 &ChromeURLDataManager::AddDataSource,
357 make_scoped_refptr(favicon_source)));
358 }
359
[email protected]35506352009-08-07 18:58:19360 // Update the extension permissions. Doing this each time we create an EFD
361 // ensures that new processes are informed of permissions for newly installed
362 // extensions.
[email protected]cccf90932009-08-23 17:56:25363 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19364 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25365 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
366 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50367 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
368 extension->id(), incognito_enabled));
[email protected]68f07912010-03-05 18:33:58369
370 NotificationService::current()->Notify(
371 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
372 Source<Profile>(profile_),
373 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35374}
375
[email protected]32dda362009-06-05 19:07:01376ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01377 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58378
379 NotificationService::current()->Notify(
380 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
381 Source<Profile>(profile_),
382 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01383}
384
[email protected]0ec92032010-03-24 19:59:41385Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
386 bool include_incognito) {
387 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12388
[email protected]0ec92032010-03-24 19:59:41389 // If the delegate has an associated browser and that browser is in the right
390 // incognito state, we can return it.
391 if (browser) {
392 if (include_incognito || !browser->profile()->IsOffTheRecord())
393 return browser;
394 }
[email protected]9c45b7182009-08-04 16:44:43395
[email protected]0ec92032010-03-24 19:59:41396 // Otherwise, try to default to a reasonable browser.
397 Profile* profile = render_view_host()->process()->profile();
398
399 // Make sure we don't return an incognito browser without proper access.
400 if (!include_incognito)
401 profile = profile->GetOriginalProfile();
402
[email protected]aa916d572010-08-13 02:59:31403 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31404 include_incognito);
[email protected]0ec92032010-03-24 19:59:41405
[email protected]0ec92032010-03-24 19:59:41406 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42407 // a background_page onload chrome.tabs api call can make it into here
408 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41409 // A similar situation may arise during shutdown.
410 // TODO(rafaelw): Delay creation of background_page until the browser
411 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
412 return browser;
[email protected]b27257562009-11-16 23:28:26413}
414
[email protected]8b8e7c92010-08-19 18:05:56415void ExtensionFunctionDispatcher::HandleRequest(
416 const ViewHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01417 scoped_refptr<ExtensionFunction> function(
[email protected]8b8e7c92010-08-19 18:05:56418 FactoryRegistry::instance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01419 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43420 function->set_profile(profile_);
421 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56422 function->SetArgs(&params.arguments);
423 function->set_source_url(params.source_url);
424 function->set_request_id(params.request_id);
425 function->set_has_callback(params.has_callback);
426 function->set_user_gesture(params.user_gesture);
[email protected]d13950e2009-12-04 01:43:02427 ExtensionsService* service = profile()->GetExtensionsService();
428 DCHECK(service);
[email protected]cb0ce1e022010-03-10 19:54:41429 Extension* extension = service->GetExtensionById(extension_id(), false);
430 DCHECK(extension);
431 function->set_include_incognito(service->IsIncognitoEnabled(extension));
432
[email protected]d13950e2009-12-04 01:43:02433 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56434 if (quota->Assess(extension_id(), function, &params.arguments,
435 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26436 // See crbug.com/39178.
437 ExternalProtocolHandler::PermitLaunchUrl();
438
[email protected]d13950e2009-12-04 01:43:02439 function->Run();
440 } else {
441 render_view_host_->SendExtensionResponse(function->request_id(), false,
442 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
443 }
[email protected]bfdffe2b2009-04-24 22:05:35444}
445
[email protected]c6619182009-05-12 14:59:32446void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
447 bool success) {
[email protected]c6619182009-05-12 14:59:32448 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33449 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35450}
451
452void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14453 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45454 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35455 " : terminating renderer.";
456 if (RenderProcessHost::run_renderer_in_process()) {
457 // In single process mode it is better if we don't suicide but just crash.
458 CHECK(false);
459 } else {
460 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46461 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35462 ResultCodes::KILLED_BAD_MESSAGE, false);
463 }
464}
465
466Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58467 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35468}