blob: 78a2c0321b38d48810bf697ca27f1957d6eb3fc1 [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"
9#include "base/values.h"
[email protected]a95631cb2009-12-10 01:59:1110#include "chrome/browser/browser.h"
[email protected]0ec92032010-03-24 19:59:4111#include "chrome/browser/browser_list.h"
[email protected]a95631cb2009-12-10 01:59:1112#include "chrome/browser/browser_window.h"
[email protected]912256b32009-09-18 09:47:3513#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5614#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]9dd97bc2010-01-14 01:40:0415#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3516#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]f93914852009-05-26 06:05:4017#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
[email protected]ec9ac0df2009-10-01 18:06:4718#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]446255952010-03-17 20:41:5819#include "chrome/browser/extensions/extension_clipboard_api.h"
[email protected]2e3b5202010-03-23 06:52:4120#include "chrome/browser/extensions/extension_context_menu_api.h"
[email protected]b27257562009-11-16 23:28:2621#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]bfdffe2b2009-04-24 22:05:3522#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0123#include "chrome/browser/extensions/extension_history_api.h"
[email protected]f5205412010-03-16 00:19:3424#include "chrome/browser/extensions/extension_idle_api.h"
[email protected]198bcfe2009-09-09 22:56:2825#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]f34e79632010-03-17 02:34:0826#include "chrome/browser/extensions/extension_infobar_module.h"
[email protected]e916901c2009-05-07 00:14:3127#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4328#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]f7f3a5f2009-05-01 22:02:3429#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]f93914852009-05-26 06:05:4030#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
[email protected]1c1c77a52009-11-03 00:37:3131#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5832#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3533#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3534#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4035#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1436#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4337#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]d13950e2009-12-04 01:43:0238#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5639#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3140#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3541#include "chrome/browser/renderer_host/render_process_host.h"
42#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1943#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3544#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4345#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3546
47// FactoryRegistry -------------------------------------------------------------
48
49namespace {
50
[email protected]b83e4602009-05-15 22:58:3351// Template for defining ExtensionFunctionFactory.
52template<class T>
53ExtensionFunction* NewExtensionFunction() {
54 return new T();
55}
[email protected]bfdffe2b2009-04-24 22:05:3556
[email protected]b83e4602009-05-15 22:58:3357// Contains a list of all known extension functions and allows clients to
58// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3559class FactoryRegistry {
60 public:
61 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3362 FactoryRegistry() { ResetFunctions(); }
63
64 // Resets all functions to their default values.
65 void ResetFunctions();
66
67 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3568 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3369
70 // Allows overriding of specific functions (e.g. for testing). Functions
71 // must be previously registered. Returns true if successful.
72 bool OverrideFunction(const std::string& name,
73 ExtensionFunctionFactory factory);
74
75 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3576 ExtensionFunction* NewFunction(const std::string& name);
77
78 private:
[email protected]61424c062009-10-14 23:14:5979 template<class T>
80 void RegisterFunction() {
81 factories_[T::function_name()] = &NewExtensionFunction<T>;
82 }
83
[email protected]bfdffe2b2009-04-24 22:05:3584 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
85 FactoryMap factories_;
86};
87
[email protected]bfdffe2b2009-04-24 22:05:3588FactoryRegistry* FactoryRegistry::instance() {
89 return Singleton<FactoryRegistry>::get();
90}
91
[email protected]b83e4602009-05-15 22:58:3392void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3593 // Register all functions here.
94
[email protected]e515f5d2009-05-05 03:05:0095 // Windows
[email protected]61424c062009-10-14 23:14:5996 RegisterFunction<GetWindowFunction>();
97 RegisterFunction<GetCurrentWindowFunction>();
98 RegisterFunction<GetLastFocusedWindowFunction>();
99 RegisterFunction<GetAllWindowsFunction>();
100 RegisterFunction<CreateWindowFunction>();
101 RegisterFunction<UpdateWindowFunction>();
102 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:33103
[email protected]e515f5d2009-05-05 03:05:00104 // Tabs
[email protected]61424c062009-10-14 23:14:59105 RegisterFunction<GetTabFunction>();
106 RegisterFunction<GetSelectedTabFunction>();
107 RegisterFunction<GetAllTabsInWindowFunction>();
108 RegisterFunction<CreateTabFunction>();
109 RegisterFunction<UpdateTabFunction>();
110 RegisterFunction<MoveTabFunction>();
111 RegisterFunction<RemoveTabFunction>();
112 RegisterFunction<DetectTabLanguageFunction>();
113 RegisterFunction<CaptureVisibleTabFunction>();
114 RegisterFunction<TabsExecuteScriptFunction>();
115 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35116
[email protected]f7f3a5f2009-05-01 22:02:34117 // Page Actions.
[email protected]61424c062009-10-14 23:14:59118 RegisterFunction<EnablePageActionFunction>();
119 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46120 RegisterFunction<PageActionShowFunction>();
121 RegisterFunction<PageActionHideFunction>();
122 RegisterFunction<PageActionSetIconFunction>();
123 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29124 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34125
[email protected]ec9ac0df2009-10-01 18:06:47126 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59127 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24128 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59129 RegisterFunction<BrowserActionSetBadgeTextFunction>();
130 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50131 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47132
[email protected]f7f3a5f2009-05-01 22:02:34133 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59134 RegisterFunction<GetBookmarksFunction>();
135 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55136 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59137 RegisterFunction<GetBookmarkTreeFunction>();
138 RegisterFunction<SearchBookmarksFunction>();
139 RegisterFunction<RemoveBookmarkFunction>();
140 RegisterFunction<RemoveTreeBookmarkFunction>();
141 RegisterFunction<CreateBookmarkFunction>();
142 RegisterFunction<MoveBookmarkFunction>();
143 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43144
[email protected]f34e79632010-03-17 02:34:08145 // Infobars.
146 RegisterFunction<ShowInfoBarFunction>();
147
[email protected]9dd97bc2010-01-14 01:40:04148 // BookmarkManager
149 RegisterFunction<CopyBookmarkManagerFunction>();
150 RegisterFunction<CutBookmarkManagerFunction>();
151 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57152 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56153 RegisterFunction<ImportBookmarksFunction>();
154 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18155 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04156 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16157 RegisterFunction<StartDragBookmarkManagerFunction>();
158 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31159 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04160
[email protected]de768a832009-10-30 05:25:01161 // History
162 RegisterFunction<AddUrlHistoryFunction>();
163 RegisterFunction<DeleteAllHistoryFunction>();
164 RegisterFunction<DeleteRangeHistoryFunction>();
165 RegisterFunction<DeleteUrlHistoryFunction>();
166 RegisterFunction<GetVisitsHistoryFunction>();
167 RegisterFunction<SearchHistoryFunction>();
168
[email protected]f5205412010-03-16 00:19:34169 // Idle
170 RegisterFunction<ExtensionIdleQueryStateFunction>();
171
[email protected]9c45b7182009-08-04 16:44:43172 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59173 RegisterFunction<ToolstripExpandFunction>();
174 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14175
[email protected]198bcfe2009-09-09 22:56:28176 // I18N.
[email protected]61424c062009-10-14 23:14:59177 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28178
[email protected]1c1c77a52009-11-03 00:37:31179 // Popup API.
180 RegisterFunction<PopupShowFunction>();
181
[email protected]381162b2010-01-28 17:29:35182 // Processes.
183 RegisterFunction<GetProcessForTabFunction>();
184
[email protected]438772df2010-02-26 18:08:43185 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34186 RegisterFunction<MetricsRecordUserActionFunction>();
187 RegisterFunction<MetricsRecordValueFunction>();
188 RegisterFunction<MetricsRecordPercentageFunction>();
189 RegisterFunction<MetricsRecordCountFunction>();
190 RegisterFunction<MetricsRecordSmallCountFunction>();
191 RegisterFunction<MetricsRecordMediumCountFunction>();
192 RegisterFunction<MetricsRecordTimeFunction>();
193 RegisterFunction<MetricsRecordMediumTimeFunction>();
194 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43195
[email protected]25fd1b2e2009-08-17 20:57:14196 // Test.
[email protected]61424c062009-10-14 23:14:59197 RegisterFunction<ExtensionTestPassFunction>();
198 RegisterFunction<ExtensionTestFailFunction>();
199 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02200 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50201 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]5cbe1e22010-01-30 01:18:56202
203 // Accessibility.
204 RegisterFunction<GetFocusedControlFunction>();
205 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58206
207 // Clipboard.
208 RegisterFunction<ExecuteCopyClipboardFunction>();
209 RegisterFunction<ExecuteCutClipboardFunction>();
210 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41211
212 // Context Menus.
213 RegisterFunction<CreateContextMenuFunction>();
214 RegisterFunction<RemoveContextMenuFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35215}
216
[email protected]b83e4602009-05-15 22:58:33217void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
218 for (FactoryMap::iterator iter = factories_.begin();
219 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35220 names->push_back(iter->first);
221 }
222}
223
[email protected]b83e4602009-05-15 22:58:33224bool FactoryRegistry::OverrideFunction(const std::string& name,
225 ExtensionFunctionFactory factory) {
226 FactoryMap::iterator iter = factories_.find(name);
227 if (iter == factories_.end()) {
228 return false;
229 } else {
230 iter->second = factory;
231 return true;
232 }
233}
234
[email protected]bfdffe2b2009-04-24 22:05:35235ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
236 FactoryMap::iterator iter = factories_.find(name);
237 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33238 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56239 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33240 return function;
[email protected]bfdffe2b2009-04-24 22:05:35241}
242
[email protected]b83e4602009-05-15 22:58:33243}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35244
[email protected]bfdffe2b2009-04-24 22:05:35245// ExtensionFunctionDispatcher -------------------------------------------------
246
247void ExtensionFunctionDispatcher::GetAllFunctionNames(
248 std::vector<std::string>* names) {
249 FactoryRegistry::instance()->GetAllNames(names);
250}
251
[email protected]b83e4602009-05-15 22:58:33252bool ExtensionFunctionDispatcher::OverrideFunction(
253 const std::string& name, ExtensionFunctionFactory factory) {
254 return FactoryRegistry::instance()->OverrideFunction(name, factory);
255}
256
257void ExtensionFunctionDispatcher::ResetFunctions() {
258 FactoryRegistry::instance()->ResetFunctions();
259}
260
[email protected]811bfe32009-07-01 08:46:25261std::set<ExtensionFunctionDispatcher*>*
262 ExtensionFunctionDispatcher::all_instances() {
263 static std::set<ExtensionFunctionDispatcher*> instances;
264 return &instances;
265}
266
[email protected]a91afcb2010-03-25 21:15:02267ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
268 RenderViewHost* render_view_host,
269 Delegate* delegate,
270 const GURL& url) {
271 ExtensionsService* service =
272 render_view_host->process()->profile()->GetExtensionsService();
273 DCHECK(service);
274
275 Extension* extension = service->GetExtensionByURL(url);
276 if (extension)
277 return new ExtensionFunctionDispatcher(render_view_host, delegate,
278 extension, url);
279 else
280 return NULL;
281}
282
[email protected]bfdffe2b2009-04-24 22:05:35283ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
284 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12285 Delegate* delegate,
[email protected]a91afcb2010-03-25 21:15:02286 Extension* extension,
[email protected]811bfe32009-07-01 08:46:25287 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30288 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58289 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12290 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25291 url_(url),
[email protected]32dda362009-06-05 19:07:01292 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43293 // TODO(erikkay) should we do something for these errors in Release?
294 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19295 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43296
[email protected]811bfe32009-07-01 08:46:25297 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35298
[email protected]45776222009-07-15 20:21:58299 // Notify the ExtensionProcessManager that the view was created.
300 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
301 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45302 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19303
[email protected]db7331a2010-02-25 22:10:50304 bool incognito_enabled =
[email protected]cb0ce1e022010-03-10 19:54:41305 profile()->GetExtensionsService()->IsIncognitoEnabled(extension);
[email protected]db7331a2010-02-25 22:10:50306
[email protected]35506352009-08-07 18:58:19307 // Update the extension permissions. Doing this each time we create an EFD
308 // ensures that new processes are informed of permissions for newly installed
309 // extensions.
[email protected]cccf90932009-08-23 17:56:25310 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19311 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25312 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
313 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50314 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
315 extension->id(), incognito_enabled));
[email protected]68f07912010-03-05 18:33:58316
317 NotificationService::current()->Notify(
318 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
319 Source<Profile>(profile_),
320 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35321}
322
[email protected]32dda362009-06-05 19:07:01323ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25324 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01325 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58326
327 NotificationService::current()->Notify(
328 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
329 Source<Profile>(profile_),
330 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01331}
332
[email protected]0ec92032010-03-24 19:59:41333Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
334 bool include_incognito) {
335 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12336
[email protected]0ec92032010-03-24 19:59:41337 // If the delegate has an associated browser and that browser is in the right
338 // incognito state, we can return it.
339 if (browser) {
340 if (include_incognito || !browser->profile()->IsOffTheRecord())
341 return browser;
342 }
[email protected]9c45b7182009-08-04 16:44:43343
[email protected]0ec92032010-03-24 19:59:41344 // Otherwise, try to default to a reasonable browser.
345 Profile* profile = render_view_host()->process()->profile();
346
347 // Make sure we don't return an incognito browser without proper access.
348 if (!include_incognito)
349 profile = profile->GetOriginalProfile();
350
[email protected]62b0b532010-03-26 22:44:31351 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_ANY,
352 include_incognito);
[email protected]0ec92032010-03-24 19:59:41353
[email protected]0ec92032010-03-24 19:59:41354 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
355 // a toolstrip or background_page onload chrome.tabs api call can make it
356 // into here before the browser is sufficiently initialized to return here.
357 // A similar situation may arise during shutdown.
358 // TODO(rafaelw): Delay creation of background_page until the browser
359 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
360 return browser;
[email protected]b27257562009-11-16 23:28:26361}
362
[email protected]c7ad50f2009-09-11 06:28:15363Extension* ExtensionFunctionDispatcher::GetExtension() {
364 ExtensionsService* service = profile()->GetExtensionsService();
365 DCHECK(service);
366
[email protected]61b411612009-11-10 23:17:41367 Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]c7ad50f2009-09-11 06:28:15368 DCHECK(extension);
369
370 return extension;
371}
372
[email protected]bfdffe2b2009-04-24 22:05:35373void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58374 const Value* args,
[email protected]c6619182009-05-12 14:59:32375 int request_id,
376 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01377 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35378 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01379 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33380 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32381 function->set_request_id(request_id);
382 function->set_has_callback(has_callback);
[email protected]d13950e2009-12-04 01:43:02383 ExtensionsService* service = profile()->GetExtensionsService();
384 DCHECK(service);
[email protected]cb0ce1e022010-03-10 19:54:41385 Extension* extension = service->GetExtensionById(extension_id(), false);
386 DCHECK(extension);
387 function->set_include_incognito(service->IsIncognitoEnabled(extension));
388
[email protected]d13950e2009-12-04 01:43:02389 ExtensionsQuotaService* quota = service->quota_service();
390 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
391 function->Run();
392 } else {
393 render_view_host_->SendExtensionResponse(function->request_id(), false,
394 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
395 }
[email protected]bfdffe2b2009-04-24 22:05:35396}
397
[email protected]c6619182009-05-12 14:59:32398void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
399 bool success) {
[email protected]c6619182009-05-12 14:59:32400 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33401 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35402}
403
404void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14405 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45406 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35407 " : terminating renderer.";
408 if (RenderProcessHost::run_renderer_in_process()) {
409 // In single process mode it is better if we don't suicide but just crash.
410 CHECK(false);
411 } else {
412 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46413 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35414 ResultCodes::KILLED_BAD_MESSAGE, false);
415 }
416}
417
418Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58419 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35420}