| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/event_bindings.h" | 5 #include "extensions/renderer/event_bindings.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 135 } |
| 136 | 136 |
| 137 EventBindings::~EventBindings() {} | 137 EventBindings::~EventBindings() {} |
| 138 | 138 |
| 139 // Attach an event name to an object. | 139 // Attach an event name to an object. |
| 140 void EventBindings::AttachEvent( | 140 void EventBindings::AttachEvent( |
| 141 const v8::FunctionCallbackInfo<v8::Value>& args) { | 141 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 142 CHECK_EQ(1, args.Length()); | 142 CHECK_EQ(1, args.Length()); |
| 143 CHECK(args[0]->IsString()); | 143 CHECK(args[0]->IsString()); |
| 144 | 144 |
| 145 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); | 145 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 146 | 146 |
| 147 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) | 147 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) |
| 148 return; | 148 return; |
| 149 | 149 |
| 150 std::string extension_id = context()->GetExtensionID(); | 150 std::string extension_id = context()->GetExtensionID(); |
| 151 EventListenerCounts& listener_counts = g_listener_counts.Get()[extension_id]; | 151 EventListenerCounts& listener_counts = g_listener_counts.Get()[extension_id]; |
| 152 if (++listener_counts[event_name] == 1) { | 152 if (++listener_counts[event_name] == 1) { |
| 153 content::RenderThread::Get()->Send(new ExtensionHostMsg_AddListener( | 153 content::RenderThread::Get()->Send(new ExtensionHostMsg_AddListener( |
| 154 extension_id, context()->GetURL(), event_name)); | 154 extension_id, context()->GetURL(), event_name)); |
| 155 } | 155 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) | 209 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) |
| 210 return; | 210 return; |
| 211 | 211 |
| 212 std::string extension_id = context()->GetExtensionID(); | 212 std::string extension_id = context()->GetExtensionID(); |
| 213 | 213 |
| 214 scoped_ptr<base::DictionaryValue> filter; | 214 scoped_ptr<base::DictionaryValue> filter; |
| 215 scoped_ptr<content::V8ValueConverter> converter( | 215 scoped_ptr<content::V8ValueConverter> converter( |
| 216 content::V8ValueConverter::create()); | 216 content::V8ValueConverter::create()); |
| 217 | 217 |
| 218 base::DictionaryValue* filter_dict = NULL; | 218 base::DictionaryValue* filter_dict = NULL; |
| 219 base::Value* filter_value = | 219 base::Value* filter_value = converter->FromV8Value( |
| 220 converter->FromV8Value(args[1]->ToObject(), context()->v8_context()); | 220 v8::Local<v8::Object>::Cast(args[1]), context()->v8_context()); |
| 221 if (!filter_value) { | 221 if (!filter_value) { |
| 222 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | 222 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 223 return; | 223 return; |
| 224 } | 224 } |
| 225 if (!filter_value->GetAsDictionary(&filter_dict)) { | 225 if (!filter_value->GetAsDictionary(&filter_dict)) { |
| 226 delete filter_value; | 226 delete filter_value; |
| 227 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | 227 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 228 return; | 228 return; |
| 229 } | 229 } |
| 230 | 230 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 269 } |
| 270 | 270 |
| 271 event_filter.RemoveEventMatcher(matcher_id); | 271 event_filter.RemoveEventMatcher(matcher_id); |
| 272 } | 272 } |
| 273 | 273 |
| 274 void EventBindings::MatchAgainstEventFilter( | 274 void EventBindings::MatchAgainstEventFilter( |
| 275 const v8::FunctionCallbackInfo<v8::Value>& args) { | 275 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 276 v8::Isolate* isolate = args.GetIsolate(); | 276 v8::Isolate* isolate = args.GetIsolate(); |
| 277 typedef std::set<EventFilter::MatcherID> MatcherIDs; | 277 typedef std::set<EventFilter::MatcherID> MatcherIDs; |
| 278 EventFilter& event_filter = g_event_filter.Get(); | 278 EventFilter& event_filter = g_event_filter.Get(); |
| 279 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); | 279 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 280 EventFilteringInfo info = ParseFromObject(args[1]->ToObject(), isolate); | 280 EventFilteringInfo info = |
| 281 ParseFromObject(args[1]->ToObject(isolate), isolate); |
| 281 // Only match events routed to this context's RenderView or ones that don't | 282 // Only match events routed to this context's RenderView or ones that don't |
| 282 // have a routingId in their filter. | 283 // have a routingId in their filter. |
| 283 MatcherIDs matched_event_filters = event_filter.MatchEvent( | 284 MatcherIDs matched_event_filters = event_filter.MatchEvent( |
| 284 event_name, info, context()->GetRenderView()->GetRoutingID()); | 285 event_name, info, context()->GetRenderView()->GetRoutingID()); |
| 285 v8::Handle<v8::Array> array( | 286 v8::Handle<v8::Array> array( |
| 286 v8::Array::New(isolate, matched_event_filters.size())); | 287 v8::Array::New(isolate, matched_event_filters.size())); |
| 287 int i = 0; | 288 int i = 0; |
| 288 for (MatcherIDs::iterator it = matched_event_filters.begin(); | 289 for (MatcherIDs::iterator it = matched_event_filters.begin(); |
| 289 it != matched_event_filters.end(); | 290 it != matched_event_filters.end(); |
| 290 ++it) { | 291 ++it) { |
| 291 array->Set(v8::Integer::New(isolate, i++), v8::Integer::New(isolate, *it)); | 292 array->Set(v8::Integer::New(isolate, i++), v8::Integer::New(isolate, *it)); |
| 292 } | 293 } |
| 293 args.GetReturnValue().Set(array); | 294 args.GetReturnValue().Set(array); |
| 294 } | 295 } |
| 295 | 296 |
| 296 scoped_ptr<EventMatcher> EventBindings::ParseEventMatcher( | 297 scoped_ptr<EventMatcher> EventBindings::ParseEventMatcher( |
| 297 base::DictionaryValue* filter_dict) { | 298 base::DictionaryValue* filter_dict) { |
| 298 return scoped_ptr<EventMatcher>(new EventMatcher( | 299 return scoped_ptr<EventMatcher>(new EventMatcher( |
| 299 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()), | 300 scoped_ptr<base::DictionaryValue>(filter_dict->DeepCopy()), |
| 300 context()->GetRenderView()->GetRoutingID())); | 301 context()->GetRenderView()->GetRoutingID())); |
| 301 } | 302 } |
| 302 | 303 |
| 303 } // namespace extensions | 304 } // namespace extensions |
| OLD | NEW |