blob: 6e719fc80ab5f345080f3c0af97e80ae34879ee7 [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]9dd97bc2010-01-14 01:40:0413#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3514#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]f93914852009-05-26 06:05:4015#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
[email protected]ec9ac0df2009-10-01 18:06:4716#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]b27257562009-11-16 23:28:2617#include "chrome/browser/extensions/extension_dom_ui.h"
[email protected]bfdffe2b2009-04-24 22:05:3518#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0119#include "chrome/browser/extensions/extension_history_api.h"
[email protected]198bcfe2009-09-09 22:56:2820#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]e916901c2009-05-07 00:14:3121#include "chrome/browser/extensions/extension_message_service.h"
[email protected]f7f3a5f2009-05-01 22:02:3422#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]f93914852009-05-26 06:05:4023#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
[email protected]1c1c77a52009-11-03 00:37:3124#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5825#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]bfdffe2b2009-04-24 22:05:3526#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4027#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1428#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4329#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]d13950e2009-12-04 01:43:0230#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5631#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3132#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3533#include "chrome/browser/renderer_host/render_process_host.h"
34#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1935#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3536#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4337#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3538
39// FactoryRegistry -------------------------------------------------------------
40
41namespace {
42
[email protected]b83e4602009-05-15 22:58:3343// Template for defining ExtensionFunctionFactory.
44template<class T>
45ExtensionFunction* NewExtensionFunction() {
46 return new T();
47}
[email protected]bfdffe2b2009-04-24 22:05:3548
[email protected]b83e4602009-05-15 22:58:3349// Contains a list of all known extension functions and allows clients to
50// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3551class FactoryRegistry {
52 public:
53 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3354 FactoryRegistry() { ResetFunctions(); }
55
56 // Resets all functions to their default values.
57 void ResetFunctions();
58
59 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3560 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3361
62 // Allows overriding of specific functions (e.g. for testing). Functions
63 // must be previously registered. Returns true if successful.
64 bool OverrideFunction(const std::string& name,
65 ExtensionFunctionFactory factory);
66
67 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3568 ExtensionFunction* NewFunction(const std::string& name);
69
70 private:
[email protected]61424c062009-10-14 23:14:5971 template<class T>
72 void RegisterFunction() {
73 factories_[T::function_name()] = &NewExtensionFunction<T>;
74 }
75
[email protected]bfdffe2b2009-04-24 22:05:3576 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
77 FactoryMap factories_;
78};
79
[email protected]bfdffe2b2009-04-24 22:05:3580FactoryRegistry* FactoryRegistry::instance() {
81 return Singleton<FactoryRegistry>::get();
82}
83
[email protected]b83e4602009-05-15 22:58:3384void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3585 // Register all functions here.
86
[email protected]e515f5d2009-05-05 03:05:0087 // Windows
[email protected]61424c062009-10-14 23:14:5988 RegisterFunction<GetWindowFunction>();
89 RegisterFunction<GetCurrentWindowFunction>();
90 RegisterFunction<GetLastFocusedWindowFunction>();
91 RegisterFunction<GetAllWindowsFunction>();
92 RegisterFunction<CreateWindowFunction>();
93 RegisterFunction<UpdateWindowFunction>();
94 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:3395
[email protected]e515f5d2009-05-05 03:05:0096 // Tabs
[email protected]61424c062009-10-14 23:14:5997 RegisterFunction<GetTabFunction>();
98 RegisterFunction<GetSelectedTabFunction>();
99 RegisterFunction<GetAllTabsInWindowFunction>();
100 RegisterFunction<CreateTabFunction>();
101 RegisterFunction<UpdateTabFunction>();
102 RegisterFunction<MoveTabFunction>();
103 RegisterFunction<RemoveTabFunction>();
104 RegisterFunction<DetectTabLanguageFunction>();
105 RegisterFunction<CaptureVisibleTabFunction>();
106 RegisterFunction<TabsExecuteScriptFunction>();
107 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35108
[email protected]f7f3a5f2009-05-01 22:02:34109 // Page Actions.
[email protected]61424c062009-10-14 23:14:59110 RegisterFunction<EnablePageActionFunction>();
111 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46112 RegisterFunction<PageActionShowFunction>();
113 RegisterFunction<PageActionHideFunction>();
114 RegisterFunction<PageActionSetIconFunction>();
115 RegisterFunction<PageActionSetTitleFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34116
[email protected]ec9ac0df2009-10-01 18:06:47117 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59118 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24119 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59120 RegisterFunction<BrowserActionSetBadgeTextFunction>();
121 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47122
[email protected]f7f3a5f2009-05-01 22:02:34123 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59124 RegisterFunction<GetBookmarksFunction>();
125 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55126 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59127 RegisterFunction<GetBookmarkTreeFunction>();
128 RegisterFunction<SearchBookmarksFunction>();
129 RegisterFunction<RemoveBookmarkFunction>();
130 RegisterFunction<RemoveTreeBookmarkFunction>();
131 RegisterFunction<CreateBookmarkFunction>();
132 RegisterFunction<MoveBookmarkFunction>();
133 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43134
[email protected]9dd97bc2010-01-14 01:40:04135 // BookmarkManager
136 RegisterFunction<CopyBookmarkManagerFunction>();
137 RegisterFunction<CutBookmarkManagerFunction>();
138 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56139 RegisterFunction<ImportBookmarksFunction>();
140 RegisterFunction<ExportBookmarksFunction>();
[email protected]9dd97bc2010-01-14 01:40:04141 RegisterFunction<BookmarkManagerGetStringsFunction>();
142
[email protected]de768a832009-10-30 05:25:01143 // History
144 RegisterFunction<AddUrlHistoryFunction>();
145 RegisterFunction<DeleteAllHistoryFunction>();
146 RegisterFunction<DeleteRangeHistoryFunction>();
147 RegisterFunction<DeleteUrlHistoryFunction>();
148 RegisterFunction<GetVisitsHistoryFunction>();
149 RegisterFunction<SearchHistoryFunction>();
150
[email protected]9c45b7182009-08-04 16:44:43151 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59152 RegisterFunction<ToolstripExpandFunction>();
153 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14154
[email protected]198bcfe2009-09-09 22:56:28155 // I18N.
[email protected]61424c062009-10-14 23:14:59156 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28157
[email protected]1c1c77a52009-11-03 00:37:31158 // Popup API.
159 RegisterFunction<PopupShowFunction>();
160
[email protected]25fd1b2e2009-08-17 20:57:14161 // Test.
[email protected]61424c062009-10-14 23:14:59162 RegisterFunction<ExtensionTestPassFunction>();
163 RegisterFunction<ExtensionTestFailFunction>();
164 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02165 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35166}
167
[email protected]b83e4602009-05-15 22:58:33168void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
169 for (FactoryMap::iterator iter = factories_.begin();
170 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35171 names->push_back(iter->first);
172 }
173}
174
[email protected]b83e4602009-05-15 22:58:33175bool FactoryRegistry::OverrideFunction(const std::string& name,
176 ExtensionFunctionFactory factory) {
177 FactoryMap::iterator iter = factories_.find(name);
178 if (iter == factories_.end()) {
179 return false;
180 } else {
181 iter->second = factory;
182 return true;
183 }
184}
185
[email protected]bfdffe2b2009-04-24 22:05:35186ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
187 FactoryMap::iterator iter = factories_.find(name);
188 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33189 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56190 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33191 return function;
[email protected]bfdffe2b2009-04-24 22:05:35192}
193
[email protected]b83e4602009-05-15 22:58:33194}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35195
[email protected]a95631cb2009-12-10 01:59:11196// ExtensionFunctionDispatcher::Delegate ---------------------------------------
197
198gfx::NativeWindow ExtensionFunctionDispatcher::Delegate::
199 GetFrameNativeWindow() {
200 Browser* browser = GetBrowser();
201 // If a browser is bound to this dispatcher, then return the widget hosting
202 // the window. Extensions hosted in ExternalTabContainer objects may not
203 // have a running browser instance.
204 if (browser)
205 return browser->window()->GetNativeHandle();
206
207 return NULL;
208}
209
[email protected]bfdffe2b2009-04-24 22:05:35210// ExtensionFunctionDispatcher -------------------------------------------------
211
212void ExtensionFunctionDispatcher::GetAllFunctionNames(
213 std::vector<std::string>* names) {
214 FactoryRegistry::instance()->GetAllNames(names);
215}
216
[email protected]b83e4602009-05-15 22:58:33217bool ExtensionFunctionDispatcher::OverrideFunction(
218 const std::string& name, ExtensionFunctionFactory factory) {
219 return FactoryRegistry::instance()->OverrideFunction(name, factory);
220}
221
222void ExtensionFunctionDispatcher::ResetFunctions() {
223 FactoryRegistry::instance()->ResetFunctions();
224}
225
[email protected]811bfe32009-07-01 08:46:25226std::set<ExtensionFunctionDispatcher*>*
227 ExtensionFunctionDispatcher::all_instances() {
228 static std::set<ExtensionFunctionDispatcher*> instances;
229 return &instances;
230}
231
[email protected]bfdffe2b2009-04-24 22:05:35232ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
233 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12234 Delegate* delegate,
[email protected]811bfe32009-07-01 08:46:25235 const GURL& url)
[email protected]bfdffe2b2009-04-24 22:05:35236 : render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12237 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25238 url_(url),
[email protected]32dda362009-06-05 19:07:01239 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43240 // TODO(erikkay) should we do something for these errors in Release?
241 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19242
243 Extension* extension =
244 profile()->GetExtensionsService()->GetExtensionByURL(url);
245 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43246
[email protected]811bfe32009-07-01 08:46:25247 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35248
[email protected]45776222009-07-15 20:21:58249 // Notify the ExtensionProcessManager that the view was created.
250 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
251 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45252 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19253
254 // Update the extension permissions. Doing this each time we create an EFD
255 // ensures that new processes are informed of permissions for newly installed
256 // extensions.
[email protected]cccf90932009-08-23 17:56:25257 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19258 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25259 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
260 extension->url(), extension->host_permissions()));
[email protected]bfdffe2b2009-04-24 22:05:35261}
262
[email protected]32dda362009-06-05 19:07:01263ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25264 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01265 peer_->dispatcher_ = NULL;
266}
267
[email protected]7eecaed52009-05-07 21:44:12268Browser* ExtensionFunctionDispatcher::GetBrowser() {
[email protected]4ba60e22009-08-14 21:02:11269 return delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12270}
271
[email protected]b27257562009-11-16 23:28:26272ExtensionPopupHost* ExtensionFunctionDispatcher::GetPopupHost() {
273 ExtensionHost* extension_host = GetExtensionHost();
274 if (extension_host) {
275 DCHECK(!GetExtensionDOMUI()) <<
276 "Function dispatcher registered in too many environments.";
277 return extension_host->popup_host();
278 } else {
279 ExtensionDOMUI* dom_ui = GetExtensionDOMUI();
280 return dom_ui->popup_host();
281 }
282}
283
[email protected]9c45b7182009-08-04 16:44:43284ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
[email protected]9c45b7182009-08-04 16:44:43285 return delegate_->GetExtensionHost();
286}
287
[email protected]b27257562009-11-16 23:28:26288ExtensionDOMUI* ExtensionFunctionDispatcher::GetExtensionDOMUI() {
289 return delegate_->GetExtensionDOMUI();
290}
291
[email protected]c7ad50f2009-09-11 06:28:15292Extension* ExtensionFunctionDispatcher::GetExtension() {
293 ExtensionsService* service = profile()->GetExtensionsService();
294 DCHECK(service);
295
[email protected]61b411612009-11-10 23:17:41296 Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]c7ad50f2009-09-11 06:28:15297 DCHECK(extension);
298
299 return extension;
300}
301
[email protected]bfdffe2b2009-04-24 22:05:35302void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58303 const Value* args,
[email protected]c6619182009-05-12 14:59:32304 int request_id,
305 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01306 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35307 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01308 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33309 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32310 function->set_request_id(request_id);
311 function->set_has_callback(has_callback);
[email protected]d13950e2009-12-04 01:43:02312
313 ExtensionsService* service = profile()->GetExtensionsService();
314 DCHECK(service);
315 ExtensionsQuotaService* quota = service->quota_service();
316 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
317 function->Run();
318 } else {
319 render_view_host_->SendExtensionResponse(function->request_id(), false,
320 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
321 }
[email protected]bfdffe2b2009-04-24 22:05:35322}
323
[email protected]c6619182009-05-12 14:59:32324void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
325 bool success) {
[email protected]c6619182009-05-12 14:59:32326 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33327 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35328}
329
330void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14331 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45332 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35333 " : terminating renderer.";
334 if (RenderProcessHost::run_renderer_in_process()) {
335 // In single process mode it is better if we don't suicide but just crash.
336 CHECK(false);
337 } else {
338 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46339 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35340 ResultCodes::KILLED_BAD_MESSAGE, false);
341 }
342}
343
344Profile* ExtensionFunctionDispatcher::profile() {
345 return render_view_host_->process()->profile();
346}
[email protected]a95631cb2009-12-10 01:59:11347
348gfx::NativeWindow ExtensionFunctionDispatcher::GetFrameNativeWindow() {
349 return delegate_ ? delegate_->GetFrameNativeWindow() : NULL;
350}