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