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

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

Issue 2718543004: [Extensions Bindings] Add ChromeSetting custom type (Closed)
Patch Set: . 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 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 #include "extensions/renderer/storage_area.h" 5 #include "extensions/renderer/storage_area.h"
6 6
7 #include "base/memory/ptr_util.h" 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" 8 #include "extensions/common/api/storage.h"
11 #include "extensions/common/extension_api.h" 9 #include "extensions/renderer/api_custom_types.h"
12 #include "extensions/renderer/api_request_handler.h" 10 #include "extensions/renderer/api_request_handler.h"
13 #include "extensions/renderer/api_signature.h" 11 #include "extensions/renderer/api_signature.h"
14 #include "gin/arguments.h" 12 #include "gin/arguments.h"
15 #include "gin/handle.h" 13 #include "gin/handle.h"
16 #include "gin/object_template_builder.h" 14 #include "gin/object_template_builder.h"
17 #include "gin/wrappable.h" 15 #include "gin/wrappable.h"
18 16
19 namespace extensions { 17 namespace extensions {
20 18
21 namespace { 19 namespace {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 const APITypeReferenceMap* type_refs, 145 const APITypeReferenceMap* type_refs,
148 const std::string& name) 146 const std::string& name)
149 : request_handler_(request_handler), type_refs_(type_refs), name_(name) {} 147 : request_handler_(request_handler), type_refs_(type_refs), name_(name) {}
150 StorageArea::~StorageArea() = default; 148 StorageArea::~StorageArea() = default;
151 149
152 // static 150 // static
153 v8::Local<v8::Object> StorageArea::CreateStorageArea( 151 v8::Local<v8::Object> StorageArea::CreateStorageArea(
154 v8::Local<v8::Context> context, 152 v8::Local<v8::Context> context,
155 const std::string& property_name, 153 const std::string& property_name,
156 APIRequestHandler* request_handler, 154 APIRequestHandler* request_handler,
155 APIEventHandler* event_handler,
157 APITypeReferenceMap* type_refs) { 156 APITypeReferenceMap* type_refs) {
158 v8::Context::Scope context_scope(context); 157 v8::Context::Scope context_scope(context);
159 v8::Local<v8::Object> object; 158 v8::Local<v8::Object> object;
160 if (property_name == "local") { 159 if (property_name == "local") {
161 gin::Handle<LocalStorageArea> handle = 160 gin::Handle<LocalStorageArea> handle =
162 gin::CreateHandle(context->GetIsolate(), 161 gin::CreateHandle(context->GetIsolate(),
163 new LocalStorageArea(request_handler, type_refs)); 162 new LocalStorageArea(request_handler, type_refs));
164 object = handle.ToV8().As<v8::Object>(); 163 object = handle.ToV8().As<v8::Object>();
165 } else if (property_name == "sync") { 164 } else if (property_name == "sync") {
166 gin::Handle<SyncStorageArea> handle = gin::CreateHandle( 165 gin::Handle<SyncStorageArea> handle = gin::CreateHandle(
(...skipping 19 matching lines...) Expand all
186 185
187 std::vector<v8::Local<v8::Value>> argument_list; 186 std::vector<v8::Local<v8::Value>> argument_list;
188 if (arguments->Length() > 0) { 187 if (arguments->Length() > 0) {
189 // Just copying handles should never fail. 188 // Just copying handles should never fail.
190 CHECK(arguments->GetRemaining(&argument_list)); 189 CHECK(arguments->GetRemaining(&argument_list));
191 } 190 }
192 191
193 std::unique_ptr<base::ListValue> converted_arguments; 192 std::unique_ptr<base::ListValue> converted_arguments;
194 v8::Local<v8::Function> callback; 193 v8::Local<v8::Function> callback;
195 std::string error; 194 std::string error;
196 if (!GetFunctionSchema("storage", "storage.StorageArea", method_name) 195 if (!api_custom_types::GetFunctionSchema("storage", "storage.StorageArea",
196 method_name)
197 .ParseArgumentsToJSON(context, argument_list, *type_refs_, 197 .ParseArgumentsToJSON(context, argument_list, *type_refs_,
198 &converted_arguments, &callback, &error)) { 198 &converted_arguments, &callback, &error)) {
199 arguments->ThrowTypeError("Invalid invocation"); 199 arguments->ThrowTypeError("Invalid invocation");
200 return; 200 return;
201 } 201 }
202 202
203 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(name_)); 203 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(name_));
204 request_handler_->StartRequest(context, "storage." + method_name, 204 request_handler_->StartRequest(context, "storage." + method_name,
205 std::move(converted_arguments), callback, 205 std::move(converted_arguments), callback,
206 v8::Local<v8::Function>()); 206 v8::Local<v8::Function>());
207 } 207 }
208 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 209 } // namespace extensions
OLDNEW
« extensions/renderer/chrome_setting.cc ('K') | « extensions/renderer/storage_area.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698