Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: extensions/renderer/argument_spec.h

Issue 2947463002: [Extensions Bindings] Add a bindings/ subdirectory under renderer (Closed)
Patch Set: . Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/api_type_reference_map.cc ('k') | extensions/renderer/argument_spec.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef EXTENSIONS_RENDERER_ARGUMENT_SPEC_H_
6 #define EXTENSIONS_RENDERER_ARGUMENT_SPEC_H_
7
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <vector>
13
14 #include "base/macros.h"
15 #include "base/optional.h"
16 #include "base/strings/string_piece.h"
17 #include "v8/include/v8.h"
18
19 namespace base {
20 class DictionaryValue;
21 class Value;
22 }
23
24 namespace extensions {
25 class APITypeReferenceMap;
26
27 enum class ArgumentType {
28 INTEGER,
29 DOUBLE,
30 BOOLEAN,
31 STRING,
32 OBJECT,
33 LIST,
34 BINARY,
35 FUNCTION,
36 ANY,
37 REF,
38 CHOICES,
39 };
40
41 // A description of a given Argument to an Extension.
42 class ArgumentSpec {
43 public:
44 using PropertiesMap = std::map<std::string, std::unique_ptr<ArgumentSpec>>;
45
46 // Reads the description from |value| and sets associated fields.
47 // TODO(devlin): We should strongly think about generating these instead of
48 // populating them at runtime.
49 explicit ArgumentSpec(const base::Value& value);
50 explicit ArgumentSpec(ArgumentType type);
51 ~ArgumentSpec();
52
53 // Returns true if the passed |value| matches this specification. If
54 // |out_value| is non-null, converts the value to a base::Value and populates
55 // |out_value|. Otherwise, no conversion is performed.
56 bool ParseArgument(v8::Local<v8::Context> context,
57 v8::Local<v8::Value> value,
58 const APITypeReferenceMap& refs,
59 std::unique_ptr<base::Value>* out_value,
60 std::string* error) const;
61
62 // Returns a type name for this argument. Note: This should only be used to
63 // surface errors to developers.
64 const std::string& GetTypeName() const;
65
66 const std::string& name() const { return name_; }
67 bool optional() const { return optional_; }
68 ArgumentType type() const { return type_; }
69 const std::set<std::string>& enum_values() const { return enum_values_; }
70
71 void set_name(base::StringPiece name) { name_ = name.as_string(); }
72 void set_optional(bool optional) { optional_ = optional; }
73 void set_ref(base::StringPiece ref) { ref_ = ref.as_string(); }
74 void set_minimum(int minimum) { minimum_ = minimum; }
75 void set_properties(PropertiesMap properties) {
76 properties_ = std::move(properties);
77 }
78 void set_list_element_type(std::unique_ptr<ArgumentSpec> list_element_type) {
79 list_element_type_ = std::move(list_element_type);
80 }
81 void set_choices(std::vector<std::unique_ptr<ArgumentSpec>> choices) {
82 choices_ = std::move(choices);
83 }
84 void set_enum_values(std::set<std::string> enum_values) {
85 enum_values_ = std::move(enum_values);
86 }
87 void set_additional_properties(
88 std::unique_ptr<ArgumentSpec> additional_properties) {
89 additional_properties_ = std::move(additional_properties);
90 }
91
92 private:
93 // Initializes this object according to |type_string| and |dict|.
94 void InitializeType(const base::DictionaryValue* dict);
95
96 // Returns true if this argument refers to a fundamental type.
97 bool IsFundamentalType() const;
98
99 // Conversion functions. These should only be used if the spec is of the given
100 // type (otherwise, they will DCHECK).
101 bool ParseArgumentToFundamental(v8::Local<v8::Context> context,
102 v8::Local<v8::Value> value,
103 std::unique_ptr<base::Value>* out_value,
104 std::string* error) const;
105 bool ParseArgumentToObject(v8::Local<v8::Context> context,
106 v8::Local<v8::Object> object,
107 const APITypeReferenceMap& refs,
108 std::unique_ptr<base::Value>* out_value,
109 std::string* error) const;
110 bool ParseArgumentToArray(v8::Local<v8::Context> context,
111 v8::Local<v8::Array> value,
112 const APITypeReferenceMap& refs,
113 std::unique_ptr<base::Value>* out_value,
114 std::string* error) const;
115 bool ParseArgumentToAny(v8::Local<v8::Context> context,
116 v8::Local<v8::Value> value,
117 std::unique_ptr<base::Value>* out_value,
118 std::string* error) const;
119
120 // Returns an error message indicating the type of |value| does not match the
121 // expected type.
122 std::string GetInvalidTypeError(v8::Local<v8::Value> value) const;
123
124 // The name of the argument.
125 std::string name_;
126
127 // The type of the argument.
128 ArgumentType type_ = ArgumentType::INTEGER;
129
130 // A readable type name for this argument, lazily initialized.
131 mutable std::string type_name_;
132
133 // Whether or not the argument is required.
134 bool optional_ = false;
135
136 // Whether to preserve null properties found in objects.
137 bool preserve_null_ = false;
138
139 // The reference the argument points to, if any. Note that if this is set,
140 // none of the following fields describing the argument will be.
141 base::Optional<std::string> ref_;
142
143 // The type of instance an object should be, if any. Only applicable for
144 // ArgumentType::OBJECT. If specified, the argument must contain the instance
145 // type in its prototype chain.
146 base::Optional<std::string> instance_of_;
147
148 // A minimum and maximum for integer and double values, if any.
149 base::Optional<int> minimum_;
150 base::Optional<int> maximum_;
151
152 // A minimium length for strings or arrays.
153 base::Optional<size_t> min_length_;
154
155 // A maximum length for strings or arrays.
156 base::Optional<size_t> max_length_;
157
158 // A map of required properties; present only for objects. Note that any
159 // properties *not* defined in this map will be dropped during conversion.
160 std::map<std::string, std::unique_ptr<ArgumentSpec>> properties_;
161
162 // The type of item that should be in the list; present only for lists.
163 std::unique_ptr<ArgumentSpec> list_element_type_;
164
165 // The different possible specs this argument can map to. Only populated for
166 // arguments of type CHOICES.
167 std::vector<std::unique_ptr<ArgumentSpec>> choices_;
168
169 // The possible enum values, if defined for this argument.
170 std::set<std::string> enum_values_;
171
172 // The specification for 'additional properties'. This is used when we want
173 // to allow the API to pass an object with arbitrary properties. Only
174 // applicable for ArgumentType::OBJECT.
175 std::unique_ptr<ArgumentSpec> additional_properties_;
176
177 DISALLOW_COPY_AND_ASSIGN(ArgumentSpec);
178 };
179
180 } // namespace extensions
181
182 #endif // EXTENSIONS_RENDERER_ARGUMENT_SPEC_H_
OLDNEW
« no previous file with comments | « extensions/renderer/api_type_reference_map.cc ('k') | extensions/renderer/argument_spec.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698