| 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 <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 base::Unretained(this))); | 90 base::Unretained(this))); |
| 91 } | 91 } |
| 92 | 92 |
| 93 virtual ~ExtensionImpl() {} | 93 virtual ~ExtensionImpl() {} |
| 94 | 94 |
| 95 // Attach an event name to an object. | 95 // Attach an event name to an object. |
| 96 void AttachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | 96 void AttachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 97 CHECK_EQ(1, args.Length()); | 97 CHECK_EQ(1, args.Length()); |
| 98 CHECK(args[0]->IsString()); | 98 CHECK(args[0]->IsString()); |
| 99 | 99 |
| 100 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); | 100 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 101 | 101 |
| 102 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) | 102 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) |
| 103 return; | 103 return; |
| 104 | 104 |
| 105 std::string extension_id = context()->GetExtensionID(); | 105 std::string extension_id = context()->GetExtensionID(); |
| 106 EventListenerCounts& listener_counts = | 106 EventListenerCounts& listener_counts = |
| 107 g_listener_counts.Get()[extension_id]; | 107 g_listener_counts.Get()[extension_id]; |
| 108 if (++listener_counts[event_name] == 1) { | 108 if (++listener_counts[event_name] == 1) { |
| 109 content::RenderThread::Get()->Send( | 109 content::RenderThread::Get()->Send( |
| 110 new ExtensionHostMsg_AddListener(extension_id, event_name)); | 110 new ExtensionHostMsg_AddListener(extension_id, event_name)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // This is called the first time the page has added a listener. Since | 113 // This is called the first time the page has added a listener. Since |
| 114 // the background page is the only lazy page, we know this is the first | 114 // the background page is the only lazy page, we know this is the first |
| 115 // time this listener has been registered. | 115 // time this listener has been registered. |
| 116 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) { | 116 if (IsLazyBackgroundPage(GetRenderView(), context()->extension())) { |
| 117 content::RenderThread::Get()->Send( | 117 content::RenderThread::Get()->Send( |
| 118 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); | 118 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 | 121 |
| 122 void DetachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | 122 void DetachEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 123 CHECK_EQ(2, args.Length()); | 123 CHECK_EQ(2, args.Length()); |
| 124 CHECK(args[0]->IsString()); | 124 CHECK(args[0]->IsString()); |
| 125 CHECK(args[1]->IsBoolean()); | 125 CHECK(args[1]->IsBoolean()); |
| 126 | 126 |
| 127 std::string event_name = *v8::String::AsciiValue(args[0]); | 127 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 128 bool is_manual = args[1]->BooleanValue(); | 128 bool is_manual = args[1]->BooleanValue(); |
| 129 | 129 |
| 130 std::string extension_id = context()->GetExtensionID(); | 130 std::string extension_id = context()->GetExtensionID(); |
| 131 EventListenerCounts& listener_counts = | 131 EventListenerCounts& listener_counts = |
| 132 g_listener_counts.Get()[extension_id]; | 132 g_listener_counts.Get()[extension_id]; |
| 133 | 133 |
| 134 if (--listener_counts[event_name] == 0) { | 134 if (--listener_counts[event_name] == 0) { |
| 135 content::RenderThread::Get()->Send( | 135 content::RenderThread::Get()->Send( |
| 136 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); | 136 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); |
| 137 } | 137 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 150 // MatcherID AttachFilteredEvent(string event_name, object filter) | 150 // MatcherID AttachFilteredEvent(string event_name, object filter) |
| 151 // event_name - Name of the event to attach. | 151 // event_name - Name of the event to attach. |
| 152 // filter - Which instances of the named event are we interested in. | 152 // filter - Which instances of the named event are we interested in. |
| 153 // returns the id assigned to the listener, which will be returned from calls | 153 // returns the id assigned to the listener, which will be returned from calls |
| 154 // to MatchAgainstEventFilter where this listener matches. | 154 // to MatchAgainstEventFilter where this listener matches. |
| 155 void AttachFilteredEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { | 155 void AttachFilteredEvent(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 156 CHECK_EQ(2, args.Length()); | 156 CHECK_EQ(2, args.Length()); |
| 157 CHECK(args[0]->IsString()); | 157 CHECK(args[0]->IsString()); |
| 158 CHECK(args[1]->IsObject()); | 158 CHECK(args[1]->IsObject()); |
| 159 | 159 |
| 160 std::string event_name = *v8::String::AsciiValue(args[0]); | 160 std::string event_name = *v8::String::Utf8Value(args[0]); |
| 161 | 161 |
| 162 // This method throws an exception if it returns false. | 162 // This method throws an exception if it returns false. |
| 163 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) | 163 if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context())) |
| 164 return; | 164 return; |
| 165 | 165 |
| 166 std::string extension_id = context()->GetExtensionID(); | 166 std::string extension_id = context()->GetExtensionID(); |
| 167 if (extension_id.empty()) { | 167 if (extension_id.empty()) { |
| 168 args.GetReturnValue().Set(static_cast<int32_t>(-1)); | 168 args.GetReturnValue().Set(static_cast<int32_t>(-1)); |
| 169 return; | 169 return; |
| 170 } | 170 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 lazy)); | 261 lazy)); |
| 262 } | 262 } |
| 263 | 263 |
| 264 event_filter.RemoveEventMatcher(matcher_id); | 264 event_filter.RemoveEventMatcher(matcher_id); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void MatchAgainstEventFilter( | 267 void MatchAgainstEventFilter( |
| 268 const v8::FunctionCallbackInfo<v8::Value>& args) { | 268 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 269 typedef std::set<EventFilter::MatcherID> MatcherIDs; | 269 typedef std::set<EventFilter::MatcherID> MatcherIDs; |
| 270 EventFilter& event_filter = g_event_filter.Get(); | 270 EventFilter& event_filter = g_event_filter.Get(); |
| 271 std::string event_name = *v8::String::AsciiValue(args[0]->ToString()); | 271 std::string event_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 272 EventFilteringInfo info = | 272 EventFilteringInfo info = |
| 273 ParseFromObject(args[1]->ToObject(), args.GetIsolate()); | 273 ParseFromObject(args[1]->ToObject(), args.GetIsolate()); |
| 274 // Only match events routed to this context's RenderView or ones that don't | 274 // Only match events routed to this context's RenderView or ones that don't |
| 275 // have a routingId in their filter. | 275 // have a routingId in their filter. |
| 276 MatcherIDs matched_event_filters = event_filter.MatchEvent( | 276 MatcherIDs matched_event_filters = event_filter.MatchEvent( |
| 277 event_name, info, context()->GetRenderView()->GetRoutingID()); | 277 event_name, info, context()->GetRenderView()->GetRoutingID()); |
| 278 v8::Handle<v8::Array> array(v8::Array::New(matched_event_filters.size())); | 278 v8::Handle<v8::Array> array( |
| 279 v8::Array::New(args.GetIsolate(), matched_event_filters.size())); |
| 279 int i = 0; | 280 int i = 0; |
| 280 for (MatcherIDs::iterator it = matched_event_filters.begin(); | 281 for (MatcherIDs::iterator it = matched_event_filters.begin(); |
| 281 it != matched_event_filters.end(); ++it) { | 282 it != matched_event_filters.end(); ++it) { |
| 282 array->Set(v8::Integer::New(i++), v8::Integer::New(*it)); | 283 array->Set(v8::Integer::New(i++), v8::Integer::New(*it)); |
| 283 } | 284 } |
| 284 args.GetReturnValue().Set(array); | 285 args.GetReturnValue().Set(array); |
| 285 } | 286 } |
| 286 | 287 |
| 287 static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object, | 288 static EventFilteringInfo ParseFromObject(v8::Handle<v8::Object> object, |
| 288 v8::Isolate* isolate) { | 289 v8::Isolate* isolate) { |
| 289 EventFilteringInfo info; | 290 EventFilteringInfo info; |
| 290 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url")); | 291 v8::Handle<v8::String> url(v8::String::NewFromUtf8(isolate, "url")); |
| 291 if (object->Has(url)) { | 292 if (object->Has(url)) { |
| 292 v8::Handle<v8::Value> url_value(object->Get(url)); | 293 v8::Handle<v8::Value> url_value(object->Get(url)); |
| 293 info.SetURL(GURL(*v8::String::AsciiValue(url_value))); | 294 info.SetURL(GURL(*v8::String::Utf8Value(url_value))); |
| 294 } | 295 } |
| 295 v8::Handle<v8::String> instance_id( | 296 v8::Handle<v8::String> instance_id( |
| 296 v8::String::NewFromUtf8(isolate, "instanceId")); | 297 v8::String::NewFromUtf8(isolate, "instanceId")); |
| 297 if (object->Has(instance_id)) { | 298 if (object->Has(instance_id)) { |
| 298 v8::Handle<v8::Value> instance_id_value(object->Get(instance_id)); | 299 v8::Handle<v8::Value> instance_id_value(object->Get(instance_id)); |
| 299 info.SetInstanceID(instance_id_value->IntegerValue()); | 300 info.SetInstanceID(instance_id_value->IntegerValue()); |
| 300 } | 301 } |
| 301 v8::Handle<v8::String> service_type( | 302 v8::Handle<v8::String> service_type( |
| 302 v8::String::NewFromUtf8(isolate, "serviceType")); | 303 v8::String::NewFromUtf8(isolate, "serviceType")); |
| 303 if (object->Has(service_type)) { | 304 if (object->Has(service_type)) { |
| 304 v8::Handle<v8::Value> service_type_value(object->Get(service_type)); | 305 v8::Handle<v8::Value> service_type_value(object->Get(service_type)); |
| 305 info.SetServiceType(*v8::String::AsciiValue(service_type_value)); | 306 info.SetServiceType(*v8::String::Utf8Value(service_type_value)); |
| 306 } | 307 } |
| 307 return info; | 308 return info; |
| 308 } | 309 } |
| 309 | 310 |
| 310 private: | 311 private: |
| 311 static bool IsLazyBackgroundPage(content::RenderView* render_view, | 312 static bool IsLazyBackgroundPage(content::RenderView* render_view, |
| 312 const Extension* extension) { | 313 const Extension* extension) { |
| 313 if (!render_view) | 314 if (!render_view) |
| 314 return false; | 315 return false; |
| 315 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 316 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 327 | 328 |
| 328 } // namespace | 329 } // namespace |
| 329 | 330 |
| 330 // static | 331 // static |
| 331 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, | 332 ChromeV8Extension* EventBindings::Create(Dispatcher* dispatcher, |
| 332 ChromeV8Context* context) { | 333 ChromeV8Context* context) { |
| 333 return new ExtensionImpl(dispatcher, context); | 334 return new ExtensionImpl(dispatcher, context); |
| 334 } | 335 } |
| 335 | 336 |
| 336 } // namespace extensions | 337 } // namespace extensions |
| OLD | NEW |