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 | 10 |
11 namespace extensions { | 11 namespace extensions { |
12 | 12 |
13 APIBindingsSystem::Request::Request() {} | 13 APIBindingsSystem::Request::Request() {} |
14 APIBindingsSystem::Request::~Request() {} | 14 APIBindingsSystem::Request::~Request() {} |
15 | 15 |
16 APIBindingsSystem::APIBindingsSystem( | 16 APIBindingsSystem::APIBindingsSystem(const binding::RunJSFunction& call_js, |
17 const APIRequestHandler::CallJSFunction& call_js, | 17 const GetAPISchemaMethod& get_api_schema, |
18 const GetAPISchemaMethod& get_api_schema, | 18 const SendRequestMethod& send_request) |
19 const SendRequestMethod& send_request) | |
20 : request_handler_(call_js), | 19 : request_handler_(call_js), |
| 20 event_handler_(call_js), |
21 get_api_schema_(get_api_schema), | 21 get_api_schema_(get_api_schema), |
22 send_request_(send_request) {} | 22 send_request_(send_request) {} |
23 | 23 |
24 APIBindingsSystem::~APIBindingsSystem() {} | 24 APIBindingsSystem::~APIBindingsSystem() {} |
25 | 25 |
26 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( | 26 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
27 const std::string& api_name, | 27 const std::string& api_name, |
28 v8::Local<v8::Context> context, | 28 v8::Local<v8::Context> context, |
29 v8::Isolate* isolate, | 29 v8::Isolate* isolate, |
30 const APIBinding::AvailabilityCallback& is_available) { | 30 const APIBinding::AvailabilityCallback& is_available) { |
31 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; | 31 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; |
32 if (!binding) | 32 if (!binding) |
33 binding = CreateNewAPIBinding(api_name); | 33 binding = CreateNewAPIBinding(api_name); |
34 return binding->CreateInstance(context, isolate, is_available); | 34 return binding->CreateInstance( |
| 35 context, isolate, &event_handler_, is_available); |
35 } | 36 } |
36 | 37 |
37 std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( | 38 std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( |
38 const std::string& api_name) { | 39 const std::string& api_name) { |
39 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); | 40 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); |
40 | 41 |
41 const base::ListValue* function_definitions = nullptr; | 42 const base::ListValue* function_definitions = nullptr; |
42 CHECK(api_schema.GetList("functions", &function_definitions)); | 43 CHECK(api_schema.GetList("functions", &function_definitions)); |
43 const base::ListValue* type_definitions = nullptr; | 44 const base::ListValue* type_definitions = nullptr; |
44 // Type definitions might not exist for the given API. | 45 // Type definitions might not exist for the given API. |
45 api_schema.GetList("types", &type_definitions); | 46 api_schema.GetList("types", &type_definitions); |
| 47 const base::ListValue* event_definitions = nullptr; |
| 48 api_schema.GetList("events", &event_definitions); |
46 | 49 |
47 return base::MakeUnique<APIBinding>( | 50 return base::MakeUnique<APIBinding>( |
48 api_name, *function_definitions, type_definitions, | 51 api_name, *function_definitions, type_definitions, event_definitions, |
49 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), | 52 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), |
50 &type_reference_map_); | 53 &type_reference_map_); |
51 } | 54 } |
52 | 55 |
53 void APIBindingsSystem::CompleteRequest(const std::string& request_id, | 56 void APIBindingsSystem::CompleteRequest(const std::string& request_id, |
54 const base::ListValue& response) { | 57 const base::ListValue& response) { |
55 request_handler_.CompleteRequest(request_id, response); | 58 request_handler_.CompleteRequest(request_id, response); |
56 } | 59 } |
57 | 60 |
| 61 void APIBindingsSystem::FireEventInContext(const std::string& event_name, |
| 62 v8::Local<v8::Context> context, |
| 63 const base::ListValue& response) { |
| 64 event_handler_.FireEventInContext(event_name, context, response); |
| 65 } |
| 66 |
58 void APIBindingsSystem::OnAPICall(const std::string& name, | 67 void APIBindingsSystem::OnAPICall(const std::string& name, |
59 std::unique_ptr<base::ListValue> arguments, | 68 std::unique_ptr<base::ListValue> arguments, |
60 v8::Isolate* isolate, | 69 v8::Isolate* isolate, |
61 v8::Local<v8::Context> context, | 70 v8::Local<v8::Context> context, |
62 v8::Local<v8::Function> callback) { | 71 v8::Local<v8::Function> callback) { |
63 auto request = base::MakeUnique<Request>(); | 72 auto request = base::MakeUnique<Request>(); |
64 if (!callback.IsEmpty()) { | 73 if (!callback.IsEmpty()) { |
65 request->request_id = | 74 request->request_id = |
66 request_handler_.AddPendingRequest(isolate, callback, context); | 75 request_handler_.AddPendingRequest(isolate, callback, context); |
67 } | 76 } |
68 request->arguments = std::move(arguments); | 77 request->arguments = std::move(arguments); |
69 request->method_name = name; | 78 request->method_name = name; |
70 | 79 |
71 send_request_.Run(std::move(request)); | 80 send_request_.Run(std::move(request)); |
72 } | 81 } |
73 | 82 |
74 } // namespace extensions | 83 } // namespace extensions |
OLD | NEW |