Chromium Code Reviews| 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_bindings_system.h" | 5 #include "extensions/renderer/api_bindings_system.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "extensions/renderer/api_binding_hooks.h" | |
| 10 | 11 |
| 11 namespace extensions { | 12 namespace extensions { |
| 12 | 13 |
| 13 APIBindingsSystem::Request::Request() {} | 14 APIBindingsSystem::Request::Request() {} |
| 14 APIBindingsSystem::Request::~Request() {} | 15 APIBindingsSystem::Request::~Request() {} |
| 15 | 16 |
| 16 APIBindingsSystem::APIBindingsSystem(const binding::RunJSFunction& call_js, | 17 APIBindingsSystem::APIBindingsSystem(const binding::RunJSFunction& call_js, |
| 17 const GetAPISchemaMethod& get_api_schema, | 18 const GetAPISchemaMethod& get_api_schema, |
| 18 const SendRequestMethod& send_request) | 19 const SendRequestMethod& send_request) |
| 19 : request_handler_(call_js), | 20 : request_handler_(call_js), |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 40 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); | 41 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); |
| 41 | 42 |
| 42 const base::ListValue* function_definitions = nullptr; | 43 const base::ListValue* function_definitions = nullptr; |
| 43 CHECK(api_schema.GetList("functions", &function_definitions)); | 44 CHECK(api_schema.GetList("functions", &function_definitions)); |
| 44 const base::ListValue* type_definitions = nullptr; | 45 const base::ListValue* type_definitions = nullptr; |
| 45 // Type definitions might not exist for the given API. | 46 // Type definitions might not exist for the given API. |
| 46 api_schema.GetList("types", &type_definitions); | 47 api_schema.GetList("types", &type_definitions); |
| 47 const base::ListValue* event_definitions = nullptr; | 48 const base::ListValue* event_definitions = nullptr; |
| 48 api_schema.GetList("events", &event_definitions); | 49 api_schema.GetList("events", &event_definitions); |
| 49 | 50 |
| 51 // Find the hooks for the API. If none are registered, we create one. | |
|
jbroman
2016/12/08 16:58:56
nit: Is there a need to heap-allocate one when we
Devlin
2016/12/08 19:12:03
Mostly just because I didn't want to have to nullc
| |
| 52 std::unique_ptr<APIBindingHooks> hooks; | |
| 53 auto iter = binding_hooks_.find(api_name); | |
| 54 if (iter != binding_hooks_.end()) { | |
| 55 hooks = std::move(iter->second); | |
| 56 binding_hooks_.erase(iter); | |
| 57 } else { | |
| 58 hooks = base::MakeUnique<APIBindingHooks>(api_name); | |
| 59 } | |
| 60 | |
| 50 return base::MakeUnique<APIBinding>( | 61 return base::MakeUnique<APIBinding>( |
| 51 api_name, *function_definitions, type_definitions, event_definitions, | 62 api_name, *function_definitions, type_definitions, event_definitions, |
| 52 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), | 63 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), |
| 53 &type_reference_map_); | 64 std::move(hooks), &type_reference_map_); |
| 54 } | 65 } |
| 55 | 66 |
| 56 void APIBindingsSystem::CompleteRequest(int request_id, | 67 void APIBindingsSystem::CompleteRequest(int request_id, |
| 57 const base::ListValue& response) { | 68 const base::ListValue& response) { |
| 58 request_handler_.CompleteRequest(request_id, response); | 69 request_handler_.CompleteRequest(request_id, response); |
| 59 } | 70 } |
| 60 | 71 |
| 61 void APIBindingsSystem::FireEventInContext(const std::string& event_name, | 72 void APIBindingsSystem::FireEventInContext(const std::string& event_name, |
| 62 v8::Local<v8::Context> context, | 73 v8::Local<v8::Context> context, |
| 63 const base::ListValue& response) { | 74 const base::ListValue& response) { |
| 64 event_handler_.FireEventInContext(event_name, context, response); | 75 event_handler_.FireEventInContext(event_name, context, response); |
| 65 } | 76 } |
| 66 | 77 |
| 78 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( | |
| 79 const std::string& api_name) { | |
| 80 DCHECK(api_bindings_.empty()) | |
| 81 << "Hook registration must happen before creating any binding instances."; | |
| 82 auto iter = binding_hooks_.find(api_name); | |
|
jbroman
2016/12/08 16:58:56
super-duper-nit: This can be written without doing
Devlin
2016/12/08 19:12:03
I like it, done.
| |
| 83 if (iter != binding_hooks_.end()) | |
| 84 return iter->second.get(); | |
| 85 auto hooks = base::MakeUnique<APIBindingHooks>(api_name); | |
| 86 APIBindingHooks* hooks_weak = hooks.get(); | |
| 87 binding_hooks_[api_name] = std::move(hooks); | |
| 88 return hooks_weak; | |
| 89 } | |
| 90 | |
| 67 void APIBindingsSystem::OnAPICall(const std::string& name, | 91 void APIBindingsSystem::OnAPICall(const std::string& name, |
| 68 std::unique_ptr<base::ListValue> arguments, | 92 std::unique_ptr<base::ListValue> arguments, |
| 69 v8::Isolate* isolate, | 93 v8::Isolate* isolate, |
| 70 v8::Local<v8::Context> context, | 94 v8::Local<v8::Context> context, |
| 71 v8::Local<v8::Function> callback) { | 95 v8::Local<v8::Function> callback) { |
| 72 auto request = base::MakeUnique<Request>(); | 96 auto request = base::MakeUnique<Request>(); |
| 73 if (!callback.IsEmpty()) { | 97 if (!callback.IsEmpty()) { |
| 74 request->request_id = | 98 request->request_id = |
| 75 request_handler_.AddPendingRequest(isolate, callback, context); | 99 request_handler_.AddPendingRequest(isolate, callback, context); |
| 76 request->has_callback = true; | 100 request->has_callback = true; |
| 77 } | 101 } |
| 78 // TODO(devlin): Query and curry user gestures around. | 102 // TODO(devlin): Query and curry user gestures around. |
| 79 request->has_user_gesture = false; | 103 request->has_user_gesture = false; |
| 80 request->arguments = std::move(arguments); | 104 request->arguments = std::move(arguments); |
| 81 request->method_name = name; | 105 request->method_name = name; |
| 82 | 106 |
| 83 send_request_.Run(std::move(request), context); | 107 send_request_.Run(std::move(request), context); |
| 84 } | 108 } |
| 85 | 109 |
| 86 } // namespace extensions | 110 } // namespace extensions |
| OLD | NEW |