OLD | NEW |
---|---|
(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 template <typename T> | |
24 void DecorateTemplateBuilder(gin::ObjectTemplateBuilder* builder) { | |
25 auto handler = [](const char* method_name, gin::Arguments* arguments) { | |
26 T* holder = nullptr; | |
27 if (!arguments->GetHolder(&holder)) { | |
28 arguments->ThrowTypeError("Invalid receiver for StorageArea method."); | |
29 return; | |
30 } | |
31 holder->storage_area().HandleFunctionCall(method_name, arguments); | |
32 }; | |
33 | |
34 for (const char* key : {"get", "set", "remove", "clear", "getBytesInUse"}) | |
lazyboy
2017/02/22 03:37:03
These comes from "js_module" key in current JS bin
Devlin
2017/02/22 20:56:45
Done.
| |
35 builder->SetMethod(key, base::Bind(handler, key)); | |
36 } | |
37 | |
38 // gin::Wrappables for each of the storage areas. Since each has slightly | |
39 // different properties, and the object template is shared between all | |
40 // instances, this is a little verbose. | |
41 class LocalStorageArea final : public gin::Wrappable<LocalStorageArea> { | |
42 public: | |
43 LocalStorageArea(APIRequestHandler* request_handler, | |
44 const APITypeReferenceMap* type_refs) | |
45 : storage_area_(request_handler, type_refs, "local") {} | |
46 ~LocalStorageArea() override = default; | |
47 | |
48 static gin::WrapperInfo kWrapperInfo; | |
49 | |
50 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
51 v8::Isolate* isolate) override { | |
52 gin::ObjectTemplateBuilder builder = | |
53 Wrappable<LocalStorageArea>::GetObjectTemplateBuilder(isolate); | |
54 DecorateTemplateBuilder<LocalStorageArea>(&builder); | |
55 builder.SetValue("QUOTA_BYTES", api::storage::local::QUOTA_BYTES); | |
lazyboy
2017/02/22 03:37:03
What's the plan for documentation generation for t
Devlin
2017/02/22 20:56:45
Right now, they're still half-coming-from-the-json
lazyboy
2017/02/22 22:15:24
Yes. Agreed.
| |
56 return builder; | |
57 } | |
58 | |
59 StorageArea& storage_area() { return storage_area_; } | |
lazyboy
2017/02/22 03:37:03
optional: Should storage_area_ be part of a common
Devlin
2017/02/22 20:56:45
Unfortunately, we can't. gin::Wrappable<> does no
| |
60 | |
61 private: | |
62 StorageArea storage_area_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(LocalStorageArea); | |
65 }; | |
66 | |
67 gin::WrapperInfo LocalStorageArea::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
68 | |
69 class SyncStorageArea final : public gin::Wrappable<SyncStorageArea> { | |
70 public: | |
71 SyncStorageArea(APIRequestHandler* request_handler, | |
72 const APITypeReferenceMap* type_refs) | |
73 : storage_area_(request_handler, type_refs, "sync") {} | |
74 ~SyncStorageArea() override = default; | |
75 | |
76 static gin::WrapperInfo kWrapperInfo; | |
77 | |
78 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
79 v8::Isolate* isolate) override { | |
80 gin::ObjectTemplateBuilder builder = | |
81 Wrappable<SyncStorageArea>::GetObjectTemplateBuilder(isolate); | |
82 DecorateTemplateBuilder<SyncStorageArea>(&builder); | |
83 builder.SetValue("QUOTA_BYTES", api::storage::sync::QUOTA_BYTES); | |
84 builder.SetValue("QUOTA_BYTES_PER_ITEM", | |
85 api::storage::sync::QUOTA_BYTES_PER_ITEM); | |
86 builder.SetValue("MAX_ITEMS", api::storage::sync::MAX_ITEMS); | |
87 builder.SetValue("MAX_WRITE_OPERATIONS_PER_HOUR", | |
88 api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR); | |
89 builder.SetValue("MAX_WRITE_OPERATIONS_PER_MINUTE", | |
90 api::storage::sync::MAX_WRITE_OPERATIONS_PER_MINUTE); | |
91 builder.SetValue( | |
92 "MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE", | |
93 api::storage::sync::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE); | |
94 return builder; | |
95 } | |
96 | |
97 StorageArea& storage_area() { return storage_area_; } | |
98 | |
99 private: | |
100 StorageArea storage_area_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(SyncStorageArea); | |
103 }; | |
104 | |
105 gin::WrapperInfo SyncStorageArea::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
106 | |
107 class ManagedStorageArea final : public gin::Wrappable<ManagedStorageArea> { | |
108 public: | |
109 ManagedStorageArea(APIRequestHandler* request_handler, | |
110 const APITypeReferenceMap* type_refs) | |
111 : storage_area_(request_handler, type_refs, "managed") {} | |
112 ~ManagedStorageArea() override = default; | |
113 | |
114 static gin::WrapperInfo kWrapperInfo; | |
115 | |
116 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
117 v8::Isolate* isolate) override { | |
118 gin::ObjectTemplateBuilder builder = | |
119 Wrappable<ManagedStorageArea>::GetObjectTemplateBuilder(isolate); | |
120 DecorateTemplateBuilder<ManagedStorageArea>(&builder); | |
121 return builder; | |
122 } | |
123 | |
124 StorageArea& storage_area() { return storage_area_; } | |
125 | |
126 private: | |
127 StorageArea storage_area_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(ManagedStorageArea); | |
130 }; | |
131 | |
132 gin::WrapperInfo ManagedStorageArea::kWrapperInfo = {gin::kEmbedderNativeGin}; | |
133 | |
134 } // namespace | |
135 | |
136 StorageArea::StorageArea(APIRequestHandler* request_handler, | |
137 const APITypeReferenceMap* type_refs, | |
138 const std::string& name) | |
139 : request_handler_(request_handler), type_refs_(type_refs), name_(name) {} | |
140 StorageArea::~StorageArea() = default; | |
141 | |
142 // static | |
143 v8::Local<v8::Object> StorageArea::CreateStorageArea( | |
144 v8::Local<v8::Context> context, | |
145 const std::string& property_name, | |
146 APIRequestHandler* request_handler, | |
147 APITypeReferenceMap* type_refs) { | |
148 v8::Context::Scope context_scope(context); | |
149 v8::Local<v8::Object> object; | |
150 if (property_name == "local") { | |
151 gin::Handle<LocalStorageArea> handle = | |
152 gin::CreateHandle(context->GetIsolate(), | |
153 new LocalStorageArea(request_handler, type_refs)); | |
154 object = handle.ToV8().As<v8::Object>(); | |
155 } else if (property_name == "sync") { | |
156 gin::Handle<SyncStorageArea> handle = gin::CreateHandle( | |
157 context->GetIsolate(), new SyncStorageArea(request_handler, type_refs)); | |
158 object = handle.ToV8().As<v8::Object>(); | |
159 } else { | |
160 CHECK_EQ("managed", property_name); | |
161 gin::Handle<ManagedStorageArea> handle = | |
162 gin::CreateHandle(context->GetIsolate(), | |
163 new ManagedStorageArea(request_handler, type_refs)); | |
164 object = handle.ToV8().As<v8::Object>(); | |
165 } | |
166 return object; | |
167 } | |
168 | |
169 void StorageArea::HandleFunctionCall(const std::string& method_name, | |
170 gin::Arguments* arguments) { | |
171 v8::Isolate* isolate = arguments->isolate(); | |
172 v8::HandleScope handle_scope(isolate); | |
173 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
jbroman
2017/02/21 20:22:07
nit: I think we've been using holder context elsew
Devlin
2017/02/22 20:56:45
Ah, yes, we have. It's a little messier here, bec
jbroman
2017/02/22 21:05:28
I have no objection to something along those lines
| |
174 | |
175 std::vector<v8::Local<v8::Value>> argument_list; | |
176 if (arguments->Length() > 0) { | |
177 // Just copying handles should never fail. | |
178 CHECK(arguments->GetRemaining(&argument_list)); | |
179 } | |
180 | |
181 std::unique_ptr<base::ListValue> converted_arguments; | |
182 v8::Local<v8::Function> callback; | |
183 std::string error; | |
184 if (!GetFunctionSchema("storage", "storage.StorageArea", method_name) | |
185 .ParseArgumentsToJSON(context, argument_list, *type_refs_, | |
186 &converted_arguments, &callback, &error)) { | |
187 arguments->ThrowTypeError("Invalid invocation"); | |
188 return; | |
189 } | |
190 | |
191 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(name_)); | |
192 request_handler_->StartRequest(context, "storage." + method_name, | |
193 std::move(converted_arguments), callback, | |
194 v8::Local<v8::Function>()); | |
195 } | |
196 | |
197 const APISignature& StorageArea::GetFunctionSchema( | |
198 base::StringPiece api_name, | |
199 base::StringPiece type_name, | |
200 base::StringPiece function_name) { | |
201 std::string full_name = base::StringPrintf( | |
202 "%s.%s.%s", api_name.data(), type_name.data(), function_name.data()); | |
203 auto iter = signatures_.find(full_name); | |
204 if (iter != signatures_.end()) | |
205 return *iter->second; | |
206 | |
207 const base::DictionaryValue* full_schema = | |
208 ExtensionAPI::GetSharedInstance()->GetSchema(api_name.as_string()); | |
209 const base::ListValue* types = nullptr; | |
210 CHECK(full_schema->GetList("types", &types)); | |
211 const base::DictionaryValue* type_schema = nullptr; | |
212 for (const auto& type : *types) { | |
213 const base::DictionaryValue* type_dict = nullptr; | |
214 CHECK(type->GetAsDictionary(&type_dict)); | |
215 std::string id; | |
216 CHECK(type_dict->GetString("id", &id)); | |
217 if (id == type_name) { | |
218 type_schema = type_dict; | |
219 break; | |
220 } | |
221 } | |
222 CHECK(type_schema); | |
223 const base::ListValue* type_functions = nullptr; | |
224 CHECK(type_schema->GetList("functions", &type_functions)); | |
225 const base::ListValue* parameters = nullptr; | |
226 for (const auto& function : *type_functions) { | |
227 const base::DictionaryValue* function_dict = nullptr; | |
228 CHECK(function->GetAsDictionary(&function_dict)); | |
229 std::string name; | |
230 CHECK(function_dict->GetString("name", &name)); | |
231 if (name == function_name) { | |
232 CHECK(function_dict->GetList("parameters", ¶meters)); | |
233 break; | |
234 } | |
235 } | |
236 CHECK(parameters); | |
237 auto signature = base::MakeUnique<APISignature>(*parameters); | |
238 auto raw_signature = signature.get(); | |
jbroman
2017/02/21 20:22:07
nit: use auto* (or for this use, even "const auto*
Devlin
2017/02/22 20:56:45
Done.
| |
239 signatures_[full_name] = std::move(signature); | |
240 return *raw_signature; | |
241 } | |
242 | |
243 } // namespace extensions | |
OLD | NEW |