blob: 8791bb0551c8ce93fc8099f8f1cc3e63574b8bfe [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]bfdffe2b2009-04-24 22:05:357#include "base/process_util.h"
8#include "base/singleton.h"
[email protected]584b8e3f2010-04-10 00:23:379#include "base/ref_counted.h"
[email protected]bfdffe2b2009-04-24 22:05:3510#include "base/values.h"
[email protected]a95631cb2009-12-10 01:59:1111#include "chrome/browser/browser.h"
[email protected]0ec92032010-03-24 19:59:4112#include "chrome/browser/browser_list.h"
[email protected]a95631cb2009-12-10 01:59:1113#include "chrome/browser/browser_window.h"
[email protected]584b8e3f2010-04-10 00:23:3714#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
15#include "chrome/browser/dom_ui/dom_ui_favicon_source.h"
[email protected]912256b32009-09-18 09:47:3516#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5617#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]9dd97bc2010-01-14 01:40:0418#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3519#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]f93914852009-05-26 06:05:4020#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
[email protected]ec9ac0df2009-10-01 18:06:4721#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]446255952010-03-17 20:41:5822#include "chrome/browser/extensions/extension_clipboard_api.h"
[email protected]2e3b5202010-03-23 06:52:4123#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]b27257562009-11-16 23:28:2624#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]bfdffe2b2009-04-24 22:05:3525#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0126#include "chrome/browser/extensions/extension_history_api.h"
[email protected]f5205412010-03-16 00:19:3427#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]198bcfe2009-09-09 22:56:2828#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]f34e79632010-03-17 02:34:0829#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]e916901c2009-05-07 00:14:3130#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4331#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]f7f3a5f2009-05-01 22:02:3432#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]f93914852009-05-26 06:05:4033#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
[email protected]1c1c77a52009-11-03 00:37:3134#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5835#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3536#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3537#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4038#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1439#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4340#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]d13950e2009-12-04 01:43:0241#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5642#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3143#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3544#include "chrome/browser/renderer_host/render_process_host.h"
45#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1946#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3547#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4348#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3549
50// FactoryRegistry -------------------------------------------------------------
51
52namespace {
53
[email protected]b83e4602009-05-15 22:58:3354// Template for defining ExtensionFunctionFactory.
55template<class T>
56ExtensionFunction* NewExtensionFunction() {
57 return new T();
58}
[email protected]bfdffe2b2009-04-24 22:05:3559
[email protected]b83e4602009-05-15 22:58:3360// Contains a list of all known extension functions and allows clients to
61// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3562class FactoryRegistry {
63 public:
64 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3365 FactoryRegistry() { ResetFunctions(); }
66
67 // Resets all functions to their default values.
68 void ResetFunctions();
69
70 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3571 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3372
73 // Allows overriding of specific functions (e.g. for testing). Functions
74 // must be previously registered. Returns true if successful.
75 bool OverrideFunction(const std::string& name,
76 ExtensionFunctionFactory factory);
77
78 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3579 ExtensionFunction* NewFunction(const std::string& name);
80
81 private:
[email protected]61424c062009-10-14 23:14:5982 template<class T>
83 void RegisterFunction() {
84 factories_[T::function_name()] = &NewExtensionFunction<T>;
85 }
86
[email protected]bfdffe2b2009-04-24 22:05:3587 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
88 FactoryMap factories_;
89};
90
[email protected]bfdffe2b2009-04-24 22:05:3591FactoryRegistry* FactoryRegistry::instance() {
92 return Singleton<FactoryRegistry>::get();
93}
94
[email protected]b83e4602009-05-15 22:58:3395void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3596 // Register all functions here.
97
[email protected]e515f5d2009-05-05 03:05:0098 // Windows
[email protected]61424c062009-10-14 23:14:5999 RegisterFunction<GetWindowFunction>();
100 RegisterFunction<GetCurrentWindowFunction>();
101 RegisterFunction<GetLastFocusedWindowFunction>();
102 RegisterFunction<GetAllWindowsFunction>();
103 RegisterFunction<CreateWindowFunction>();
104 RegisterFunction<UpdateWindowFunction>();
105 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33106
[email protected]e515f5d2009-05-05 03:05:00107 // Tabs
[email protected]61424c062009-10-14 23:14:59108 RegisterFunction<GetTabFunction>();
109 RegisterFunction<GetSelectedTabFunction>();
110 RegisterFunction<GetAllTabsInWindowFunction>();
111 RegisterFunction<CreateTabFunction>();
112 RegisterFunction<UpdateTabFunction>();
113 RegisterFunction<MoveTabFunction>();
114 RegisterFunction<RemoveTabFunction>();
115 RegisterFunction<DetectTabLanguageFunction>();
116 RegisterFunction<CaptureVisibleTabFunction>();
117 RegisterFunction<TabsExecuteScriptFunction>();
118 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35119
[email protected]f7f3a5f2009-05-01 22:02:34120 // Page Actions.
[email protected]61424c062009-10-14 23:14:59121 RegisterFunction<EnablePageActionFunction>();
122 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46123 RegisterFunction<PageActionShowFunction>();
124 RegisterFunction<PageActionHideFunction>();
125 RegisterFunction<PageActionSetIconFunction>();
126 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29127 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34128
[email protected]ec9ac0df2009-10-01 18:06:47129 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59130 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24131 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59132 RegisterFunction<BrowserActionSetBadgeTextFunction>();
133 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50134 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47135
[email protected]f7f3a5f2009-05-01 22:02:34136 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59137 RegisterFunction<GetBookmarksFunction>();
138 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55139 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59140 RegisterFunction<GetBookmarkTreeFunction>();
141 RegisterFunction<SearchBookmarksFunction>();
142 RegisterFunction<RemoveBookmarkFunction>();
143 RegisterFunction<RemoveTreeBookmarkFunction>();
144 RegisterFunction<CreateBookmarkFunction>();
145 RegisterFunction<MoveBookmarkFunction>();
146 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43147
[email protected]f34e79632010-03-17 02:34:08148 // Infobars.
149 RegisterFunction<ShowInfoBarFunction>();
150
[email protected]9dd97bc2010-01-14 01:40:04151 // BookmarkManager
152 RegisterFunction<CopyBookmarkManagerFunction>();
153 RegisterFunction<CutBookmarkManagerFunction>();
154 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57155 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56156 RegisterFunction<ImportBookmarksFunction>();
157 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18158 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04159 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16160 RegisterFunction<StartDragBookmarkManagerFunction>();
161 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31162 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04163
[email protected]de768a832009-10-30 05:25:01164 // History
165 RegisterFunction<AddUrlHistoryFunction>();
166 RegisterFunction<DeleteAllHistoryFunction>();
167 RegisterFunction<DeleteRangeHistoryFunction>();
168 RegisterFunction<DeleteUrlHistoryFunction>();
169 RegisterFunction<GetVisitsHistoryFunction>();
170 RegisterFunction<SearchHistoryFunction>();
171
[email protected]f5205412010-03-16 00:19:34172 // Idle
173 RegisterFunction<ExtensionIdleQueryStateFunction>();
174
[email protected]9c45b7182009-08-04 16:44:43175 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59176 RegisterFunction<ToolstripExpandFunction>();
177 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14178
[email protected]198bcfe2009-09-09 22:56:28179 // I18N.
[email protected]61424c062009-10-14 23:14:59180 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28181
[email protected]1c1c77a52009-11-03 00:37:31182 // Popup API.
183 RegisterFunction<PopupShowFunction>();
184
[email protected]381162b2010-01-28 17:29:35185 // Processes.
186 RegisterFunction<GetProcessForTabFunction>();
187
[email protected]438772df2010-02-26 18:08:43188 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34189 RegisterFunction<MetricsRecordUserActionFunction>();
190 RegisterFunction<MetricsRecordValueFunction>();
191 RegisterFunction<MetricsRecordPercentageFunction>();
192 RegisterFunction<MetricsRecordCountFunction>();
193 RegisterFunction<MetricsRecordSmallCountFunction>();
194 RegisterFunction<MetricsRecordMediumCountFunction>();
195 RegisterFunction<MetricsRecordTimeFunction>();
196 RegisterFunction<MetricsRecordMediumTimeFunction>();
197 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43198
[email protected]25fd1b2e2009-08-17 20:57:14199 // Test.
[email protected]61424c062009-10-14 23:14:59200 RegisterFunction<ExtensionTestPassFunction>();
201 RegisterFunction<ExtensionTestFailFunction>();
202 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02203 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50204 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]5cbe1e22010-01-30 01:18:56205
206 // Accessibility.
207 RegisterFunction<GetFocusedControlFunction>();
208 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58209
210 // Clipboard.
211 RegisterFunction<ExecuteCopyClipboardFunction>();
212 RegisterFunction<ExecuteCutClipboardFunction>();
213 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41214
215 // Context Menus.
216 RegisterFunction<CreateContextMenuFunction>();
217 RegisterFunction<RemoveContextMenuFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35218}
219
[email protected]b83e4602009-05-15 22:58:33220void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
221 for (FactoryMap::iterator iter = factories_.begin();
222 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35223 names->push_back(iter->first);
224 }
225}
226
[email protected]b83e4602009-05-15 22:58:33227bool FactoryRegistry::OverrideFunction(const std::string& name,
228 ExtensionFunctionFactory factory) {
229 FactoryMap::iterator iter = factories_.find(name);
230 if (iter == factories_.end()) {
231 return false;
232 } else {
233 iter->second = factory;
234 return true;
235 }
236}
237
[email protected]bfdffe2b2009-04-24 22:05:35238ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
239 FactoryMap::iterator iter = factories_.find(name);
240 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33241 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56242 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33243 return function;
[email protected]bfdffe2b2009-04-24 22:05:35244}
245
[email protected]b83e4602009-05-15 22:58:33246}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35247
[email protected]bfdffe2b2009-04-24 22:05:35248// ExtensionFunctionDispatcher -------------------------------------------------
249
250void ExtensionFunctionDispatcher::GetAllFunctionNames(
251 std::vector<std::string>* names) {
252 FactoryRegistry::instance()->GetAllNames(names);
253}
254
[email protected]b83e4602009-05-15 22:58:33255bool ExtensionFunctionDispatcher::OverrideFunction(
256 const std::string& name, ExtensionFunctionFactory factory) {
257 return FactoryRegistry::instance()->OverrideFunction(name, factory);
258}
259
260void ExtensionFunctionDispatcher::ResetFunctions() {
261 FactoryRegistry::instance()->ResetFunctions();
262}
263
[email protected]811bfe32009-07-01 08:46:25264std::set<ExtensionFunctionDispatcher*>*
265 ExtensionFunctionDispatcher::all_instances() {
266 static std::set<ExtensionFunctionDispatcher*> instances;
267 return &instances;
268}
269
[email protected]a91afcb2010-03-25 21:15:02270ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
271 RenderViewHost* render_view_host,
272 Delegate* delegate,
273 const GURL& url) {
274 ExtensionsService* service =
275 render_view_host->process()->profile()->GetExtensionsService();
276 DCHECK(service);
277
278 Extension* extension = service->GetExtensionByURL(url);
279 if (extension)
280 return new ExtensionFunctionDispatcher(render_view_host, delegate,
281 extension, url);
282 else
283 return NULL;
284}
285
[email protected]bfdffe2b2009-04-24 22:05:35286ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
287 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12288 Delegate* delegate,
[email protected]a91afcb2010-03-25 21:15:02289 Extension* extension,
[email protected]811bfe32009-07-01 08:46:25290 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30291 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58292 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12293 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25294 url_(url),
[email protected]32dda362009-06-05 19:07:01295 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43296 // TODO(erikkay) should we do something for these errors in Release?
297 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19298 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43299
[email protected]811bfe32009-07-01 08:46:25300 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35301
[email protected]45776222009-07-15 20:21:58302 // Notify the ExtensionProcessManager that the view was created.
303 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
304 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45305 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19306
[email protected]db7331a2010-02-25 22:10:50307 bool incognito_enabled =
[email protected]cb0ce1e022010-03-10 19:54:41308 profile()->GetExtensionsService()->IsIncognitoEnabled(extension);
[email protected]db7331a2010-02-25 22:10:50309
[email protected]584b8e3f2010-04-10 00:23:37310 // If the extension has permission to load chrome://favicon/ resources we need
311 // to make sure that the DOMUIFavIconSource is registered with the
312 // ChromeURLDataManager.
313 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
314 DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_);
315 ChromeThread::PostTask(
316 ChromeThread::IO, FROM_HERE,
317 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(),
318 &ChromeURLDataManager::AddDataSource,
319 make_scoped_refptr(favicon_source)));
320 }
321
[email protected]35506352009-08-07 18:58:19322 // Update the extension permissions. Doing this each time we create an EFD
323 // ensures that new processes are informed of permissions for newly installed
324 // extensions.
[email protected]cccf90932009-08-23 17:56:25325 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19326 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25327 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
328 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50329 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
330 extension->id(), incognito_enabled));
[email protected]68f07912010-03-05 18:33:58331
332 NotificationService::current()->Notify(
333 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
334 Source<Profile>(profile_),
335 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35336}
337
[email protected]32dda362009-06-05 19:07:01338ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25339 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01340 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58341
342 NotificationService::current()->Notify(
343 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
344 Source<Profile>(profile_),
345 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01346}
347
[email protected]0ec92032010-03-24 19:59:41348Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
349 bool include_incognito) {
350 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12351
[email protected]0ec92032010-03-24 19:59:41352 // If the delegate has an associated browser and that browser is in the right
353 // incognito state, we can return it.
354 if (browser) {
355 if (include_incognito || !browser->profile()->IsOffTheRecord())
356 return browser;
357 }
[email protected]9c45b7182009-08-04 16:44:43358
[email protected]0ec92032010-03-24 19:59:41359 // Otherwise, try to default to a reasonable browser.
360 Profile* profile = render_view_host()->process()->profile();
361
362 // Make sure we don't return an incognito browser without proper access.
363 if (!include_incognito)
364 profile = profile->GetOriginalProfile();
365
[email protected]62b0b532010-03-26 22:44:31366 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_ANY,
367 include_incognito);
[email protected]0ec92032010-03-24 19:59:41368
[email protected]0ec92032010-03-24 19:59:41369 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
370 // a toolstrip or background_page onload chrome.tabs api call can make it
371 // into here before the browser is sufficiently initialized to return here.
372 // A similar situation may arise during shutdown.
373 // TODO(rafaelw): Delay creation of background_page until the browser
374 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
375 return browser;
[email protected]b27257562009-11-16 23:28:26376}
377
[email protected]bfdffe2b2009-04-24 22:05:35378void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58379 const Value* args,
[email protected]bb64b512010-04-02 21:01:39380 const GURL& source_url,
[email protected]c6619182009-05-12 14:59:32381 int request_id,
382 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01383 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35384 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01385 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43386 function->set_profile(profile_);
387 function->set_extension_id(extension_id());
[email protected]b83e4602009-05-15 22:58:33388 function->SetArgs(args);
[email protected]bb64b512010-04-02 21:01:39389 function->set_source_url(source_url);
[email protected]c6619182009-05-12 14:59:32390 function->set_request_id(request_id);
391 function->set_has_callback(has_callback);
[email protected]d13950e2009-12-04 01:43:02392 ExtensionsService* service = profile()->GetExtensionsService();
393 DCHECK(service);
[email protected]cb0ce1e022010-03-10 19:54:41394 Extension* extension = service->GetExtensionById(extension_id(), false);
395 DCHECK(extension);
396 function->set_include_incognito(service->IsIncognitoEnabled(extension));
397
[email protected]d13950e2009-12-04 01:43:02398 ExtensionsQuotaService* quota = service->quota_service();
399 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
400 function->Run();
401 } else {
402 render_view_host_->SendExtensionResponse(function->request_id(), false,
403 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
404 }
[email protected]bfdffe2b2009-04-24 22:05:35405}
406
[email protected]c6619182009-05-12 14:59:32407void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
408 bool success) {
[email protected]c6619182009-05-12 14:59:32409 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33410 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35411}
412
413void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14414 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45415 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35416 " : terminating renderer.";
417 if (RenderProcessHost::run_renderer_in_process()) {
418 // In single process mode it is better if we don't suicide but just crash.
419 CHECK(false);
420 } else {
421 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46422 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35423 ResultCodes::KILLED_BAD_MESSAGE, false);
424 }
425}
426
427Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58428 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35429}