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 bool invalid_invocation = false; |
192 APIBindingHooks::RequestResult::NOT_HANDLED; | 192 std::vector<v8::Local<v8::Value>> argument_list; |
193 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, | 193 { |
194 signature, arguments, | 194 v8::TryCatch try_catch(isolate); |
195 *type_refs_); | 195 invalid_invocation = arguments->Length() > 0 && |
196 !arguments->GetRemaining(&argument_list); | |
197 if (try_catch.HasCaught()) { | |
jbroman
2017/01/02 19:46:38
Why would this ever catch? Nothing about checking
Devlin
2017/01/04 17:57:02
Nope, shouldn't catch (and also should't fail). S
| |
198 try_catch.ReThrow(); | |
199 return; | |
200 } | |
201 } | |
202 // Throw the type error here (rather than above when parsing) to avoid just | |
203 // catching it. | |
204 if (invalid_invocation) { | |
205 arguments->ThrowTypeError("Invalid invocation"); | |
206 return; | |
207 } | |
196 | 208 |
197 switch (hooks_result) { | 209 { |
198 case APIBindingHooks::RequestResult::INVALID_INVOCATION: | 210 v8::TryCatch try_catch(isolate); |
199 arguments->ThrowTypeError("Invalid invocation"); | 211 APIBindingHooks::RequestResult hooks_result = |
200 return; | 212 APIBindingHooks::RequestResult::NOT_HANDLED; |
201 case APIBindingHooks::RequestResult::HANDLED: | 213 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, |
202 return; // Our work here is done. | 214 signature, &argument_list, |
203 case APIBindingHooks::RequestResult::NOT_HANDLED: | 215 *type_refs_); |
204 break; // Handle in the default manner. | 216 |
217 switch (hooks_result) { | |
218 case APIBindingHooks::RequestResult::INVALID_INVOCATION: | |
219 invalid_invocation = true; | |
220 // Throw a type error below so that it's not caught by our try-catch. | |
221 break; | |
222 case APIBindingHooks::RequestResult::THROWN: | |
223 DCHECK(try_catch.HasCaught()); | |
224 try_catch.ReThrow(); | |
225 return; | |
226 case APIBindingHooks::RequestResult::HANDLED: | |
227 return; // Our work here is done. | |
228 case APIBindingHooks::RequestResult::NOT_HANDLED: | |
229 break; // Handle in the default manner. | |
230 } | |
231 } | |
232 | |
233 if (invalid_invocation) { | |
234 arguments->ThrowTypeError("Invalid invocation"); | |
235 return; | |
205 } | 236 } |
206 | 237 |
207 std::unique_ptr<base::ListValue> converted_arguments; | 238 std::unique_ptr<base::ListValue> converted_arguments; |
208 v8::Local<v8::Function> callback; | 239 v8::Local<v8::Function> callback; |
209 bool conversion_success = false; | |
210 { | 240 { |
211 v8::TryCatch try_catch(isolate); | 241 v8::TryCatch try_catch(isolate); |
212 conversion_success = signature->ParseArgumentsToJSON( | 242 invalid_invocation = !signature->ParseArgumentsToJSON( |
213 arguments, *type_refs_, &converted_arguments, &callback, &error); | 243 context, argument_list, *type_refs_, |
244 &converted_arguments, &callback, &error); | |
214 if (try_catch.HasCaught()) { | 245 if (try_catch.HasCaught()) { |
215 DCHECK(!converted_arguments); | 246 DCHECK(!converted_arguments); |
216 try_catch.ReThrow(); | 247 try_catch.ReThrow(); |
217 return; | 248 return; |
218 } | 249 } |
219 } | 250 } |
220 if (!conversion_success) { | 251 if (invalid_invocation) { |
221 arguments->ThrowTypeError("Invalid invocation"); | 252 arguments->ThrowTypeError("Invalid invocation"); |
222 return; | 253 return; |
223 } | 254 } |
224 | 255 |
225 DCHECK(converted_arguments); | 256 DCHECK(converted_arguments); |
226 method_callback_.Run(name, std::move(converted_arguments), isolate, context, | 257 method_callback_.Run(name, std::move(converted_arguments), isolate, context, |
227 callback); | 258 callback); |
228 } | 259 } |
229 | 260 |
230 } // namespace extensions | 261 } // namespace extensions |
OLD | NEW |