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.h" | 5 #include "extensions/renderer/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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 const APISignature* signature, | 183 const APISignature* signature, |
184 gin::Arguments* arguments) { | 184 gin::Arguments* arguments) { |
185 std::string error; | 185 std::string error; |
186 v8::Isolate* isolate = arguments->isolate(); | 186 v8::Isolate* isolate = arguments->isolate(); |
187 v8::HandleScope handle_scope(isolate); | 187 v8::HandleScope handle_scope(isolate); |
188 | 188 |
189 // Since this is called synchronously from the JS entry point, | 189 // Since this is called synchronously from the JS entry point, |
190 // GetCurrentContext() should always be correct. | 190 // GetCurrentContext() should always be correct. |
191 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 191 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
192 | 192 |
193 APIBindingHooks::RequestResult hooks_result = | 193 std::vector<v8::Local<v8::Value>> argument_list; |
194 APIBindingHooks::RequestResult::NOT_HANDLED; | 194 if (arguments->Length() > 0) { |
195 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, | 195 // Just copying handles should never fail. |
196 signature, arguments, | 196 CHECK(arguments->GetRemaining(&argument_list)); |
197 *type_refs_); | 197 } |
198 | 198 |
199 switch (hooks_result) { | 199 bool invalid_invocation = false; |
200 case APIBindingHooks::RequestResult::INVALID_INVOCATION: | 200 { |
201 arguments->ThrowTypeError("Invalid invocation"); | 201 v8::TryCatch try_catch(isolate); |
202 return; | 202 APIBindingHooks::RequestResult hooks_result = |
203 case APIBindingHooks::RequestResult::HANDLED: | 203 APIBindingHooks::RequestResult::NOT_HANDLED; |
204 return; // Our work here is done. | 204 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, |
205 case APIBindingHooks::RequestResult::NOT_HANDLED: | 205 signature, &argument_list, |
206 break; // Handle in the default manner. | 206 *type_refs_); |
| 207 |
| 208 switch (hooks_result) { |
| 209 case APIBindingHooks::RequestResult::INVALID_INVOCATION: |
| 210 invalid_invocation = true; |
| 211 // Throw a type error below so that it's not caught by our try-catch. |
| 212 break; |
| 213 case APIBindingHooks::RequestResult::THROWN: |
| 214 DCHECK(try_catch.HasCaught()); |
| 215 try_catch.ReThrow(); |
| 216 return; |
| 217 case APIBindingHooks::RequestResult::HANDLED: |
| 218 return; // Our work here is done. |
| 219 case APIBindingHooks::RequestResult::NOT_HANDLED: |
| 220 break; // Handle in the default manner. |
| 221 } |
| 222 } |
| 223 |
| 224 if (invalid_invocation) { |
| 225 arguments->ThrowTypeError("Invalid invocation"); |
| 226 return; |
207 } | 227 } |
208 | 228 |
209 std::unique_ptr<base::ListValue> converted_arguments; | 229 std::unique_ptr<base::ListValue> converted_arguments; |
210 v8::Local<v8::Function> callback; | 230 v8::Local<v8::Function> callback; |
211 bool conversion_success = false; | |
212 { | 231 { |
213 v8::TryCatch try_catch(isolate); | 232 v8::TryCatch try_catch(isolate); |
214 conversion_success = signature->ParseArgumentsToJSON( | 233 invalid_invocation = !signature->ParseArgumentsToJSON( |
215 arguments, *type_refs_, &converted_arguments, &callback, &error); | 234 context, argument_list, *type_refs_, |
| 235 &converted_arguments, &callback, &error); |
216 if (try_catch.HasCaught()) { | 236 if (try_catch.HasCaught()) { |
217 DCHECK(!converted_arguments); | 237 DCHECK(!converted_arguments); |
218 try_catch.ReThrow(); | 238 try_catch.ReThrow(); |
219 return; | 239 return; |
220 } | 240 } |
221 } | 241 } |
222 if (!conversion_success) { | 242 if (invalid_invocation) { |
223 arguments->ThrowTypeError("Invalid invocation"); | 243 arguments->ThrowTypeError("Invalid invocation"); |
224 return; | 244 return; |
225 } | 245 } |
226 | 246 |
227 DCHECK(converted_arguments); | 247 DCHECK(converted_arguments); |
228 method_callback_.Run(name, std::move(converted_arguments), isolate, context, | 248 method_callback_.Run(name, std::move(converted_arguments), isolate, context, |
229 callback); | 249 callback); |
230 } | 250 } |
231 | 251 |
232 } // namespace extensions | 252 } // namespace extensions |
OLD | NEW |