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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 void APIBindingJSUtil::InvalidateEvent(gin::Arguments* arguments, | 118 void APIBindingJSUtil::InvalidateEvent(gin::Arguments* arguments, |
112 v8::Local<v8::Object> event) { | 119 v8::Local<v8::Object> event) { |
113 v8::Isolate* isolate = arguments->isolate(); | 120 v8::Isolate* isolate = arguments->isolate(); |
114 v8::HandleScope handle_scope(isolate); | 121 v8::HandleScope handle_scope(isolate); |
115 v8::Local<v8::Object> holder; | 122 v8::Local<v8::Object> holder; |
116 CHECK(arguments->GetHolder(&holder)); | 123 CHECK(arguments->GetHolder(&holder)); |
117 v8::Local<v8::Context> context = holder->CreationContext(); | 124 v8::Local<v8::Context> context = holder->CreationContext(); |
118 event_handler_->InvalidateCustomEvent(context, event); | 125 event_handler_->InvalidateCustomEvent(context, event); |
119 } | 126 } |
120 | 127 |
| 128 void APIBindingJSUtil::SetLastError(gin::Arguments* arguments, |
| 129 const std::string& error) { |
| 130 v8::Isolate* isolate = arguments->isolate(); |
| 131 v8::HandleScope handle_scope(isolate); |
| 132 v8::Local<v8::Object> holder; |
| 133 CHECK(arguments->GetHolder(&holder)); |
| 134 v8::Local<v8::Context> context = holder->CreationContext(); |
| 135 |
| 136 request_handler_->last_error()->SetError(context, error); |
| 137 } |
| 138 |
| 139 void APIBindingJSUtil::ClearLastError(gin::Arguments* arguments) { |
| 140 v8::Isolate* isolate = arguments->isolate(); |
| 141 v8::HandleScope handle_scope(isolate); |
| 142 v8::Local<v8::Object> holder; |
| 143 CHECK(arguments->GetHolder(&holder)); |
| 144 v8::Local<v8::Context> context = holder->CreationContext(); |
| 145 |
| 146 bool report_if_unchecked = false; |
| 147 request_handler_->last_error()->ClearError(context, report_if_unchecked); |
| 148 } |
| 149 |
| 150 void APIBindingJSUtil::HasLastError(gin::Arguments* arguments) { |
| 151 v8::Isolate* isolate = arguments->isolate(); |
| 152 v8::HandleScope handle_scope(isolate); |
| 153 v8::Local<v8::Object> holder; |
| 154 CHECK(arguments->GetHolder(&holder)); |
| 155 v8::Local<v8::Context> context = holder->CreationContext(); |
| 156 |
| 157 bool has_last_error = request_handler_->last_error()->HasError(context); |
| 158 arguments->Return(has_last_error); |
| 159 } |
| 160 |
| 161 void APIBindingJSUtil::RunCallbackWithLastError( |
| 162 gin::Arguments* arguments, |
| 163 const std::string& error, |
| 164 v8::Local<v8::Function> callback) { |
| 165 v8::Isolate* isolate = arguments->isolate(); |
| 166 v8::HandleScope handle_scope(isolate); |
| 167 v8::Local<v8::Object> holder; |
| 168 CHECK(arguments->GetHolder(&holder)); |
| 169 v8::Local<v8::Context> context = holder->CreationContext(); |
| 170 |
| 171 request_handler_->last_error()->SetError(context, error); |
| 172 run_js_.Run(callback, context, 0, nullptr); |
| 173 |
| 174 bool report_if_unchecked = true; |
| 175 request_handler_->last_error()->ClearError(context, report_if_unchecked); |
| 176 } |
| 177 |
121 } // namespace extensions | 178 } // namespace extensions |
OLD | NEW |