| 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: |
| 54 // Common implementation for ParseArgumentsToJSON and ParseArgumentsToV8. |
| 55 bool ParseArgumentsImpl(gin::Arguments* arguments, |
| 56 const ArgumentSpec::RefMap& type_refs, |
| 57 std::vector<v8::Local<v8::Value>>* v8_out, |
| 58 std::unique_ptr<base::ListValue>* json_out, |
| 59 v8::Local<v8::Function>* callback_out, |
| 60 std::string* error) const; |
| 61 |
| 42 // Attempts to match an argument from |arguments| to the given |spec|. | 62 // 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 | 63 // If the next argmument does not match and |spec| is optional, uses a null |
| 44 // base::Value is returned. | 64 // value. |
| 45 // If the argument matches, |arguments| is advanced and the converted value is | 65 // If the argument matches, |arguments| is advanced, and the matched value is |
| 46 // returned. | 66 // added to either |v8_out| or |json_out|. |
| 47 // If the argument does not match and it is not optional, returns null and | 67 // Returns true on success. |
| 48 // populates error. | 68 bool ParseArgument(const ArgumentSpec& spec, |
| 49 std::unique_ptr<base::Value> ParseArgument( | 69 v8::Local<v8::Context> context, |
| 50 const ArgumentSpec& spec, | 70 gin::Arguments* arguments, |
| 51 v8::Local<v8::Context> context, | 71 const ArgumentSpec::RefMap& type_refs, |
| 52 gin::Arguments* arguments, | 72 std::vector<v8::Local<v8::Value>>* v8_out, |
| 53 const ArgumentSpec::RefMap& type_refs, | 73 base::ListValue* json_out, |
| 54 std::string* error) const; | 74 std::string* error) const; |
| 55 | 75 |
| 56 // Parses the callback from |arguments| according to |callback_spec|. Since | 76 // Parses the callback from |arguments| according to |callback_spec|. Since |
| 57 // the callback isn't converted into a base::Value, this is different from | 77 // the callback can be treated differently than the rest of the arguments, |
| 58 // ParseArgument() above. | 78 // this differs from ParseArgument() above. |
| 59 bool ParseCallback(gin::Arguments* arguments, | 79 bool ParseCallback(gin::Arguments* arguments, |
| 60 const ArgumentSpec& callback_spec, | 80 const ArgumentSpec& callback_spec, |
| 61 std::string* error, | 81 std::string* error, |
| 62 v8::Local<v8::Function>* callback_out) const; | 82 v8::Local<v8::Function>* callback_out) const; |
| 63 | 83 |
| 64 // The list of expected arguments. | 84 // The list of expected arguments. |
| 65 std::vector<std::unique_ptr<ArgumentSpec>> signature_; | 85 std::vector<std::unique_ptr<ArgumentSpec>> signature_; |
| 66 | 86 |
| 67 DISALLOW_COPY_AND_ASSIGN(APISignature); | 87 DISALLOW_COPY_AND_ASSIGN(APISignature); |
| 68 }; | 88 }; |
| 69 | 89 |
| 70 } // namespace extensions | 90 } // namespace extensions |
| 71 | 91 |
| 72 #endif // EXTENSIONS_RENDERER_API_SIGNATURE_H_ | 92 #endif // EXTENSIONS_RENDERER_API_SIGNATURE_H_ |
| OLD | NEW |