| 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 #ifndef EXTENSIONS_RENDERER_API_SIGNATURE_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_SIGNATURE_H_ |
| 6 #define EXTENSIONS_RENDERER_API_SIGNATURE_H_ | 6 #define EXTENSIONS_RENDERER_API_SIGNATURE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "extensions/renderer/argument_spec.h" | 12 #include "extensions/renderer/argument_spec.h" |
| 13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class Value; | 16 class Value; |
| 17 class ListValue; | 17 class ListValue; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace gin { | |
| 21 class Arguments; | |
| 22 } | |
| 23 | |
| 24 namespace extensions { | 20 namespace extensions { |
| 25 | 21 |
| 26 // A representation of the expected signature for an API method, along with the | 22 // A representation of the expected signature for an API method, along with the |
| 27 // ability to match provided arguments and convert them to base::Values. | 23 // ability to match provided arguments and convert them to base::Values. |
| 28 class APISignature { | 24 class APISignature { |
| 29 public: | 25 public: |
| 30 APISignature(const base::ListValue& specification); | 26 APISignature(const base::ListValue& specification); |
| 31 ~APISignature(); | 27 ~APISignature(); |
| 32 | 28 |
| 33 // Parses |arguments| against this signature, and populates |args_out| with | 29 // Parses |arguments| against this signature, and populates |args_out| with |
| 34 // the v8 values (performing no conversion). The resulting vector may differ | 30 // the v8 values (performing no conversion). The resulting vector may differ |
| 35 // from the list of arguments passed in because it will include null-filled | 31 // from the list of arguments passed in because it will include null-filled |
| 36 // optional arguments. | 32 // optional arguments. |
| 37 // Returns true if the arguments were successfully parsed and converted. | 33 // Returns true if the arguments were successfully parsed and converted. |
| 38 bool ParseArgumentsToV8(gin::Arguments* arguments, | 34 bool ParseArgumentsToV8(v8::Local<v8::Context> context, |
| 35 const std::vector<v8::Local<v8::Value>>& arguments, |
| 39 const ArgumentSpec::RefMap& type_refs, | 36 const ArgumentSpec::RefMap& type_refs, |
| 40 std::vector<v8::Local<v8::Value>>* args_out, | 37 std::vector<v8::Local<v8::Value>>* args_out, |
| 41 std::string* error) const; | 38 std::string* error) const; |
| 42 | 39 |
| 43 // Parses |arguments| against this signature, converting to a base::ListValue. | 40 // Parses |arguments| against this signature, converting to a base::ListValue. |
| 44 // Returns true if the arguments were successfully parsed and converted, and | 41 // Returns true if the arguments were successfully parsed and converted, and |
| 45 // populates |args_out| and |callback_out| with the JSON arguments and | 42 // populates |args_out| and |callback_out| with the JSON arguments and |
| 46 // callback values, respectively. On failure, returns false populates |error|. | 43 // callback values, respectively. On failure, returns false populates |error|. |
| 47 bool ParseArgumentsToJSON(gin::Arguments* arguments, | 44 bool ParseArgumentsToJSON(v8::Local<v8::Context> context, |
| 45 const std::vector<v8::Local<v8::Value>>& arguments, |
| 48 const ArgumentSpec::RefMap& type_refs, | 46 const ArgumentSpec::RefMap& type_refs, |
| 49 std::unique_ptr<base::ListValue>* args_out, | 47 std::unique_ptr<base::ListValue>* args_out, |
| 50 v8::Local<v8::Function>* callback_out, | 48 v8::Local<v8::Function>* callback_out, |
| 51 std::string* error) const; | 49 std::string* error) const; |
| 52 | 50 |
| 53 private: | 51 private: |
| 54 // The list of expected arguments. | 52 // The list of expected arguments. |
| 55 std::vector<std::unique_ptr<ArgumentSpec>> signature_; | 53 std::vector<std::unique_ptr<ArgumentSpec>> signature_; |
| 56 | 54 |
| 57 DISALLOW_COPY_AND_ASSIGN(APISignature); | 55 DISALLOW_COPY_AND_ASSIGN(APISignature); |
| 58 }; | 56 }; |
| 59 | 57 |
| 60 } // namespace extensions | 58 } // namespace extensions |
| 61 | 59 |
| 62 #endif // EXTENSIONS_RENDERER_API_SIGNATURE_H_ | 60 #endif // EXTENSIONS_RENDERER_API_SIGNATURE_H_ |
| OLD | NEW |