[Extensions Bindings] Separate out js util and bindings hooks
Custom bindings need to be able to both set custom hooks and callbacks
on an API object and also to be able to access common js util in order
to send requests, modify the last error, etc.
In the case of custom bindings associated with a particular API (e.g.
runtime_custom_bindings.js is associated with the chrome.runtime API),
we have an associated API binding object (the chrome.runtime binding).
However, there are also shared custom binding files, like messaging.js,
which don't have a single API object, but still require the common JS
util.
Separate out the js util from the APIBindingBridge in order to be able
to expose these utils to the shared files that won't have an associated
bridge (since they don't have an associated API).
BUG=653596
Review-Url: https://codereview.chromium.org/2731413002
Cr-Commit-Position: refs/heads/master@{#455843}
diff --git a/extensions/renderer/module_system.cc b/extensions/renderer/module_system.cc
index 85e1531..55c6d64 100644
--- a/extensions/renderer/module_system.cc
+++ b/extensions/renderer/module_system.cc
@@ -561,6 +561,11 @@
get_internal_api_.Set(GetIsolate(), get_internal_api);
}
+void ModuleSystem::SetJSBindingUtilGetter(const JSBindingUtilGetter& getter) {
+ DCHECK(js_binding_util_getter_.is_null());
+ js_binding_util_getter_ = getter;
+}
+
v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code,
v8::Local<v8::String> name) {
return context_->RunScript(
@@ -641,7 +646,7 @@
v8::Local<v8::String> left = ToV8StringUnsafe(
GetIsolate(),
"(function(define, require, requireNative, requireAsync, exports, "
- "console, privates, apiBridge, getInternalApi,"
+ "console, privates, apiBridge, bindingUtil, getInternalApi,"
"$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {"
"'use strict';");
v8::Local<v8::String> right = ToV8StringUnsafe(GetIsolate(), "\n})");
@@ -748,6 +753,19 @@
.ToLocalChecked();
}
+ v8::Local<v8::Value> binding_util;
+ if (!js_binding_util_getter_.is_null()) {
+ js_binding_util_getter_.Run(v8_context, &binding_util);
+ if (binding_util.IsEmpty()) {
+ // The NativeExtensionBindingsSystem was destroyed. This shouldn't happen,
+ // but JS makes the impossible possible!
+ NOTREACHED();
+ return v8::Undefined(GetIsolate());
+ }
+ } else {
+ binding_util = v8::Undefined(GetIsolate());
+ }
+
// These must match the argument order in WrapSource.
v8::Local<v8::Value> args[] = {
// AMD.
@@ -765,6 +783,7 @@
GetPropertyUnsafe(v8_context, natives, "privates",
v8::NewStringType::kInternalized),
api_bridge, // exposed as apiBridge.
+ binding_util, // exposed as bindingUtil.
get_internal_api, // exposed as getInternalApi.
// Each safe builtin. Keep in order with the arguments in WrapSource.
context_->safe_builtins()->GetArray(),