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

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

Issue 2438623002: [Extensions Bindings] Add APIBindingsSystem (Closed)
Patch Set: lazyboy's Created 4 years, 1 month 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_binding.h ('k') | extensions/renderer/api_binding_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "extensions/renderer/api_binding.h" 5 #include "extensions/renderer/api_binding.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 CHECK(args.GetData(&external)); 153 CHECK(args.GetData(&external));
154 auto callback = static_cast<APIBinding::HandlerCallback*>(external->Value()); 154 auto callback = static_cast<APIBinding::HandlerCallback*>(external->Value());
155 callback->Run(&args); 155 callback->Run(&args);
156 } 156 }
157 157
158 } // namespace 158 } // namespace
159 159
160 APIBinding::APIPerContextData::APIPerContextData() {} 160 APIBinding::APIPerContextData::APIPerContextData() {}
161 APIBinding::APIPerContextData::~APIPerContextData() {} 161 APIBinding::APIPerContextData::~APIPerContextData() {}
162 162
163 APIBinding::APIBinding(const std::string& name, 163 APIBinding::APIBinding(const std::string& api_name,
164 const base::ListValue& function_definitions, 164 const base::ListValue& function_definitions,
165 const base::ListValue& type_definitions, 165 const base::ListValue* type_definitions,
166 const APIMethodCallback& callback, 166 const APIMethodCallback& callback,
167 ArgumentSpec::RefMap* type_refs) 167 ArgumentSpec::RefMap* type_refs)
168 : method_callback_(callback), type_refs_(type_refs), weak_factory_(this) { 168 : api_name_(api_name),
169 method_callback_(callback),
170 type_refs_(type_refs),
171 weak_factory_(this) {
169 DCHECK(!method_callback_.is_null()); 172 DCHECK(!method_callback_.is_null());
170 for (const auto& func : function_definitions) { 173 for (const auto& func : function_definitions) {
171 const base::DictionaryValue* func_dict = nullptr; 174 const base::DictionaryValue* func_dict = nullptr;
172 CHECK(func->GetAsDictionary(&func_dict)); 175 CHECK(func->GetAsDictionary(&func_dict));
173 std::string name; 176 std::string name;
174 CHECK(func_dict->GetString("name", &name)); 177 CHECK(func_dict->GetString("name", &name));
175 std::unique_ptr<APISignature> spec = GetAPISignature(*func_dict); 178 std::unique_ptr<APISignature> spec = GetAPISignature(*func_dict);
176 signatures_[name] = std::move(spec); 179 signatures_[name] = std::move(spec);
177 } 180 }
178 for (const auto& type : type_definitions) { 181 if (type_definitions) {
179 const base::DictionaryValue* type_dict = nullptr; 182 for (const auto& type : *type_definitions) {
180 CHECK(type->GetAsDictionary(&type_dict)); 183 const base::DictionaryValue* type_dict = nullptr;
181 std::string id; 184 CHECK(type->GetAsDictionary(&type_dict));
182 CHECK(type_dict->GetString("id", &id)); 185 std::string id;
183 DCHECK(type_refs->find(id) == type_refs->end()); 186 CHECK(type_dict->GetString("id", &id));
184 // TODO(devlin): refs are sometimes preceeded by the API namespace; we might 187 DCHECK(type_refs->find(id) == type_refs->end());
185 // need to take that into account. 188 // TODO(devlin): refs are sometimes preceeded by the API namespace; we
186 (*type_refs)[id] = base::MakeUnique<ArgumentSpec>(*type_dict); 189 // might need to take that into account.
190 (*type_refs)[id] = base::MakeUnique<ArgumentSpec>(*type_dict);
191 }
187 } 192 }
188 } 193 }
189 194
190 APIBinding::~APIBinding() {} 195 APIBinding::~APIBinding() {}
191 196
192 v8::Local<v8::Object> APIBinding::CreateInstance(v8::Local<v8::Context> context, 197 v8::Local<v8::Object> APIBinding::CreateInstance(v8::Local<v8::Context> context,
193 v8::Isolate* isolate) { 198 v8::Isolate* isolate) {
194 // TODO(devlin): APIs may change depending on which features are available, 199 // TODO(devlin): APIs may change depending on which features are available,
195 // but we should be able to cache the unconditional methods on an object 200 // but we should be able to cache the unconditional methods on an object
196 // template, create the object, and then add any conditional methods. 201 // template, create the object, and then add any conditional methods.
197 v8::Local<v8::Object> object = v8::Object::New(isolate); 202 v8::Local<v8::Object> object = v8::Object::New(isolate);
198 gin::PerContextData* per_context_data = gin::PerContextData::From(context); 203 gin::PerContextData* per_context_data = gin::PerContextData::From(context);
199 DCHECK(per_context_data); 204 DCHECK(per_context_data);
200 APIPerContextData* data = static_cast<APIPerContextData*>( 205 APIPerContextData* data = static_cast<APIPerContextData*>(
201 per_context_data->GetUserData(kExtensionAPIPerContextKey)); 206 per_context_data->GetUserData(kExtensionAPIPerContextKey));
202 if (!data) { 207 if (!data) {
203 auto api_data = base::MakeUnique<APIPerContextData>(); 208 auto api_data = base::MakeUnique<APIPerContextData>();
204 data = api_data.get(); 209 data = api_data.get();
205 per_context_data->SetUserData(kExtensionAPIPerContextKey, 210 per_context_data->SetUserData(kExtensionAPIPerContextKey,
206 api_data.release()); 211 api_data.release());
207 } 212 }
208 for (const auto& sig : signatures_) { 213 for (const auto& sig : signatures_) {
214 std::string full_method_name =
215 base::StringPrintf("%s.%s", api_name_.c_str(), sig.first.c_str());
209 auto handler_callback = base::MakeUnique<HandlerCallback>( 216 auto handler_callback = base::MakeUnique<HandlerCallback>(
210 base::Bind(&APIBinding::HandleCall, weak_factory_.GetWeakPtr(), 217 base::Bind(&APIBinding::HandleCall, weak_factory_.GetWeakPtr(),
211 sig.first, sig.second.get())); 218 full_method_name, sig.second.get()));
212 // TODO(devlin): We should be able to cache these in a function template. 219 // TODO(devlin): We should be able to cache these in a function template.
213 v8::MaybeLocal<v8::Function> maybe_function = 220 v8::MaybeLocal<v8::Function> maybe_function =
214 v8::Function::New(context, &CallbackHelper, 221 v8::Function::New(context, &CallbackHelper,
215 v8::External::New(isolate, handler_callback.get()), 222 v8::External::New(isolate, handler_callback.get()),
216 0, v8::ConstructorBehavior::kThrow); 223 0, v8::ConstructorBehavior::kThrow);
217 data->context_callbacks.push_back(std::move(handler_callback)); 224 data->context_callbacks.push_back(std::move(handler_callback));
218 v8::Maybe<bool> success = object->CreateDataProperty( 225 v8::Maybe<bool> success = object->CreateDataProperty(
219 context, gin::StringToSymbol(isolate, sig.first), 226 context, gin::StringToSymbol(isolate, sig.first),
220 maybe_function.ToLocalChecked()); 227 maybe_function.ToLocalChecked());
221 DCHECK(success.IsJust()); 228 DCHECK(success.IsJust());
(...skipping 26 matching lines...) Expand all
248 return; 255 return;
249 } 256 }
250 257
251 // Since this is called synchronously from the JS entry point, 258 // Since this is called synchronously from the JS entry point,
252 // GetCurrentContext() should always be correct. 259 // GetCurrentContext() should always be correct.
253 method_callback_.Run(name, std::move(parsed_arguments), isolate, 260 method_callback_.Run(name, std::move(parsed_arguments), isolate,
254 isolate->GetCurrentContext(), callback); 261 isolate->GetCurrentContext(), callback);
255 } 262 }
256 263
257 } // namespace extensions 264 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_binding.h ('k') | extensions/renderer/api_binding_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698