blob: d7105cb2d810e2cba2f160fe9c8ac0c55a187c6a [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]45776222009-07-15 20:21:5820#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]bfdffe2b2009-04-24 22:05:3521#include "chrome/browser/extensions/extension_tabs_module.h"
[email protected]f93914852009-05-26 06:05:4022#include "chrome/browser/extensions/extension_tabs_module_constants.h"
[email protected]25fd1b2e2009-08-17 20:57:1423#include "chrome/browser/extensions/extension_test_api.h"
[email protected]9c45b7182009-08-04 16:44:4324#include "chrome/browser/extensions/extension_toolstrip_api.h"
[email protected]e916901c2009-05-07 00:14:3125#include "chrome/browser/profile.h"
[email protected]bfdffe2b2009-04-24 22:05:3526#include "chrome/browser/renderer_host/render_process_host.h"
27#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]35506352009-08-07 18:58:1928#include "chrome/common/render_messages.h"
[email protected]bfdffe2b2009-04-24 22:05:3529#include "chrome/common/result_codes.h"
[email protected]9c45b7182009-08-04 16:44:4330#include "chrome/common/url_constants.h"
[email protected]bfdffe2b2009-04-24 22:05:3531
32// FactoryRegistry -------------------------------------------------------------
33
34namespace {
35
[email protected]b83e4602009-05-15 22:58:3336// Template for defining ExtensionFunctionFactory.
37template<class T>
38ExtensionFunction* NewExtensionFunction() {
39 return new T();
40}
[email protected]bfdffe2b2009-04-24 22:05:3541
[email protected]b83e4602009-05-15 22:58:3342// Contains a list of all known extension functions and allows clients to
43// create instances of them.
[email protected]bfdffe2b2009-04-24 22:05:3544class FactoryRegistry {
45 public:
46 static FactoryRegistry* instance();
[email protected]b83e4602009-05-15 22:58:3347 FactoryRegistry() { ResetFunctions(); }
48
49 // Resets all functions to their default values.
50 void ResetFunctions();
51
52 // Adds all function names to 'names'.
[email protected]bfdffe2b2009-04-24 22:05:3553 void GetAllNames(std::vector<std::string>* names);
[email protected]b83e4602009-05-15 22:58:3354
55 // Allows overriding of specific functions (e.g. for testing). Functions
56 // must be previously registered. Returns true if successful.
57 bool OverrideFunction(const std::string& name,
58 ExtensionFunctionFactory factory);
59
60 // Factory method for the ExtensionFunction registered as 'name'.
[email protected]bfdffe2b2009-04-24 22:05:3561 ExtensionFunction* NewFunction(const std::string& name);
62
63 private:
[email protected]61424c062009-10-14 23:14:5964 template<class T>
65 void RegisterFunction() {
66 factories_[T::function_name()] = &NewExtensionFunction<T>;
67 }
68
[email protected]bfdffe2b2009-04-24 22:05:3569 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
70 FactoryMap factories_;
71};
72
[email protected]bfdffe2b2009-04-24 22:05:3573FactoryRegistry* FactoryRegistry::instance() {
74 return Singleton<FactoryRegistry>::get();
75}
76
[email protected]b83e4602009-05-15 22:58:3377void FactoryRegistry::ResetFunctions() {
[email protected]bfdffe2b2009-04-24 22:05:3578 // Register all functions here.
79
[email protected]e515f5d2009-05-05 03:05:0080 // Windows
[email protected]61424c062009-10-14 23:14:5981 RegisterFunction<GetWindowFunction>();
82 RegisterFunction<GetCurrentWindowFunction>();
83 RegisterFunction<GetLastFocusedWindowFunction>();
84 RegisterFunction<GetAllWindowsFunction>();
85 RegisterFunction<CreateWindowFunction>();
86 RegisterFunction<UpdateWindowFunction>();
87 RegisterFunction<RemoveWindowFunction>();
[email protected]b83e4602009-05-15 22:58:3388
[email protected]e515f5d2009-05-05 03:05:0089 // Tabs
[email protected]61424c062009-10-14 23:14:5990 RegisterFunction<GetTabFunction>();
91 RegisterFunction<GetSelectedTabFunction>();
92 RegisterFunction<GetAllTabsInWindowFunction>();
93 RegisterFunction<CreateTabFunction>();
94 RegisterFunction<UpdateTabFunction>();
95 RegisterFunction<MoveTabFunction>();
96 RegisterFunction<RemoveTabFunction>();
97 RegisterFunction<DetectTabLanguageFunction>();
98 RegisterFunction<CaptureVisibleTabFunction>();
99 RegisterFunction<TabsExecuteScriptFunction>();
100 RegisterFunction<TabsInsertCSSFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35101
[email protected]f7f3a5f2009-05-01 22:02:34102 // Page Actions.
[email protected]61424c062009-10-14 23:14:59103 RegisterFunction<EnablePageActionFunction>();
104 RegisterFunction<DisablePageActionFunction>();
[email protected]744ef172009-10-16 21:53:46105 RegisterFunction<PageActionShowFunction>();
106 RegisterFunction<PageActionHideFunction>();
107 RegisterFunction<PageActionSetIconFunction>();
108 RegisterFunction<PageActionSetTitleFunction>();
[email protected]f7f3a5f2009-05-01 22:02:34109
[email protected]ec9ac0df2009-10-01 18:06:47110 // Browser Actions.
[email protected]61424c062009-10-14 23:14:59111 RegisterFunction<BrowserActionSetIconFunction>();
[email protected]1288ba02009-10-15 00:02:24112 RegisterFunction<BrowserActionSetTitleFunction>();
[email protected]61424c062009-10-14 23:14:59113 RegisterFunction<BrowserActionSetBadgeTextFunction>();
114 RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
[email protected]ec9ac0df2009-10-01 18:06:47115
[email protected]f7f3a5f2009-05-01 22:02:34116 // Bookmarks.
[email protected]61424c062009-10-14 23:14:59117 RegisterFunction<GetBookmarksFunction>();
118 RegisterFunction<GetBookmarkChildrenFunction>();
119 RegisterFunction<GetBookmarkTreeFunction>();
120 RegisterFunction<SearchBookmarksFunction>();
121 RegisterFunction<RemoveBookmarkFunction>();
122 RegisterFunction<RemoveTreeBookmarkFunction>();
123 RegisterFunction<CreateBookmarkFunction>();
124 RegisterFunction<MoveBookmarkFunction>();
125 RegisterFunction<UpdateBookmarkFunction>();
[email protected]9c45b7182009-08-04 16:44:43126
[email protected]de768a832009-10-30 05:25:01127 // History
128 RegisterFunction<AddUrlHistoryFunction>();
129 RegisterFunction<DeleteAllHistoryFunction>();
130 RegisterFunction<DeleteRangeHistoryFunction>();
131 RegisterFunction<DeleteUrlHistoryFunction>();
132 RegisterFunction<GetVisitsHistoryFunction>();
133 RegisterFunction<SearchHistoryFunction>();
134
[email protected]9c45b7182009-08-04 16:44:43135 // Toolstrips.
[email protected]61424c062009-10-14 23:14:59136 RegisterFunction<ToolstripExpandFunction>();
137 RegisterFunction<ToolstripCollapseFunction>();
[email protected]25fd1b2e2009-08-17 20:57:14138
[email protected]198bcfe2009-09-09 22:56:28139 // I18N.
[email protected]61424c062009-10-14 23:14:59140 RegisterFunction<GetAcceptLanguagesFunction>();
[email protected]198bcfe2009-09-09 22:56:28141
[email protected]25fd1b2e2009-08-17 20:57:14142 // Test.
[email protected]61424c062009-10-14 23:14:59143 RegisterFunction<ExtensionTestPassFunction>();
144 RegisterFunction<ExtensionTestFailFunction>();
145 RegisterFunction<ExtensionTestLogFunction>();
[email protected]bfdffe2b2009-04-24 22:05:35146}
147
[email protected]b83e4602009-05-15 22:58:33148void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
149 for (FactoryMap::iterator iter = factories_.begin();
150 iter != factories_.end(); ++iter) {
[email protected]bfdffe2b2009-04-24 22:05:35151 names->push_back(iter->first);
152 }
153}
154
[email protected]b83e4602009-05-15 22:58:33155bool FactoryRegistry::OverrideFunction(const std::string& name,
156 ExtensionFunctionFactory factory) {
157 FactoryMap::iterator iter = factories_.find(name);
158 if (iter == factories_.end()) {
159 return false;
160 } else {
161 iter->second = factory;
162 return true;
163 }
164}
165
[email protected]bfdffe2b2009-04-24 22:05:35166ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
167 FactoryMap::iterator iter = factories_.find(name);
168 DCHECK(iter != factories_.end());
[email protected]b83e4602009-05-15 22:58:33169 ExtensionFunction* function = iter->second();
[email protected]76a3db852009-07-24 02:14:56170 function->set_name(name);
[email protected]b83e4602009-05-15 22:58:33171 return function;
[email protected]bfdffe2b2009-04-24 22:05:35172}
173
[email protected]b83e4602009-05-15 22:58:33174}; // namespace
[email protected]bfdffe2b2009-04-24 22:05:35175
176// ExtensionFunctionDispatcher -------------------------------------------------
177
178void ExtensionFunctionDispatcher::GetAllFunctionNames(
179 std::vector<std::string>* names) {
180 FactoryRegistry::instance()->GetAllNames(names);
181}
182
[email protected]b83e4602009-05-15 22:58:33183bool ExtensionFunctionDispatcher::OverrideFunction(
184 const std::string& name, ExtensionFunctionFactory factory) {
185 return FactoryRegistry::instance()->OverrideFunction(name, factory);
186}
187
188void ExtensionFunctionDispatcher::ResetFunctions() {
189 FactoryRegistry::instance()->ResetFunctions();
190}
191
[email protected]811bfe32009-07-01 08:46:25192std::set<ExtensionFunctionDispatcher*>*
193 ExtensionFunctionDispatcher::all_instances() {
194 static std::set<ExtensionFunctionDispatcher*> instances;
195 return &instances;
196}
197
[email protected]bfdffe2b2009-04-24 22:05:35198ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
199 RenderViewHost* render_view_host,
[email protected]7eecaed52009-05-07 21:44:12200 Delegate* delegate,
[email protected]811bfe32009-07-01 08:46:25201 const GURL& url)
[email protected]bfdffe2b2009-04-24 22:05:35202 : render_view_host_(render_view_host),
[email protected]7eecaed52009-05-07 21:44:12203 delegate_(delegate),
[email protected]811bfe32009-07-01 08:46:25204 url_(url),
[email protected]32dda362009-06-05 19:07:01205 ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
[email protected]9c45b7182009-08-04 16:44:43206 // TODO(erikkay) should we do something for these errors in Release?
207 DCHECK(url.SchemeIs(chrome::kExtensionScheme));
[email protected]35506352009-08-07 18:58:19208
209 Extension* extension =
210 profile()->GetExtensionsService()->GetExtensionByURL(url);
211 DCHECK(extension);
[email protected]9c45b7182009-08-04 16:44:43212
[email protected]811bfe32009-07-01 08:46:25213 all_instances()->insert(this);
[email protected]0f6053962009-07-09 19:26:35214
[email protected]45776222009-07-15 20:21:58215 // Notify the ExtensionProcessManager that the view was created.
216 ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
217 epm->RegisterExtensionProcess(extension_id(),
[email protected]76543b92009-08-31 17:27:45218 render_view_host->process()->id());
[email protected]35506352009-08-07 18:58:19219
220 // Update the extension permissions. Doing this each time we create an EFD
221 // ensures that new processes are informed of permissions for newly installed
222 // extensions.
[email protected]cccf90932009-08-23 17:56:25223 render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
[email protected]35506352009-08-07 18:58:19224 extension->id(), extension->api_permissions()));
[email protected]cccf90932009-08-23 17:56:25225 render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
226 extension->url(), extension->host_permissions()));
[email protected]bfdffe2b2009-04-24 22:05:35227}
228
[email protected]32dda362009-06-05 19:07:01229ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
[email protected]811bfe32009-07-01 08:46:25230 all_instances()->erase(this);
[email protected]32dda362009-06-05 19:07:01231 peer_->dispatcher_ = NULL;
232}
233
[email protected]7eecaed52009-05-07 21:44:12234Browser* ExtensionFunctionDispatcher::GetBrowser() {
[email protected]4ba60e22009-08-14 21:02:11235 return delegate_->GetBrowser();
[email protected]7eecaed52009-05-07 21:44:12236}
237
[email protected]9c45b7182009-08-04 16:44:43238ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
[email protected]9c45b7182009-08-04 16:44:43239 return delegate_->GetExtensionHost();
240}
241
[email protected]c7ad50f2009-09-11 06:28:15242Extension* ExtensionFunctionDispatcher::GetExtension() {
243 ExtensionsService* service = profile()->GetExtensionsService();
244 DCHECK(service);
245
246 Extension* extension = service->GetExtensionById(extension_id());
247 DCHECK(extension);
248
249 return extension;
250}
251
[email protected]bfdffe2b2009-04-24 22:05:35252void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
[email protected]e4dad9fb2009-10-06 18:15:58253 const Value* args,
[email protected]c6619182009-05-12 14:59:32254 int request_id,
255 bool has_callback) {
[email protected]32dda362009-06-05 19:07:01256 scoped_refptr<ExtensionFunction> function(
[email protected]bfdffe2b2009-04-24 22:05:35257 FactoryRegistry::instance()->NewFunction(name));
[email protected]32dda362009-06-05 19:07:01258 function->set_dispatcher_peer(peer_);
[email protected]b83e4602009-05-15 22:58:33259 function->SetArgs(args);
[email protected]c6619182009-05-12 14:59:32260 function->set_request_id(request_id);
261 function->set_has_callback(has_callback);
[email protected]bfdffe2b2009-04-24 22:05:35262 function->Run();
263}
264
[email protected]c6619182009-05-12 14:59:32265void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
266 bool success) {
[email protected]c6619182009-05-12 14:59:32267 render_view_host_->SendExtensionResponse(function->request_id(), success,
[email protected]b83e4602009-05-15 22:58:33268 function->GetResult(), function->GetError());
[email protected]bfdffe2b2009-04-24 22:05:35269}
270
271void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
[email protected]25fd1b2e2009-08-17 20:57:14272 LOG(ERROR) << "bad extension message " <<
[email protected]76543b92009-08-31 17:27:45273 api->name() <<
[email protected]bfdffe2b2009-04-24 22:05:35274 " : terminating renderer.";
275 if (RenderProcessHost::run_renderer_in_process()) {
276 // In single process mode it is better if we don't suicide but just crash.
277 CHECK(false);
278 } else {
279 NOTREACHED();
280 base::KillProcess(render_view_host_->process()->process().handle(),
281 ResultCodes::KILLED_BAD_MESSAGE, false);
282 }
283}
284
285Profile* ExtensionFunctionDispatcher::profile() {
286 return render_view_host_->process()->profile();
287}