#ifndef EXTENSIONS_RENDERER_NATIVE_EXTENSION_BINDINGS_SYSTEM_H_
#define EXTENSIONS_RENDERER_NATIVE_EXTENSION_BINDINGS_SYSTEM_H_
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/mojom/event_dispatcher.mojom-forward.h"
#include "extensions/renderer/api/messaging/native_renderer_messaging_service.h"
#include "extensions/renderer/bindings/api_binding_types.h"
#include "extensions/renderer/bindings/api_bindings_system.h"
#include "extensions/renderer/bindings/event_emitter.h"
#include "extensions/renderer/feature_cache.h"
#include "v8/include/v8-forward.h"
#include "v8/include/v8-persistent-handle.h"
namespace extensions {
class IPCMessageSender;
class ScriptContext;
class ScriptContextSetIterable;
class NativeExtensionBindingsSystem {
public:
class Delegate {
public:
virtual ScriptContextSetIterable* GetScriptContextSet() = 0;
virtual ~Delegate() = default;
};
explicit NativeExtensionBindingsSystem(
Delegate* delegate,
std::unique_ptr<IPCMessageSender> ipc_message_sender);
NativeExtensionBindingsSystem(const NativeExtensionBindingsSystem&) = delete;
NativeExtensionBindingsSystem& operator=(
const NativeExtensionBindingsSystem&) = delete;
~NativeExtensionBindingsSystem();
void DidCreateScriptContext(ScriptContext* context);
void WillReleaseScriptContext(ScriptContext* context);
void UpdateBindingsForContext(ScriptContext* context);
void DispatchEventInContext(
const std::string& event_name,
const base::Value::List& event_args,
const mojom::EventFilteringInfoPtr& filtering_info,
ScriptContext* context);
bool HasEventListenerInContext(const std::string& event_name,
ScriptContext* context);
void HandleResponse(int request_id,
bool success,
const base::Value::List& response,
const std::string& error,
mojom::ExtraResponseDataPtr extra_data = nullptr);
IPCMessageSender* GetIPCMessageSender();
void UpdateBindings(const ExtensionId& extension_id,
bool permissions_changed,
ScriptContextSetIterable* script_context_set);
void OnExtensionRemoved(const ExtensionId& id);
APIBindingsSystem* api_system() { return &api_system_; }
NativeRendererMessagingService* messaging_service() {
return &messaging_service_;
}
Delegate* delegate() { return delegate_; }
v8::Local<v8::Object> GetAPIObjectForTesting(ScriptContext* context,
const std::string& api_name);
private:
void SendRequest(std::unique_ptr<APIRequestHandler::Request> request,
v8::Local<v8::Context> context);
void OnEventListenerChanged(const std::string& event_name,
binding::EventListenersChanged change,
const base::Value::Dict* filter,
bool was_manual,
v8::Local<v8::Context> context);
static void BindingAccessor(v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value>& info);
static void ThrowDeveloperModeRestrictedError(
v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value>& info);
static v8::Local<v8::Object> GetAPIHelper(v8::Local<v8::Context> context,
v8::Local<v8::String> name);
static v8::Local<v8::Object> GetLastErrorParents(
v8::Local<v8::Context> context,
v8::Local<v8::Object>* secondary_parent);
static void GetInternalAPI(const v8::FunctionCallbackInfo<v8::Value>& info);
void GetJSBindingUtil(v8::Local<v8::Context> context,
v8::Local<v8::Value>* binding_util_out);
void UpdateContentCapabilities(ScriptContext* context);
void SetScriptingParams(ScriptContext* context);
const raw_ptr<Delegate> delegate_;
std::unique_ptr<IPCMessageSender> ipc_message_sender_;
APIBindingsSystem api_system_;
NativeRendererMessagingService messaging_service_;
FeatureCache feature_cache_;
v8::Eternal<v8::FunctionTemplate> get_internal_api_;
base::WeakPtrFactory<NativeExtensionBindingsSystem> weak_factory_{this};
};
}
#endif