Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / renderer / extensions / event_bindings.cc
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.
4
5 #include "chrome/renderer/extensions/event_bindings.h"
6
7 #include <map>
8 #include <set>
9 #include <string>
10 #include <vector>
11
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"
39 #include "url/gurl.h"
40 #include "v8/include/v8.h"
41
42 using blink::WebFrame;
43 using blink::WebURL;
44 using content::RenderThread;
45
46 namespace extensions {
47
48 namespace {
49
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
52 // and 1.
53 typedef std::map<std::string, int> EventListenerCounts;
54
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;
58
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
62 // between 0 and 1.
63 typedef std::map<std::string, linked_ptr<ValueCounter> >
64     FilteredEventListenerCounts;
65
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;
69
70 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER;
71
72 // TODO(koz): Merge this into EventBindings.
73 class ExtensionImpl : public ChromeV8Extension {
74  public:
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)));
90   }
91
92   virtual ~ExtensionImpl() {}
93
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());
98
99     std::string event_name = *v8::String::Utf8Value(args[0]->ToString());
100
101     if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
102       return;
103
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));
110     }
111
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));
118     }
119   }
120
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());
125
126     std::string event_name = *v8::String::Utf8Value(args[0]);
127     bool is_manual = args[1]->BooleanValue();
128
129     std::string extension_id = context()->GetExtensionID();
130     EventListenerCounts& listener_counts =
131         g_listener_counts.Get()[extension_id];
132
133     if (--listener_counts[event_name] == 0) {
134       content::RenderThread::Get()->Send(
135           new ExtensionHostMsg_RemoveListener(extension_id, event_name));
136     }
137
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));
146     }
147   }
148
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());
158
159     std::string event_name = *v8::String::Utf8Value(args[0]);
160
161     // This method throws an exception if it returns false.
162     if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
163       return;
164
165     std::string extension_id = context()->GetExtensionID();
166     if (extension_id.empty()) {
167       args.GetReturnValue().Set(static_cast<int32_t>(-1));
168       return;
169     }
170
171     scoped_ptr<base::DictionaryValue> filter;
172     scoped_ptr<content::V8ValueConverter> converter(
173         content::V8ValueConverter::create());
174
175     base::DictionaryValue* filter_dict = NULL;
176     base::Value* filter_value =
177         converter->FromV8Value(args[1]->ToObject(), context()->v8_context());
178     if (!filter_value) {
179       args.GetReturnValue().Set(static_cast<int32_t>(-1));
180       return;
181     }
182     if (!filter_value->GetAsDictionary(&filter_dict)) {
183       delete filter_value;
184       args.GetReturnValue().Set(static_cast<int32_t>(-1));
185       return;
186     }
187
188     filter.reset(filter_dict);
189     EventFilter& event_filter = g_event_filter.Get();
190     int id = event_filter.AddEventMatcher(event_name, ParseEventMatcher(
191         filter.get()));
192
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,
198                                                    *filter, lazy));
199     }
200
201     args.GetReturnValue().Set(static_cast<int32_t>(id));
202   }
203
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);
214
215     int result = counts[event_name]->Add(*filter);
216     return 1 == result;
217   }
218
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())
228       return false;
229     return 0 == it->second->Remove(*filter);
230   }
231
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();
241
242     std::string extension_id = context()->GetExtensionID();
243     if (extension_id.empty())
244       return;
245
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);
250
251     const std::string& event_name = event_filter.GetEventName(matcher_id);
252
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(),
260                                                       lazy));
261     }
262
263     event_filter.RemoveEventMatcher(matcher_id);
264   }
265
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()));
280     int i = 0;
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));
285     }
286     args.GetReturnValue().Set(array);
287   }
288
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)));
296     }
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());
302     }
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));
308     }
309     return info;
310   }
311
312  private:
313   static bool IsLazyBackgroundPage(content::RenderView* render_view,
314                                    const Extension* extension) {
315     if (!render_view)
316       return false;
317     ExtensionHelper* helper = ExtensionHelper::Get(render_view);
318     return (extension && BackgroundInfo::HasLazyBackgroundPage(extension) &&
319             helper->view_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
320   }
321
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()));
327   }
328 };
329
330 }  // namespace
331
332 // static
333 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher,
334                                          ChromeV8Context* context) {
335   return new ExtensionImpl(dispatcher, context);
336 }
337
338 }  // namespace extensions