blob: 51cc8feb1eb8fa5a30fb835bd99f49be462ba2eb [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]56ad3792010-05-28 17:45:3339#include "chrome/browser/extensions/extension_omnibox_api.h"
[email protected]f7f3a5f2009-05-01 22:02:3440#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]1c1c77a52009-11-03 00:37:3141#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5842#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3543#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]a9c23a52010-08-04 09:13:4444#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]17d40f02010-07-01 01:18:0645#include "chrome/browser/extensions/extension_rlz_module.h"
[email protected]8b8e7c92010-08-19 18:05:5646#include "chrome/browser/extensions/extension_sidebar_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3547#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]25fd1b2e2009-08-17 20:57:1448#include "chrome/browser/extensions/extension_test_api.h"
[email protected]8abe0a32010-08-12 00:40:2249#if defined(OS_CHROMEOS)
50#include "chrome/browser/extensions/extension_tts_api.h"
51#endif
[email protected]d13950e2009-12-04 01:43:0252#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5653#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3154#include "chrome/browser/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]35506352009-08-07 18:58:1957#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0258#include "chrome/common/render_messages_params.h"
[email protected]bfdffe2b2009-04-24 22:05:3559#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4360#include "chrome/common/url_constants.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]d6833852010-08-20 18:00:4563
[email protected]bfdffe2b2009-04-24 22:05:3564// FactoryRegistry -------------------------------------------------------------
65
66namespace {
67
[email protected]b83e4602009-05-15 22:58:3368// Template for defining ExtensionFunctionFactory.
69template<class T>
70ExtensionFunction* NewExtensionFunction() {
71 return new T();
72}
[email protected]bfdffe2b2009-04-24 22:05:3573
[email protected]b83e4602009-05-15 22:58:3374// Contains a list of all known extension functions and allows clients to
75// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3576class FactoryRegistry {
77 public:
78 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3379 FactoryRegistry() { ResetFunctions(); }
80
81 // Resets all functions to their default values.
82 void ResetFunctions();
83
84 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3585 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3386
87 // Allows overriding of specific functions (e.g. for testing). Functions
88 // must be previously registered. Returns true if successful.
89 bool OverrideFunction(const std::string& name,
90 ExtensionFunctionFactory factory);
91
92 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3593 ExtensionFunction* NewFunction(const std::string& name);
94
95 private:
[email protected]61424c062009-10-14 23:14:5996 template<class T>
97 void RegisterFunction() {
98 factories_[T::function_name()] = &NewExtensionFunction<T>;
99 }
100
[email protected]bfdffe2b2009-04-24 22:05:35101 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
102 FactoryMap factories_;
103};
104
[email protected]bfdffe2b2009-04-24 22:05:35105FactoryRegistry* FactoryRegistry::instance() {
106 return Singleton<FactoryRegistry>::get();
107}
108
[email protected]b83e4602009-05-15 22:58:33109void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35110 // Register all functions here.
111
[email protected]e515f5d2009-05-05 03:05:00112 // Windows
[email protected]61424c062009-10-14 23:14:59113 RegisterFunction<GetWindowFunction>();
114 RegisterFunction<GetCurrentWindowFunction>();
115 RegisterFunction<GetLastFocusedWindowFunction>();
116 RegisterFunction<GetAllWindowsFunction>();
117 RegisterFunction<CreateWindowFunction>();
118 RegisterFunction<UpdateWindowFunction>();
119 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33120
[email protected]e515f5d2009-05-05 03:05:00121 // Tabs
[email protected]61424c062009-10-14 23:14:59122 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41123 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59124 RegisterFunction<GetSelectedTabFunction>();
125 RegisterFunction<GetAllTabsInWindowFunction>();
126 RegisterFunction<CreateTabFunction>();
127 RegisterFunction<UpdateTabFunction>();
128 RegisterFunction<MoveTabFunction>();
129 RegisterFunction<RemoveTabFunction>();
130 RegisterFunction<DetectTabLanguageFunction>();
131 RegisterFunction<CaptureVisibleTabFunction>();
132 RegisterFunction<TabsExecuteScriptFunction>();
133 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35134
[email protected]f7f3a5f2009-05-01 22:02:34135 // Page Actions.
[email protected]61424c062009-10-14 23:14:59136 RegisterFunction<EnablePageActionFunction>();
137 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46138 RegisterFunction<PageActionShowFunction>();
139 RegisterFunction<PageActionHideFunction>();
140 RegisterFunction<PageActionSetIconFunction>();
141 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29142 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34143
[email protected]ec9ac0df2009-10-01 18:06:47144 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59145 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24146 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59147 RegisterFunction<BrowserActionSetBadgeTextFunction>();
148 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50149 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47150
[email protected]f7f3a5f2009-05-01 22:02:34151 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59152 RegisterFunction<GetBookmarksFunction>();
153 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55154 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59155 RegisterFunction<GetBookmarkTreeFunction>();
156 RegisterFunction<SearchBookmarksFunction>();
157 RegisterFunction<RemoveBookmarkFunction>();
158 RegisterFunction<RemoveTreeBookmarkFunction>();
159 RegisterFunction<CreateBookmarkFunction>();
160 RegisterFunction<MoveBookmarkFunction>();
161 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43162
[email protected]f34e79632010-03-17 02:34:08163 // Infobars.
164 RegisterFunction<ShowInfoBarFunction>();
165
[email protected]9dd97bc2010-01-14 01:40:04166 // BookmarkManager
167 RegisterFunction<CopyBookmarkManagerFunction>();
168 RegisterFunction<CutBookmarkManagerFunction>();
169 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57170 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56171 RegisterFunction<ImportBookmarksFunction>();
172 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18173 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04174 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16175 RegisterFunction<StartDragBookmarkManagerFunction>();
176 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31177 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04178
[email protected]de768a832009-10-30 05:25:01179 // History
180 RegisterFunction<AddUrlHistoryFunction>();
181 RegisterFunction<DeleteAllHistoryFunction>();
182 RegisterFunction<DeleteRangeHistoryFunction>();
183 RegisterFunction<DeleteUrlHistoryFunction>();
184 RegisterFunction<GetVisitsHistoryFunction>();
185 RegisterFunction<SearchHistoryFunction>();
186
[email protected]f5205412010-03-16 00:19:34187 // Idle
188 RegisterFunction<ExtensionIdleQueryStateFunction>();
189
[email protected]198bcfe2009-09-09 22:56:28190 // I18N.
[email protected]61424c062009-10-14 23:14:59191 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28192
[email protected]1c1c77a52009-11-03 00:37:31193 // Popup API.
194 RegisterFunction<PopupShowFunction>();
195
[email protected]381162b2010-01-28 17:29:35196 // Processes.
197 RegisterFunction<GetProcessForTabFunction>();
198
[email protected]438772df2010-02-26 18:08:43199 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34200 RegisterFunction<MetricsRecordUserActionFunction>();
201 RegisterFunction<MetricsRecordValueFunction>();
202 RegisterFunction<MetricsRecordPercentageFunction>();
203 RegisterFunction<MetricsRecordCountFunction>();
204 RegisterFunction<MetricsRecordSmallCountFunction>();
205 RegisterFunction<MetricsRecordMediumCountFunction>();
206 RegisterFunction<MetricsRecordTimeFunction>();
207 RegisterFunction<MetricsRecordMediumTimeFunction>();
208 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43209
[email protected]17d40f02010-07-01 01:18:06210 // RLZ.
211#if defined(OS_WIN)
212 RegisterFunction<RlzRecordProductEventFunction>();
213 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21214 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06215 RegisterFunction<RlzClearProductStateFunction>();
216#endif
217
[email protected]898bbd32010-05-18 18:52:29218 // Cookies.
219 RegisterFunction<GetCookieFunction>();
220 RegisterFunction<GetAllCookiesFunction>();
221 RegisterFunction<SetCookieFunction>();
222 RegisterFunction<RemoveCookieFunction>();
223 RegisterFunction<GetAllCookieStoresFunction>();
224
[email protected]25fd1b2e2009-08-17 20:57:14225 // Test.
[email protected]61424c062009-10-14 23:14:59226 RegisterFunction<ExtensionTestPassFunction>();
227 RegisterFunction<ExtensionTestFailFunction>();
228 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02229 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50230 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07231 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]5cbe1e22010-01-30 01:18:56232
233 // Accessibility.
234 RegisterFunction<GetFocusedControlFunction>();
235 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58236
[email protected]8abe0a32010-08-12 00:40:22237#if defined(OS_CHROMEOS)
238 // Text-to-speech.
239 RegisterFunction<ExtensionTtsSpeakFunction>();
240 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
241 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
242#endif
243
[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]a9c23a52010-08-04 09:13:44257
258 // Proxies.
259 RegisterFunction<UseCustomProxySettingsFunction>();
[email protected]8b8e7c92010-08-19 18:05:56260
261 // Sidebar.
262 RegisterFunction<CollapseSidebarFunction>();
263 RegisterFunction<ExpandSidebarFunction>();
264 RegisterFunction<GetStateSidebarFunction>();
265 RegisterFunction<HideSidebarFunction>();
266 RegisterFunction<NavigateSidebarFunction>();
267 RegisterFunction<SetBadgeTextSidebarFunction>();
268 RegisterFunction<SetIconSidebarFunction>();
269 RegisterFunction<SetTitleSidebarFunction>();
270 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45271
272#if defined(TOOLKIT_VIEWS)
273 // Input.
274 RegisterFunction<SendKeyboardEventInputFunction>();
275#endif
[email protected]cffd7892010-08-26 17:43:28276
277 // Management.
278 RegisterFunction<GetAllExtensionsFunction>();
279 RegisterFunction<SetEnabledFunction>();
280 RegisterFunction<InstallFunction>();
281 RegisterFunction<UninstallFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35282}
283
[email protected]b83e4602009-05-15 22:58:33284void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
285 for (FactoryMap::iterator iter = factories_.begin();
286 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35287 names->push_back(iter->first);
288 }
289}
290
[email protected]b83e4602009-05-15 22:58:33291bool FactoryRegistry::OverrideFunction(const std::string& name,
292 ExtensionFunctionFactory factory) {
293 FactoryMap::iterator iter = factories_.find(name);
294 if (iter == factories_.end()) {
295 return false;
296 } else {
297 iter->second = factory;
298 return true;
299 }
300}
301
[email protected]bfdffe2b2009-04-24 22:05:35302ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
303 FactoryMap::iterator iter = factories_.find(name);
304 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33305 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56306 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33307 return function;
[email protected]bfdffe2b2009-04-24 22:05:35308}
309
[email protected]b83e4602009-05-15 22:58:33310}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35311
[email protected]bfdffe2b2009-04-24 22:05:35312// ExtensionFunctionDispatcher -------------------------------------------------
313
314void ExtensionFunctionDispatcher::GetAllFunctionNames(
315 std::vector<std::string>* names) {
316 FactoryRegistry::instance()->GetAllNames(names);
317}
318
[email protected]b83e4602009-05-15 22:58:33319bool ExtensionFunctionDispatcher::OverrideFunction(
320 const std::string& name, ExtensionFunctionFactory factory) {
321 return FactoryRegistry::instance()->OverrideFunction(name, factory);
322}
323
324void ExtensionFunctionDispatcher::ResetFunctions() {
325 FactoryRegistry::instance()->ResetFunctions();
326}
327
[email protected]a91afcb2010-03-25 21:15:02328ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
329 RenderViewHost* render_view_host,
330 Delegate* delegate,
331 const GURL& url) {
332 ExtensionsService* service =
333 render_view_host->process()->profile()->GetExtensionsService();
334 DCHECK(service);
335
336 Extension* extension = service->GetExtensionByURL(url);
337 if (extension)
338 return new ExtensionFunctionDispatcher(render_view_host, delegate,
339 extension, url);
340 else
341 return NULL;
342}
343
[email protected]bfdffe2b2009-04-24 22:05:35344ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
345 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12346 Delegate* delegate,
[email protected]a91afcb2010-03-25 21:15:02347 Extension* extension,
[email protected]811bfe32009-07-01 08:46:25348 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30349 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58350 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12351 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25352 url_(url),
[email protected]32dda362009-06-05 19:07:01353 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43354 // TODO(erikkay) should we do something for these errors in Release?
355 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19356 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43357
[email protected]45776222009-07-15 20:21:58358 // Notify the ExtensionProcessManager that the view was created.
359 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
360 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45361 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19362
[email protected]db7331a2010-02-25 22:10:50363 bool incognito_enabled =
[email protected]cb0ce1e022010-03-10 19:54:41364 profile()->GetExtensionsService()->IsIncognitoEnabled(extension);
[email protected]db7331a2010-02-25 22:10:50365
[email protected]584b8e3f2010-04-10 00:23:37366 // If the extension has permission to load chrome://favicon/ resources we need
367 // to make sure that the DOMUIFavIconSource is registered with the
368 // ChromeURLDataManager.
369 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
370 DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_);
371 ChromeThread::PostTask(
372 ChromeThread::IO, FROM_HERE,
373 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(),
374 &ChromeURLDataManager::AddDataSource,
375 make_scoped_refptr(favicon_source)));
376 }
377
[email protected]35506352009-08-07 18:58:19378 // Update the extension permissions. Doing this each time we create an EFD
379 // ensures that new processes are informed of permissions for newly installed
380 // extensions.
[email protected]cccf90932009-08-23 17:56:25381 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19382 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25383 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
384 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50385 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
386 extension->id(), incognito_enabled));
[email protected]68f07912010-03-05 18:33:58387
388 NotificationService::current()->Notify(
389 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
390 Source<Profile>(profile_),
391 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35392}
393
[email protected]32dda362009-06-05 19:07:01394ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]32dda362009-06-05 19:07:01395 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58396
397 NotificationService::current()->Notify(
398 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
399 Source<Profile>(profile_),
400 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01401}
402
[email protected]0ec92032010-03-24 19:59:41403Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
404 bool include_incognito) {
405 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12406
[email protected]0ec92032010-03-24 19:59:41407 // If the delegate has an associated browser and that browser is in the right
408 // incognito state, we can return it.
409 if (browser) {
410 if (include_incognito || !browser->profile()->IsOffTheRecord())
411 return browser;
412 }
[email protected]9c45b7182009-08-04 16:44:43413
[email protected]0ec92032010-03-24 19:59:41414 // Otherwise, try to default to a reasonable browser.
415 Profile* profile = render_view_host()->process()->profile();
416
417 // Make sure we don't return an incognito browser without proper access.
418 if (!include_incognito)
419 profile = profile->GetOriginalProfile();
420
[email protected]aa916d572010-08-13 02:59:31421 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31422 include_incognito);
[email protected]0ec92032010-03-24 19:59:41423
[email protected]0ec92032010-03-24 19:59:41424 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42425 // a background_page onload chrome.tabs api call can make it into here
426 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41427 // A similar situation may arise during shutdown.
428 // TODO(rafaelw): Delay creation of background_page until the browser
429 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
430 return browser;
[email protected]b27257562009-11-16 23:28:26431}
432
[email protected]8b8e7c92010-08-19 18:05:56433void ExtensionFunctionDispatcher::HandleRequest(
434 const ViewHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01435 scoped_refptr<ExtensionFunction> function(
[email protected]8b8e7c92010-08-19 18:05:56436 FactoryRegistry::instance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01437 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43438 function->set_profile(profile_);
439 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56440 function->SetArgs(&params.arguments);
441 function->set_source_url(params.source_url);
442 function->set_request_id(params.request_id);
443 function->set_has_callback(params.has_callback);
444 function->set_user_gesture(params.user_gesture);
[email protected]d13950e2009-12-04 01:43:02445 ExtensionsService* service = profile()->GetExtensionsService();
446 DCHECK(service);
[email protected]cb0ce1e022010-03-10 19:54:41447 Extension* extension = service->GetExtensionById(extension_id(), false);
448 DCHECK(extension);
449 function->set_include_incognito(service->IsIncognitoEnabled(extension));
450
[email protected]d13950e2009-12-04 01:43:02451 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56452 if (quota->Assess(extension_id(), function, &params.arguments,
453 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26454 // See crbug.com/39178.
455 ExternalProtocolHandler::PermitLaunchUrl();
456
[email protected]d13950e2009-12-04 01:43:02457 function->Run();
458 } else {
459 render_view_host_->SendExtensionResponse(function->request_id(), false,
460 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
461 }
[email protected]bfdffe2b2009-04-24 22:05:35462}
463
[email protected]c6619182009-05-12 14:59:32464void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
465 bool success) {
[email protected]c6619182009-05-12 14:59:32466 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33467 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35468}
469
470void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14471 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45472 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35473 " : terminating renderer.";
474 if (RenderProcessHost::run_renderer_in_process()) {
475 // In single process mode it is better if we don't suicide but just crash.
476 CHECK(false);
477 } else {
478 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46479 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35480 ResultCodes::KILLED_BAD_MESSAGE, false);
481 }
482}
483
484Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58485 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35486}