blob: 69e4d542dbe54ae3ea251c4b25041765a9b14f83 [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]f7f3a5f2009-05-01 22:02:3423#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]f93914852009-05-26 06:05:4024#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
[email protected]1c1c77a52009-11-03 00:37:3125#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5826#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]381162b2010-01-28 17:29:3527#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3528#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4029#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1430#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4331#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]d13950e2009-12-04 01:43:0232#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5633#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3134#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3535#include "chrome/browser/renderer_host/render_process_host.h"
36#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1937#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3538#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4339#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3540
41// FactoryRegistry -------------------------------------------------------------
42
43namespace {
44
[email protected]b83e4602009-05-15 22:58:3345// Template for defining ExtensionFunctionFactory.
46template<class T>
47ExtensionFunction* NewExtensionFunction() {
48 return new T();
49}
[email protected]bfdffe2b2009-04-24 22:05:3550
[email protected]b83e4602009-05-15 22:58:3351// Contains a list of all known extension functions and allows clients to
52// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3553class FactoryRegistry {
54 public:
55 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3356 FactoryRegistry() { ResetFunctions(); }
57
58 // Resets all functions to their default values.
59 void ResetFunctions();
60
61 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3562 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3363
64 // Allows overriding of specific functions (e.g. for testing). Functions
65 // must be previously registered. Returns true if successful.
66 bool OverrideFunction(const std::string& name,
67 ExtensionFunctionFactory factory);
68
69 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3570 ExtensionFunction* NewFunction(const std::string& name);
71
72 private:
[email protected]61424c062009-10-14 23:14:5973 template<class T>
74 void RegisterFunction() {
75 factories_[T::function_name()] = &NewExtensionFunction<T>;
76 }
77
[email protected]bfdffe2b2009-04-24 22:05:3578 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
79 FactoryMap factories_;
80};
81
[email protected]bfdffe2b2009-04-24 22:05:3582FactoryRegistry* FactoryRegistry::instance() {
83 return Singleton<FactoryRegistry>::get();
84}
85
[email protected]b83e4602009-05-15 22:58:3386void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3587 // Register all functions here.
88
[email protected]e515f5d2009-05-05 03:05:0089 // Windows
[email protected]61424c062009-10-14 23:14:5990 RegisterFunction<GetWindowFunction>();
91 RegisterFunction<GetCurrentWindowFunction>();
92 RegisterFunction<GetLastFocusedWindowFunction>();
93 RegisterFunction<GetAllWindowsFunction>();
94 RegisterFunction<CreateWindowFunction>();
95 RegisterFunction<UpdateWindowFunction>();
96 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:3397
[email protected]e515f5d2009-05-05 03:05:0098 // Tabs
[email protected]61424c062009-10-14 23:14:5999 RegisterFunction<GetTabFunction>();
100 RegisterFunction<GetSelectedTabFunction>();
101 RegisterFunction<GetAllTabsInWindowFunction>();
102 RegisterFunction<CreateTabFunction>();
103 RegisterFunction<UpdateTabFunction>();
104 RegisterFunction<MoveTabFunction>();
105 RegisterFunction<RemoveTabFunction>();
106 RegisterFunction<DetectTabLanguageFunction>();
107 RegisterFunction<CaptureVisibleTabFunction>();
108 RegisterFunction<TabsExecuteScriptFunction>();
109 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35110
[email protected]f7f3a5f2009-05-01 22:02:34111 // Page Actions.
[email protected]61424c062009-10-14 23:14:59112 RegisterFunction<EnablePageActionFunction>();
113 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46114 RegisterFunction<PageActionShowFunction>();
115 RegisterFunction<PageActionHideFunction>();
116 RegisterFunction<PageActionSetIconFunction>();
117 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29118 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34119
[email protected]ec9ac0df2009-10-01 18:06:47120 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59121 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24122 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59123 RegisterFunction<BrowserActionSetBadgeTextFunction>();
124 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]85ae9592010-02-03 20:58:50125 RegisterFunction<BrowserActionSetPopupFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47126
[email protected]f7f3a5f2009-05-01 22:02:34127 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59128 RegisterFunction<GetBookmarksFunction>();
129 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55130 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59131 RegisterFunction<GetBookmarkTreeFunction>();
132 RegisterFunction<SearchBookmarksFunction>();
133 RegisterFunction<RemoveBookmarkFunction>();
134 RegisterFunction<RemoveTreeBookmarkFunction>();
135 RegisterFunction<CreateBookmarkFunction>();
136 RegisterFunction<MoveBookmarkFunction>();
137 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43138
[email protected]9dd97bc2010-01-14 01:40:04139 // BookmarkManager
140 RegisterFunction<CopyBookmarkManagerFunction>();
141 RegisterFunction<CutBookmarkManagerFunction>();
142 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]03b3bbf2010-01-29 23:54:57143 RegisterFunction<CanPasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56144 RegisterFunction<ImportBookmarksFunction>();
145 RegisterFunction<ExportBookmarksFunction>();
[email protected]d406e2e2010-01-30 21:45:18146 RegisterFunction<SortChildrenBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04147 RegisterFunction<BookmarkManagerGetStringsFunction>();
[email protected]ced90ae12010-02-20 02:06:16148 RegisterFunction<StartDragBookmarkManagerFunction>();
149 RegisterFunction<DropBookmarkManagerFunction>();
[email protected]9dd97bc2010-01-14 01:40:04150
[email protected]de768a832009-10-30 05:25:01151 // History
152 RegisterFunction<AddUrlHistoryFunction>();
153 RegisterFunction<DeleteAllHistoryFunction>();
154 RegisterFunction<DeleteRangeHistoryFunction>();
155 RegisterFunction<DeleteUrlHistoryFunction>();
156 RegisterFunction<GetVisitsHistoryFunction>();
157 RegisterFunction<SearchHistoryFunction>();
158
[email protected]9c45b7182009-08-04 16:44:43159 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59160 RegisterFunction<ToolstripExpandFunction>();
161 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14162
[email protected]198bcfe2009-09-09 22:56:28163 // I18N.
[email protected]61424c062009-10-14 23:14:59164 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28165
[email protected]1c1c77a52009-11-03 00:37:31166 // Popup API.
167 RegisterFunction<PopupShowFunction>();
168
[email protected]381162b2010-01-28 17:29:35169 // Processes.
170 RegisterFunction<GetProcessForTabFunction>();
171
[email protected]25fd1b2e2009-08-17 20:57:14172 // Test.
[email protected]61424c062009-10-14 23:14:59173 RegisterFunction<ExtensionTestPassFunction>();
174 RegisterFunction<ExtensionTestFailFunction>();
175 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02176 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]5cbe1e22010-01-30 01:18:56177
178 // Accessibility.
179 RegisterFunction<GetFocusedControlFunction>();
180 RegisterFunction<SetAccessibilityEnabledFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35181}
182
[email protected]b83e4602009-05-15 22:58:33183void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
184 for (FactoryMap::iterator iter = factories_.begin();
185 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35186 names->push_back(iter->first);
187 }
188}
189
[email protected]b83e4602009-05-15 22:58:33190bool FactoryRegistry::OverrideFunction(const std::string& name,
191 ExtensionFunctionFactory factory) {
192 FactoryMap::iterator iter = factories_.find(name);
193 if (iter == factories_.end()) {
194 return false;
195 } else {
196 iter->second = factory;
197 return true;
198 }
199}
200
[email protected]bfdffe2b2009-04-24 22:05:35201ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
202 FactoryMap::iterator iter = factories_.find(name);
203 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33204 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56205 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33206 return function;
[email protected]bfdffe2b2009-04-24 22:05:35207}
208
[email protected]b83e4602009-05-15 22:58:33209}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35210
[email protected]a95631cb2009-12-10 01:59:11211// ExtensionFunctionDispatcher::Delegate ---------------------------------------
212
213gfx::NativeWindow ExtensionFunctionDispatcher::Delegate::
214 GetFrameNativeWindow() {
215 Browser* browser = GetBrowser();
216 // If a browser is bound to this dispatcher, then return the widget hosting
217 // the window. Extensions hosted in ExternalTabContainer objects may not
218 // have a running browser instance.
219 if (browser)
220 return browser->window()->GetNativeHandle();
221
222 return NULL;
223}
224
[email protected]bfdffe2b2009-04-24 22:05:35225// ExtensionFunctionDispatcher -------------------------------------------------
226
227void ExtensionFunctionDispatcher::GetAllFunctionNames(
228 std::vector<std::string>* names) {
229 FactoryRegistry::instance()->GetAllNames(names);
230}
231
[email protected]b83e4602009-05-15 22:58:33232bool ExtensionFunctionDispatcher::OverrideFunction(
233 const std::string& name, ExtensionFunctionFactory factory) {
234 return FactoryRegistry::instance()->OverrideFunction(name, factory);
235}
236
237void ExtensionFunctionDispatcher::ResetFunctions() {
238 FactoryRegistry::instance()->ResetFunctions();
239}
240
[email protected]811bfe32009-07-01 08:46:25241std::set<ExtensionFunctionDispatcher*>*
242 ExtensionFunctionDispatcher::all_instances() {
243 static std::set<ExtensionFunctionDispatcher*> instances;
244 return &instances;
245}
246
[email protected]bfdffe2b2009-04-24 22:05:35247ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
248 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12249 Delegate* delegate,
[email protected]811bfe32009-07-01 08:46:25250 const GURL& url)
[email protected]bfdffe2b2009-04-24 22:05:35251 : render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12252 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25253 url_(url),
[email protected]32dda362009-06-05 19:07:01254 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43255 // TODO(erikkay) should we do something for these errors in Release?
256 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19257
258 Extension* extension =
259 profile()->GetExtensionsService()->GetExtensionByURL(url);
260 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43261
[email protected]811bfe32009-07-01 08:46:25262 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35263
[email protected]45776222009-07-15 20:21:58264 // Notify the ExtensionProcessManager that the view was created.
265 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
266 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45267 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19268
269 // Update the extension permissions. Doing this each time we create an EFD
270 // ensures that new processes are informed of permissions for newly installed
271 // extensions.
[email protected]cccf90932009-08-23 17:56:25272 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19273 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25274 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
275 extension->url(), extension->host_permissions()));
[email protected]bfdffe2b2009-04-24 22:05:35276}
277
[email protected]32dda362009-06-05 19:07:01278ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25279 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01280 peer_->dispatcher_ = NULL;
281}
282
[email protected]7eecaed52009-05-07 21:44:12283Browser* ExtensionFunctionDispatcher::GetBrowser() {
[email protected]4ba60e22009-08-14 21:02:11284 return delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12285}
286
[email protected]b27257562009-11-16 23:28:26287ExtensionPopupHost* ExtensionFunctionDispatcher::GetPopupHost() {
288 ExtensionHost* extension_host = GetExtensionHost();
289 if (extension_host) {
290 DCHECK(!GetExtensionDOMUI()) <<
291 "Function dispatcher registered in too many environments.";
292 return extension_host->popup_host();
293 } else {
294 ExtensionDOMUI* dom_ui = GetExtensionDOMUI();
295 return dom_ui->popup_host();
296 }
297}
298
[email protected]9c45b7182009-08-04 16:44:43299ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
[email protected]9c45b7182009-08-04 16:44:43300 return delegate_->GetExtensionHost();
301}
302
[email protected]b27257562009-11-16 23:28:26303ExtensionDOMUI* ExtensionFunctionDispatcher::GetExtensionDOMUI() {
304 return delegate_->GetExtensionDOMUI();
305}
306
[email protected]c7ad50f2009-09-11 06:28:15307Extension* ExtensionFunctionDispatcher::GetExtension() {
308 ExtensionsService* service = profile()->GetExtensionsService();
309 DCHECK(service);
310
[email protected]61b411612009-11-10 23:17:41311 Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]c7ad50f2009-09-11 06:28:15312 DCHECK(extension);
313
314 return extension;
315}
316
[email protected]bfdffe2b2009-04-24 22:05:35317void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58318 const Value* args,
[email protected]c6619182009-05-12 14:59:32319 int request_id,
320 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01321 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35322 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01323 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33324 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32325 function->set_request_id(request_id);
326 function->set_has_callback(has_callback);
[email protected]d13950e2009-12-04 01:43:02327
328 ExtensionsService* service = profile()->GetExtensionsService();
329 DCHECK(service);
330 ExtensionsQuotaService* quota = service->quota_service();
331 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
332 function->Run();
333 } else {
334 render_view_host_->SendExtensionResponse(function->request_id(), false,
335 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
336 }
[email protected]bfdffe2b2009-04-24 22:05:35337}
338
[email protected]c6619182009-05-12 14:59:32339void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
340 bool success) {
[email protected]c6619182009-05-12 14:59:32341 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33342 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35343}
344
345void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14346 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45347 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35348 " : terminating renderer.";
349 if (RenderProcessHost::run_renderer_in_process()) {
350 // In single process mode it is better if we don't suicide but just crash.
351 CHECK(false);
352 } else {
353 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46354 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35355 ResultCodes::KILLED_BAD_MESSAGE, false);
356 }
357}
358
359Profile* ExtensionFunctionDispatcher::profile() {
360 return render_view_host_->process()->profile();
361}
[email protected]a95631cb2009-12-10 01:59:11362
363gfx::NativeWindow ExtensionFunctionDispatcher::GetFrameNativeWindow() {
364 return delegate_ ? delegate_->GetFrameNativeWindow() : NULL;
365}