blob: 621b5b454f97d5266e87749974d1c7876e76cec8 [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#include "chrome/browser/extensions/extension_tts_api.h"
[email protected]63cda0c2010-09-01 04:41:2350#include "chrome/browser/extensions/extension_webstore_private_api.h"
[email protected]d13950e2009-12-04 01:43:0251#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5652#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3153#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3554#include "chrome/browser/renderer_host/render_process_host.h"
55#include "chrome/browser/renderer_host/render_view_host.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]d6833852010-08-20 18:00:4562
[email protected]bfdffe2b2009-04-24 22:05:3563// FactoryRegistry -------------------------------------------------------------
64
65namespace {
66
[email protected]b83e4602009-05-15 22:58:3367// Template for defining ExtensionFunctionFactory.
68template<class T>
69ExtensionFunction* NewExtensionFunction() {
70 return new T();
71}
[email protected]bfdffe2b2009-04-24 22:05:3572
[email protected]b83e4602009-05-15 22:58:3373// Contains a list of all known extension functions and allows clients to
74// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3575class FactoryRegistry {
76 public:
77 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3378 FactoryRegistry() { ResetFunctions(); }
79
80 // Resets all functions to their default values.
81 void ResetFunctions();
82
83 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3584 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3385
86 // Allows overriding of specific functions (e.g. for testing). Functions
87 // must be previously registered. Returns true if successful.
88 bool OverrideFunction(const std::string& name,
89 ExtensionFunctionFactory factory);
90
91 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3592 ExtensionFunction* NewFunction(const std::string& name);
93
94 private:
[email protected]61424c062009-10-14 23:14:5995 template<class T>
96 void RegisterFunction() {
97 factories_[T::function_name()] = &NewExtensionFunction<T>;
98 }
99
[email protected]bfdffe2b2009-04-24 22:05:35100 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
101 FactoryMap factories_;
102};
103
[email protected]bfdffe2b2009-04-24 22:05:35104FactoryRegistry* FactoryRegistry::instance() {
105 return Singleton<FactoryRegistry>::get();
106}
107
[email protected]b83e4602009-05-15 22:58:33108void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:35109 // Register all functions here.
110
[email protected]e515f5d2009-05-05 03:05:00111 // Windows
[email protected]61424c062009-10-14 23:14:59112 RegisterFunction<GetWindowFunction>();
113 RegisterFunction<GetCurrentWindowFunction>();
114 RegisterFunction<GetLastFocusedWindowFunction>();
115 RegisterFunction<GetAllWindowsFunction>();
116 RegisterFunction<CreateWindowFunction>();
117 RegisterFunction<UpdateWindowFunction>();
118 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33119
[email protected]e515f5d2009-05-05 03:05:00120 // Tabs
[email protected]61424c062009-10-14 23:14:59121 RegisterFunction<GetTabFunction>();
[email protected]e3eafb292010-04-14 21:30:41122 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59123 RegisterFunction<GetSelectedTabFunction>();
124 RegisterFunction<GetAllTabsInWindowFunction>();
125 RegisterFunction<CreateTabFunction>();
126 RegisterFunction<UpdateTabFunction>();
127 RegisterFunction<MoveTabFunction>();
128 RegisterFunction<RemoveTabFunction>();
129 RegisterFunction<DetectTabLanguageFunction>();
130 RegisterFunction<CaptureVisibleTabFunction>();
131 RegisterFunction<TabsExecuteScriptFunction>();
132 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35133
[email protected]f7f3a5f2009-05-01 22:02:34134 // Page Actions.
[email protected]61424c062009-10-14 23:14:59135 RegisterFunction<EnablePageActionFunction>();
136 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46137 RegisterFunction<PageActionShowFunction>();
138 RegisterFunction<PageActionHideFunction>();
139 RegisterFunction<PageActionSetIconFunction>();
140 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29141 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34142
[email protected]ec9ac0df2009-10-01 18:06:47143 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59144 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24145 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59146 RegisterFunction<BrowserActionSetBadgeTextFunction>();
147 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50148 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47149
[email protected]f7f3a5f2009-05-01 22:02:34150 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59151 RegisterFunction<GetBookmarksFunction>();
152 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55153 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59154 RegisterFunction<GetBookmarkTreeFunction>();
155 RegisterFunction<SearchBookmarksFunction>();
156 RegisterFunction<RemoveBookmarkFunction>();
157 RegisterFunction<RemoveTreeBookmarkFunction>();
158 RegisterFunction<CreateBookmarkFunction>();
159 RegisterFunction<MoveBookmarkFunction>();
160 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43161
[email protected]f34e79632010-03-17 02:34:08162 // Infobars.
163 RegisterFunction<ShowInfoBarFunction>();
164
[email protected]9dd97bc2010-01-14 01:40:04165 // BookmarkManager
166 RegisterFunction<CopyBookmarkManagerFunction>();
167 RegisterFunction<CutBookmarkManagerFunction>();
168 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57169 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56170 RegisterFunction<ImportBookmarksFunction>();
171 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18172 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04173 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16174 RegisterFunction<StartDragBookmarkManagerFunction>();
175 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31176 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04177
[email protected]de768a832009-10-30 05:25:01178 // History
179 RegisterFunction<AddUrlHistoryFunction>();
180 RegisterFunction<DeleteAllHistoryFunction>();
181 RegisterFunction<DeleteRangeHistoryFunction>();
182 RegisterFunction<DeleteUrlHistoryFunction>();
183 RegisterFunction<GetVisitsHistoryFunction>();
184 RegisterFunction<SearchHistoryFunction>();
185
[email protected]f5205412010-03-16 00:19:34186 // Idle
187 RegisterFunction<ExtensionIdleQueryStateFunction>();
188
[email protected]198bcfe2009-09-09 22:56:28189 // I18N.
[email protected]61424c062009-10-14 23:14:59190 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28191
[email protected]1c1c77a52009-11-03 00:37:31192 // Popup API.
193 RegisterFunction<PopupShowFunction>();
194
[email protected]381162b2010-01-28 17:29:35195 // Processes.
196 RegisterFunction<GetProcessForTabFunction>();
197
[email protected]438772df2010-02-26 18:08:43198 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34199 RegisterFunction<MetricsRecordUserActionFunction>();
200 RegisterFunction<MetricsRecordValueFunction>();
201 RegisterFunction<MetricsRecordPercentageFunction>();
202 RegisterFunction<MetricsRecordCountFunction>();
203 RegisterFunction<MetricsRecordSmallCountFunction>();
204 RegisterFunction<MetricsRecordMediumCountFunction>();
205 RegisterFunction<MetricsRecordTimeFunction>();
206 RegisterFunction<MetricsRecordMediumTimeFunction>();
207 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43208
[email protected]17d40f02010-07-01 01:18:06209 // RLZ.
210#if defined(OS_WIN)
211 RegisterFunction<RlzRecordProductEventFunction>();
212 RegisterFunction<RlzGetAccessPointRlzFunction>();
[email protected]3a612be2010-07-17 03:11:21213 RegisterFunction<RlzSendFinancialPingFunction>();
[email protected]17d40f02010-07-01 01:18:06214 RegisterFunction<RlzClearProductStateFunction>();
215#endif
216
[email protected]898bbd32010-05-18 18:52:29217 // Cookies.
218 RegisterFunction<GetCookieFunction>();
219 RegisterFunction<GetAllCookiesFunction>();
220 RegisterFunction<SetCookieFunction>();
221 RegisterFunction<RemoveCookieFunction>();
222 RegisterFunction<GetAllCookieStoresFunction>();
223
[email protected]25fd1b2e2009-08-17 20:57:14224 // Test.
[email protected]61424c062009-10-14 23:14:59225 RegisterFunction<ExtensionTestPassFunction>();
226 RegisterFunction<ExtensionTestFailFunction>();
227 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02228 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50229 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]745feedb2010-08-02 04:08:07230 RegisterFunction<ExtensionTestSendMessageFunction>();
[email protected]761e7162010-10-18 19:26:39231 RegisterFunction<ExtensionTestGetConfigFunction>();
[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 // Text-to-speech.
238 RegisterFunction<ExtensionTtsSpeakFunction>();
239 RegisterFunction<ExtensionTtsStopSpeakingFunction>();
240 RegisterFunction<ExtensionTtsIsSpeakingFunction>();
[email protected]8abe0a32010-08-12 00:40:22241
[email protected]446255952010-03-17 20:41:58242 // Clipboard.
243 RegisterFunction<ExecuteCopyClipboardFunction>();
244 RegisterFunction<ExecuteCutClipboardFunction>();
245 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41246
247 // Context Menus.
248 RegisterFunction<CreateContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15249 RegisterFunction<UpdateContextMenuFunction>();
[email protected]2e3b5202010-03-23 06:52:41250 RegisterFunction<RemoveContextMenuFunction>();
[email protected]66dbfb2c2010-05-12 20:20:15251 RegisterFunction<RemoveAllContextMenusFunction>();
[email protected]56ad3792010-05-28 17:45:33252
253 // Omnibox.
254 RegisterFunction<OmniboxSendSuggestionsFunction>();
[email protected]a9c23a52010-08-04 09:13:44255
256 // Proxies.
257 RegisterFunction<UseCustomProxySettingsFunction>();
[email protected]8b8e7c92010-08-19 18:05:56258
259 // Sidebar.
260 RegisterFunction<CollapseSidebarFunction>();
261 RegisterFunction<ExpandSidebarFunction>();
262 RegisterFunction<GetStateSidebarFunction>();
263 RegisterFunction<HideSidebarFunction>();
264 RegisterFunction<NavigateSidebarFunction>();
265 RegisterFunction<SetBadgeTextSidebarFunction>();
266 RegisterFunction<SetIconSidebarFunction>();
267 RegisterFunction<SetTitleSidebarFunction>();
268 RegisterFunction<ShowSidebarFunction>();
[email protected]d6833852010-08-20 18:00:45269
270#if defined(TOOLKIT_VIEWS)
271 // Input.
272 RegisterFunction<SendKeyboardEventInputFunction>();
273#endif
[email protected]cffd7892010-08-26 17:43:28274
275 // Management.
276 RegisterFunction<GetAllExtensionsFunction>();
[email protected]e558ff82010-09-23 20:19:56277 RegisterFunction<LaunchAppFunction>();
[email protected]cffd7892010-08-26 17:43:28278 RegisterFunction<SetEnabledFunction>();
[email protected]cffd7892010-08-26 17:43:28279 RegisterFunction<UninstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23280
281 // WebstorePrivate.
[email protected]d6885592010-10-11 19:38:24282 RegisterFunction<GetBrowserLoginFunction>();
[email protected]63cda0c2010-09-01 04:41:23283 RegisterFunction<GetStoreLoginFunction>();
[email protected]e558ff82010-09-23 20:19:56284 RegisterFunction<InstallFunction>();
[email protected]63cda0c2010-09-01 04:41:23285 RegisterFunction<SetStoreLoginFunction>();
[email protected]d6885592010-10-11 19:38:24286 RegisterFunction<PromptBrowserLoginFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35287}
288
[email protected]b83e4602009-05-15 22:58:33289void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
290 for (FactoryMap::iterator iter = factories_.begin();
291 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35292 names->push_back(iter->first);
293 }
294}
295
[email protected]b83e4602009-05-15 22:58:33296bool FactoryRegistry::OverrideFunction(const std::string& name,
297 ExtensionFunctionFactory factory) {
298 FactoryMap::iterator iter = factories_.find(name);
299 if (iter == factories_.end()) {
300 return false;
301 } else {
302 iter->second = factory;
303 return true;
304 }
305}
306
[email protected]bfdffe2b2009-04-24 22:05:35307ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
308 FactoryMap::iterator iter = factories_.find(name);
309 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33310 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56311 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33312 return function;
[email protected]bfdffe2b2009-04-24 22:05:35313}
314
[email protected]b83e4602009-05-15 22:58:33315}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35316
317// ExtensionFunctionDispatcher -------------------------------------------------
318
319void ExtensionFunctionDispatcher::GetAllFunctionNames(
320 std::vector<std::string>* names) {
321 FactoryRegistry::instance()->GetAllNames(names);
322}
323
[email protected]b83e4602009-05-15 22:58:33324bool ExtensionFunctionDispatcher::OverrideFunction(
325 const std::string& name, ExtensionFunctionFactory factory) {
326 return FactoryRegistry::instance()->OverrideFunction(name, factory);
327}
328
329void ExtensionFunctionDispatcher::ResetFunctions() {
330 FactoryRegistry::instance()->ResetFunctions();
331}
332
[email protected]a91afcb2010-03-25 21:15:02333ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
334 RenderViewHost* render_view_host,
335 Delegate* delegate,
336 const GURL& url) {
337 ExtensionsService* service =
338 render_view_host->process()->profile()->GetExtensionsService();
339 DCHECK(service);
340
[email protected]583d45c12010-08-31 02:48:12341 if (!service->ExtensionBindingsAllowed(url))
342 return NULL;
343
[email protected]a91afcb2010-03-25 21:15:02344 Extension* extension = service->GetExtensionByURL(url);
[email protected]583d45c12010-08-31 02:48:12345 if (!extension)
346 extension = service->GetExtensionByWebExtent(url);
347
[email protected]a91afcb2010-03-25 21:15:02348 if (extension)
349 return new ExtensionFunctionDispatcher(render_view_host, delegate,
350 extension, url);
351 else
352 return NULL;
353}
354
[email protected]bfdffe2b2009-04-24 22:05:35355ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
356 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12357 Delegate* delegate,
[email protected]a91afcb2010-03-25 21:15:02358 Extension* extension,
[email protected]811bfe32009-07-01 08:46:25359 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30360 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58361 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12362 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25363 url_(url),
[email protected]583d45c12010-08-31 02:48:12364 extension_id_(extension->id()),
[email protected]32dda362009-06-05 19:07:01365 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43366 // TODO(erikkay) should we do something for these errors in Release?
[email protected]35506352009-08-07 18:58:19367 DCHECK(extension);
[email protected]583d45c12010-08-31 02:48:12368 DCHECK(url.SchemeIs(chrome::kExtensionScheme) ||
369 extension->location() == Extension::COMPONENT);
[email protected]9c45b7182009-08-04 16:44:43370
[email protected]45776222009-07-15 20:21:58371 // Notify the ExtensionProcessManager that the view was created.
372 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
373 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45374 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19375
[email protected]db7331a2010-02-25 22:10:50376 bool incognito_enabled =
[email protected]cb0ce1e022010-03-10 19:54:41377 profile()->GetExtensionsService()->IsIncognitoEnabled(extension);
[email protected]db7331a2010-02-25 22:10:50378
[email protected]584b8e3f2010-04-10 00:23:37379 // If the extension has permission to load chrome://favicon/ resources we need
380 // to make sure that the DOMUIFavIconSource is registered with the
381 // ChromeURLDataManager.
382 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
383 DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_);
[email protected]ca4b5fa32010-10-09 12:42:18384 BrowserThread::PostTask(
385 BrowserThread::IO, FROM_HERE,
[email protected]584b8e3f2010-04-10 00:23:37386 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(),
387 &ChromeURLDataManager::AddDataSource,
388 make_scoped_refptr(favicon_source)));
389 }
390
[email protected]35506352009-08-07 18:58:19391 // Update the extension permissions. Doing this each time we create an EFD
392 // ensures that new processes are informed of permissions for newly installed
393 // extensions.
[email protected]cccf90932009-08-23 17:56:25394 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19395 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25396 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
397 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50398 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
[email protected]bc535ee52010-08-31 18:40:32399 extension->id(), incognito_enabled, extension->incognito_split_mode()));
[email protected]68f07912010-03-05 18:33:58400
401 NotificationService::current()->Notify(
402 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
403 Source<Profile>(profile_),
404 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35405}
406
[email protected]32dda362009-06-05 19:07:01407ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
408 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58409
410 NotificationService::current()->Notify(
411 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
412 Source<Profile>(profile_),
413 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01414}
415
[email protected]0ec92032010-03-24 19:59:41416Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
417 bool include_incognito) {
418 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12419
[email protected]bc535ee52010-08-31 18:40:32420 // If the delegate has an associated browser, that is always the right answer.
421 if (browser)
422 return browser;
[email protected]9c45b7182009-08-04 16:44:43423
[email protected]bc535ee52010-08-31 18:40:32424 // Otherwise, try to default to a reasonable browser. If |include_incognito|
425 // is true, we will also search browsers in the incognito version of this
426 // profile. Note that the profile may already be incognito, in which case
427 // we will search the incognito version only, regardless of the value of
428 // |include_incognito|.
[email protected]0ec92032010-03-24 19:59:41429 Profile* profile = render_view_host()->process()->profile();
[email protected]aa916d572010-08-13 02:59:31430 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_NORMAL,
[email protected]62b0b532010-03-26 22:44:31431 include_incognito);
[email protected]0ec92032010-03-24 19:59:41432
[email protected]0ec92032010-03-24 19:59:41433 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
[email protected]6d7a6042010-08-12 20:12:42434 // a background_page onload chrome.tabs api call can make it into here
435 // before the browser is sufficiently initialized to return here.
[email protected]0ec92032010-03-24 19:59:41436 // A similar situation may arise during shutdown.
437 // TODO(rafaelw): Delay creation of background_page until the browser
438 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
439 return browser;
[email protected]b27257562009-11-16 23:28:26440}
441
[email protected]8b8e7c92010-08-19 18:05:56442void ExtensionFunctionDispatcher::HandleRequest(
443 const ViewHostMsg_DomMessage_Params& params) {
[email protected]32dda362009-06-05 19:07:01444 scoped_refptr<ExtensionFunction> function(
[email protected]8b8e7c92010-08-19 18:05:56445 FactoryRegistry::instance()->NewFunction(params.name));
[email protected]32dda362009-06-05 19:07:01446 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43447 function->set_profile(profile_);
448 function->set_extension_id(extension_id());
[email protected]8b8e7c92010-08-19 18:05:56449 function->SetArgs(&params.arguments);
450 function->set_source_url(params.source_url);
451 function->set_request_id(params.request_id);
452 function->set_has_callback(params.has_callback);
453 function->set_user_gesture(params.user_gesture);
[email protected]d13950e2009-12-04 01:43:02454 ExtensionsService* service = profile()->GetExtensionsService();
455 DCHECK(service);
[email protected]cb0ce1e022010-03-10 19:54:41456 Extension* extension = service->GetExtensionById(extension_id(), false);
457 DCHECK(extension);
[email protected]bc535ee52010-08-31 18:40:32458 function->set_include_incognito(service->IsIncognitoEnabled(extension) &&
459 !extension->incognito_split_mode());
[email protected]cb0ce1e022010-03-10 19:54:41460
[email protected]583d45c12010-08-31 02:48:12461 if (!service->ExtensionBindingsAllowed(function->source_url()) ||
[email protected]246c05f2010-09-10 09:12:11462 !extension->HasApiPermission(function->name())) {
[email protected]583d45c12010-08-31 02:48:12463 render_view_host_->BlockExtensionRequest(function->request_id());
464 return;
465 }
466
[email protected]d13950e2009-12-04 01:43:02467 ExtensionsQuotaService* quota = service->quota_service();
[email protected]8b8e7c92010-08-19 18:05:56468 if (quota->Assess(extension_id(), function, &params.arguments,
469 base::TimeTicks::Now())) {
[email protected]d070ec62010-07-27 21:28:26470 // See crbug.com/39178.
471 ExternalProtocolHandler::PermitLaunchUrl();
472
[email protected]d13950e2009-12-04 01:43:02473 function->Run();
474 } else {
475 render_view_host_->SendExtensionResponse(function->request_id(), false,
476 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
477 }
[email protected]bfdffe2b2009-04-24 22:05:35478}
479
[email protected]c6619182009-05-12 14:59:32480void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
481 bool success) {
[email protected]c6619182009-05-12 14:59:32482 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33483 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35484}
485
486void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14487 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45488 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35489 " : terminating renderer.";
490 if (RenderProcessHost::run_renderer_in_process()) {
491 // In single process mode it is better if we don't suicide but just crash.
492 CHECK(false);
493 } else {
494 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46495 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35496 ResultCodes::KILLED_BAD_MESSAGE, false);
497 }
498}
499
500Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58501 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35502}