blob: ba8595678d8450dbae7d44dbf485e4bc26246b53 [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]381162b2010-01-28 17:29:3526#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3527#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4028#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1429#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4330#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]d13950e2009-12-04 01:43:0231#include "chrome/browser/extensions/extensions_quota_service.h"
[email protected]b1748b1d82009-11-30 20:32:5632#include "chrome/browser/extensions/extensions_service.h"
[email protected]e916901c2009-05-07 00:14:3133#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3534#include "chrome/browser/renderer_host/render_process_host.h"
35#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1936#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3537#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4338#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3539
40// FactoryRegistry -------------------------------------------------------------
41
42namespace {
43
[email protected]b83e4602009-05-15 22:58:3344// Template for defining ExtensionFunctionFactory.
45template<class T>
46ExtensionFunction* NewExtensionFunction() {
47 return new T();
48}
[email protected]bfdffe2b2009-04-24 22:05:3549
[email protected]b83e4602009-05-15 22:58:3350// Contains a list of all known extension functions and allows clients to
51// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3552class FactoryRegistry {
53 public:
54 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3355 FactoryRegistry() { ResetFunctions(); }
56
57 // Resets all functions to their default values.
58 void ResetFunctions();
59
60 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3561 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3362
63 // Allows overriding of specific functions (e.g. for testing). Functions
64 // must be previously registered. Returns true if successful.
65 bool OverrideFunction(const std::string& name,
66 ExtensionFunctionFactory factory);
67
68 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3569 ExtensionFunction* NewFunction(const std::string& name);
70
71 private:
[email protected]61424c062009-10-14 23:14:5972 template<class T>
73 void RegisterFunction() {
74 factories_[T::function_name()] = &NewExtensionFunction<T>;
75 }
76
[email protected]bfdffe2b2009-04-24 22:05:3577 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
78 FactoryMap factories_;
79};
80
[email protected]bfdffe2b2009-04-24 22:05:3581FactoryRegistry* FactoryRegistry::instance() {
82 return Singleton<FactoryRegistry>::get();
83}
84
[email protected]b83e4602009-05-15 22:58:3385void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3586 // Register all functions here.
87
[email protected]e515f5d2009-05-05 03:05:0088 // Windows
[email protected]61424c062009-10-14 23:14:5989 RegisterFunction<GetWindowFunction>();
90 RegisterFunction<GetCurrentWindowFunction>();
91 RegisterFunction<GetLastFocusedWindowFunction>();
92 RegisterFunction<GetAllWindowsFunction>();
93 RegisterFunction<CreateWindowFunction>();
94 RegisterFunction<UpdateWindowFunction>();
95 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:3396
[email protected]e515f5d2009-05-05 03:05:0097 // Tabs
[email protected]61424c062009-10-14 23:14:5998 RegisterFunction<GetTabFunction>();
99 RegisterFunction<GetSelectedTabFunction>();
100 RegisterFunction<GetAllTabsInWindowFunction>();
101 RegisterFunction<CreateTabFunction>();
102 RegisterFunction<UpdateTabFunction>();
103 RegisterFunction<MoveTabFunction>();
104 RegisterFunction<RemoveTabFunction>();
105 RegisterFunction<DetectTabLanguageFunction>();
106 RegisterFunction<CaptureVisibleTabFunction>();
107 RegisterFunction<TabsExecuteScriptFunction>();
108 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35109
[email protected]f7f3a5f2009-05-01 22:02:34110 // Page Actions.
[email protected]61424c062009-10-14 23:14:59111 RegisterFunction<EnablePageActionFunction>();
112 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46113 RegisterFunction<PageActionShowFunction>();
114 RegisterFunction<PageActionHideFunction>();
115 RegisterFunction<PageActionSetIconFunction>();
116 RegisterFunction<PageActionSetTitleFunction>();
[email protected]e478d6702010-01-28 00:10:29117 RegisterFunction<PageActionSetPopupFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34118
[email protected]ec9ac0df2009-10-01 18:06:47119 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59120 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24121 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59122 RegisterFunction<BrowserActionSetBadgeTextFunction>();
123 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47124
[email protected]f7f3a5f2009-05-01 22:02:34125 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59126 RegisterFunction<GetBookmarksFunction>();
127 RegisterFunction<GetBookmarkChildrenFunction>();
[email protected]a3c94c712009-12-18 19:23:55128 RegisterFunction<GetBookmarkRecentFunction>();
[email protected]61424c062009-10-14 23:14:59129 RegisterFunction<GetBookmarkTreeFunction>();
130 RegisterFunction<SearchBookmarksFunction>();
131 RegisterFunction<RemoveBookmarkFunction>();
132 RegisterFunction<RemoveTreeBookmarkFunction>();
133 RegisterFunction<CreateBookmarkFunction>();
134 RegisterFunction<MoveBookmarkFunction>();
135 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43136
[email protected]9dd97bc2010-01-14 01:40:04137 // BookmarkManager
138 RegisterFunction<CopyBookmarkManagerFunction>();
139 RegisterFunction<CutBookmarkManagerFunction>();
140 RegisterFunction<PasteBookmarkManagerFunction>();
[email protected]cb6cf792010-01-28 00:04:56141 RegisterFunction<ImportBookmarksFunction>();
142 RegisterFunction<ExportBookmarksFunction>();
[email protected]9dd97bc2010-01-14 01:40:04143 RegisterFunction<BookmarkManagerGetStringsFunction>();
144
[email protected]de768a832009-10-30 05:25:01145 // History
146 RegisterFunction<AddUrlHistoryFunction>();
147 RegisterFunction<DeleteAllHistoryFunction>();
148 RegisterFunction<DeleteRangeHistoryFunction>();
149 RegisterFunction<DeleteUrlHistoryFunction>();
150 RegisterFunction<GetVisitsHistoryFunction>();
151 RegisterFunction<SearchHistoryFunction>();
152
[email protected]9c45b7182009-08-04 16:44:43153 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59154 RegisterFunction<ToolstripExpandFunction>();
155 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14156
[email protected]198bcfe2009-09-09 22:56:28157 // I18N.
[email protected]61424c062009-10-14 23:14:59158 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28159
[email protected]1c1c77a52009-11-03 00:37:31160 // Popup API.
161 RegisterFunction<PopupShowFunction>();
162
[email protected]381162b2010-01-28 17:29:35163 // Processes.
164 RegisterFunction<GetProcessForTabFunction>();
165
[email protected]25fd1b2e2009-08-17 20:57:14166 // Test.
[email protected]61424c062009-10-14 23:14:59167 RegisterFunction<ExtensionTestPassFunction>();
168 RegisterFunction<ExtensionTestFailFunction>();
169 RegisterFunction<ExtensionTestLogFunction>();
[email protected]d13950e2009-12-04 01:43:02170 RegisterFunction<ExtensionTestQuotaResetFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35171}
172
[email protected]b83e4602009-05-15 22:58:33173void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
174 for (FactoryMap::iterator iter = factories_.begin();
175 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35176 names->push_back(iter->first);
177 }
178}
179
[email protected]b83e4602009-05-15 22:58:33180bool FactoryRegistry::OverrideFunction(const std::string& name,
181 ExtensionFunctionFactory factory) {
182 FactoryMap::iterator iter = factories_.find(name);
183 if (iter == factories_.end()) {
184 return false;
185 } else {
186 iter->second = factory;
187 return true;
188 }
189}
190
[email protected]bfdffe2b2009-04-24 22:05:35191ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
192 FactoryMap::iterator iter = factories_.find(name);
193 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33194 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56195 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33196 return function;
[email protected]bfdffe2b2009-04-24 22:05:35197}
198
[email protected]b83e4602009-05-15 22:58:33199}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35200
[email protected]a95631cb2009-12-10 01:59:11201// ExtensionFunctionDispatcher::Delegate ---------------------------------------
202
203gfx::NativeWindow ExtensionFunctionDispatcher::Delegate::
204 GetFrameNativeWindow() {
205 Browser* browser = GetBrowser();
206 // If a browser is bound to this dispatcher, then return the widget hosting
207 // the window. Extensions hosted in ExternalTabContainer objects may not
208 // have a running browser instance.
209 if (browser)
210 return browser->window()->GetNativeHandle();
211
212 return NULL;
213}
214
[email protected]bfdffe2b2009-04-24 22:05:35215// ExtensionFunctionDispatcher -------------------------------------------------
216
217void ExtensionFunctionDispatcher::GetAllFunctionNames(
218 std::vector<std::string>* names) {
219 FactoryRegistry::instance()->GetAllNames(names);
220}
221
[email protected]b83e4602009-05-15 22:58:33222bool ExtensionFunctionDispatcher::OverrideFunction(
223 const std::string& name, ExtensionFunctionFactory factory) {
224 return FactoryRegistry::instance()->OverrideFunction(name, factory);
225}
226
227void ExtensionFunctionDispatcher::ResetFunctions() {
228 FactoryRegistry::instance()->ResetFunctions();
229}
230
[email protected]811bfe32009-07-01 08:46:25231std::set<ExtensionFunctionDispatcher*>*
232 ExtensionFunctionDispatcher::all_instances() {
233 static std::set<ExtensionFunctionDispatcher*> instances;
234 return &instances;
235}
236
[email protected]bfdffe2b2009-04-24 22:05:35237ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
238 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12239 Delegate* delegate,
[email protected]811bfe32009-07-01 08:46:25240 const GURL& url)
[email protected]bfdffe2b2009-04-24 22:05:35241 : render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12242 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25243 url_(url),
[email protected]32dda362009-06-05 19:07:01244 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43245 // TODO(erikkay) should we do something for these errors in Release?
246 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19247
248 Extension* extension =
249 profile()->GetExtensionsService()->GetExtensionByURL(url);
250 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43251
[email protected]811bfe32009-07-01 08:46:25252 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35253
[email protected]45776222009-07-15 20:21:58254 // Notify the ExtensionProcessManager that the view was created.
255 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
256 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45257 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19258
259 // Update the extension permissions. Doing this each time we create an EFD
260 // ensures that new processes are informed of permissions for newly installed
261 // extensions.
[email protected]cccf90932009-08-23 17:56:25262 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19263 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25264 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
265 extension->url(), extension->host_permissions()));
[email protected]bfdffe2b2009-04-24 22:05:35266}
267
[email protected]32dda362009-06-05 19:07:01268ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25269 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01270 peer_->dispatcher_ = NULL;
271}
272
[email protected]7eecaed52009-05-07 21:44:12273Browser* ExtensionFunctionDispatcher::GetBrowser() {
[email protected]4ba60e22009-08-14 21:02:11274 return delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12275}
276
[email protected]b27257562009-11-16 23:28:26277ExtensionPopupHost* ExtensionFunctionDispatcher::GetPopupHost() {
278 ExtensionHost* extension_host = GetExtensionHost();
279 if (extension_host) {
280 DCHECK(!GetExtensionDOMUI()) <<
281 "Function dispatcher registered in too many environments.";
282 return extension_host->popup_host();
283 } else {
284 ExtensionDOMUI* dom_ui = GetExtensionDOMUI();
285 return dom_ui->popup_host();
286 }
287}
288
[email protected]9c45b7182009-08-04 16:44:43289ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
[email protected]9c45b7182009-08-04 16:44:43290 return delegate_->GetExtensionHost();
291}
292
[email protected]b27257562009-11-16 23:28:26293ExtensionDOMUI* ExtensionFunctionDispatcher::GetExtensionDOMUI() {
294 return delegate_->GetExtensionDOMUI();
295}
296
[email protected]c7ad50f2009-09-11 06:28:15297Extension* ExtensionFunctionDispatcher::GetExtension() {
298 ExtensionsService* service = profile()->GetExtensionsService();
299 DCHECK(service);
300
[email protected]61b411612009-11-10 23:17:41301 Extension* extension = service->GetExtensionById(extension_id(), false);
[email protected]c7ad50f2009-09-11 06:28:15302 DCHECK(extension);
303
304 return extension;
305}
306
[email protected]bfdffe2b2009-04-24 22:05:35307void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58308 const Value* args,
[email protected]c6619182009-05-12 14:59:32309 int request_id,
310 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01311 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35312 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01313 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33314 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32315 function->set_request_id(request_id);
316 function->set_has_callback(has_callback);
[email protected]d13950e2009-12-04 01:43:02317
318 ExtensionsService* service = profile()->GetExtensionsService();
319 DCHECK(service);
320 ExtensionsQuotaService* quota = service->quota_service();
321 if (quota->Assess(extension_id(), function, args, base::TimeTicks::Now())) {
322 function->Run();
323 } else {
324 render_view_host_->SendExtensionResponse(function->request_id(), false,
325 std::string(), QuotaLimitHeuristic::kGenericOverQuotaError);
326 }
[email protected]bfdffe2b2009-04-24 22:05:35327}
328
[email protected]c6619182009-05-12 14:59:32329void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
330 bool success) {
[email protected]c6619182009-05-12 14:59:32331 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33332 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35333}
334
335void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14336 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45337 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35338 " : terminating renderer.";
339 if (RenderProcessHost::run_renderer_in_process()) {
340 // In single process mode it is better if we don't suicide but just crash.
341 CHECK(false);
342 } else {
343 NOTREACHED();
[email protected]201b2732009-11-13 18:57:46344 base::KillProcess(render_view_host_->process()->GetHandle(),
[email protected]bfdffe2b2009-04-24 22:05:35345 ResultCodes::KILLED_BAD_MESSAGE, false);
346 }
347}
348
349Profile* ExtensionFunctionDispatcher::profile() {
350 return render_view_host_->process()->profile();
351}
[email protected]a95631cb2009-12-10 01:59:11352
353gfx::NativeWindow ExtensionFunctionDispatcher::GetFrameNativeWindow() {
354 return delegate_ ? delegate_->GetFrameNativeWindow() : NULL;
355}