OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/extensions/event_bindings.h" | 5 #include "chrome/renderer/extensions/event_bindings.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 | 63 |
64 // A map of extension IDs to filtered listener counts for that extension. | 64 // A map of extension IDs to filtered listener counts for that extension. |
65 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > | 65 base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> > |
66 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; | 66 g_filtered_listener_counts = LAZY_INSTANCE_INITIALIZER; |
67 | 67 |
68 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; | 68 base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER; |
69 | 69 |
70 // TODO(koz): Merge this into EventBindings. | 70 // TODO(koz): Merge this into EventBindings. |
71 class ExtensionImpl : public ChromeV8Extension { | 71 class ExtensionImpl : public ChromeV8Extension { |
72 public: | 72 public: |
73 explicit ExtensionImpl(Dispatcher* dispatcher, | 73 explicit ExtensionImpl(Dispatcher* dispatcher, ChromeV8Context* context) |
74 v8::Handle<v8::Context> v8_context) | 74 : ChromeV8Extension(dispatcher, context) { |
75 : ChromeV8Extension(dispatcher, v8_context) { | |
76 RouteFunction("AttachEvent", | 75 RouteFunction("AttachEvent", |
77 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this))); | 76 base::Bind(&ExtensionImpl::AttachEvent, base::Unretained(this))); |
78 RouteFunction("DetachEvent", | 77 RouteFunction("DetachEvent", |
79 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this))); | 78 base::Bind(&ExtensionImpl::DetachEvent, base::Unretained(this))); |
80 RouteFunction("AttachFilteredEvent", | 79 RouteFunction("AttachFilteredEvent", |
81 base::Bind(&ExtensionImpl::AttachFilteredEvent, | 80 base::Bind(&ExtensionImpl::AttachFilteredEvent, |
82 base::Unretained(this))); | 81 base::Unretained(this))); |
83 RouteFunction("DetachFilteredEvent", | 82 RouteFunction("DetachFilteredEvent", |
84 base::Bind(&ExtensionImpl::DetachFilteredEvent, | 83 base::Bind(&ExtensionImpl::DetachFilteredEvent, |
85 base::Unretained(this))); | 84 base::Unretained(this))); |
86 RouteFunction("MatchAgainstEventFilter", | 85 RouteFunction("MatchAgainstEventFilter", |
87 base::Bind(&ExtensionImpl::MatchAgainstEventFilter, | 86 base::Bind(&ExtensionImpl::MatchAgainstEventFilter, |
88 base::Unretained(this))); | 87 base::Unretained(this))); |
89 } | 88 } |
90 | 89 |
91 virtual ~ExtensionImpl() {} | 90 virtual ~ExtensionImpl() {} |
92 | 91 |
93 // Attach an event name to an object. | 92 // Attach an event name to an object. |
94 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { | 93 v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) { |
95 DCHECK(args.Length() == 1); | 94 DCHECK(args.Length() == 1); |
96 // TODO(erikkay) should enforce that event name is a string in the bindings | 95 // TODO(erikkay) should enforce that event name is a string in the bindings |
97 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); | 96 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); |
98 | 97 |
99 if (args[0]->IsString()) { | 98 if (args[0]->IsString()) { |
100 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); | 99 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); |
101 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); | 100 ChromeV8Context* context = GetContext(); |
not at google - send to devlin
2013/05/29 17:41:55
ditto etc
marja
2013/05/31 10:06:44
Done.
| |
102 ChromeV8Context* context = context_set.GetByV8Context(v8_context()); | |
103 CHECK(context); | 101 CHECK(context); |
104 | 102 |
105 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context)) | 103 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context)) |
106 return v8::Undefined(); | 104 return v8::Undefined(); |
107 | 105 |
108 std::string extension_id = context->GetExtensionID(); | 106 std::string extension_id = context->GetExtensionID(); |
109 EventListenerCounts& listener_counts = | 107 EventListenerCounts& listener_counts = |
110 g_listener_counts.Get()[extension_id]; | 108 g_listener_counts.Get()[extension_id]; |
111 if (++listener_counts[event_name] == 1) { | 109 if (++listener_counts[event_name] == 1) { |
112 content::RenderThread::Get()->Send( | 110 content::RenderThread::Get()->Send( |
(...skipping 13 matching lines...) Expand all Loading... | |
126 | 124 |
127 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { | 125 v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { |
128 DCHECK(args.Length() == 2); | 126 DCHECK(args.Length() == 2); |
129 // TODO(erikkay) should enforce that event name is a string in the bindings | 127 // TODO(erikkay) should enforce that event name is a string in the bindings |
130 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); | 128 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); |
131 | 129 |
132 if (args[0]->IsString() && args[1]->IsBoolean()) { | 130 if (args[0]->IsString() && args[1]->IsBoolean()) { |
133 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); | 131 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); |
134 bool is_manual = args[1]->BooleanValue(); | 132 bool is_manual = args[1]->BooleanValue(); |
135 | 133 |
136 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); | 134 ChromeV8Context* context = GetContext(); |
137 ChromeV8Context* context = context_set.GetByV8Context(v8_context()); | |
138 if (!context) | 135 if (!context) |
139 return v8::Undefined(); | 136 return v8::Undefined(); |
140 | 137 |
141 std::string extension_id = context->GetExtensionID(); | 138 std::string extension_id = context->GetExtensionID(); |
142 EventListenerCounts& listener_counts = | 139 EventListenerCounts& listener_counts = |
143 g_listener_counts.Get()[extension_id]; | 140 g_listener_counts.Get()[extension_id]; |
144 | 141 |
145 if (--listener_counts[event_name] == 0) { | 142 if (--listener_counts[event_name] == 0) { |
146 content::RenderThread::Get()->Send( | 143 content::RenderThread::Get()->Send( |
147 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); | 144 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); |
(...skipping 15 matching lines...) Expand all Loading... | |
163 // MatcherID AttachFilteredEvent(string event_name, object filter) | 160 // MatcherID AttachFilteredEvent(string event_name, object filter) |
164 // event_name - Name of the event to attach. | 161 // event_name - Name of the event to attach. |
165 // filter - Which instances of the named event are we interested in. | 162 // filter - Which instances of the named event are we interested in. |
166 // returns the id assigned to the listener, which will be returned from calls | 163 // returns the id assigned to the listener, which will be returned from calls |
167 // to MatchAgainstEventFilter where this listener matches. | 164 // to MatchAgainstEventFilter where this listener matches. |
168 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) { | 165 v8::Handle<v8::Value> AttachFilteredEvent(const v8::Arguments& args) { |
169 DCHECK_EQ(2, args.Length()); | 166 DCHECK_EQ(2, args.Length()); |
170 DCHECK(args[0]->IsString()); | 167 DCHECK(args[0]->IsString()); |
171 DCHECK(args[1]->IsObject()); | 168 DCHECK(args[1]->IsObject()); |
172 | 169 |
173 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); | 170 ChromeV8Context* context = GetContext(); |
174 ChromeV8Context* context = context_set.GetByV8Context(v8_context()); | |
175 DCHECK(context); | 171 DCHECK(context); |
176 if (!context) | 172 if (!context) |
177 return v8::Integer::New(-1); | 173 return v8::Integer::New(-1); |
178 | 174 |
179 std::string event_name = *v8::String::AsciiValue(args[0]); | 175 std::string event_name = *v8::String::AsciiValue(args[0]); |
180 // This method throws an exception if it returns false. | 176 // This method throws an exception if it returns false. |
181 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context)) | 177 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context)) |
182 return v8::Undefined(); | 178 return v8::Undefined(); |
183 | 179 |
184 std::string extension_id = context->GetExtensionID(); | 180 std::string extension_id = context->GetExtensionID(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 | 241 |
246 // void DetachFilteredEvent(int id, bool manual) | 242 // void DetachFilteredEvent(int id, bool manual) |
247 // id - Id of the event to detach. | 243 // id - Id of the event to detach. |
248 // manual - false if this is part of the extension unload process where all | 244 // manual - false if this is part of the extension unload process where all |
249 // listeners are automatically detached. | 245 // listeners are automatically detached. |
250 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) { | 246 v8::Handle<v8::Value> DetachFilteredEvent(const v8::Arguments& args) { |
251 DCHECK_EQ(2, args.Length()); | 247 DCHECK_EQ(2, args.Length()); |
252 DCHECK(args[0]->IsInt32()); | 248 DCHECK(args[0]->IsInt32()); |
253 DCHECK(args[1]->IsBoolean()); | 249 DCHECK(args[1]->IsBoolean()); |
254 bool is_manual = args[1]->BooleanValue(); | 250 bool is_manual = args[1]->BooleanValue(); |
255 const ChromeV8ContextSet& context_set = dispatcher_->v8_context_set(); | 251 ChromeV8Context* context = GetContext(); |
256 ChromeV8Context* context = context_set.GetByV8Context(v8_context()); | |
257 if (!context) | 252 if (!context) |
258 return v8::Undefined(); | 253 return v8::Undefined(); |
259 | 254 |
260 std::string extension_id = context->GetExtensionID(); | 255 std::string extension_id = context->GetExtensionID(); |
261 if (extension_id.empty()) | 256 if (extension_id.empty()) |
262 return v8::Undefined(); | 257 return v8::Undefined(); |
263 | 258 |
264 int matcher_id = args[0]->Int32Value(); | 259 int matcher_id = args[0]->Int32Value(); |
265 EventFilter& event_filter = g_event_filter.Get(); | 260 EventFilter& event_filter = g_event_filter.Get(); |
266 EventMatcher* event_matcher = | 261 EventMatcher* event_matcher = |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 base::DictionaryValue* filter_dict) { | 319 base::DictionaryValue* filter_dict) { |
325 return scoped_ptr<EventMatcher>(new EventMatcher( | 320 return scoped_ptr<EventMatcher>(new EventMatcher( |
326 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()))); | 321 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()))); |
327 } | 322 } |
328 }; | 323 }; |
329 | 324 |
330 } // namespace | 325 } // namespace |
331 | 326 |
332 // static | 327 // static |
333 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, | 328 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, |
334 v8::Handle<v8::Context> context) { | 329 ChromeV8Context* context) { |
335 return new ExtensionImpl(dispatcher, context); | 330 return new ExtensionImpl(dispatcher, context); |
336 } | 331 } |
337 | 332 |
338 } // namespace extensions | 333 } // namespace extensions |
OLD | NEW |