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

Side by Side Diff: extensions/renderer/storage_area.cc

Issue 2704823002: [Extensions Bindings] Add support for custom property types (Closed)
Patch Set: asan fix Created 3 years, 10 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/storage_area.h ('k') | no next file » | 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 2017 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 #include "extensions/renderer/storage_area.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/values.h"
10 #include "extensions/common/api/storage.h"
11 #include "extensions/common/extension_api.h"
12 #include "extensions/renderer/api_request_handler.h"
13 #include "extensions/renderer/api_signature.h"
14 #include "gin/arguments.h"
15 #include "gin/handle.h"
16 #include "gin/object_template_builder.h"
17 #include "gin/wrappable.h"
18
19 namespace extensions {
20
21 namespace {
22
23 #define DEFINE_STORAGE_AREA_HANDLERS() \
24 void Get(gin::Arguments* arguments) { \
25 storage_area_.HandleFunctionCall("get", arguments); \
26 } \
27 void Set(gin::Arguments* arguments) { \
28 storage_area_.HandleFunctionCall("set", arguments); \
29 } \
30 void Remove(gin::Arguments* arguments) { \
31 storage_area_.HandleFunctionCall("remove", arguments); \
32 } \
33 void Clear(gin::Arguments* arguments) { \
34 storage_area_.HandleFunctionCall("clear", arguments); \
35 } \
36 void GetBytesInUse(gin::Arguments* arguments) { \
37 storage_area_.HandleFunctionCall("getBytesInUse", arguments); \
38 }
39
40 // gin::Wrappables for each of the storage areas. Since each has slightly
41 // different properties, and the object template is shared between all
42 // instances, this is a little verbose.
43 class LocalStorageArea final : public gin::Wrappable<LocalStorageArea> {
44 public:
45 LocalStorageArea(APIRequestHandler* request_handler,
46 const APITypeReferenceMap* type_refs)
47 : storage_area_(request_handler, type_refs, "local") {}
48 ~LocalStorageArea() override = default;
49
50 static gin::WrapperInfo kWrapperInfo;
51
52 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
53 v8::Isolate* isolate) override {
54 return Wrappable<LocalStorageArea>::GetObjectTemplateBuilder(isolate)
55 .SetMethod("get", &LocalStorageArea::Get)
56 .SetMethod("set", &LocalStorageArea::Set)
57 .SetMethod("remove", &LocalStorageArea::Remove)
58 .SetMethod("clear", &LocalStorageArea::Clear)
59 .SetMethod("getBytesInUse", &LocalStorageArea::GetBytesInUse)
60 .SetValue("QUOTA_BYTES", api::storage::local::QUOTA_BYTES);
61 }
62
63 private:
64 DEFINE_STORAGE_AREA_HANDLERS()
65
66 StorageArea storage_area_;
67
68 DISALLOW_COPY_AND_ASSIGN(LocalStorageArea);
69 };
70
71 gin::WrapperInfo LocalStorageArea::kWrapperInfo = {gin::kEmbedderNativeGin};
72
73 class SyncStorageArea final : public gin::Wrappable<SyncStorageArea> {
74 public:
75 SyncStorageArea(APIRequestHandler* request_handler,
76 const APITypeReferenceMap* type_refs)
77 : storage_area_(request_handler, type_refs, "sync") {}
78 ~SyncStorageArea() override = default;
79
80 static gin::WrapperInfo kWrapperInfo;
81
82 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
83 v8::Isolate* isolate) override {
84 return Wrappable<SyncStorageArea>::GetObjectTemplateBuilder(isolate)
85 .SetMethod("get", &SyncStorageArea::Get)
86 .SetMethod("set", &SyncStorageArea::Set)
87 .SetMethod("remove", &SyncStorageArea::Remove)
88 .SetMethod("clear", &SyncStorageArea::Clear)
89 .SetMethod("getBytesInUse", &SyncStorageArea::GetBytesInUse)
90 .SetValue("QUOTA_BYTES", api::storage::sync::QUOTA_BYTES)
91 .SetValue("QUOTA_BYTES_PER_ITEM",
92 api::storage::sync::QUOTA_BYTES_PER_ITEM)
93 .SetValue("MAX_ITEMS", api::storage::sync::MAX_ITEMS)
94 .SetValue("MAX_WRITE_OPERATIONS_PER_HOUR",
95 api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR)
96 .SetValue("MAX_WRITE_OPERATIONS_PER_MINUTE",
97 api::storage::sync::MAX_WRITE_OPERATIONS_PER_MINUTE)
98 .SetValue(
99 "MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE",
100 api::storage::sync::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE);
101 }
102
103 private:
104 DEFINE_STORAGE_AREA_HANDLERS()
105
106 StorageArea storage_area_;
107
108 DISALLOW_COPY_AND_ASSIGN(SyncStorageArea);
109 };
110
111 gin::WrapperInfo SyncStorageArea::kWrapperInfo = {gin::kEmbedderNativeGin};
112
113 class ManagedStorageArea final : public gin::Wrappable<ManagedStorageArea> {
114 public:
115 ManagedStorageArea(APIRequestHandler* request_handler,
116 const APITypeReferenceMap* type_refs)
117 : storage_area_(request_handler, type_refs, "managed") {}
118 ~ManagedStorageArea() override = default;
119
120 static gin::WrapperInfo kWrapperInfo;
121
122 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
123 v8::Isolate* isolate) override {
124 return Wrappable<ManagedStorageArea>::GetObjectTemplateBuilder(isolate)
125 .SetMethod("get", &ManagedStorageArea::Get)
126 .SetMethod("set", &ManagedStorageArea::Set)
127 .SetMethod("remove", &ManagedStorageArea::Remove)
128 .SetMethod("clear", &ManagedStorageArea::Clear)
129 .SetMethod("getBytesInUse", &ManagedStorageArea::GetBytesInUse);
130 }
131
132 private:
133 DEFINE_STORAGE_AREA_HANDLERS()
134
135 StorageArea storage_area_;
136
137 DISALLOW_COPY_AND_ASSIGN(ManagedStorageArea);
138 };
139
140 gin::WrapperInfo ManagedStorageArea::kWrapperInfo = {gin::kEmbedderNativeGin};
141
142 #undef DEFINE_STORAGE_AREA_HANDLERS
143
144 } // namespace
145
146 StorageArea::StorageArea(APIRequestHandler* request_handler,
147 const APITypeReferenceMap* type_refs,
148 const std::string& name)
149 : request_handler_(request_handler), type_refs_(type_refs), name_(name) {}
150 StorageArea::~StorageArea() = default;
151
152 // static
153 v8::Local<v8::Object> StorageArea::CreateStorageArea(
154 v8::Local<v8::Context> context,
155 const std::string& property_name,
156 APIRequestHandler* request_handler,
157 APITypeReferenceMap* type_refs) {
158 v8::Context::Scope context_scope(context);
159 v8::Local<v8::Object> object;
160 if (property_name == "local") {
161 gin::Handle<LocalStorageArea> handle =
162 gin::CreateHandle(context->GetIsolate(),
163 new LocalStorageArea(request_handler, type_refs));
164 object = handle.ToV8().As<v8::Object>();
165 } else if (property_name == "sync") {
166 gin::Handle<SyncStorageArea> handle = gin::CreateHandle(
167 context->GetIsolate(), new SyncStorageArea(request_handler, type_refs));
168 object = handle.ToV8().As<v8::Object>();
169 } else {
170 CHECK_EQ("managed", property_name);
171 gin::Handle<ManagedStorageArea> handle =
172 gin::CreateHandle(context->GetIsolate(),
173 new ManagedStorageArea(request_handler, type_refs));
174 object = handle.ToV8().As<v8::Object>();
175 }
176 return object;
177 }
178
179 void StorageArea::HandleFunctionCall(const std::string& method_name,
180 gin::Arguments* arguments) {
181 v8::Isolate* isolate = arguments->isolate();
182 v8::HandleScope handle_scope(isolate);
183 v8::Local<v8::Object> holder;
184 CHECK(arguments->GetHolder(&holder));
185 v8::Local<v8::Context> context = holder->CreationContext();
186
187 std::vector<v8::Local<v8::Value>> argument_list;
188 if (arguments->Length() > 0) {
189 // Just copying handles should never fail.
190 CHECK(arguments->GetRemaining(&argument_list));
191 }
192
193 std::unique_ptr<base::ListValue> converted_arguments;
194 v8::Local<v8::Function> callback;
195 std::string error;
196 if (!GetFunctionSchema("storage", "storage.StorageArea", method_name)
197 .ParseArgumentsToJSON(context, argument_list, *type_refs_,
198 &converted_arguments, &callback, &error)) {
199 arguments->ThrowTypeError("Invalid invocation");
200 return;
201 }
202
203 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(name_));
204 request_handler_->StartRequest(context, "storage." + method_name,
205 std::move(converted_arguments), callback,
206 v8::Local<v8::Function>());
207 }
208
209 const APISignature& StorageArea::GetFunctionSchema(
210 base::StringPiece api_name,
211 base::StringPiece type_name,
212 base::StringPiece function_name) {
213 std::string full_name = base::StringPrintf(
214 "%s.%s.%s", api_name.data(), type_name.data(), function_name.data());
215 auto iter = signatures_.find(full_name);
216 if (iter != signatures_.end())
217 return *iter->second;
218
219 const base::DictionaryValue* full_schema =
220 ExtensionAPI::GetSharedInstance()->GetSchema(api_name.as_string());
221 const base::ListValue* types = nullptr;
222 CHECK(full_schema->GetList("types", &types));
223 const base::DictionaryValue* type_schema = nullptr;
224 for (const auto& type : *types) {
225 const base::DictionaryValue* type_dict = nullptr;
226 CHECK(type->GetAsDictionary(&type_dict));
227 std::string id;
228 CHECK(type_dict->GetString("id", &id));
229 if (id == type_name) {
230 type_schema = type_dict;
231 break;
232 }
233 }
234 CHECK(type_schema);
235 const base::ListValue* type_functions = nullptr;
236 CHECK(type_schema->GetList("functions", &type_functions));
237 const base::ListValue* parameters = nullptr;
238 for (const auto& function : *type_functions) {
239 const base::DictionaryValue* function_dict = nullptr;
240 CHECK(function->GetAsDictionary(&function_dict));
241 std::string name;
242 CHECK(function_dict->GetString("name", &name));
243 if (name == function_name) {
244 CHECK(function_dict->GetList("parameters", &parameters));
245 break;
246 }
247 }
248 CHECK(parameters);
249 auto signature = base::MakeUnique<APISignature>(*parameters);
250 const auto* raw_signature = signature.get();
251 signatures_[full_name] = std::move(signature);
252 return *raw_signature;
253 }
254
255 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/storage_area.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698