1 // Copyright (c) 2012 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.
5 #include "chrome/renderer/extensions/event_bindings.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/lazy_instance.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "chrome/common/extensions/value_counter.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/renderer/extensions/chrome_v8_context.h"
20 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
21 #include "chrome/renderer/extensions/chrome_v8_extension.h"
22 #include "chrome/renderer/extensions/dispatcher.h"
23 #include "chrome/renderer/extensions/extension_helper.h"
24 #include "chrome/renderer/extensions/user_script_slave.h"
25 #include "content/public/renderer/render_thread.h"
26 #include "content/public/renderer/render_view.h"
27 #include "content/public/renderer/v8_value_converter.h"
28 #include "extensions/common/event_filter.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/extension_messages.h"
31 #include "extensions/common/manifest_handlers/background_info.h"
32 #include "extensions/common/view_type.h"
33 #include "grit/renderer_resources.h"
34 #include "third_party/WebKit/public/platform/WebURL.h"
35 #include "third_party/WebKit/public/platform/WebURLRequest.h"
36 #include "third_party/WebKit/public/web/WebDocument.h"
37 #include "third_party/WebKit/public/web/WebFrame.h"
38 #include "third_party/WebKit/public/web/WebView.h"
40 #include "v8/include/v8.h"
42 using blink::WebFrame;
44 using content::RenderThread;
46 namespace extensions {
50 // A map of event names to the number of contexts listening to that event.
51 // We notify the browser about event listeners when we transition between 0
53 typedef std::map<std::string, int> EventListenerCounts;
55 // A map of extension IDs to listener counts for that extension.
56 base::LazyInstance<std::map<std::string, EventListenerCounts> >
57 g_listener_counts = LAZY_INSTANCE_INITIALIZER;
59 // A map of event names to a (filter -> count) map. The map is used to keep
60 // track of which filters are in effect for which events.
61 // We notify the browser about filtered event listeners when we transition
63 typedef std::map<std::string, linked_ptr<ValueCounter> >
64 FilteredEventListenerCounts;
66 // A map of extension IDs to filtered listener counts for that extension.
67 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
68 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER;
70 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER;
72 // TODO(koz): Merge this into EventBindings.
73 class ExtensionImpl : public ChromeV8Extension {
75 explicit ExtensionImpl(Dispatcher* dispatcher, ChromeV8Context* context)
76 : ChromeV8Extension(dispatcher, context) {
77 RouteFunction("AttachEvent",
78 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this)));
79 RouteFunction("DetachEvent",
80 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this)));
81 RouteFunction("AttachFilteredEvent",
82 base::Bind(&ExtensionImpl::AttachFilteredEvent,
83 base::Unretained(this)));
84 RouteFunction("DetachFilteredEvent",
85 base::Bind(&ExtensionImpl::DetachFilteredEvent,
86 base::Unretained(this)));
87 RouteFunction("MatchAgainstEventFilter",
88 base::Bind(&ExtensionImpl::MatchAgainstEventFilter,
89 base::Unretained(this)));
92 virtual ~ExtensionImpl() {}
94 // Attach an event name to an object.
95 void AttachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) {
96 CHECK_EQ(1, args.Length());
97 CHECK(args[0]->IsString());
99 std::string event_name = *v8::String::Utf8Value(args[0]->ToString());
101 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
104 std::string extension_id = context()->GetExtensionID();
105 EventListenerCounts& listener_counts =
106 g_listener_counts.Get()[extension_id];
107 if (++listener_counts[event_name] == 1) {
108 content::RenderThread::Get()->Send(
109 new ExtensionHostMsg_AddListener(extension_id, event_name));
112 // This is called the first time the page has added a listener. Since
113 // the background page is the only lazy page, we know this is the first
114 // time this listener has been registered.
115 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) {
116 content::RenderThread::Get()->Send(
117 new ExtensionHostMsg_AddLazyListener(extension_id, event_name));
121 void DetachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) {
122 CHECK_EQ(2, args.Length());
123 CHECK(args[0]->IsString());
124 CHECK(args[1]->IsBoolean());
126 std::string event_name = *v8::String::Utf8Value(args[0]);
127 bool is_manual = args[1]->BooleanValue();
129 std::string extension_id = context()->GetExtensionID();
130 EventListenerCounts& listener_counts =
131 g_listener_counts.Get()[extension_id];
133 if (--listener_counts[event_name] == 0) {
134 content::RenderThread::Get()->Send(
135 new ExtensionHostMsg_RemoveListener(extension_id, event_name));
138 // DetachEvent is called when the last listener for the context is
139 // removed. If the context is the background page, and it removes the
140 // last listener manually, then we assume that it is no longer interested
141 // in being awakened for this event.
142 if (is_manual && IsLazyBackgroundPage(GetRenderView(),
143 context()->extension())) {
144 content::RenderThread::Get()->Send(
145 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name));
149 // MatcherID AttachFilteredEvent(string event_name, object filter)
150 // event_name - Name of the event to attach.
151 // filter - Which instances of the named event are we interested in.
152 // returns the id assigned to the listener, which will be returned from calls
153 // to MatchAgainstEventFilter where this listener matches.
154 void AttachFilteredEvent(const v8::FunctionCallbackInfo<v8::Value>& args) {
155 CHECK_EQ(2, args.Length());
156 CHECK(args[0]->IsString());
157 CHECK(args[1]->IsObject());
159 std::string event_name = *v8::String::Utf8Value(args[0]);
161 // This method throws an exception if it returns false.
162 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
165 std::string extension_id = context()->GetExtensionID();
166 if (extension_id.empty()) {
167 args.GetReturnValue().Set(static_cast<int32_t>(-1));
171 scoped_ptr<base::DictionaryValue> filter;
172 scoped_ptr<content::V8ValueConverter> converter(
173 content::V8ValueConverter::create());
175 base::DictionaryValue* filter_dict = NULL;
176 base::Value* filter_value =
177 converter->FromV8Value(args[1]->ToObject(), context()->v8_context());
179 args.GetReturnValue().Set(static_cast<int32_t>(-1));
182 if (!filter_value->GetAsDictionary(&filter_dict)) {
184 args.GetReturnValue().Set(static_cast<int32_t>(-1));
188 filter.reset(filter_dict);
189 EventFilter& event_filter = g_event_filter.Get();
190 int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher(
193 // Only send IPCs the first time a filter gets added.
194 if (AddFilter(event_name, extension_id, filter.get())) {
195 bool lazy = IsLazyBackgroundPage(GetRenderView(), context()->extension());
196 content::RenderThread::Get()->Send(
197 new ExtensionHostMsg_AddFilteredListener(extension_id, event_name,
201 args.GetReturnValue().Set(static_cast<int32_t>(id));
204 // Add a filter to |event_name| in |extension_id|, returning true if it
205 // was the first filter for that event in that extension.
206 static bool AddFilter(const std::string& event_name,
207 const std::string& extension_id,
208 base::DictionaryValue* filter) {
209 FilteredEventListenerCounts& counts =
210 g_filtered_listener_counts.Get()[extension_id];
211 FilteredEventListenerCounts::iterator it = counts.find(event_name);
212 if (it == counts.end())
213 counts[event_name].reset(new ValueCounter);
215 int result = counts[event_name]->Add(*filter);
219 // Remove a filter from |event_name| in |extension_id|, returning true if it
220 // was the last filter for that event in that extension.
221 static bool RemoveFilter(const std::string& event_name,
222 const std::string& extension_id,
223 base::DictionaryValue* filter) {
224 FilteredEventListenerCounts& counts =
225 g_filtered_listener_counts.Get()[extension_id];
226 FilteredEventListenerCounts::iterator it = counts.find(event_name);
227 if (it == counts.end())
229 return 0 == it->second->Remove(*filter);
232 // void DetachFilteredEvent(int id, bool manual)
233 // id - Id of the event to detach.
234 // manual - false if this is part of the extension unload process where all
235 // listeners are automatically detached.
236 void DetachFilteredEvent(const v8::FunctionCallbackInfo<v8::Value>& args) {
237 CHECK_EQ(2, args.Length());
238 CHECK(args[0]->IsInt32());
239 CHECK(args[1]->IsBoolean());
240 bool is_manual = args[1]->BooleanValue();
242 std::string extension_id = context()->GetExtensionID();
243 if (extension_id.empty())
246 int matcher_id = args[0]->Int32Value();
247 EventFilter& event_filter = g_event_filter.Get();
248 EventMatcher* event_matcher =
249 event_filter.GetEventMatcher(matcher_id);
251 const std::string& event_name = event_filter.GetEventName(matcher_id);
253 // Only send IPCs the last time a filter gets removed.
254 if (RemoveFilter(event_name, extension_id, event_matcher->value())) {
255 bool lazy = is_manual && IsLazyBackgroundPage(GetRenderView(),
256 context()->extension());
257 content::RenderThread::Get()->Send(
258 new ExtensionHostMsg_RemoveFilteredListener(extension_id, event_name,
259 *event_matcher->value(),
263 event_filter.RemoveEventMatcher(matcher_id);
266 void MatchAgainstEventFilter(
267 const v8::FunctionCallbackInfo<v8::Value>& args) {
268 v8::Isolate* isolate = args.GetIsolate();
269 typedef std::set<EventFilter::MatcherID> MatcherIDs;
270 EventFilter& event_filter = g_event_filter.Get();
271 std::string event_name = *v8::String::Utf8Value(args[0]->ToString());
272 EventFilteringInfo info =
273 ParseFromObject(args[1]->ToObject(), isolate);
274 // Only match events routed to this context's RenderView or ones that don't
275 // have a routingId in their filter.
276 MatcherIDs matched_event_filters = event_filter.MatchEvent(
277 event_name, info, context()->GetRenderView()->GetRoutingID());
278 v8::Handle<v8::Array> array(
279 v8::Array::New(isolate, matched_event_filters.size()));
281 for (MatcherIDs::iterator it = matched_event_filters.begin();
282 it != matched_event_filters.end(); ++it) {
283 array->Set(v8::Integer::New(isolate, i++),
284 v8::Integer::New(isolate, *it));
286 args.GetReturnValue().Set(array);
289 static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object,
290 v8::Isolate* isolate) {
291 EventFilteringInfo info;
292 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url"));
293 if (object->Has(url)) {
294 v8::Handle<v8::Value> url_value(object->Get(url));
295 info.SetURL(GURL(*v8::String::Utf8Value(url_value)));
297 v8::Handle<v8::String> instance_id(
298 v8::String::NewFromUtf8(isolate, "instanceId"));
299 if (object->Has(instance_id)) {
300 v8::Handle<v8::Value> instance_id_value(object->Get(instance_id));
301 info.SetInstanceID(instance_id_value->IntegerValue());
303 v8::Handle<v8::String> service_type(
304 v8::String::NewFromUtf8(isolate, "serviceType"));
305 if (object->Has(service_type)) {
306 v8::Handle<v8::Value> service_type_value(object->Get(service_type));
307 info.SetServiceType(*v8::String::Utf8Value(service_type_value));
313 static bool IsLazyBackgroundPage(content::RenderView* render_view,
314 const Extension* extension) {
317 ExtensionHelper* helper = ExtensionHelper::Get(render_view);
318 return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) &&
319 helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
322 scoped_ptr<EventMatcher> ParseEventMatcher(
323 base::DictionaryValue* filter_dict) {
324 return scoped_ptr<EventMatcher>(new EventMatcher(
325 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()),
326 context()->GetRenderView()->GetRoutingID()));
333 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher,
334 ChromeV8Context* context) {
335 return new ExtensionImpl(dispatcher, context);
338 } // namespace extensions