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 // Check for a custom hook to handle the method. | 191 APIBindingHooks::RequestResult hooks_result = |
192 if (binding_hooks_->HandleRequest(api_name_, name, context, | 192 APIBindingHooks::RequestResult::NOT_HANDLED; |
193 signature, arguments)) { | 193 hooks_result = binding_hooks_->HandleRequest(api_name_, name, context, |
194 return; // Handled by a custom hook. | 194 signature, arguments, |
| 195 *type_refs_); |
| 196 |
| 197 switch (hooks_result) { |
| 198 case APIBindingHooks::RequestResult::INVALID_INVOCATION: |
| 199 arguments->ThrowTypeError("Invalid invocation"); |
| 200 return; |
| 201 case APIBindingHooks::RequestResult::HANDLED: |
| 202 return; // Our work here is done. |
| 203 case APIBindingHooks::RequestResult::NOT_HANDLED: |
| 204 break; // Handle in the default manner. |
195 } | 205 } |
196 | 206 |
197 std::unique_ptr<base::ListValue> parsed_arguments; | 207 std::unique_ptr<base::ListValue> converted_arguments; |
198 v8::Local<v8::Function> callback; | 208 v8::Local<v8::Function> callback; |
| 209 bool conversion_success = false; |
199 { | 210 { |
200 v8::TryCatch try_catch(isolate); | 211 v8::TryCatch try_catch(isolate); |
201 parsed_arguments = | 212 conversion_success = signature->ParseArgumentsToJSON( |
202 signature->ParseArguments(arguments, *type_refs_, &callback, &error); | 213 arguments, *type_refs_, &converted_arguments, &callback, &error); |
203 if (try_catch.HasCaught()) { | 214 if (try_catch.HasCaught()) { |
204 DCHECK(!parsed_arguments); | 215 DCHECK(!converted_arguments); |
205 try_catch.ReThrow(); | 216 try_catch.ReThrow(); |
206 return; | 217 return; |
207 } | 218 } |
208 } | 219 } |
209 if (!parsed_arguments) { | 220 if (!conversion_success) { |
210 arguments->ThrowTypeError("Invalid invocation"); | 221 arguments->ThrowTypeError("Invalid invocation"); |
211 return; | 222 return; |
212 } | 223 } |
213 | 224 |
214 method_callback_.Run(name, std::move(parsed_arguments), isolate, | 225 DCHECK(converted_arguments); |
215 context, callback); | 226 method_callback_.Run(name, std::move(converted_arguments), isolate, context, |
| 227 callback); |
216 } | 228 } |
217 | 229 |
218 } // namespace extensions | 230 } // namespace extensions |
OLD | NEW |