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
|
// 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/renderer/extensions/extension_process_bindings.h"
#include "base/singleton.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "chrome/renderer/extensions/bindings_utils.h"
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/renderer_extension_bindings.h"
#include "chrome/renderer/js_only_v8_extensions.h"
#include "chrome/renderer/render_view.h"
#include "grit/renderer_resources.h"
#include "webkit/glue/webframe.h"
using bindings_utils::GetStringResource;
using bindings_utils::ContextInfo;
using bindings_utils::ContextList;
using bindings_utils::GetContexts;
using bindings_utils::GetPendingRequestMap;
using bindings_utils::PendingRequest;
using bindings_utils::ExtensionBase;
namespace {
// A map of extension ID to vector of page action ids.
typedef std::map< std::string, std::vector<std::string> > PageActionIdMap;
const char kExtensionName[] = "chrome/ExtensionProcessBindings";
const char* kExtensionDeps[] = {
BaseJsV8Extension::kName,
EventBindings::kName,
JsonSchemaJsV8Extension::kName,
RendererExtensionBindings::kName,
};
struct SingletonData {
std::set<std::string> function_names_;
PageActionIdMap page_action_ids_;
};
static std::set<std::string>* GetFunctionNameSet() {
return &Singleton<SingletonData>()->function_names_;
}
static PageActionIdMap* GetPageActionMap() {
return &Singleton<SingletonData>()->page_action_ids_;
}
class ExtensionImpl : public ExtensionBase {
public:
ExtensionImpl() : ExtensionBase(
kExtensionName, GetStringResource<IDR_EXTENSION_PROCESS_BINDINGS_JS>(),
arraysize(kExtensionDeps), kExtensionDeps) {}
static void SetFunctionNames(const std::vector<std::string>& names) {
std::set<std::string>* name_set = GetFunctionNameSet();
for (size_t i = 0; i < names.size(); ++i) {
name_set->insert(names[i]);
}
}
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
v8::Handle<v8::String> name) {
if (name->Equals(v8::String::New("GetViews"))) {
return v8::FunctionTemplate::New(GetViews);
} else if (name->Equals(v8::String::New("GetNextRequestId"))) {
return v8::FunctionTemplate::New(GetNextRequestId);
} else if (name->Equals(v8::String::New("GetCurrentPageActions"))) {
return v8::FunctionTemplate::New(GetCurrentPageActions);
} else if (name->Equals(v8::String::New("StartRequest"))) {
return v8::FunctionTemplate::New(StartRequest);
}
return ExtensionBase::GetNativeFunction(name);
}
private:
static std::string ExtensionIdFromCurrentContext() {
RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext();
DCHECK(renderview);
GURL url = renderview->webview()->GetMainFrame()->GetURL();
return url.host();
}
static v8::Handle<v8::Value> GetViews(const v8::Arguments& args) {
std::string extension_id = ExtensionIdFromCurrentContext();
ContextList contexts =
bindings_utils::GetContextsForExtension(extension_id);
DCHECK(contexts.size() > 0);
v8::Local<v8::Array> views = v8::Array::New(contexts.size());
int index = 0;
ContextList::const_iterator it = contexts.begin();
for (; it != contexts.end(); ++it) {
v8::Local<v8::Value> window = (*it)->context->Global()->Get(
v8::String::New("window"));
DCHECK(!window.IsEmpty());
views->Set(v8::Integer::New(index), window);
index++;
}
return views;
}
static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) {
static int next_request_id = 0;
return v8::Integer::New(next_request_id++);
}
static v8::Handle<v8::Value> GetCurrentPageActions(
const v8::Arguments& args) {
std::string extension_id = ExtensionIdFromCurrentContext();
PageActionIdMap* page_action_map =
GetPageActionMap();
PageActionIdMap::const_iterator it =
page_action_map->find(extension_id);
std::vector<std::string> page_actions;
size_t size = 0;
if (it != page_action_map->end()) {
page_actions = it->second;
size = page_actions.size();
}
v8::Local<v8::Array> page_action_vector = v8::Array::New(size);
for (size_t i = 0; i < size; ++i) {
std::string page_action_id = page_actions[i];
page_action_vector->Set(v8::Integer::New(i),
v8::String::New(page_action_id.c_str()));
}
return page_action_vector;
}
// Starts an API request to the browser, with an optional callback. The
// callback will be dispatched to EventBindings::HandleResponse.
static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args) {
// Get the current RenderView so that we can send a routed IPC message from
// the correct source.
RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext();
if (!renderview)
return v8::Undefined();
if (args.Length() != 4 || !args[0]->IsString() || !args[1]->IsString() ||
!args[2]->IsInt32() || !args[3]->IsBoolean())
return v8::Undefined();
std::string name = *v8::String::AsciiValue(args[0]);
if (GetFunctionNameSet()->find(name) == GetFunctionNameSet()->end()) {
NOTREACHED() << "Unexpected function " << name;
return v8::Undefined();
}
std::string json_args = *v8::String::Utf8Value(args[1]);
int request_id = args[2]->Int32Value();
bool has_callback = args[3]->BooleanValue();
v8::Persistent<v8::Context> current_context =
v8::Persistent<v8::Context>::New(v8::Context::GetCurrent());
DCHECK(!current_context.IsEmpty());
GetPendingRequestMap()[request_id].reset(new PendingRequest(
current_context, *v8::String::AsciiValue(args.Data())));
renderview->SendExtensionRequest(name, json_args, request_id, has_callback);
return v8::Undefined();
}
};
} // namespace
v8::Extension* ExtensionProcessBindings::Get() {
return new ExtensionImpl();
}
void ExtensionProcessBindings::SetFunctionNames(
const std::vector<std::string>& names) {
ExtensionImpl::SetFunctionNames(names);
}
// static
void ExtensionProcessBindings::HandleResponse(int request_id, bool success,
const std::string& response,
const std::string& error) {
PendingRequest* request = GetPendingRequestMap()[request_id].get();
if (!request)
return; // The frame went away.
v8::HandleScope handle_scope;
v8::Handle<v8::Value> argv[5];
argv[0] = v8::Integer::New(request_id);
argv[1] = v8::String::New(request->name.c_str());
argv[2] = v8::Boolean::New(success);
argv[3] = v8::String::New(response.c_str());
argv[4] = v8::String::New(error.c_str());
bindings_utils::CallFunctionInContext(
request->context, "handleResponse", arraysize(argv), argv);
GetPendingRequestMap().erase(request_id);
}
// static
void ExtensionProcessBindings::SetPageActions(
const std::string& extension_id,
const std::vector<std::string>& page_actions) {
PageActionIdMap& page_action_map = *GetPageActionMap();
if (!page_actions.empty()) {
page_action_map[extension_id] = page_actions;
} else {
if (page_action_map.find(extension_id) != page_action_map.end())
page_action_map.erase(extension_id);
}
}
|