blob: 8f868ebbe425aa1be15af47f538db99768532540 [file] [log] [blame]
[email protected]bfdffe2b2009-04-24 22:05:351// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// 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"
11#include "chrome/browser/browser_window.h"
[email protected]912256b32009-09-18 09:47:3512#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]5cbe1e22010-01-30 01:18:5613#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]9dd97bc2010-01-14 01:40:0414#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3515#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]f93914852009-05-26 06:05:4016#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
[email protected]ec9ac0df2009-10-01 18:06:4717#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]b27257562009-11-16 23:28:2618#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]bfdffe2b2009-04-24 22:05:3519#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0120#include "chrome/browser/extensions/extension_history_api.h"
[email protected]198bcfe2009-09-09 22:56:2821#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]e916901c2009-05-07 00:14:3122#include "chrome/browser/extensions/extension_message_service.h"
[email protected]438772df2010-02-26 18:08:4323#include "chrome/browser/extensions/extension_metrics_module.h"
[email protected]f7f3a5f2009-05-01 22:02:3424#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]f93914852009-05-26 06:05:4025#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
[email protected]1c1c77a52009-11-03 00:37:3126#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5827#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3528#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3529#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4030#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1431#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4332#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]d13950e2009-12-04 01:43:0233#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5634#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3135#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3536#include "chrome/browser/renderer_host/render_process_host.h"
37#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]438772df2010-02-26 18:08:4338#include "chrome/common/chrome_switches.h"
[email protected]35506352009-08-07 18:58:1939#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3540#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4341#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3542
43// FactoryRegistry -------------------------------------------------------------
44
45namespace {
46
[email protected]b83e4602009-05-15 22:58:3347// Template for defining ExtensionFunctionFactory.
48template<class T>
49ExtensionFunction* NewExtensionFunction() {
50 return new T();
51}
[email protected]bfdffe2b2009-04-24 22:05:3552
[email protected]b83e4602009-05-15 22:58:3353// Contains a list of all known extension functions and allows clients to
54// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3555class FactoryRegistry {
56 public:
57 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3358 FactoryRegistry() { ResetFunctions(); }
59
60 // Resets all functions to their default values.
61 void ResetFunctions();
62
63 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3564 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3365
66 // Allows overriding of specific functions (e.g. for testing). Functions
67 // must be previously registered. Returns true if successful.
68 bool OverrideFunction(const std::string& name,
69 ExtensionFunctionFactory factory);
70
71 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3572 ExtensionFunction* NewFunction(const std::string& name);
73
74 private:
[email protected]61424c062009-10-14 23:14:5975 template<class T>
76 void RegisterFunction() {
77 factories_[T::function_name()] = &NewExtensionFunction<T>;
78 }
79
[email protected]bfdffe2b2009-04-24 22:05:3580 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
81 FactoryMap factories_;
82};
83
[email protected]bfdffe2b2009-04-24 22:05:3584FactoryRegistry* FactoryRegistry::instance() {
85 return Singleton<FactoryRegistry>::get();
86}
87
[email protected]b83e4602009-05-15 22:58:3388void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3589 // Register all functions here.
90
[email protected]e515f5d2009-05-05 03:05:0091 // Windows
[email protected]61424c062009-10-14 23:14:5992 RegisterFunction<GetWindowFunction>();
93 RegisterFunction<GetCurrentWindowFunction>();
94 RegisterFunction<GetLastFocusedWindowFunction>();
95 RegisterFunction<GetAllWindowsFunction>();
96 RegisterFunction<CreateWindowFunction>();
97 RegisterFunction<UpdateWindowFunction>();
98 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:3399
[email protected]e515f5d2009-05-05 03:05:00100 // Tabs
[email protected]61424c062009-10-14 23:14:59101 RegisterFunction<GetTabFunction>();
102 RegisterFunction<GetSelectedTabFunction>();
103 RegisterFunction<GetAllTabsInWindowFunction>();
104 RegisterFunction<CreateTabFunction>();
105 RegisterFunction<UpdateTabFunction>();
106 RegisterFunction<MoveTabFunction>();
107 RegisterFunction<RemoveTabFunction>();
108 RegisterFunction<DetectTabLanguageFunction>();
109 RegisterFunction<CaptureVisibleTabFunction>();
110 RegisterFunction<TabsExecuteScriptFunction>();
111 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35112
[email protected]f7f3a5f2009-05-01 22:02:34113 // Page Actions.
[email protected]61424c062009-10-14 23:14:59114 RegisterFunction<EnablePageActionFunction>();
115 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46116 RegisterFunction<PageActionShowFunction>();
117 RegisterFunction<PageActionHideFunction>();
118 RegisterFunction<PageActionSetIconFunction>();
119 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29120 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34121
[email protected]ec9ac0df2009-10-01 18:06:47122 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59123 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24124 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59125 RegisterFunction<BrowserActionSetBadgeTextFunction>();
126 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50127 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47128
[email protected]f7f3a5f2009-05-01 22:02:34129 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59130 RegisterFunction<GetBookmarksFunction>();
131 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55132 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59133 RegisterFunction<GetBookmarkTreeFunction>();
134 RegisterFunction<SearchBookmarksFunction>();
135 RegisterFunction<RemoveBookmarkFunction>();
136 RegisterFunction<RemoveTreeBookmarkFunction>();
137 RegisterFunction<CreateBookmarkFunction>();
138 RegisterFunction<MoveBookmarkFunction>();
139 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43140
[email protected]9dd97bc2010-01-14 01:40:04141 // BookmarkManager
142 RegisterFunction<CopyBookmarkManagerFunction>();
143 RegisterFunction<CutBookmarkManagerFunction>();
144 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57145 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56146 RegisterFunction<ImportBookmarksFunction>();
147 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18148 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04149 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16150 RegisterFunction<StartDragBookmarkManagerFunction>();
151 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04152
[email protected]de768a832009-10-30 05:25:01153 // History
154 RegisterFunction<AddUrlHistoryFunction>();
155 RegisterFunction<DeleteAllHistoryFunction>();
156 RegisterFunction<DeleteRangeHistoryFunction>();
157 RegisterFunction<DeleteUrlHistoryFunction>();
158 RegisterFunction<GetVisitsHistoryFunction>();
159 RegisterFunction<SearchHistoryFunction>();
160
[email protected]9c45b7182009-08-04 16:44:43161 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59162 RegisterFunction<ToolstripExpandFunction>();
163 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14164
[email protected]198bcfe2009-09-09 22:56:28165 // I18N.
[email protected]61424c062009-10-14 23:14:59166 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28167
[email protected]1c1c77a52009-11-03 00:37:31168 // Popup API.
169 RegisterFunction<PopupShowFunction>();
170
[email protected]381162b2010-01-28 17:29:35171 // Processes.
172 RegisterFunction<GetProcessForTabFunction>();
173
[email protected]438772df2010-02-26 18:08:43174 // Metrics.
175 if (CommandLine::ForCurrentProcess()->HasSwitch(
176 switches::kEnableMetricsExtensionApi)) {
177 RegisterFunction<MetricsRecordUserActionFunction>();
178 RegisterFunction<MetricsRecordValueFunction>();
179 RegisterFunction<MetricsRecordPercentageFunction>();
180 RegisterFunction<MetricsRecordCountFunction>();
181 RegisterFunction<MetricsRecordSmallCountFunction>();
182 RegisterFunction<MetricsRecordMediumCountFunction>();
183 RegisterFunction<MetricsRecordTimeFunction>();
184 RegisterFunction<MetricsRecordMediumTimeFunction>();
185 RegisterFunction<MetricsRecordLongTimeFunction>();
186 }
187
[email protected]25fd1b2e2009-08-17 20:57:14188 // Test.
[email protected]61424c062009-10-14 23:14:59189 RegisterFunction<ExtensionTestPassFunction>();
190 RegisterFunction<ExtensionTestFailFunction>();
191 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02192 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]db7331a2010-02-25 22:10:50193 RegisterFunction<ExtensionTestCreateIncognitoTabFunction>();
[email protected]5cbe1e22010-01-30 01:18:56194
195 // Accessibility.
196 RegisterFunction<GetFocusedControlFunction>();
197 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35198}
199
[email protected]b83e4602009-05-15 22:58:33200void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
201 for (FactoryMap::iterator iter = factories_.begin();
202 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35203 names->push_back(iter->first);
204 }
205}
206
[email protected]b83e4602009-05-15 22:58:33207bool FactoryRegistry::OverrideFunction(const std::string& name,
208 ExtensionFunctionFactory factory) {
209 FactoryMap::iterator iter = factories_.find(name);
210 if (iter == factories_.end()) {
211 return false;
212 } else {
213 iter->second = factory;
214 return true;
215 }
216}
217
[email protected]bfdffe2b2009-04-24 22:05:35218ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
219 FactoryMap::iterator iter = factories_.find(name);
220 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33221 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56222 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33223 return function;
[email protected]bfdffe2b2009-04-24 22:05:35224}
225
[email protected]b83e4602009-05-15 22:58:33226}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35227
[email protected]a95631cb2009-12-10 01:59:11228// ExtensionFunctionDispatcher::Delegate ---------------------------------------
229
230gfx::NativeWindow ExtensionFunctionDispatcher::Delegate::
231 GetFrameNativeWindow() {
[email protected]db7331a2010-02-25 22:10:50232 Browser* browser = GetBrowser(true);
[email protected]a95631cb2009-12-10 01:59:11233 // If a browser is bound to this dispatcher, then return the widget hosting
234 // the window. Extensions hosted in ExternalTabContainer objects may not
235 // have a running browser instance.
236 if (browser)
237 return browser->window()->GetNativeHandle();
238
239 return NULL;
240}
241
[email protected]bfdffe2b2009-04-24 22:05:35242// ExtensionFunctionDispatcher -------------------------------------------------
243
244void ExtensionFunctionDispatcher::GetAllFunctionNames(
245 std::vector<std::string>* names) {
246 FactoryRegistry::instance()->GetAllNames(names);
247}
248
[email protected]b83e4602009-05-15 22:58:33249bool ExtensionFunctionDispatcher::OverrideFunction(
250 const std::string& name, ExtensionFunctionFactory factory) {
251 return FactoryRegistry::instance()->OverrideFunction(name, factory);
252}
253
254void ExtensionFunctionDispatcher::ResetFunctions() {
255 FactoryRegistry::instance()->ResetFunctions();
256}
257
[email protected]811bfe32009-07-01 08:46:25258std::set<ExtensionFunctionDispatcher*>*
259 ExtensionFunctionDispatcher::all_instances() {
260 static std::set<ExtensionFunctionDispatcher*> instances;
261 return &instances;
262}
263
[email protected]bfdffe2b2009-04-24 22:05:35264ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
265 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12266 Delegate* delegate,
[email protected]811bfe32009-07-01 08:46:25267 const GURL& url)
[email protected]bfdffe2b2009-04-24 22:05:35268 : render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12269 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25270 url_(url),
[email protected]32dda362009-06-05 19:07:01271 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43272 // TODO(erikkay) should we do something for these errors in Release?
273 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19274
275 Extension* extension =
276 profile()->GetExtensionsService()->GetExtensionByURL(url);
277 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43278
[email protected]811bfe32009-07-01 08:46:25279 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35280
[email protected]45776222009-07-15 20:21:58281 // Notify the ExtensionProcessManager that the view was created.
282 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
283 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45284 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19285
[email protected]db7331a2010-02-25 22:10:50286 bool incognito_enabled =
287 profile()->GetExtensionsService()->IsIncognitoEnabled(extension->id());
288
[email protected]35506352009-08-07 18:58:19289 // Update the extension permissions. Doing this each time we create an EFD
290 // ensures that new processes are informed of permissions for newly installed
291 // extensions.
[email protected]cccf90932009-08-23 17:56:25292 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19293 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25294 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
295 extension->url(), extension->host_permissions()));
[email protected]db7331a2010-02-25 22:10:50296 render_view_host->Send(new ViewMsg_Extension_ExtensionSetIncognitoEnabled(
297 extension->id(), incognito_enabled));
[email protected]bfdffe2b2009-04-24 22:05:35298}
299
[email protected]32dda362009-06-05 19:07:01300ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25301 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01302 peer_->dispatcher_ = NULL;
303}
304
[email protected]db7331a2010-02-25 22:10:50305Browser* ExtensionFunctionDispatcher::GetBrowser(bool include_incognito) {
306 return delegate_->GetBrowser(include_incognito);
[email protected]7eecaed52009-05-07 21:44:12307}
308
[email protected]b27257562009-11-16 23:28:26309ExtensionPopupHost* ExtensionFunctionDispatcher::GetPopupHost() {
310 ExtensionHost* extension_host = GetExtensionHost();
311 if (extension_host) {
312 DCHECK(!GetExtensionDOMUI()) <<
313 "Function dispatcher registered in too many environments.";
314 return extension_host->popup_host();
315 } else {
316 ExtensionDOMUI* dom_ui = GetExtensionDOMUI();
317 return dom_ui->popup_host();
318 }
319}
320
[email protected]9c45b7182009-08-04 16:44:43321ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
[email protected]9c45b7182009-08-04 16:44:43322 return delegate_->GetExtensionHost();
323}
324
[email protected]b27257562009-11-16 23:28:26325ExtensionDOMUI* ExtensionFunctionDispatcher::GetExtensionDOMUI() {
326 return delegate_->GetExtensionDOMUI();
327}
328
[email protected]c7ad50f2009-09-11 06:28:15329Extension* ExtensionFunctionDispatcher::GetExtension() {
330 ExtensionsService* service = profile()->GetExtensionsService();
331 DCHECK(service);
332
[email protected]61b411612009-11-10 23:17:41333 Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]c7ad50f2009-09-11 06:28:15334 DCHECK(extension);
335
336 return extension;
337}
338
[email protected]bfdffe2b2009-04-24 22:05:35339void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58340 const Value* args,
[email protected]c6619182009-05-12 14:59:32341 int request_id,
342 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01343 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35344 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01345 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33346 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32347 function->set_request_id(request_id);
348 function->set_has_callback(has_callback);
[email protected]db7331a2010-02-25 22:10:50349 function->set_include_incognito(
350 profile()->GetExtensionsService()->IsIncognitoEnabled(extension_id()));
[email protected]d13950e2009-12-04 01:43:02351
352 ExtensionsService* service = profile()->GetExtensionsService();
353 DCHECK(service);
354 ExtensionsQuotaService* quota = service->quota_service();
355 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
356 function->Run();
357 } else {
358 render_view_host_->SendExtensionResponse(function->request_id(), false,
359 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
360 }
[email protected]bfdffe2b2009-04-24 22:05:35361}
362
[email protected]c6619182009-05-12 14:59:32363void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
364 bool success) {
[email protected]c6619182009-05-12 14:59:32365 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33366 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35367}
368
369void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14370 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45371 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35372 " : terminating renderer.";
373 if (RenderProcessHost::run_renderer_in_process()) {
374 // In single process mode it is better if we don't suicide but just crash.
375 CHECK(false);
376 } else {
377 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46378 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35379 ResultCodes::KILLED_BAD_MESSAGE, false);
380 }
381}
382
383Profile* ExtensionFunctionDispatcher::profile() {
384 return render_view_host_->process()->profile();
385}
[email protected]a95631cb2009-12-10 01:59:11386
387gfx::NativeWindow ExtensionFunctionDispatcher::GetFrameNativeWindow() {
388 return delegate_ ? delegate_->GetFrameNativeWindow() : NULL;
389}