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/bindings/api_binding.h" | 5 #include "extensions/renderer/bindings/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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 APIBinding::HandlerCallback callback; | 101 APIBinding::HandlerCallback callback; |
102 }; | 102 }; |
103 | 103 |
104 // TODO(devlin): Maybe separate EventData into two classes? Rules, actions, and | 104 // TODO(devlin): Maybe separate EventData into two classes? Rules, actions, and |
105 // conditions should never be present on vanilla events. | 105 // conditions should never be present on vanilla events. |
106 struct APIBinding::EventData { | 106 struct APIBinding::EventData { |
107 EventData(std::string exposed_name, | 107 EventData(std::string exposed_name, |
108 std::string full_name, | 108 std::string full_name, |
109 bool supports_filters, | 109 bool supports_filters, |
110 bool supports_rules, | 110 bool supports_rules, |
| 111 bool supports_lazy_listeners, |
111 int max_listeners, | 112 int max_listeners, |
112 bool notify_on_change, | 113 bool notify_on_change, |
113 std::vector<std::string> actions, | 114 std::vector<std::string> actions, |
114 std::vector<std::string> conditions, | 115 std::vector<std::string> conditions, |
115 APIBinding* binding) | 116 APIBinding* binding) |
116 : exposed_name(std::move(exposed_name)), | 117 : exposed_name(std::move(exposed_name)), |
117 full_name(std::move(full_name)), | 118 full_name(std::move(full_name)), |
118 supports_filters(supports_filters), | 119 supports_filters(supports_filters), |
119 supports_rules(supports_rules), | 120 supports_rules(supports_rules), |
| 121 supports_lazy_listeners(supports_lazy_listeners), |
120 max_listeners(max_listeners), | 122 max_listeners(max_listeners), |
121 notify_on_change(notify_on_change), | 123 notify_on_change(notify_on_change), |
122 actions(std::move(actions)), | 124 actions(std::move(actions)), |
123 conditions(std::move(conditions)), | 125 conditions(std::move(conditions)), |
124 binding(binding) {} | 126 binding(binding) {} |
125 | 127 |
126 // The name of the event on the API object (e.g. onCreated). | 128 // The name of the event on the API object (e.g. onCreated). |
127 std::string exposed_name; | 129 std::string exposed_name; |
128 | 130 |
129 // The fully-specified name of the event (e.g. tabs.onCreated). | 131 // The fully-specified name of the event (e.g. tabs.onCreated). |
130 std::string full_name; | 132 std::string full_name; |
131 | 133 |
132 // Whether the event supports filters. | 134 // Whether the event supports filters. |
133 bool supports_filters; | 135 bool supports_filters; |
134 | 136 |
135 // Whether the event supports rules. | 137 // Whether the event supports rules. |
136 bool supports_rules; | 138 bool supports_rules; |
137 | 139 |
| 140 // Whether the event supports lazy listeners. |
| 141 bool supports_lazy_listeners; |
| 142 |
138 // The maximum number of listeners this event supports. | 143 // The maximum number of listeners this event supports. |
139 int max_listeners; | 144 int max_listeners; |
140 | 145 |
141 // Whether to notify the browser of listener changes. | 146 // Whether to notify the browser of listener changes. |
142 bool notify_on_change; | 147 bool notify_on_change; |
143 | 148 |
144 // The associated actions and conditions for declarative events. | 149 // The associated actions and conditions for declarative events. |
145 std::vector<std::string> actions; | 150 std::vector<std::string> actions; |
146 std::vector<std::string> conditions; | 151 std::vector<std::string> conditions; |
147 | 152 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); | 283 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); |
279 const base::ListValue* filters = nullptr; | 284 const base::ListValue* filters = nullptr; |
280 bool supports_filters = | 285 bool supports_filters = |
281 event_dict->GetList("filters", &filters) && !filters->empty(); | 286 event_dict->GetList("filters", &filters) && !filters->empty(); |
282 | 287 |
283 std::vector<std::string> rule_actions; | 288 std::vector<std::string> rule_actions; |
284 std::vector<std::string> rule_conditions; | 289 std::vector<std::string> rule_conditions; |
285 const base::DictionaryValue* options = nullptr; | 290 const base::DictionaryValue* options = nullptr; |
286 bool supports_rules = false; | 291 bool supports_rules = false; |
287 bool notify_on_change = true; | 292 bool notify_on_change = true; |
| 293 bool supports_lazy_listeners = true; |
288 int max_listeners = binding::kNoListenerMax; | 294 int max_listeners = binding::kNoListenerMax; |
289 if (event_dict->GetDictionary("options", &options)) { | 295 if (event_dict->GetDictionary("options", &options)) { |
290 bool temp_supports_filters = false; | 296 bool temp_supports_filters = false; |
291 // TODO(devlin): For some reason, schemas indicate supporting filters | 297 // TODO(devlin): For some reason, schemas indicate supporting filters |
292 // either through having a 'filters' property *or* through having | 298 // either through having a 'filters' property *or* through having |
293 // a 'supportsFilters' property. We should clean that up. | 299 // a 'supportsFilters' property. We should clean that up. |
294 supports_filters |= | 300 supports_filters |= |
295 (options->GetBoolean("supportsFilters", &temp_supports_filters) && | 301 (options->GetBoolean("supportsFilters", &temp_supports_filters) && |
296 temp_supports_filters); | 302 temp_supports_filters); |
297 if (options->GetBoolean("supportsRules", &supports_rules) && | 303 if (options->GetBoolean("supportsRules", &supports_rules) && |
(...skipping 12 matching lines...) Expand all Loading... |
310 } | 316 } |
311 }; | 317 }; |
312 get_values("actions", &rule_actions); | 318 get_values("actions", &rule_actions); |
313 get_values("conditions", &rule_conditions); | 319 get_values("conditions", &rule_conditions); |
314 } | 320 } |
315 | 321 |
316 options->GetInteger("maxListeners", &max_listeners); | 322 options->GetInteger("maxListeners", &max_listeners); |
317 bool unmanaged = false; | 323 bool unmanaged = false; |
318 if (options->GetBoolean("unmanaged", &unmanaged)) | 324 if (options->GetBoolean("unmanaged", &unmanaged)) |
319 notify_on_change = !unmanaged; | 325 notify_on_change = !unmanaged; |
| 326 if (options->GetBoolean("supportsLazyListeners", |
| 327 &supports_lazy_listeners)) { |
| 328 DCHECK(!supports_lazy_listeners) |
| 329 << "Don't specify supportsLazyListeners: true; it's the default."; |
| 330 } |
320 } | 331 } |
321 | 332 |
322 events_.push_back(base::MakeUnique<EventData>( | 333 events_.push_back(base::MakeUnique<EventData>( |
323 std::move(name), std::move(full_name), supports_filters, | 334 std::move(name), std::move(full_name), supports_filters, |
324 supports_rules, max_listeners, notify_on_change, | 335 supports_rules, supports_lazy_listeners, max_listeners, |
325 std::move(rule_actions), std::move(rule_conditions), this)); | 336 notify_on_change, std::move(rule_actions), std::move(rule_conditions), |
| 337 this)); |
326 } | 338 } |
327 } | 339 } |
328 } | 340 } |
329 | 341 |
330 APIBinding::~APIBinding() {} | 342 APIBinding::~APIBinding() {} |
331 | 343 |
332 v8::Local<v8::Object> APIBinding::CreateInstance( | 344 v8::Local<v8::Object> APIBinding::CreateInstance( |
333 v8::Local<v8::Context> context) { | 345 v8::Local<v8::Context> context) { |
334 DCHECK(IsContextValid(context)); | 346 DCHECK(IsContextValid(context)); |
335 v8::Isolate* isolate = context->GetIsolate(); | 347 v8::Isolate* isolate = context->GetIsolate(); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 } else if (event_data->supports_rules) { | 526 } else if (event_data->supports_rules) { |
515 gin::Handle<DeclarativeEvent> event = gin::CreateHandle( | 527 gin::Handle<DeclarativeEvent> event = gin::CreateHandle( |
516 isolate, new DeclarativeEvent( | 528 isolate, new DeclarativeEvent( |
517 event_data->full_name, event_data->binding->type_refs_, | 529 event_data->full_name, event_data->binding->type_refs_, |
518 event_data->binding->request_handler_, event_data->actions, | 530 event_data->binding->request_handler_, event_data->actions, |
519 event_data->conditions, 0)); | 531 event_data->conditions, 0)); |
520 retval = event.ToV8(); | 532 retval = event.ToV8(); |
521 } else { | 533 } else { |
522 retval = event_data->binding->event_handler_->CreateEventInstance( | 534 retval = event_data->binding->event_handler_->CreateEventInstance( |
523 event_data->full_name, event_data->supports_filters, | 535 event_data->full_name, event_data->supports_filters, |
524 event_data->max_listeners, event_data->notify_on_change, context); | 536 event_data->supports_lazy_listeners, event_data->max_listeners, |
| 537 event_data->notify_on_change, context); |
525 } | 538 } |
526 info.GetReturnValue().Set(retval); | 539 info.GetReturnValue().Set(retval); |
527 } | 540 } |
528 | 541 |
529 void APIBinding::GetCustomPropertyObject( | 542 void APIBinding::GetCustomPropertyObject( |
530 v8::Local<v8::Name> property_name, | 543 v8::Local<v8::Name> property_name, |
531 const v8::PropertyCallbackInfo<v8::Value>& info) { | 544 const v8::PropertyCallbackInfo<v8::Value>& info) { |
532 v8::Isolate* isolate = info.GetIsolate(); | 545 v8::Isolate* isolate = info.GetIsolate(); |
533 v8::HandleScope handle_scope(isolate); | 546 v8::HandleScope handle_scope(isolate); |
534 v8::Local<v8::Context> context = info.Holder()->CreationContext(); | 547 v8::Local<v8::Context> context = info.Holder()->CreationContext(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 arguments->ThrowTypeError(api_errors::InvocationError( | 664 arguments->ThrowTypeError(api_errors::InvocationError( |
652 name, signature->GetExpectedSignature(), error)); | 665 name, signature->GetExpectedSignature(), error)); |
653 return; | 666 return; |
654 } | 667 } |
655 | 668 |
656 request_handler_->StartRequest(context, name, std::move(converted_arguments), | 669 request_handler_->StartRequest(context, name, std::move(converted_arguments), |
657 callback, custom_callback, thread); | 670 callback, custom_callback, thread); |
658 } | 671 } |
659 | 672 |
660 } // namespace extensions | 673 } // namespace extensions |
OLD | NEW |