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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 APIBinding::HandlerCallback callback; | 100 APIBinding::HandlerCallback callback; |
101 }; | 101 }; |
102 | 102 |
103 // TODO(devlin): Maybe separate EventData into two classes? Rules, actions, and | 103 // TODO(devlin): Maybe separate EventData into two classes? Rules, actions, and |
104 // conditions should never be present on vanilla events. | 104 // conditions should never be present on vanilla events. |
105 struct APIBinding::EventData { | 105 struct APIBinding::EventData { |
106 EventData(std::string exposed_name, | 106 EventData(std::string exposed_name, |
107 std::string full_name, | 107 std::string full_name, |
108 bool supports_filters, | 108 bool supports_filters, |
109 bool supports_rules, | 109 bool supports_rules, |
110 bool supports_lazy_listeners, | |
110 int max_listeners, | 111 int max_listeners, |
111 bool notify_on_change, | 112 bool notify_on_change, |
112 std::vector<std::string> actions, | 113 std::vector<std::string> actions, |
113 std::vector<std::string> conditions, | 114 std::vector<std::string> conditions, |
114 APIBinding* binding) | 115 APIBinding* binding) |
115 : exposed_name(std::move(exposed_name)), | 116 : exposed_name(std::move(exposed_name)), |
116 full_name(std::move(full_name)), | 117 full_name(std::move(full_name)), |
117 supports_filters(supports_filters), | 118 supports_filters(supports_filters), |
118 supports_rules(supports_rules), | 119 supports_rules(supports_rules), |
120 supports_lazy_listeners(supports_lazy_listeners), | |
119 max_listeners(max_listeners), | 121 max_listeners(max_listeners), |
120 notify_on_change(notify_on_change), | 122 notify_on_change(notify_on_change), |
121 actions(std::move(actions)), | 123 actions(std::move(actions)), |
122 conditions(std::move(conditions)), | 124 conditions(std::move(conditions)), |
123 binding(binding) {} | 125 binding(binding) {} |
124 | 126 |
125 // The name of the event on the API object (e.g. onCreated). | 127 // The name of the event on the API object (e.g. onCreated). |
126 std::string exposed_name; | 128 std::string exposed_name; |
127 | 129 |
128 // The fully-specified name of the event (e.g. tabs.onCreated). | 130 // The fully-specified name of the event (e.g. tabs.onCreated). |
129 std::string full_name; | 131 std::string full_name; |
130 | 132 |
131 // Whether the event supports filters. | 133 // Whether the event supports filters. |
132 bool supports_filters; | 134 bool supports_filters; |
133 | 135 |
134 // Whether the event supports rules. | 136 // Whether the event supports rules. |
135 bool supports_rules; | 137 bool supports_rules; |
136 | 138 |
139 // Whether the event supports lazy listeners. | |
140 bool supports_lazy_listeners; | |
141 | |
137 // The maximum number of listeners this event supports. | 142 // The maximum number of listeners this event supports. |
138 int max_listeners; | 143 int max_listeners; |
139 | 144 |
140 // Whether to notify the browser of listener changes. | 145 // Whether to notify the browser of listener changes. |
141 bool notify_on_change; | 146 bool notify_on_change; |
142 | 147 |
143 // The associated actions and conditions for declarative events. | 148 // The associated actions and conditions for declarative events. |
144 std::vector<std::string> actions; | 149 std::vector<std::string> actions; |
145 std::vector<std::string> conditions; | 150 std::vector<std::string> conditions; |
146 | 151 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); | 280 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); |
276 const base::ListValue* filters = nullptr; | 281 const base::ListValue* filters = nullptr; |
277 bool supports_filters = | 282 bool supports_filters = |
278 event_dict->GetList("filters", &filters) && !filters->empty(); | 283 event_dict->GetList("filters", &filters) && !filters->empty(); |
279 | 284 |
280 std::vector<std::string> rule_actions; | 285 std::vector<std::string> rule_actions; |
281 std::vector<std::string> rule_conditions; | 286 std::vector<std::string> rule_conditions; |
282 const base::DictionaryValue* options = nullptr; | 287 const base::DictionaryValue* options = nullptr; |
283 bool supports_rules = false; | 288 bool supports_rules = false; |
284 bool notify_on_change = true; | 289 bool notify_on_change = true; |
290 bool supports_lazy_listeners = true; | |
285 int max_listeners = binding::kNoListenerMax; | 291 int max_listeners = binding::kNoListenerMax; |
286 if (event_dict->GetDictionary("options", &options)) { | 292 if (event_dict->GetDictionary("options", &options)) { |
287 bool temp_supports_filters = false; | 293 bool temp_supports_filters = false; |
288 // TODO(devlin): For some reason, schemas indicate supporting filters | 294 // TODO(devlin): For some reason, schemas indicate supporting filters |
289 // either through having a 'filters' property *or* through having | 295 // either through having a 'filters' property *or* through having |
290 // a 'supportsFilters' property. We should clean that up. | 296 // a 'supportsFilters' property. We should clean that up. |
291 supports_filters |= | 297 supports_filters |= |
292 (options->GetBoolean("supportsFilters", &temp_supports_filters) && | 298 (options->GetBoolean("supportsFilters", &temp_supports_filters) && |
293 temp_supports_filters); | 299 temp_supports_filters); |
294 if (options->GetBoolean("supportsRules", &supports_rules) && | 300 if (options->GetBoolean("supportsRules", &supports_rules) && |
(...skipping 12 matching lines...) Expand all Loading... | |
307 } | 313 } |
308 }; | 314 }; |
309 get_values("actions", &rule_actions); | 315 get_values("actions", &rule_actions); |
310 get_values("conditions", &rule_conditions); | 316 get_values("conditions", &rule_conditions); |
311 } | 317 } |
312 | 318 |
313 options->GetInteger("maxListeners", &max_listeners); | 319 options->GetInteger("maxListeners", &max_listeners); |
314 bool unmanaged = false; | 320 bool unmanaged = false; |
315 if (options->GetBoolean("unmanaged", &unmanaged)) | 321 if (options->GetBoolean("unmanaged", &unmanaged)) |
316 notify_on_change = !unmanaged; | 322 notify_on_change = !unmanaged; |
323 bool temp_supports_lazy_listeners = false; | |
jbroman
2017/07/10 15:33:02
nit: Why is it so bad that we have to prevent "sup
lazyboy
2017/07/14 00:04:42
I like it this way though: API schema owners need
Devlin
2017/07/14 15:37:54
I think jbroman@ was just saying that it seems exc
lazyboy
2017/07/17 23:38:50
Sure, I thought it was to consider removing the re
| |
324 if (options->GetBoolean("supportsLazyListeners", | |
325 &temp_supports_lazy_listeners)) { | |
326 DCHECK(!temp_supports_lazy_listeners) | |
327 << "Don't specify supportsLazyListeners: true; it's the default."; | |
328 supports_lazy_listeners = false; | |
329 } | |
317 } | 330 } |
318 | 331 |
319 events_.push_back(base::MakeUnique<EventData>( | 332 events_.push_back(base::MakeUnique<EventData>( |
320 std::move(name), std::move(full_name), supports_filters, | 333 std::move(name), std::move(full_name), supports_filters, |
321 supports_rules, max_listeners, notify_on_change, | 334 supports_rules, supports_lazy_listeners, max_listeners, |
322 std::move(rule_actions), std::move(rule_conditions), this)); | 335 notify_on_change, std::move(rule_actions), std::move(rule_conditions), |
336 this)); | |
323 } | 337 } |
324 } | 338 } |
325 } | 339 } |
326 | 340 |
327 APIBinding::~APIBinding() {} | 341 APIBinding::~APIBinding() {} |
328 | 342 |
329 v8::Local<v8::Object> APIBinding::CreateInstance( | 343 v8::Local<v8::Object> APIBinding::CreateInstance( |
330 v8::Local<v8::Context> context) { | 344 v8::Local<v8::Context> context) { |
331 DCHECK(IsContextValid(context)); | 345 DCHECK(IsContextValid(context)); |
332 v8::Isolate* isolate = context->GetIsolate(); | 346 v8::Isolate* isolate = context->GetIsolate(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 } else if (event_data->supports_rules) { | 508 } else if (event_data->supports_rules) { |
495 gin::Handle<DeclarativeEvent> event = gin::CreateHandle( | 509 gin::Handle<DeclarativeEvent> event = gin::CreateHandle( |
496 isolate, new DeclarativeEvent( | 510 isolate, new DeclarativeEvent( |
497 event_data->full_name, event_data->binding->type_refs_, | 511 event_data->full_name, event_data->binding->type_refs_, |
498 event_data->binding->request_handler_, event_data->actions, | 512 event_data->binding->request_handler_, event_data->actions, |
499 event_data->conditions, 0)); | 513 event_data->conditions, 0)); |
500 retval = event.ToV8(); | 514 retval = event.ToV8(); |
501 } else { | 515 } else { |
502 retval = event_data->binding->event_handler_->CreateEventInstance( | 516 retval = event_data->binding->event_handler_->CreateEventInstance( |
503 event_data->full_name, event_data->supports_filters, | 517 event_data->full_name, event_data->supports_filters, |
504 event_data->max_listeners, event_data->notify_on_change, context); | 518 event_data->supports_lazy_listeners, event_data->max_listeners, |
519 event_data->notify_on_change, context); | |
505 } | 520 } |
506 info.GetReturnValue().Set(retval); | 521 info.GetReturnValue().Set(retval); |
507 } | 522 } |
508 | 523 |
509 void APIBinding::GetCustomPropertyObject( | 524 void APIBinding::GetCustomPropertyObject( |
510 v8::Local<v8::Name> property_name, | 525 v8::Local<v8::Name> property_name, |
511 const v8::PropertyCallbackInfo<v8::Value>& info) { | 526 const v8::PropertyCallbackInfo<v8::Value>& info) { |
512 v8::Isolate* isolate = info.GetIsolate(); | 527 v8::Isolate* isolate = info.GetIsolate(); |
513 v8::HandleScope handle_scope(isolate); | 528 v8::HandleScope handle_scope(isolate); |
514 v8::Local<v8::Context> context = info.Holder()->CreationContext(); | 529 v8::Local<v8::Context> context = info.Holder()->CreationContext(); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
622 arguments->ThrowTypeError(api_errors::InvocationError( | 637 arguments->ThrowTypeError(api_errors::InvocationError( |
623 name, signature->GetExpectedSignature(), error)); | 638 name, signature->GetExpectedSignature(), error)); |
624 return; | 639 return; |
625 } | 640 } |
626 | 641 |
627 request_handler_->StartRequest(context, name, std::move(converted_arguments), | 642 request_handler_->StartRequest(context, name, std::move(converted_arguments), |
628 callback, custom_callback, thread); | 643 callback, custom_callback, thread); |
629 } | 644 } |
630 | 645 |
631 } // namespace extensions | 646 } // namespace extensions |
OLD | NEW |