OLD | NEW |
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 Loading... |
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 for (const auto& type : type_definitions) { |
(...skipping 20 matching lines...) Expand all Loading... |
199 DCHECK(per_context_data); | 202 DCHECK(per_context_data); |
200 APIPerContextData* data = static_cast<APIPerContextData*>( | 203 APIPerContextData* data = static_cast<APIPerContextData*>( |
201 per_context_data->GetUserData(kExtensionAPIPerContextKey)); | 204 per_context_data->GetUserData(kExtensionAPIPerContextKey)); |
202 if (!data) { | 205 if (!data) { |
203 auto api_data = base::MakeUnique<APIPerContextData>(); | 206 auto api_data = base::MakeUnique<APIPerContextData>(); |
204 data = api_data.get(); | 207 data = api_data.get(); |
205 per_context_data->SetUserData(kExtensionAPIPerContextKey, | 208 per_context_data->SetUserData(kExtensionAPIPerContextKey, |
206 api_data.release()); | 209 api_data.release()); |
207 } | 210 } |
208 for (const auto& sig : signatures_) { | 211 for (const auto& sig : signatures_) { |
| 212 std::string full_method_name = |
| 213 base::StringPrintf("%s.%s", api_name_.c_str(), sig.first.c_str()); |
209 auto handler_callback = base::MakeUnique<HandlerCallback>( | 214 auto handler_callback = base::MakeUnique<HandlerCallback>( |
210 base::Bind(&APIBinding::HandleCall, weak_factory_.GetWeakPtr(), | 215 base::Bind(&APIBinding::HandleCall, weak_factory_.GetWeakPtr(), |
211 sig.first, sig.second.get())); | 216 full_method_name, sig.second.get())); |
212 // TODO(devlin): We should be able to cache these in a function template. | 217 // TODO(devlin): We should be able to cache these in a function template. |
213 v8::MaybeLocal<v8::Function> maybe_function = | 218 v8::MaybeLocal<v8::Function> maybe_function = |
214 v8::Function::New(context, &CallbackHelper, | 219 v8::Function::New(context, &CallbackHelper, |
215 v8::External::New(isolate, handler_callback.get()), | 220 v8::External::New(isolate, handler_callback.get()), |
216 0, v8::ConstructorBehavior::kThrow); | 221 0, v8::ConstructorBehavior::kThrow); |
217 data->context_callbacks.push_back(std::move(handler_callback)); | 222 data->context_callbacks.push_back(std::move(handler_callback)); |
218 v8::Maybe<bool> success = object->CreateDataProperty( | 223 v8::Maybe<bool> success = object->CreateDataProperty( |
219 context, gin::StringToSymbol(isolate, sig.first), | 224 context, gin::StringToSymbol(isolate, sig.first), |
220 maybe_function.ToLocalChecked()); | 225 maybe_function.ToLocalChecked()); |
221 DCHECK(success.IsJust()); | 226 DCHECK(success.IsJust()); |
(...skipping 26 matching lines...) Expand all Loading... |
248 return; | 253 return; |
249 } | 254 } |
250 | 255 |
251 // Since this is called synchronously from the JS entry point, | 256 // Since this is called synchronously from the JS entry point, |
252 // GetCurrentContext() should always be correct. | 257 // GetCurrentContext() should always be correct. |
253 method_callback_.Run(name, std::move(parsed_arguments), isolate, | 258 method_callback_.Run(name, std::move(parsed_arguments), isolate, |
254 isolate->GetCurrentContext(), callback); | 259 isolate->GetCurrentContext(), callback); |
255 } | 260 } |
256 | 261 |
257 } // namespace extensions | 262 } // namespace extensions |
OLD | NEW |