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

Unified Diff: extensions/renderer/native_extension_bindings_system.cc

Issue 2657613005: [Extensions Bindings] Add chrome.runtime.lastError support (Closed)
Patch Set: . Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: extensions/renderer/native_extension_bindings_system.cc
diff --git a/extensions/renderer/native_extension_bindings_system.cc b/extensions/renderer/native_extension_bindings_system.cc
index 796302ddd06a78e0bbd69c03060b3f26e29bbf37..65417fe9904e90a534b60f46e837d71391a011f4 100644
--- a/extensions/renderer/native_extension_bindings_system.cc
+++ b/extensions/renderer/native_extension_bindings_system.cc
@@ -138,7 +138,8 @@ NativeExtensionBindingsSystem::NativeExtensionBindingsSystem(
base::Bind(&NativeExtensionBindingsSystem::SendRequest,
base::Unretained(this)),
base::Bind(&NativeExtensionBindingsSystem::OnEventListenerChanged,
- base::Unretained(this))),
+ base::Unretained(this)),
+ APILastError(base::Bind(&GetRuntime))),
weak_factory_(this) {}
NativeExtensionBindingsSystem::~NativeExtensionBindingsSystem() {}
@@ -201,7 +202,7 @@ void NativeExtensionBindingsSystem::UpdateBindingsForContext(
v8::Local<v8::String> api_name =
gin::StringToSymbol(v8_context->GetIsolate(), map_entry.first);
v8::Maybe<bool> success = chrome->SetAccessor(
- v8_context, api_name, &GetAPIHelper, nullptr, api_name);
+ v8_context, api_name, &BindingAccessor, nullptr, api_name);
if (!success.IsJust() || !success.FromJust()) {
LOG(ERROR) << "Failed to create API on Chrome object.";
return;
@@ -226,32 +227,45 @@ void NativeExtensionBindingsSystem::HandleResponse(
bool success,
const base::ListValue& response,
const std::string& error) {
- api_system_.CompleteRequest(request_id, response);
+ api_system_.CompleteRequest(request_id, response, error);
}
RequestSender* NativeExtensionBindingsSystem::GetRequestSender() {
return nullptr;
}
-// static
-void NativeExtensionBindingsSystem::GetAPIHelper(
+void NativeExtensionBindingsSystem::BindingAccessor(
v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = info.GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = info.Holder()->CreationContext();
+
+ // We use info.Data() to store a real name here instead of using the provided
+ // one to handle any weirdness from the caller (non-existent strings, etc).
+ v8::Local<v8::String> api_name = info.Data().As<v8::String>();
+ v8::Local<v8::Object> binding = GetAPIHelper(context, api_name);
+ if (!binding.IsEmpty())
+ info.GetReturnValue().Set(binding);
+}
+
+// static
+v8::Local<v8::Object> NativeExtensionBindingsSystem::GetAPIHelper(
+ v8::Local<v8::Context> context,
+ v8::Local<v8::String> api_name) {
gin::PerContextData* per_context_data = gin::PerContextData::From(context);
if (!per_context_data)
- return; // Context is shutting down.
+ return v8::Local<v8::Object>(); // Context is shutting down.
BindingsSystemPerContextData* data =
static_cast<BindingsSystemPerContextData*>(
per_context_data->GetUserData(kBindingsSystemPerContextKey));
CHECK(data);
if (!data->bindings_system) {
NOTREACHED() << "Context outlived bindings system.";
- return;
+ return v8::Local<v8::Object>();
}
+ v8::Isolate* isolate = context->GetIsolate();
v8::Local<v8::Object> apis;
if (data->api_object.IsEmpty()) {
apis = v8::Object::New(isolate);
@@ -260,16 +274,16 @@ void NativeExtensionBindingsSystem::GetAPIHelper(
apis = data->api_object.Get(isolate);
}
- // We use info.Data() to store a real name here instead of using the provided
- // one to handle any weirdness from the caller (non-existent strings, etc).
- v8::Local<v8::String> api_name = info.Data().As<v8::String>();
- v8::Local<v8::Value> result;
+ v8::Local<v8::Object> result;
v8::Maybe<bool> has_property = apis->HasRealNamedProperty(context, api_name);
if (!has_property.IsJust())
- return;
+ return v8::Local<v8::Object>();
if (has_property.FromJust()) {
- result = apis->GetRealNamedProperty(context, api_name).ToLocalChecked();
+ v8::Local<v8::Value> value =
+ apis->GetRealNamedProperty(context, api_name).ToLocalChecked();
+ DCHECK(value->IsObject());
+ result = value.As<v8::Object>();
} else {
ScriptContext* script_context =
ScriptContextSet::GetContextByV8Context(context);
@@ -296,9 +310,15 @@ void NativeExtensionBindingsSystem::GetAPIHelper(
v8::Maybe<bool> success =
apis->CreateDataProperty(context, api_name, result);
if (!success.IsJust() || !success.FromJust())
- return;
+ return v8::Local<v8::Object>();
}
- info.GetReturnValue().Set(result);
+ return result;
+}
+
+v8::Local<v8::Object> NativeExtensionBindingsSystem::GetRuntime(
+ v8::Local<v8::Context> context) {
+ return GetAPIHelper(context,
+ gin::StringToSymbol(context->GetIsolate(), "runtime"));
}
void NativeExtensionBindingsSystem::SendRequest(

Powered by Google App Engine
This is Rietveld 408576698