| 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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 // A representation of the expected signature for an API method, along with the | 26 // 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. | 27 // ability to match provided arguments and convert them to base::Values. |
| 28 class APISignature { | 28 class APISignature { |
| 29 public: | 29 public: |
| 30 APISignature(const base::ListValue& specification); | 30 APISignature(const base::ListValue& specification); |
| 31 ~APISignature(); | 31 ~APISignature(); |
| 32 | 32 |
| 33 // Parses |arguments| against this signature, and populates |args_out| with |
| 34 // 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 |
| 36 // optional arguments. |
| 37 // Returns true if the arguments were successfully parsed and converted. |
| 38 bool ParseArgumentsToV8(gin::Arguments* arguments, |
| 39 const ArgumentSpec::RefMap& type_refs, |
| 40 std::vector<v8::Local<v8::Value>>* args_out, |
| 41 std::string* error) const; |
| 42 |
| 33 // Parses |arguments| against this signature, converting to a base::ListValue. | 43 // Parses |arguments| against this signature, converting to a base::ListValue. |
| 34 // If the arguments don't match, null is returned and |error| is populated. | 44 // Returns true if the arguments were successfully parsed and converted, and |
| 35 std::unique_ptr<base::ListValue> ParseArguments( | 45 // populates |args_out| and |callback_out| with the JSON arguments and |
| 36 gin::Arguments* arguments, | 46 // callback values, respectively. On failure, returns false populates |error|. |
| 37 const ArgumentSpec::RefMap& type_refs, | 47 bool ParseArgumentsToJSON(gin::Arguments* arguments, |
| 38 v8::Local<v8::Function>* callback_out, | 48 const ArgumentSpec::RefMap& type_refs, |
| 39 std::string* error) const; | 49 std::unique_ptr<base::ListValue>* args_out, |
| 50 v8::Local<v8::Function>* callback_out, |
| 51 std::string* error) const; |
| 40 | 52 |
| 41 private: | 53 private: |
| 42 // Attempts to match an argument from |arguments| to the given |spec|. | |
| 43 // If the next argmument does not match and |spec| is optional, a null | |
| 44 // base::Value is returned. | |
| 45 // If the argument matches, |arguments| is advanced and the converted value is | |
| 46 // returned. | |
| 47 // If the argument does not match and it is not optional, returns null and | |
| 48 // populates error. | |
| 49 std::unique_ptr<base::Value> ParseArgument( | |
| 50 const ArgumentSpec& spec, | |
| 51 v8::Local<v8::Context> context, | |
| 52 gin::Arguments* arguments, | |
| 53 const ArgumentSpec::RefMap& type_refs, | |
| 54 std::string* error) const; | |
| 55 | |
| 56 // Parses the callback from |arguments| according to |callback_spec|. Since | |
| 57 // the callback isn't converted into a base::Value, this is different from | |
| 58 // ParseArgument() above. | |
| 59 bool ParseCallback(gin::Arguments* arguments, | |
| 60 const ArgumentSpec& callback_spec, | |
| 61 std::string* error, | |
| 62 v8::Local<v8::Function>* callback_out) const; | |
| 63 | |
| 64 // The list of expected arguments. | 54 // The list of expected arguments. |
| 65 std::vector<std::unique_ptr<ArgumentSpec>> signature_; | 55 std::vector<std::unique_ptr<ArgumentSpec>> signature_; |
| 66 | 56 |
| 67 DISALLOW_COPY_AND_ASSIGN(APISignature); | 57 DISALLOW_COPY_AND_ASSIGN(APISignature); |
| 68 }; | 58 }; |
| 69 | 59 |
| 70 } // namespace extensions | 60 } // namespace extensions |
| 71 | 61 |
| 72 #endif // EXTENSIONS_RENDERER_API_SIGNATURE_H_ | 62 #endif // EXTENSIONS_RENDERER_API_SIGNATURE_H_ |
| OLD | NEW |