1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "base/process_util.h"
#include "base/singleton.h"
#include "base/values.h"
#include "chrome/browser/extensions/execute_code_in_tab_function.h"
#include "chrome/browser/extensions/extension_bookmarks_module.h"
#include "chrome/browser/extensions/extension_bookmarks_module_constants.h"
#include "chrome/browser/extensions/extension_browser_actions_api.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/extensions/extension_i18n_api.h"
#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/extensions/extension_page_actions_module.h"
#include "chrome/browser/extensions/extension_page_actions_module_constants.h"
#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/extensions/extension_tabs_module_constants.h"
#include "chrome/browser/extensions/extension_test_api.h"
#include "chrome/browser/extensions/extension_toolstrip_api.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/result_codes.h"
#include "chrome/common/url_constants.h"
// FactoryRegistry -------------------------------------------------------------
namespace {
// Template for defining ExtensionFunctionFactory.
template<class T>
ExtensionFunction* NewExtensionFunction() {
return new T();
}
// Contains a list of all known extension functions and allows clients to
// create instances of them.
class FactoryRegistry {
public:
static FactoryRegistry* instance();
FactoryRegistry() { ResetFunctions(); }
// Resets all functions to their default values.
void ResetFunctions();
// Adds all function names to 'names'.
void GetAllNames(std::vector<std::string>* names);
// Allows overriding of specific functions (e.g. for testing). Functions
// must be previously registered. Returns true if successful.
bool OverrideFunction(const std::string& name,
ExtensionFunctionFactory factory);
// Factory method for the ExtensionFunction registered as 'name'.
ExtensionFunction* NewFunction(const std::string& name);
private:
template<class T>
void RegisterFunction() {
factories_[T::function_name()] = &NewExtensionFunction<T>;
}
typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap;
FactoryMap factories_;
};
FactoryRegistry* FactoryRegistry::instance() {
return Singleton<FactoryRegistry>::get();
}
void FactoryRegistry::ResetFunctions() {
// Register all functions here.
// Windows
RegisterFunction<GetWindowFunction>();
RegisterFunction<GetCurrentWindowFunction>();
RegisterFunction<GetLastFocusedWindowFunction>();
RegisterFunction<GetAllWindowsFunction>();
RegisterFunction<CreateWindowFunction>();
RegisterFunction<UpdateWindowFunction>();
RegisterFunction<RemoveWindowFunction>();
// Tabs
RegisterFunction<GetTabFunction>();
RegisterFunction<GetSelectedTabFunction>();
RegisterFunction<GetAllTabsInWindowFunction>();
RegisterFunction<CreateTabFunction>();
RegisterFunction<UpdateTabFunction>();
RegisterFunction<MoveTabFunction>();
RegisterFunction<RemoveTabFunction>();
RegisterFunction<DetectTabLanguageFunction>();
RegisterFunction<CaptureVisibleTabFunction>();
RegisterFunction<TabsExecuteScriptFunction>();
RegisterFunction<TabsInsertCSSFunction>();
// Page Actions.
RegisterFunction<EnablePageActionFunction>();
RegisterFunction<DisablePageActionFunction>();
// Browser Actions.
RegisterFunction<BrowserActionSetNameFunction>();
RegisterFunction<BrowserActionSetIconFunction>();
RegisterFunction<BrowserActionSetBadgeTextFunction>();
RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
// Bookmarks.
RegisterFunction<GetBookmarksFunction>();
RegisterFunction<GetBookmarkChildrenFunction>();
RegisterFunction<GetBookmarkTreeFunction>();
RegisterFunction<SearchBookmarksFunction>();
RegisterFunction<RemoveBookmarkFunction>();
RegisterFunction<RemoveTreeBookmarkFunction>();
RegisterFunction<CreateBookmarkFunction>();
RegisterFunction<MoveBookmarkFunction>();
RegisterFunction<UpdateBookmarkFunction>();
// Toolstrips.
RegisterFunction<ToolstripExpandFunction>();
RegisterFunction<ToolstripCollapseFunction>();
// I18N.
RegisterFunction<GetAcceptLanguagesFunction>();
// Test.
RegisterFunction<ExtensionTestPassFunction>();
RegisterFunction<ExtensionTestFailFunction>();
RegisterFunction<ExtensionTestLogFunction>();
}
void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
for (FactoryMap::iterator iter = factories_.begin();
iter != factories_.end(); ++iter) {
names->push_back(iter->first);
}
}
bool FactoryRegistry::OverrideFunction(const std::string& name,
ExtensionFunctionFactory factory) {
FactoryMap::iterator iter = factories_.find(name);
if (iter == factories_.end()) {
return false;
} else {
iter->second = factory;
return true;
}
}
ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) {
FactoryMap::iterator iter = factories_.find(name);
DCHECK(iter != factories_.end());
ExtensionFunction* function = iter->second();
function->set_name(name);
return function;
}
}; // namespace
// ExtensionFunctionDispatcher -------------------------------------------------
void ExtensionFunctionDispatcher::GetAllFunctionNames(
std::vector<std::string>* names) {
FactoryRegistry::instance()->GetAllNames(names);
}
bool ExtensionFunctionDispatcher::OverrideFunction(
const std::string& name, ExtensionFunctionFactory factory) {
return FactoryRegistry::instance()->OverrideFunction(name, factory);
}
void ExtensionFunctionDispatcher::ResetFunctions() {
FactoryRegistry::instance()->ResetFunctions();
}
std::set<ExtensionFunctionDispatcher*>*
ExtensionFunctionDispatcher::all_instances() {
static std::set<ExtensionFunctionDispatcher*> instances;
return &instances;
}
ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(
RenderViewHost* render_view_host,
Delegate* delegate,
const GURL& url)
: render_view_host_(render_view_host),
delegate_(delegate),
url_(url),
ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) {
// TODO(erikkay) should we do something for these errors in Release?
DCHECK(url.SchemeIs(chrome::kExtensionScheme));
Extension* extension =
profile()->GetExtensionsService()->GetExtensionByURL(url);
DCHECK(extension);
all_instances()->insert(this);
// Notify the ExtensionProcessManager that the view was created.
ExtensionProcessManager* epm = profile()->GetExtensionProcessManager();
epm->RegisterExtensionProcess(extension_id(),
render_view_host->process()->id());
// Update the extension permissions. Doing this each time we create an EFD
// ensures that new processes are informed of permissions for newly installed
// extensions.
render_view_host->Send(new ViewMsg_Extension_SetAPIPermissions(
extension->id(), extension->api_permissions()));
render_view_host->Send(new ViewMsg_Extension_SetHostPermissions(
extension->url(), extension->host_permissions()));
}
ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() {
all_instances()->erase(this);
peer_->dispatcher_ = NULL;
}
Browser* ExtensionFunctionDispatcher::GetBrowser() {
return delegate_->GetBrowser();
}
ExtensionHost* ExtensionFunctionDispatcher::GetExtensionHost() {
return delegate_->GetExtensionHost();
}
Extension* ExtensionFunctionDispatcher::GetExtension() {
ExtensionsService* service = profile()->GetExtensionsService();
DCHECK(service);
Extension* extension = service->GetExtensionById(extension_id());
DCHECK(extension);
return extension;
}
void ExtensionFunctionDispatcher::HandleRequest(const std::string& name,
const Value* args,
int request_id,
bool has_callback) {
scoped_refptr<ExtensionFunction> function(
FactoryRegistry::instance()->NewFunction(name));
function->set_dispatcher_peer(peer_);
function->SetArgs(args);
function->set_request_id(request_id);
function->set_has_callback(has_callback);
function->Run();
}
void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function,
bool success) {
render_view_host_->SendExtensionResponse(function->request_id(), success,
function->GetResult(), function->GetError());
}
void ExtensionFunctionDispatcher::HandleBadMessage(ExtensionFunction* api) {
LOG(ERROR) << "bad extension message " <<
api->name() <<
" : terminating renderer.";
if (RenderProcessHost::run_renderer_in_process()) {
// In single process mode it is better if we don't suicide but just crash.
CHECK(false);
} else {
NOTREACHED();
base::KillProcess(render_view_host_->process()->process().handle(),
ResultCodes::KILLED_BAD_MESSAGE, false);
}
}
Profile* ExtensionFunctionDispatcher::profile() {
return render_view_host_->process()->profile();
}
|