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 if (arguments->Length() > 0) { |
193 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, | 193 // Just copying handles should never fail. |
194 signature, arguments, | 194 CHECK(arguments->GetRemaining(&argument_list)); |
195 *type_refs_); | 195 } |
196 | 196 |
197 switch (hooks_result) { | 197 bool invalid_invocation = false; |
198 case APIBindingHooks::RequestResult::INVALID_INVOCATION: | 198 { |
199 arguments->ThrowTypeError("Invalid invocation"); | 199 v8::TryCatch try_catch(isolate); |
200 return; | 200 APIBindingHooks::RequestResult hooks_result = |
201 case APIBindingHooks::RequestResult::HANDLED: | 201 APIBindingHooks::RequestResult::NOT_HANDLED; |
202 return; // Our work here is done. | 202 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, |
203 case APIBindingHooks::RequestResult::NOT_HANDLED: | 203 signature, &argument_list, |
204 break; // Handle in the default manner. | 204 *type_refs_); |
| 205 |
| 206 switch (hooks_result) { |
| 207 case APIBindingHooks::RequestResult::INVALID_INVOCATION: |
| 208 invalid_invocation = true; |
| 209 // Throw a type error below so that it's not caught by our try-catch. |
| 210 break; |
| 211 case APIBindingHooks::RequestResult::THROWN: |
| 212 DCHECK(try_catch.HasCaught()); |
| 213 try_catch.ReThrow(); |
| 214 return; |
| 215 case APIBindingHooks::RequestResult::HANDLED: |
| 216 return; // Our work here is done. |
| 217 case APIBindingHooks::RequestResult::NOT_HANDLED: |
| 218 break; // Handle in the default manner. |
| 219 } |
| 220 } |
| 221 |
| 222 if (invalid_invocation) { |
| 223 arguments->ThrowTypeError("Invalid invocation"); |
| 224 return; |
205 } | 225 } |
206 | 226 |
207 std::unique_ptr<base::ListValue> converted_arguments; | 227 std::unique_ptr<base::ListValue> converted_arguments; |
208 v8::Local<v8::Function> callback; | 228 v8::Local<v8::Function> callback; |
209 bool conversion_success = false; | |
210 { | 229 { |
211 v8::TryCatch try_catch(isolate); | 230 v8::TryCatch try_catch(isolate); |
212 conversion_success = signature->ParseArgumentsToJSON( | 231 invalid_invocation = !signature->ParseArgumentsToJSON( |
213 arguments, *type_refs_, &converted_arguments, &callback, &error); | 232 context, argument_list, *type_refs_, |
| 233 &converted_arguments, &callback, &error); |
214 if (try_catch.HasCaught()) { | 234 if (try_catch.HasCaught()) { |
215 DCHECK(!converted_arguments); | 235 DCHECK(!converted_arguments); |
216 try_catch.ReThrow(); | 236 try_catch.ReThrow(); |
217 return; | 237 return; |
218 } | 238 } |
219 } | 239 } |
220 if (!conversion_success) { | 240 if (invalid_invocation) { |
221 arguments->ThrowTypeError("Invalid invocation"); | 241 arguments->ThrowTypeError("Invalid invocation"); |
222 return; | 242 return; |
223 } | 243 } |
224 | 244 |
225 DCHECK(converted_arguments); | 245 DCHECK(converted_arguments); |
226 method_callback_.Run(name, std::move(converted_arguments), isolate, context, | 246 method_callback_.Run(name, std::move(converted_arguments), isolate, context, |
227 callback); | 247 callback); |
228 } | 248 } |
229 | 249 |
230 } // namespace extensions | 250 } // namespace extensions |
OLD | NEW |