blob: a3f8a4ccfa9bd7466e98b8e00c1ed7e61ca7f902 [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]912256b32009-09-18 09:47:3510#include "chrome/browser/extensions/execute_code_in_tab_function.h"
[email protected]bfdffe2b2009-04-24 22:05:3511#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]f93914852009-05-26 06:05:4012#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
[email protected]ec9ac0df2009-10-01 18:06:4713#include "chrome/browser/extensions/extension_browser_actions_api.h"
[email protected]bfdffe2b2009-04-24 22:05:3514#include "chrome/browser/extensions/extension_function.h"
[email protected]de768a832009-10-30 05:25:0115#include "chrome/browser/extensions/extension_history_api.h"
[email protected]198bcfe2009-09-09 22:56:2816#include "chrome/browser/extensions/extension_i18n_api.h"
[email protected]e916901c2009-05-07 00:14:3117#include "chrome/browser/extensions/extension_message_service.h"
[email protected]f7f3a5f2009-05-01 22:02:3418#include "chrome/browser/extensions/extension_page_actions_module.h"
[email protected]f93914852009-05-26 06:05:4019#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
[email protected]1c1c77a52009-11-03 00:37:3120#include "chrome/browser/extensions/extension_popup_api.h"
[email protected]45776222009-07-15 20:21:5821#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]bfdffe2b2009-04-24 22:05:3522#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4023#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1424#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4325#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]e916901c2009-05-07 00:14:3126#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3527#include "chrome/browser/renderer_host/render_process_host.h"
28#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1929#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3530#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4331#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3532
33// FactoryRegistry -------------------------------------------------------------
34
35namespace {
36
[email protected]b83e4602009-05-15 22:58:3337// Template for defining ExtensionFunctionFactory.
38template<class T>
39ExtensionFunction* NewExtensionFunction() {
40 return new T();
41}
[email protected]bfdffe2b2009-04-24 22:05:3542
[email protected]b83e4602009-05-15 22:58:3343// Contains a list of all known extension functions and allows clients to
44// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3545class FactoryRegistry {
46 public:
47 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3348 FactoryRegistry() { ResetFunctions(); }
49
50 // Resets all functions to their default values.
51 void ResetFunctions();
52
53 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3554 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3355
56 // Allows overriding of specific functions (e.g. for testing). Functions
57 // must be previously registered. Returns true if successful.
58 bool OverrideFunction(const std::string& name,
59 ExtensionFunctionFactory factory);
60
61 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3562 ExtensionFunction* NewFunction(const std::string& name);
63
64 private:
[email protected]61424c062009-10-14 23:14:5965 template<class T>
66 void RegisterFunction() {
67 factories_[T::function_name()] = &NewExtensionFunction<T>;
68 }
69
[email protected]bfdffe2b2009-04-24 22:05:3570 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
71 FactoryMap factories_;
72};
73
[email protected]bfdffe2b2009-04-24 22:05:3574FactoryRegistry* FactoryRegistry::instance() {
75 return Singleton<FactoryRegistry>::get();
76}
77
[email protected]b83e4602009-05-15 22:58:3378void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3579 // Register all functions here.
80
[email protected]e515f5d2009-05-05 03:05:0081 // Windows
[email protected]61424c062009-10-14 23:14:5982 RegisterFunction<GetWindowFunction>();
83 RegisterFunction<GetCurrentWindowFunction>();
84 RegisterFunction<GetLastFocusedWindowFunction>();
85 RegisterFunction<GetAllWindowsFunction>();
86 RegisterFunction<CreateWindowFunction>();
87 RegisterFunction<UpdateWindowFunction>();
88 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:3389
[email protected]e515f5d2009-05-05 03:05:0090 // Tabs
[email protected]61424c062009-10-14 23:14:5991 RegisterFunction<GetTabFunction>();
92 RegisterFunction<GetSelectedTabFunction>();
93 RegisterFunction<GetAllTabsInWindowFunction>();
94 RegisterFunction<CreateTabFunction>();
95 RegisterFunction<UpdateTabFunction>();
96 RegisterFunction<MoveTabFunction>();
97 RegisterFunction<RemoveTabFunction>();
98 RegisterFunction<DetectTabLanguageFunction>();
99 RegisterFunction<CaptureVisibleTabFunction>();
100 RegisterFunction<TabsExecuteScriptFunction>();
101 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35102
[email protected]f7f3a5f2009-05-01 22:02:34103 // Page Actions.
[email protected]61424c062009-10-14 23:14:59104 RegisterFunction<EnablePageActionFunction>();
105 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46106 RegisterFunction<PageActionShowFunction>();
107 RegisterFunction<PageActionHideFunction>();
108 RegisterFunction<PageActionSetIconFunction>();
109 RegisterFunction<PageActionSetTitleFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34110
[email protected]ec9ac0df2009-10-01 18:06:47111 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59112 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24113 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59114 RegisterFunction<BrowserActionSetBadgeTextFunction>();
115 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47116
[email protected]f7f3a5f2009-05-01 22:02:34117 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59118 RegisterFunction<GetBookmarksFunction>();
119 RegisterFunction<GetBookmarkChildrenFunction>();
120 RegisterFunction<GetBookmarkTreeFunction>();
121 RegisterFunction<SearchBookmarksFunction>();
122 RegisterFunction<RemoveBookmarkFunction>();
123 RegisterFunction<RemoveTreeBookmarkFunction>();
124 RegisterFunction<CreateBookmarkFunction>();
125 RegisterFunction<MoveBookmarkFunction>();
126 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43127
[email protected]de768a832009-10-30 05:25:01128 // History
129 RegisterFunction<AddUrlHistoryFunction>();
130 RegisterFunction<DeleteAllHistoryFunction>();
131 RegisterFunction<DeleteRangeHistoryFunction>();
132 RegisterFunction<DeleteUrlHistoryFunction>();
133 RegisterFunction<GetVisitsHistoryFunction>();
134 RegisterFunction<SearchHistoryFunction>();
135
[email protected]9c45b7182009-08-04 16:44:43136 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59137 RegisterFunction<ToolstripExpandFunction>();
138 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14139
[email protected]198bcfe2009-09-09 22:56:28140 // I18N.
[email protected]61424c062009-10-14 23:14:59141 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28142
[email protected]1c1c77a52009-11-03 00:37:31143 // Popup API.
144 RegisterFunction<PopupShowFunction>();
145
[email protected]25fd1b2e2009-08-17 20:57:14146 // Test.
[email protected]61424c062009-10-14 23:14:59147 RegisterFunction<ExtensionTestPassFunction>();
148 RegisterFunction<ExtensionTestFailFunction>();
149 RegisterFunction<ExtensionTestLogFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35150}
151
[email protected]b83e4602009-05-15 22:58:33152void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
153 for (FactoryMap::iterator iter = factories_.begin();
154 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35155 names->push_back(iter->first);
156 }
157}
158
[email protected]b83e4602009-05-15 22:58:33159bool FactoryRegistry::OverrideFunction(const std::string& name,
160 ExtensionFunctionFactory factory) {
161 FactoryMap::iterator iter = factories_.find(name);
162 if (iter == factories_.end()) {
163 return false;
164 } else {
165 iter->second = factory;
166 return true;
167 }
168}
169
[email protected]bfdffe2b2009-04-24 22:05:35170ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
171 FactoryMap::iterator iter = factories_.find(name);
172 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33173 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56174 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33175 return function;
[email protected]bfdffe2b2009-04-24 22:05:35176}
177
[email protected]b83e4602009-05-15 22:58:33178}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35179
180// ExtensionFunctionDispatcher -------------------------------------------------
181
182void ExtensionFunctionDispatcher::GetAllFunctionNames(
183 std::vector<std::string>* names) {
184 FactoryRegistry::instance()->GetAllNames(names);
185}
186
[email protected]b83e4602009-05-15 22:58:33187bool ExtensionFunctionDispatcher::OverrideFunction(
188 const std::string& name, ExtensionFunctionFactory factory) {
189 return FactoryRegistry::instance()->OverrideFunction(name, factory);
190}
191
192void ExtensionFunctionDispatcher::ResetFunctions() {
193 FactoryRegistry::instance()->ResetFunctions();
194}
195
[email protected]811bfe32009-07-01 08:46:25196std::set<ExtensionFunctionDispatcher*>*
197 ExtensionFunctionDispatcher::all_instances() {
198 static std::set<ExtensionFunctionDispatcher*> instances;
199 return &instances;
200}
201
[email protected]bfdffe2b2009-04-24 22:05:35202ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
203 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12204 Delegate* delegate,
[email protected]811bfe32009-07-01 08:46:25205 const GURL& url)
[email protected]bfdffe2b2009-04-24 22:05:35206 : render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12207 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25208 url_(url),
[email protected]32dda362009-06-05 19:07:01209 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43210 // TODO(erikkay) should we do something for these errors in Release?
211 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19212
213 Extension* extension =
214 profile()->GetExtensionsService()->GetExtensionByURL(url);
215 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43216
[email protected]811bfe32009-07-01 08:46:25217 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35218
[email protected]45776222009-07-15 20:21:58219 // Notify the ExtensionProcessManager that the view was created.
220 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
221 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45222 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19223
224 // Update the extension permissions. Doing this each time we create an EFD
225 // ensures that new processes are informed of permissions for newly installed
226 // extensions.
[email protected]cccf90932009-08-23 17:56:25227 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19228 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25229 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
230 extension->url(), extension->host_permissions()));
[email protected]bfdffe2b2009-04-24 22:05:35231}
232
[email protected]32dda362009-06-05 19:07:01233ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25234 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01235 peer_->dispatcher_ = NULL;
236}
237
[email protected]7eecaed52009-05-07 21:44:12238Browser* ExtensionFunctionDispatcher::GetBrowser() {
[email protected]4ba60e22009-08-14 21:02:11239 return delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12240}
241
[email protected]9c45b7182009-08-04 16:44:43242ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
[email protected]9c45b7182009-08-04 16:44:43243 return delegate_->GetExtensionHost();
244}
245
[email protected]c7ad50f2009-09-11 06:28:15246Extension* ExtensionFunctionDispatcher::GetExtension() {
247 ExtensionsService* service = profile()->GetExtensionsService();
248 DCHECK(service);
249
250 Extension* extension = service->GetExtensionById(extension_id());
251 DCHECK(extension);
252
253 return extension;
254}
255
[email protected]bfdffe2b2009-04-24 22:05:35256void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58257 const Value* args,
[email protected]c6619182009-05-12 14:59:32258 int request_id,
259 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01260 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35261 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01262 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33263 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32264 function->set_request_id(request_id);
265 function->set_has_callback(has_callback);
[email protected]bfdffe2b2009-04-24 22:05:35266 function->Run();
267}
268
[email protected]c6619182009-05-12 14:59:32269void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
270 bool success) {
[email protected]c6619182009-05-12 14:59:32271 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33272 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35273}
274
275void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14276 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45277 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35278 " : terminating renderer.";
279 if (RenderProcessHost::run_renderer_in_process()) {
280 // In single process mode it is better if we don't suicide but just crash.
281 CHECK(false);
282 } else {
283 NOTREACHED();
284 base::KillProcess(render_view_host_->process()->process().handle(),
285 ResultCodes::KILLED_BAD_MESSAGE, false);
286 }
287}
288
289Profile* ExtensionFunctionDispatcher::profile() {
290 return render_view_host_->process()->profile();
291}