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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
// Copyright (c) 2012 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/event_bindings.h"
#include <vector>
#include "base/bind.h"
#include "base/basictypes.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/common/extensions/background_info.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/extension_set.h"
#include "chrome/common/extensions/value_counter.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/view_type.h"
#include "chrome/renderer/extensions/chrome_v8_context.h"
#include "chrome/renderer/extensions/chrome_v8_context_set.h"
#include "chrome/renderer/extensions/chrome_v8_extension.h"
#include "chrome/renderer/extensions/dispatcher.h"
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_helper.h"
#include "chrome/renderer/extensions/user_script_slave.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/v8_value_converter.h"
#include "extensions/common/event_filter.h"
#include "googleurl/src/gurl.h"
#include "grit/renderer_resources.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "v8/include/v8.h"
using WebKit::WebFrame;
using WebKit::WebSecurityOrigin;
using WebKit::WebURL;
using content::RenderThread;
namespace extensions {
namespace {
// A map of event names to the number of contexts listening to that event.
// We notify the browser about event listeners when we transition between 0
// and 1.
typedef std::map<std::string, int> EventListenerCounts;
// A map of extension IDs to listener counts for that extension.
base::LazyInstance<std::map<std::string, EventListenerCounts> >
g_listener_counts = LAZY_INSTANCE_INITIALIZER;
// A map of event names to a (filter -> count) map. The map is used to keep
// track of which filters are in effect for which events.
// We notify the browser about filtered event listeners when we transition
// between 0 and 1.
typedef std::map<std::string, linked_ptr<ValueCounter> >
FilteredEventListenerCounts;
// A map of extension IDs to filtered listener counts for that extension.
base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER;
// TODO(koz): Merge this into EventBindings.
class ExtensionImpl : public ChromeV8Extension {
public:
explicit ExtensionImpl(Dispatcher* dispatcher,
v8::Handle<v8::Context> v8_context)
: ChromeV8Extension(dispatcher, v8_context) {
RouteStaticFunction("AttachEvent", &AttachEvent);
RouteStaticFunction("DetachEvent", &DetachEvent);
RouteStaticFunction("AttachFilteredEvent", &AttachFilteredEvent);
RouteStaticFunction("DetachFilteredEvent", &DetachFilteredEvent);
RouteStaticFunction("MatchAgainstEventFilter", &MatchAgainstEventFilter);
}
virtual ~ExtensionImpl() {}
// Attach an event name to an object.
static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
DCHECK(args.Length() == 1);
// TODO(erikkay) should enforce that event name is a string in the bindings
DCHECK(args[0]->IsString() || args[0]->IsUndefined());
if (args[0]->IsString()) {
ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
Dispatcher* dispatcher = self->dispatcher();
const ChromeV8ContextSet& context_set = dispatcher->v8_context_set();
ChromeV8Context* context = context_set.GetByV8Context(self->v8_context());
CHECK(context);
if (!dispatcher->CheckContextAccessToExtensionAPI(event_name, context))
return v8::Undefined();
std::string extension_id = context->GetExtensionID();
EventListenerCounts& listener_counts =
g_listener_counts.Get()[extension_id];
if (++listener_counts[event_name] == 1) {
content::RenderThread::Get()->Send(
new ExtensionHostMsg_AddListener(extension_id, event_name));
}
// This is called the first time the page has added a listener. Since
// the background page is the only lazy page, we know this is the first
// time this listener has been registered.
if (IsLazyBackgroundPage(self->GetRenderView(), context->extension())) {
content::RenderThread::Get()->Send(
new ExtensionHostMsg_AddLazyListener(extension_id, event_name));
}
}
return v8::Undefined();
}
static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) {
DCHECK(args.Length() == 2);
// TODO(erikkay) should enforce that event name is a string in the bindings
DCHECK(args[0]->IsString() || args[0]->IsUndefined());
if (args[0]->IsString() && args[1]->IsBoolean()) {
std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
bool is_manual = args[1]->BooleanValue();
ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
Dispatcher* dispatcher = self->dispatcher();
const ChromeV8ContextSet& context_set = dispatcher->v8_context_set();
ChromeV8Context* context = context_set.GetByV8Context(self->v8_context());
if (!context)
return v8::Undefined();
std::string extension_id = context->GetExtensionID();
EventListenerCounts& listener_counts =
g_listener_counts.Get()[extension_id];
if (--listener_counts[event_name] == 0) {
content::RenderThread::Get()->Send(
new ExtensionHostMsg_RemoveListener(extension_id, event_name));
}
// DetachEvent is called when the last listener for the context is
// removed. If the context is the background page, and it removes the
// last listener manually, then we assume that it is no longer interested
// in being awakened for this event.
if (is_manual && IsLazyBackgroundPage(self->GetRenderView(),
context->extension())) {
content::RenderThread::Get()->Send(
new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name));
}
}
return v8::Undefined();
}
// MatcherID AttachFilteredEvent(string event_name, object filter)
// event_name - Name of the event to attach.
// filter - Which instances of the named event are we interested in.
// returns the id assigned to the listener, which will be returned from calls
// to MatchAgainstEventFilter where this listener matches.
static v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) {
DCHECK_EQ(2, args.Length());
DCHECK(args[0]->IsString());
DCHECK(args[1]->IsObject());
ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
Dispatcher* dispatcher = self->dispatcher();
const ChromeV8ContextSet& context_set = dispatcher->v8_context_set();
ChromeV8Context* context = context_set.GetByV8Context(self->v8_context());
DCHECK(context);
if (!context)
return v8::Integer::New(-1);
std::string event_name = *v8::String::AsciiValue(args[0]);
// This method throws an exception if it returns false.
if (!dispatcher->CheckContextAccessToExtensionAPI(event_name, context))
return v8::Undefined();
std::string extension_id = context->GetExtensionID();
if (extension_id.empty())
return v8::Integer::New(-1);
scoped_ptr<base::DictionaryValue> filter;
scoped_ptr<content::V8ValueConverter> converter(
content::V8ValueConverter::create());
base::DictionaryValue* filter_dict = NULL;
base::Value* filter_value =
converter->FromV8Value(args[1]->ToObject(), context->v8_context());
if (!filter_value)
return v8::Integer::New(-1);
if (!filter_value->GetAsDictionary(&filter_dict)) {
delete filter_value;
return v8::Integer::New(-1);
}
filter.reset(filter_dict);
EventFilter& event_filter = g_event_filter.Get();
int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher(
filter.get()));
// Only send IPCs the first time a filter gets added.
if (AddFilter(event_name, extension_id, filter.get())) {
bool lazy = IsLazyBackgroundPage(self->GetRenderView(),
context->extension());
content::RenderThread::Get()->Send(
new ExtensionHostMsg_AddFilteredListener(extension_id, event_name,
*filter, lazy));
}
return v8::Integer::New(id);
}
// Add a filter to |event_name| in |extension_id|, returning true if it
// was the first filter for that event in that extension.
static bool AddFilter(const std::string& event_name,
const std::string& extension_id,
base::DictionaryValue* filter) {
FilteredEventListenerCounts& counts =
g_filtered_listener_counts.Get()[extension_id];
FilteredEventListenerCounts::iterator it = counts.find(event_name);
if (it == counts.end())
counts[event_name].reset(new ValueCounter);
int result = counts[event_name]->Add(*filter);
return 1 == result;
}
// Remove a filter from |event_name| in |extension_id|, returning true if it
// was the last filter for that event in that extension.
static bool RemoveFilter(const std::string& event_name,
const std::string& extension_id,
base::DictionaryValue* filter) {
FilteredEventListenerCounts& counts =
g_filtered_listener_counts.Get()[extension_id];
FilteredEventListenerCounts::iterator it = counts.find(event_name);
if (it == counts.end())
return false;
return 0 == it->second->Remove(*filter);
}
// void DetachFilteredEvent(int id, bool manual)
// id - Id of the event to detach.
// manual - false if this is part of the extension unload process where all
// listeners are automatically detached.
static v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) {
DCHECK_EQ(2, args.Length());
DCHECK(args[0]->IsInt32());
DCHECK(args[1]->IsBoolean());
bool is_manual = args[1]->BooleanValue();
ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args);
Dispatcher* dispatcher = self->dispatcher();
const ChromeV8ContextSet& context_set = dispatcher->v8_context_set();
ChromeV8Context* context = context_set.GetByV8Context(self->v8_context());
if (!context)
return v8::Undefined();
std::string extension_id = context->GetExtensionID();
if (extension_id.empty())
return v8::Undefined();
int matcher_id = args[0]->Int32Value();
EventFilter& event_filter = g_event_filter.Get();
EventMatcher* event_matcher =
event_filter.GetEventMatcher(matcher_id);
const std::string& event_name = event_filter.GetEventName(matcher_id);
// Only send IPCs the last time a filter gets removed.
if (RemoveFilter(event_name, extension_id, event_matcher->value())) {
bool lazy = is_manual && IsLazyBackgroundPage(self->GetRenderView(),
context->extension());
content::RenderThread::Get()->Send(
new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name,
*event_matcher->value(),
lazy));
}
event_filter.RemoveEventMatcher(matcher_id);
return v8::Undefined();
}
static v8::Handle<v8::Value> MatchAgainstEventFilter(
const v8::Arguments& args) {
typedef std::set<EventFilter::MatcherID> MatcherIDs;
EventFilter& event_filter = g_event_filter.Get();
std::string event_name = *v8::String::AsciiValue(args[0]->ToString());
EventFilteringInfo info = ParseFromObject(args[1]->ToObject());
MatcherIDs matched_event_filters = event_filter.MatchEvent(
event_name, info);
v8::Handle<v8::Array> array(v8::Array::New(matched_event_filters.size()));
int i = 0;
for (MatcherIDs::iterator it = matched_event_filters.begin();
it != matched_event_filters.end(); ++it) {
array->Set(v8::Integer::New(i++), v8::Integer::New(*it));
}
return array;
}
static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object) {
EventFilteringInfo info;
v8::Handle<v8::String> url(v8::String::New("url"));
if (object->Has(url)) {
v8::Handle<v8::Value> url_value(object->Get(url));
info.SetURL(GURL(*v8::String::AsciiValue(url_value)));
}
return info;
}
private:
static bool IsLazyBackgroundPage(content::RenderView* render_view,
const Extension* extension) {
if (!render_view)
return false;
ExtensionHelper* helper = ExtensionHelper::Get(render_view);
return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) &&
helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
}
static scoped_ptr<EventMatcher> ParseEventMatcher(
base::DictionaryValue* filter_dict) {
return scoped_ptr<EventMatcher>(new EventMatcher(
scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy())));
}
};
} // namespace
// static
ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher,
v8::Handle<v8::Context> context) {
return new ExtensionImpl(dispatcher, context);
}
} // namespace extensions
|