| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/api_binding.h" | 5 #include "extensions/renderer/api_binding.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 // TODO(devlin): Maybe separate EventData into two classes? Rules, actions, and | 100 // TODO(devlin): Maybe separate EventData into two classes? Rules, actions, and |
| 101 // conditions should never be present on vanilla events. | 101 // conditions should never be present on vanilla events. |
| 102 struct APIBinding::EventData { | 102 struct APIBinding::EventData { |
| 103 EventData(std::string exposed_name, | 103 EventData(std::string exposed_name, |
| 104 std::string full_name, | 104 std::string full_name, |
| 105 bool supports_filters, | 105 bool supports_filters, |
| 106 bool supports_rules, | 106 bool supports_rules, |
| 107 int max_listeners, | 107 int max_listeners, |
| 108 bool notify_on_change, |
| 108 std::vector<std::string> actions, | 109 std::vector<std::string> actions, |
| 109 std::vector<std::string> conditions, | 110 std::vector<std::string> conditions, |
| 110 APIBinding* binding) | 111 APIBinding* binding) |
| 111 : exposed_name(std::move(exposed_name)), | 112 : exposed_name(std::move(exposed_name)), |
| 112 full_name(std::move(full_name)), | 113 full_name(std::move(full_name)), |
| 113 supports_filters(supports_filters), | 114 supports_filters(supports_filters), |
| 114 supports_rules(supports_rules), | 115 supports_rules(supports_rules), |
| 115 max_listeners(max_listeners), | 116 max_listeners(max_listeners), |
| 117 notify_on_change(notify_on_change), |
| 116 actions(std::move(actions)), | 118 actions(std::move(actions)), |
| 117 conditions(std::move(conditions)), | 119 conditions(std::move(conditions)), |
| 118 binding(binding) {} | 120 binding(binding) {} |
| 119 | 121 |
| 120 // The name of the event on the API object (e.g. onCreated). | 122 // The name of the event on the API object (e.g. onCreated). |
| 121 std::string exposed_name; | 123 std::string exposed_name; |
| 122 | 124 |
| 123 // The fully-specified name of the event (e.g. tabs.onCreated). | 125 // The fully-specified name of the event (e.g. tabs.onCreated). |
| 124 std::string full_name; | 126 std::string full_name; |
| 125 | 127 |
| 126 // Whether the event supports filters. | 128 // Whether the event supports filters. |
| 127 bool supports_filters; | 129 bool supports_filters; |
| 128 | 130 |
| 129 // Whether the event supports rules. | 131 // Whether the event supports rules. |
| 130 bool supports_rules; | 132 bool supports_rules; |
| 131 | 133 |
| 132 // The maximum number of listeners this event supports. | 134 // The maximum number of listeners this event supports. |
| 133 int max_listeners; | 135 int max_listeners; |
| 134 | 136 |
| 137 // Whether to notify the browser of listener changes. |
| 138 bool notify_on_change; |
| 139 |
| 135 // The associated actions and conditions for declarative events. | 140 // The associated actions and conditions for declarative events. |
| 136 std::vector<std::string> actions; | 141 std::vector<std::string> actions; |
| 137 std::vector<std::string> conditions; | 142 std::vector<std::string> conditions; |
| 138 | 143 |
| 139 // The associated APIBinding. This raw pointer is safe because the | 144 // The associated APIBinding. This raw pointer is safe because the |
| 140 // EventData is only accessed from the callbacks associated with the | 145 // EventData is only accessed from the callbacks associated with the |
| 141 // APIBinding, and both the APIBinding and APIEventHandler are owned by the | 146 // APIBinding, and both the APIBinding and APIEventHandler are owned by the |
| 142 // same object (the APIBindingsSystem). | 147 // same object (the APIBindingsSystem). |
| 143 APIBinding* binding; | 148 APIBinding* binding; |
| 144 }; | 149 }; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 std::string full_name = | 264 std::string full_name = |
| 260 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); | 265 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); |
| 261 const base::ListValue* filters = nullptr; | 266 const base::ListValue* filters = nullptr; |
| 262 bool supports_filters = | 267 bool supports_filters = |
| 263 event_dict->GetList("filters", &filters) && !filters->empty(); | 268 event_dict->GetList("filters", &filters) && !filters->empty(); |
| 264 | 269 |
| 265 std::vector<std::string> rule_actions; | 270 std::vector<std::string> rule_actions; |
| 266 std::vector<std::string> rule_conditions; | 271 std::vector<std::string> rule_conditions; |
| 267 const base::DictionaryValue* options = nullptr; | 272 const base::DictionaryValue* options = nullptr; |
| 268 bool supports_rules = false; | 273 bool supports_rules = false; |
| 274 bool notify_on_change = true; |
| 269 int max_listeners = binding::kNoListenerMax; | 275 int max_listeners = binding::kNoListenerMax; |
| 270 if (event_dict->GetDictionary("options", &options)) { | 276 if (event_dict->GetDictionary("options", &options)) { |
| 271 bool temp_supports_filters = false; | 277 bool temp_supports_filters = false; |
| 272 // TODO(devlin): For some reason, schemas indicate supporting filters | 278 // TODO(devlin): For some reason, schemas indicate supporting filters |
| 273 // either through having a 'filters' property *or* through having | 279 // either through having a 'filters' property *or* through having |
| 274 // a 'supportsFilters' property. We should clean that up. | 280 // a 'supportsFilters' property. We should clean that up. |
| 275 supports_filters |= | 281 supports_filters |= |
| 276 (options->GetBoolean("supportsFilters", &temp_supports_filters) && | 282 (options->GetBoolean("supportsFilters", &temp_supports_filters) && |
| 277 temp_supports_filters); | 283 temp_supports_filters); |
| 278 if (options->GetBoolean("supportsRules", &supports_rules) && | 284 if (options->GetBoolean("supportsRules", &supports_rules) && |
| 279 supports_rules) { | 285 supports_rules) { |
| 280 bool supports_listeners = false; | 286 bool supports_listeners = false; |
| 281 DCHECK(options->GetBoolean("supportsListeners", &supports_listeners)); | 287 DCHECK(options->GetBoolean("supportsListeners", &supports_listeners)); |
| 282 DCHECK(!supports_listeners) | 288 DCHECK(!supports_listeners) |
| 283 << "Events cannot support rules and listeners."; | 289 << "Events cannot support rules and listeners."; |
| 284 auto get_values = [options](base::StringPiece name, | 290 auto get_values = [options](base::StringPiece name, |
| 285 std::vector<std::string>* out_value) { | 291 std::vector<std::string>* out_value) { |
| 286 const base::ListValue* list = nullptr; | 292 const base::ListValue* list = nullptr; |
| 287 CHECK(options->GetList(name, &list)); | 293 CHECK(options->GetList(name, &list)); |
| 288 for (const auto& entry : *list) { | 294 for (const auto& entry : *list) { |
| 289 DCHECK(entry.is_string()); | 295 DCHECK(entry.is_string()); |
| 290 out_value->push_back(entry.GetString()); | 296 out_value->push_back(entry.GetString()); |
| 291 } | 297 } |
| 292 }; | 298 }; |
| 293 get_values("actions", &rule_actions); | 299 get_values("actions", &rule_actions); |
| 294 get_values("conditions", &rule_conditions); | 300 get_values("conditions", &rule_conditions); |
| 295 } | 301 } |
| 296 | 302 |
| 297 options->GetInteger("maxListeners", &max_listeners); | 303 options->GetInteger("maxListeners", &max_listeners); |
| 304 bool unmanaged = false; |
| 305 if (options->GetBoolean("unmanaged", &unmanaged)) |
| 306 notify_on_change = !unmanaged; |
| 298 } | 307 } |
| 299 | 308 |
| 300 events_.push_back(base::MakeUnique<EventData>( | 309 events_.push_back(base::MakeUnique<EventData>( |
| 301 std::move(name), std::move(full_name), supports_filters, | 310 std::move(name), std::move(full_name), supports_filters, |
| 302 supports_rules, max_listeners, std::move(rule_actions), | 311 supports_rules, max_listeners, notify_on_change, |
| 303 std::move(rule_conditions), this)); | 312 std::move(rule_actions), std::move(rule_conditions), this)); |
| 304 } | 313 } |
| 305 } | 314 } |
| 306 } | 315 } |
| 307 | 316 |
| 308 APIBinding::~APIBinding() {} | 317 APIBinding::~APIBinding() {} |
| 309 | 318 |
| 310 v8::Local<v8::Object> APIBinding::CreateInstance( | 319 v8::Local<v8::Object> APIBinding::CreateInstance( |
| 311 v8::Local<v8::Context> context) { | 320 v8::Local<v8::Context> context) { |
| 312 DCHECK(IsContextValid(context)); | 321 DCHECK(IsContextValid(context)); |
| 313 v8::Isolate* isolate = context->GetIsolate(); | 322 v8::Isolate* isolate = context->GetIsolate(); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 } else if (event_data->supports_rules) { | 484 } else if (event_data->supports_rules) { |
| 476 gin::Handle<DeclarativeEvent> event = gin::CreateHandle( | 485 gin::Handle<DeclarativeEvent> event = gin::CreateHandle( |
| 477 isolate, new DeclarativeEvent( | 486 isolate, new DeclarativeEvent( |
| 478 event_data->full_name, event_data->binding->type_refs_, | 487 event_data->full_name, event_data->binding->type_refs_, |
| 479 event_data->binding->request_handler_, event_data->actions, | 488 event_data->binding->request_handler_, event_data->actions, |
| 480 event_data->conditions)); | 489 event_data->conditions)); |
| 481 retval = event.ToV8(); | 490 retval = event.ToV8(); |
| 482 } else { | 491 } else { |
| 483 retval = event_data->binding->event_handler_->CreateEventInstance( | 492 retval = event_data->binding->event_handler_->CreateEventInstance( |
| 484 event_data->full_name, event_data->supports_filters, | 493 event_data->full_name, event_data->supports_filters, |
| 485 event_data->max_listeners, context); | 494 event_data->max_listeners, event_data->notify_on_change, context); |
| 486 } | 495 } |
| 487 info.GetReturnValue().Set(retval); | 496 info.GetReturnValue().Set(retval); |
| 488 } | 497 } |
| 489 | 498 |
| 490 void APIBinding::GetCustomPropertyObject( | 499 void APIBinding::GetCustomPropertyObject( |
| 491 v8::Local<v8::Name> property_name, | 500 v8::Local<v8::Name> property_name, |
| 492 const v8::PropertyCallbackInfo<v8::Value>& info) { | 501 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 493 v8::Isolate* isolate = info.GetIsolate(); | 502 v8::Isolate* isolate = info.GetIsolate(); |
| 494 v8::HandleScope handle_scope(isolate); | 503 v8::HandleScope handle_scope(isolate); |
| 495 v8::Local<v8::Context> context = info.Holder()->CreationContext(); | 504 v8::Local<v8::Context> context = info.Holder()->CreationContext(); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 name, signature->GetExpectedSignature(), error)); | 615 name, signature->GetExpectedSignature(), error)); |
| 607 return; | 616 return; |
| 608 } | 617 } |
| 609 | 618 |
| 610 request_handler_->StartRequest(context, name, std::move(converted_arguments), | 619 request_handler_->StartRequest(context, name, std::move(converted_arguments), |
| 611 callback, custom_callback, | 620 callback, custom_callback, |
| 612 binding::RequestThread::UI); | 621 binding::RequestThread::UI); |
| 613 } | 622 } |
| 614 | 623 |
| 615 } // namespace extensions | 624 } // namespace extensions |
| OLD | NEW |