blob: e788c80f218a4b5fc3a31220c0fb86241b056a02 [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>();
[email protected]e3eafb292010-04-14 21:30:41109 RegisterFunction<GetCurrentTabFunction>();
[email protected]61424c062009-10-14 23:14:59110 RegisterFunction<GetSelectedTabFunction>();
111 RegisterFunction<GetAllTabsInWindowFunction>();
112 RegisterFunction<CreateTabFunction>();
113 RegisterFunction<UpdateTabFunction>();
114 RegisterFunction<MoveTabFunction>();
115 RegisterFunction<RemoveTabFunction>();
116 RegisterFunction<DetectTabLanguageFunction>();
117 RegisterFunction<CaptureVisibleTabFunction>();
118 RegisterFunction<TabsExecuteScriptFunction>();
119 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35120
[email protected]f7f3a5f2009-05-01 22:02:34121 // Page Actions.
[email protected]61424c062009-10-14 23:14:59122 RegisterFunction<EnablePageActionFunction>();
123 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46124 RegisterFunction<PageActionShowFunction>();
125 RegisterFunction<PageActionHideFunction>();
126 RegisterFunction<PageActionSetIconFunction>();
127 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29128 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34129
[email protected]ec9ac0df2009-10-01 18:06:47130 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59131 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24132 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59133 RegisterFunction<BrowserActionSetBadgeTextFunction>();
134 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50135 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47136
[email protected]f7f3a5f2009-05-01 22:02:34137 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59138 RegisterFunction<GetBookmarksFunction>();
139 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55140 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59141 RegisterFunction<GetBookmarkTreeFunction>();
142 RegisterFunction<SearchBookmarksFunction>();
143 RegisterFunction<RemoveBookmarkFunction>();
144 RegisterFunction<RemoveTreeBookmarkFunction>();
145 RegisterFunction<CreateBookmarkFunction>();
146 RegisterFunction<MoveBookmarkFunction>();
147 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43148
[email protected]f34e79632010-03-17 02:34:08149 // Infobars.
150 RegisterFunction<ShowInfoBarFunction>();
151
[email protected]9dd97bc2010-01-14 01:40:04152 // BookmarkManager
153 RegisterFunction<CopyBookmarkManagerFunction>();
154 RegisterFunction<CutBookmarkManagerFunction>();
155 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57156 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56157 RegisterFunction<ImportBookmarksFunction>();
158 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18159 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04160 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16161 RegisterFunction<StartDragBookmarkManagerFunction>();
162 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9b071852010-04-02 06:45:31163 RegisterFunction<GetSubtreeBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04164
[email protected]de768a832009-10-30 05:25:01165 // History
166 RegisterFunction<AddUrlHistoryFunction>();
167 RegisterFunction<DeleteAllHistoryFunction>();
168 RegisterFunction<DeleteRangeHistoryFunction>();
169 RegisterFunction<DeleteUrlHistoryFunction>();
170 RegisterFunction<GetVisitsHistoryFunction>();
171 RegisterFunction<SearchHistoryFunction>();
172
[email protected]f5205412010-03-16 00:19:34173 // Idle
174 RegisterFunction<ExtensionIdleQueryStateFunction>();
175
[email protected]9c45b7182009-08-04 16:44:43176 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59177 RegisterFunction<ToolstripExpandFunction>();
178 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14179
[email protected]198bcfe2009-09-09 22:56:28180 // I18N.
[email protected]61424c062009-10-14 23:14:59181 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28182
[email protected]1c1c77a52009-11-03 00:37:31183 // Popup API.
184 RegisterFunction<PopupShowFunction>();
185
[email protected]381162b2010-01-28 17:29:35186 // Processes.
187 RegisterFunction<GetProcessForTabFunction>();
188
[email protected]438772df2010-02-26 18:08:43189 // Metrics.
[email protected]cf25e4d2010-03-12 21:19:34190 RegisterFunction<MetricsRecordUserActionFunction>();
191 RegisterFunction<MetricsRecordValueFunction>();
192 RegisterFunction<MetricsRecordPercentageFunction>();
193 RegisterFunction<MetricsRecordCountFunction>();
194 RegisterFunction<MetricsRecordSmallCountFunction>();
195 RegisterFunction<MetricsRecordMediumCountFunction>();
196 RegisterFunction<MetricsRecordTimeFunction>();
197 RegisterFunction<MetricsRecordMediumTimeFunction>();
198 RegisterFunction<MetricsRecordLongTimeFunction>();
[email protected]438772df2010-02-26 18:08:43199
[email protected]25fd1b2e2009-08-17 20:57:14200 // Test.
[email protected]61424c062009-10-14 23:14:59201 RegisterFunction<ExtensionTestPassFunction>();
202 RegisterFunction<ExtensionTestFailFunction>();
203 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02204 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50205 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]5cbe1e22010-01-30 01:18:56206
207 // Accessibility.
208 RegisterFunction<GetFocusedControlFunction>();
209 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]446255952010-03-17 20:41:58210
211 // Clipboard.
212 RegisterFunction<ExecuteCopyClipboardFunction>();
213 RegisterFunction<ExecuteCutClipboardFunction>();
214 RegisterFunction<ExecutePasteClipboardFunction>();
[email protected]2e3b5202010-03-23 06:52:41215
216 // Context Menus.
217 RegisterFunction<CreateContextMenuFunction>();
218 RegisterFunction<RemoveContextMenuFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35219}
220
[email protected]b83e4602009-05-15 22:58:33221void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
222 for (FactoryMap::iterator iter = factories_.begin();
223 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35224 names->push_back(iter->first);
225 }
226}
227
[email protected]b83e4602009-05-15 22:58:33228bool FactoryRegistry::OverrideFunction(const std::string& name,
229 ExtensionFunctionFactory factory) {
230 FactoryMap::iterator iter = factories_.find(name);
231 if (iter == factories_.end()) {
232 return false;
233 } else {
234 iter->second = factory;
235 return true;
236 }
237}
238
[email protected]bfdffe2b2009-04-24 22:05:35239ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
240 FactoryMap::iterator iter = factories_.find(name);
241 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33242 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56243 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33244 return function;
[email protected]bfdffe2b2009-04-24 22:05:35245}
246
[email protected]b83e4602009-05-15 22:58:33247}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35248
[email protected]bfdffe2b2009-04-24 22:05:35249// ExtensionFunctionDispatcher -------------------------------------------------
250
251void ExtensionFunctionDispatcher::GetAllFunctionNames(
252 std::vector<std::string>* names) {
253 FactoryRegistry::instance()->GetAllNames(names);
254}
255
[email protected]b83e4602009-05-15 22:58:33256bool ExtensionFunctionDispatcher::OverrideFunction(
257 const std::string& name, ExtensionFunctionFactory factory) {
258 return FactoryRegistry::instance()->OverrideFunction(name, factory);
259}
260
261void ExtensionFunctionDispatcher::ResetFunctions() {
262 FactoryRegistry::instance()->ResetFunctions();
263}
264
[email protected]811bfe32009-07-01 08:46:25265std::set<ExtensionFunctionDispatcher*>*
266 ExtensionFunctionDispatcher::all_instances() {
267 static std::set<ExtensionFunctionDispatcher*> instances;
268 return &instances;
269}
270
[email protected]a91afcb2010-03-25 21:15:02271ExtensionFunctionDispatcher* ExtensionFunctionDispatcher::Create(
272 RenderViewHost* render_view_host,
273 Delegate* delegate,
274 const GURL& url) {
275 ExtensionsService* service =
276 render_view_host->process()->profile()->GetExtensionsService();
277 DCHECK(service);
278
279 Extension* extension = service->GetExtensionByURL(url);
280 if (extension)
281 return new ExtensionFunctionDispatcher(render_view_host, delegate,
282 extension, url);
283 else
284 return NULL;
285}
286
[email protected]bfdffe2b2009-04-24 22:05:35287ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
288 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12289 Delegate* delegate,
[email protected]a91afcb2010-03-25 21:15:02290 Extension* extension,
[email protected]811bfe32009-07-01 08:46:25291 const GURL& url)
[email protected]ebc1b682010-03-06 00:22:30292 : profile_(render_view_host->process()->profile()),
[email protected]68f07912010-03-05 18:33:58293 render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12294 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25295 url_(url),
[email protected]32dda362009-06-05 19:07:01296 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43297 // TODO(erikkay) should we do something for these errors in Release?
298 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19299 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43300
[email protected]811bfe32009-07-01 08:46:25301 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35302
[email protected]45776222009-07-15 20:21:58303 // Notify the ExtensionProcessManager that the view was created.
304 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
305 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45306 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19307
[email protected]db7331a2010-02-25 22:10:50308 bool incognito_enabled =
[email protected]cb0ce1e022010-03-10 19:54:41309 profile()->GetExtensionsService()->IsIncognitoEnabled(extension);
[email protected]db7331a2010-02-25 22:10:50310
[email protected]584b8e3f2010-04-10 00:23:37311 // If the extension has permission to load chrome://favicon/ resources we need
312 // to make sure that the DOMUIFavIconSource is registered with the
313 // ChromeURLDataManager.
314 if (extension->HasHostPermission(GURL(chrome::kChromeUIFavIconURL))) {
315 DOMUIFavIconSource* favicon_source = new DOMUIFavIconSource(profile_);
316 ChromeThread::PostTask(
317 ChromeThread::IO, FROM_HERE,
318 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(),
319 &ChromeURLDataManager::AddDataSource,
320 make_scoped_refptr(favicon_source)));
321 }
322
[email protected]35506352009-08-07 18:58:19323 // Update the extension permissions. Doing this each time we create an EFD
324 // ensures that new processes are informed of permissions for newly installed
325 // extensions.
[email protected]cccf90932009-08-23 17:56:25326 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19327 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25328 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
329 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50330 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
331 extension->id(), incognito_enabled));
[email protected]68f07912010-03-05 18:33:58332
333 NotificationService::current()->Notify(
334 NotificationType::EXTENSION_FUNCTION_DISPATCHER_CREATED,
335 Source<Profile>(profile_),
336 Details<ExtensionFunctionDispatcher>(this));
[email protected]bfdffe2b2009-04-24 22:05:35337}
338
[email protected]32dda362009-06-05 19:07:01339ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25340 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01341 peer_->dispatcher_ = NULL;
[email protected]68f07912010-03-05 18:33:58342
343 NotificationService::current()->Notify(
344 NotificationType::EXTENSION_FUNCTION_DISPATCHER_DESTROYED,
345 Source<Profile>(profile_),
346 Details<ExtensionFunctionDispatcher>(this));
[email protected]32dda362009-06-05 19:07:01347}
348
[email protected]0ec92032010-03-24 19:59:41349Browser* ExtensionFunctionDispatcher::GetCurrentBrowser(
350 bool include_incognito) {
351 Browser* browser = delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12352
[email protected]0ec92032010-03-24 19:59:41353 // If the delegate has an associated browser and that browser is in the right
354 // incognito state, we can return it.
355 if (browser) {
356 if (include_incognito || !browser->profile()->IsOffTheRecord())
357 return browser;
358 }
[email protected]9c45b7182009-08-04 16:44:43359
[email protected]0ec92032010-03-24 19:59:41360 // Otherwise, try to default to a reasonable browser.
361 Profile* profile = render_view_host()->process()->profile();
362
363 // Make sure we don't return an incognito browser without proper access.
364 if (!include_incognito)
365 profile = profile->GetOriginalProfile();
366
[email protected]62b0b532010-03-26 22:44:31367 browser = BrowserList::FindBrowserWithType(profile, Browser::TYPE_ANY,
368 include_incognito);
[email protected]0ec92032010-03-24 19:59:41369
[email protected]0ec92032010-03-24 19:59:41370 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
371 // a toolstrip or background_page onload chrome.tabs api call can make it
372 // into here before the browser is sufficiently initialized to return here.
373 // A similar situation may arise during shutdown.
374 // TODO(rafaelw): Delay creation of background_page until the browser
375 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
376 return browser;
[email protected]b27257562009-11-16 23:28:26377}
378
[email protected]bfdffe2b2009-04-24 22:05:35379void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58380 const Value* args,
[email protected]bb64b512010-04-02 21:01:39381 const GURL& source_url,
[email protected]c6619182009-05-12 14:59:32382 int request_id,
383 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01384 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35385 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01386 function->set_dispatcher_peer(peer_);
[email protected]a7664e12010-04-08 20:37:43387 function->set_profile(profile_);
388 function->set_extension_id(extension_id());
[email protected]b83e4602009-05-15 22:58:33389 function->SetArgs(args);
[email protected]bb64b512010-04-02 21:01:39390 function->set_source_url(source_url);
[email protected]c6619182009-05-12 14:59:32391 function->set_request_id(request_id);
392 function->set_has_callback(has_callback);
[email protected]d13950e2009-12-04 01:43:02393 ExtensionsService* service = profile()->GetExtensionsService();
394 DCHECK(service);
[email protected]cb0ce1e022010-03-10 19:54:41395 Extension* extension = service->GetExtensionById(extension_id(), false);
396 DCHECK(extension);
397 function->set_include_incognito(service->IsIncognitoEnabled(extension));
398
[email protected]d13950e2009-12-04 01:43:02399 ExtensionsQuotaService* quota = service->quota_service();
400 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
401 function->Run();
402 } else {
403 render_view_host_->SendExtensionResponse(function->request_id(), false,
404 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
405 }
[email protected]bfdffe2b2009-04-24 22:05:35406}
407
[email protected]c6619182009-05-12 14:59:32408void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
409 bool success) {
[email protected]c6619182009-05-12 14:59:32410 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33411 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35412}
413
414void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14415 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45416 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35417 " : terminating renderer.";
418 if (RenderProcessHost::run_renderer_in_process()) {
419 // In single process mode it is better if we don't suicide but just crash.
420 CHECK(false);
421 } else {
422 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46423 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35424 ResultCodes::KILLED_BAD_MESSAGE, false);
425 }
426}
427
428Profile* ExtensionFunctionDispatcher::profile() {
[email protected]68f07912010-03-05 18:33:58429 return profile_;
[email protected]bfdffe2b2009-04-24 22:05:35430}