| Index: extensions/renderer/api_binding_hooks.cc
|
| diff --git a/extensions/renderer/api_binding_hooks.cc b/extensions/renderer/api_binding_hooks.cc
|
| index 5cd77ac8285c117f53115cb7b924d18e55d5dba9..2debd21bf809c9bca0474d812d893608e6e1e70e 100644
|
| --- a/extensions/renderer/api_binding_hooks.cc
|
| +++ b/extensions/renderer/api_binding_hooks.cc
|
| @@ -84,33 +84,6 @@ struct APIHooksPerContextData : public base::SupportsUserData::Data {
|
| gin::WrapperInfo JSHookInterface::kWrapperInfo =
|
| {gin::kEmbedderNativeGin};
|
|
|
| -// Creates and returns JS object for the hook interface to allow for
|
| -// registering custom hooks from JS.
|
| -v8::Local<v8::Object> CreateJSHookInterface(const std::string& api_name,
|
| - v8::Local<v8::Context> context) {
|
| - gin::PerContextData* per_context_data = gin::PerContextData::From(context);
|
| - DCHECK(per_context_data);
|
| - APIHooksPerContextData* data = static_cast<APIHooksPerContextData*>(
|
| - per_context_data->GetUserData(kExtensionAPIHooksPerContextKey));
|
| - if (!data) {
|
| - auto api_data =
|
| - base::MakeUnique<APIHooksPerContextData>(context->GetIsolate());
|
| - data = api_data.get();
|
| - per_context_data->SetUserData(kExtensionAPIHooksPerContextKey,
|
| - api_data.release());
|
| - }
|
| -
|
| - DCHECK(data->hook_interfaces.find(api_name) == data->hook_interfaces.end());
|
| -
|
| - gin::Handle<JSHookInterface> hooks =
|
| - gin::CreateHandle(context->GetIsolate(), new JSHookInterface(api_name));
|
| - CHECK(!hooks.IsEmpty());
|
| - v8::Local<v8::Object> hooks_object = hooks.ToV8().As<v8::Object>();
|
| - data->hook_interfaces[api_name].Reset(context->GetIsolate(), hooks_object);
|
| -
|
| - return hooks_object;
|
| -}
|
| -
|
| } // namespace
|
|
|
| APIBindingHooks::APIBindingHooks(const binding::RunJSFunction& run_js)
|
| @@ -169,7 +142,7 @@ bool APIBindingHooks::HandleRequest(
|
| std::vector<v8::Local<v8::Value>> v8_args;
|
| // TODO(devlin): Right now, this doesn't support exceptions or return values,
|
| // which we will need to at some point.
|
| - if (arguments->GetRemaining(&v8_args)) {
|
| + if (arguments->Length() == 0 || arguments->GetRemaining(&v8_args)) {
|
| v8::Local<v8::Function> handler =
|
| js_hook_iter->second.Get(context->GetIsolate());
|
| run_js_.Run(handler, context, v8_args.size(), v8_args.data());
|
| @@ -195,9 +168,37 @@ void APIBindingHooks::InitializeInContext(
|
| v8::Local<v8::Function> function;
|
| if (!gin::ConvertFromV8(context->GetIsolate(), func_as_value, &function))
|
| return;
|
| - v8::Local<v8::Object> api_hooks = CreateJSHookInterface(api_name, context);
|
| + v8::Local<v8::Value> api_hooks = GetJSHookInterface(api_name, context);
|
| v8::Local<v8::Value> args[] = {api_hooks};
|
| run_js_.Run(function, context, arraysize(args), args);
|
| }
|
|
|
| +v8::Local<v8::Object> APIBindingHooks::GetJSHookInterface(
|
| + const std::string& api_name,
|
| + v8::Local<v8::Context> context) {
|
| + gin::PerContextData* per_context_data = gin::PerContextData::From(context);
|
| + DCHECK(per_context_data);
|
| + APIHooksPerContextData* data = static_cast<APIHooksPerContextData*>(
|
| + per_context_data->GetUserData(kExtensionAPIHooksPerContextKey));
|
| + if (!data) {
|
| + auto api_data =
|
| + base::MakeUnique<APIHooksPerContextData>(context->GetIsolate());
|
| + data = api_data.get();
|
| + per_context_data->SetUserData(kExtensionAPIHooksPerContextKey,
|
| + api_data.release());
|
| + }
|
| +
|
| + auto iter = data->hook_interfaces.find(api_name);
|
| + if (iter != data->hook_interfaces.end())
|
| + return iter->second.Get(context->GetIsolate());
|
| +
|
| + gin::Handle<JSHookInterface> hooks =
|
| + gin::CreateHandle(context->GetIsolate(), new JSHookInterface(api_name));
|
| + CHECK(!hooks.IsEmpty());
|
| + v8::Local<v8::Object> hooks_object = hooks.ToV8().As<v8::Object>();
|
| + data->hook_interfaces[api_name].Reset(context->GetIsolate(), hooks_object);
|
| +
|
| + return hooks_object;
|
| +}
|
| +
|
| } // namespace extensions
|
|
|