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_js_util.h" | 5 #include "extensions/renderer/api_binding_js_util.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "extensions/renderer/api_event_handler.h" | 8 #include "extensions/renderer/api_event_handler.h" |
9 #include "extensions/renderer/api_request_handler.h" | 9 #include "extensions/renderer/api_request_handler.h" |
10 #include "extensions/renderer/api_signature.h" | 10 #include "extensions/renderer/api_signature.h" |
11 #include "extensions/renderer/api_type_reference_map.h" | 11 #include "extensions/renderer/api_type_reference_map.h" |
12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
13 #include "gin/object_template_builder.h" | 13 #include "gin/object_template_builder.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 gin::WrapperInfo APIBindingJSUtil::kWrapperInfo = {gin::kEmbedderNativeGin}; | 17 gin::WrapperInfo APIBindingJSUtil::kWrapperInfo = {gin::kEmbedderNativeGin}; |
18 | 18 |
19 APIBindingJSUtil::APIBindingJSUtil(const APITypeReferenceMap* type_refs, | 19 APIBindingJSUtil::APIBindingJSUtil(const APITypeReferenceMap* type_refs, |
20 APIRequestHandler* request_handler, | 20 APIRequestHandler* request_handler, |
21 APIEventHandler* event_handler) | 21 APIEventHandler* event_handler, |
22 const binding::RunJSFunction& run_js) | |
22 : type_refs_(type_refs), | 23 : type_refs_(type_refs), |
23 request_handler_(request_handler), | 24 request_handler_(request_handler), |
24 event_handler_(event_handler) {} | 25 event_handler_(event_handler), |
26 run_js_(run_js) {} | |
25 | 27 |
26 APIBindingJSUtil::~APIBindingJSUtil() {} | 28 APIBindingJSUtil::~APIBindingJSUtil() {} |
27 | 29 |
28 gin::ObjectTemplateBuilder APIBindingJSUtil::GetObjectTemplateBuilder( | 30 gin::ObjectTemplateBuilder APIBindingJSUtil::GetObjectTemplateBuilder( |
29 v8::Isolate* isolate) { | 31 v8::Isolate* isolate) { |
30 return Wrappable<APIBindingJSUtil>::GetObjectTemplateBuilder(isolate) | 32 return Wrappable<APIBindingJSUtil>::GetObjectTemplateBuilder(isolate) |
31 .SetMethod("sendRequest", &APIBindingJSUtil::SendRequest) | 33 .SetMethod("sendRequest", &APIBindingJSUtil::SendRequest) |
32 .SetMethod("registerEventArgumentMassager", | 34 .SetMethod("registerEventArgumentMassager", |
33 &APIBindingJSUtil::RegisterEventArgumentMassager) | 35 &APIBindingJSUtil::RegisterEventArgumentMassager) |
34 .SetMethod("createCustomEvent", &APIBindingJSUtil::CreateCustomEvent) | 36 .SetMethod("createCustomEvent", &APIBindingJSUtil::CreateCustomEvent) |
35 .SetMethod("invalidateEvent", &APIBindingJSUtil::InvalidateEvent); | 37 .SetMethod("invalidateEvent", &APIBindingJSUtil::InvalidateEvent) |
38 .SetMethod("setLastError", &APIBindingJSUtil::SetLastError) | |
39 .SetMethod("clearLastError", &APIBindingJSUtil::ClearLastError) | |
40 .SetMethod("hasLastError", &APIBindingJSUtil::HasLastError) | |
41 .SetMethod("runCallbackWithLastError", | |
42 &APIBindingJSUtil::RunCallbackWithLastError); | |
36 } | 43 } |
37 | 44 |
38 void APIBindingJSUtil::SendRequest( | 45 void APIBindingJSUtil::SendRequest( |
39 gin::Arguments* arguments, | 46 gin::Arguments* arguments, |
40 const std::string& name, | 47 const std::string& name, |
41 const std::vector<v8::Local<v8::Value>>& request_args) { | 48 const std::vector<v8::Local<v8::Value>>& request_args) { |
42 v8::Isolate* isolate = arguments->isolate(); | 49 v8::Isolate* isolate = arguments->isolate(); |
43 v8::HandleScope handle_scope(isolate); | 50 v8::HandleScope handle_scope(isolate); |
44 v8::Local<v8::Object> holder; | 51 v8::Local<v8::Object> holder; |
45 CHECK(arguments->GetHolder(&holder)); | 52 CHECK(arguments->GetHolder(&holder)); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 void APIBindingJSUtil::InvalidateEvent(gin::Arguments* arguments, | 114 void APIBindingJSUtil::InvalidateEvent(gin::Arguments* arguments, |
108 v8::Local<v8::Object> event) { | 115 v8::Local<v8::Object> event) { |
109 v8::Isolate* isolate = arguments->isolate(); | 116 v8::Isolate* isolate = arguments->isolate(); |
110 v8::HandleScope handle_scope(isolate); | 117 v8::HandleScope handle_scope(isolate); |
111 v8::Local<v8::Object> holder; | 118 v8::Local<v8::Object> holder; |
112 CHECK(arguments->GetHolder(&holder)); | 119 CHECK(arguments->GetHolder(&holder)); |
113 v8::Local<v8::Context> context = holder->CreationContext(); | 120 v8::Local<v8::Context> context = holder->CreationContext(); |
114 event_handler_->InvalidateCustomEvent(context, event); | 121 event_handler_->InvalidateCustomEvent(context, event); |
115 } | 122 } |
116 | 123 |
124 void APIBindingJSUtil::SetLastError(gin::Arguments* arguments, | |
125 const std::string& error) { | |
126 v8::Isolate* isolate = arguments->isolate(); | |
127 v8::HandleScope handle_scope(isolate); | |
128 v8::Local<v8::Object> holder; | |
129 CHECK(arguments->GetHolder(&holder)); | |
130 v8::Local<v8::Context> context = holder->CreationContext(); | |
131 | |
132 request_handler_->last_error()->SetError(context, error); | |
133 } | |
134 | |
135 void APIBindingJSUtil::ClearLastError(gin::Arguments* arguments) { | |
136 v8::Isolate* isolate = arguments->isolate(); | |
137 v8::HandleScope handle_scope(isolate); | |
138 v8::Local<v8::Object> holder; | |
139 CHECK(arguments->GetHolder(&holder)); | |
140 v8::Local<v8::Context> context = holder->CreationContext(); | |
141 | |
142 bool report_if_unchecked = false; | |
143 request_handler_->last_error()->ClearError(context, report_if_unchecked); | |
144 } | |
145 | |
146 void APIBindingJSUtil::HasLastError(gin::Arguments* arguments) { | |
147 v8::Isolate* isolate = arguments->isolate(); | |
148 v8::HandleScope handle_scope(isolate); | |
149 v8::Local<v8::Object> holder; | |
150 CHECK(arguments->GetHolder(&holder)); | |
151 v8::Local<v8::Context> context = holder->CreationContext(); | |
152 | |
153 bool has_last_error = request_handler_->last_error()->HasError(context); | |
154 arguments->Return(has_last_error); | |
155 } | |
156 | |
157 void APIBindingJSUtil::RunCallbackWithLastError( | |
158 gin::Arguments* arguments, | |
159 const std::string& error, | |
160 v8::Local<v8::Function> callback, | |
161 std::vector<v8::Local<v8::Value>> callback_args) { | |
jbroman
2017/03/20 20:14:49
const vector&: gin doesn't move the arguments into
Devlin
2017/03/20 23:10:40
Moot now with no |callback_args|, but this was act
| |
162 v8::Isolate* isolate = arguments->isolate(); | |
163 v8::HandleScope handle_scope(isolate); | |
164 v8::Local<v8::Object> holder; | |
165 CHECK(arguments->GetHolder(&holder)); | |
166 v8::Local<v8::Context> context = holder->CreationContext(); | |
167 | |
168 request_handler_->last_error()->SetError(context, error); | |
169 if (!callback->IsNull() && !callback->IsUndefined()) | |
jbroman
2017/03/20 20:14:49
1. What's the point of calling this will null or u
Devlin
2017/03/20 23:10:40
1. If we mimic the JS api, the function is often
| |
170 run_js_.Run(callback, context, callback_args.size(), callback_args.data()); | |
171 | |
172 bool report_if_unchecked = true; | |
173 request_handler_->last_error()->ClearError(context, report_if_unchecked); | |
174 } | |
175 | |
117 } // namespace extensions | 176 } // namespace extensions |
OLD | NEW |