1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_RENDERER_BINDINGS_API_BINDING_JS_UTIL_H_
#define EXTENSIONS_RENDERER_BINDINGS_API_BINDING_JS_UTIL_H_
#include <string>
#include "base/memory/raw_ptr.h"
#include "gin/wrappable.h"
#include "v8/include/v8.h"
namespace gin {
class Arguments;
}
namespace extensions {
class APIEventHandler;
class APIRequestHandler;
class APITypeReferenceMap;
class ExceptionHandler;
// An object that exposes utility methods to the existing JS bindings, such as
// sendRequest and registering event argument massagers. If/when we get rid of
// some of our JS bindings, we can reduce or remove this class.
class APIBindingJSUtil final : public gin::Wrappable<APIBindingJSUtil> {
public:
APIBindingJSUtil(APITypeReferenceMap* type_refs,
APIRequestHandler* request_handler,
APIEventHandler* event_handler,
ExceptionHandler* exception_handler);
APIBindingJSUtil(const APIBindingJSUtil&) = delete;
APIBindingJSUtil& operator=(const APIBindingJSUtil&) = delete;
~APIBindingJSUtil() override;
static gin::WrapperInfo kWrapperInfo;
// gin::Wrappable:
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) final;
private:
// A handler to initiate an API request through the APIRequestHandler. A
// replacement for custom bindings that utilize require('sendRequest').
void SendRequest(gin::Arguments* arguments,
const std::string& name,
const v8::LocalVector<v8::Value>& request_args,
v8::Local<v8::Value> options);
// A handler to register an argument massager for a specific event.
// Replacement for event_bindings.registerArgumentMassager.
void RegisterEventArgumentMassager(gin::Arguments* arguments,
const std::string& event_name,
v8::Local<v8::Function> massager);
// A handler to allow custom bindings to create custom extension API event
// objects (e.g. foo.onBar).
void CreateCustomEvent(gin::Arguments* arguments,
v8::Local<v8::Value> v8_event_name,
bool supports_filters,
bool supports_lazy_listeners);
// Creates a new declarative event.
void CreateCustomDeclarativeEvent(
gin::Arguments* arguments,
const std::string& event_name,
const std::vector<std::string>& actions_list,
const std::vector<std::string>& conditions_list,
int webview_instance_id);
// Invalidates an event, removing its listeners and preventing any more from
// being added.
void InvalidateEvent(gin::Arguments* arguments, v8::Local<v8::Object> event);
// Sets the last error in the context.
void SetLastError(gin::Arguments* arguments, const std::string& error);
// Clears the last error in the context.
void ClearLastError(gin::Arguments* arguments);
// Returns true if there is a set lastError in the given context.
void HasLastError(gin::Arguments* arguments);
// Returns the lastError message for the given context, without marking it
// accessed.
void GetLastErrorMessage(gin::Arguments* arguments);
// Sets the lastError in the given context, runs the provided callback, and
// then clears the last error.
void RunCallbackWithLastError(gin::Arguments* arguments,
const std::string& error,
v8::Local<v8::Function> callback);
// Handles an exception with the given `message` and `exception` value.
void HandleException(gin::Arguments* arguments,
const std::string& message,
v8::Local<v8::Value> exception);
// Sets a custom exception handler to be used when an uncaught exception is
// found.
void SetExceptionHandler(gin::Arguments* arguments,
v8::Local<v8::Function> handler);
// Validates a given `value` against the specification for the type with
// `type_name`. Throws an error if the validation fails; otherwise returns
// undefined.
void ValidateType(gin::Arguments* arguments,
const std::string& type_name,
v8::Local<v8::Value> value);
// Allows custom bindings to add a signature with the given
// `custom_signature_name` to use later in argument validation. The signature
// is expected to be an array of expected types, that can be passed to
// construct an APISignature.
void AddCustomSignature(gin::Arguments* arguments,
const std::string& custom_signature_name,
v8::Local<v8::Value> signature);
// Looks up the signature with the given `custom_signature_name` and validates
// `arguments_to_validate` against it, throwing an error if the arguments
// don't match.
void ValidateCustomSignature(gin::Arguments* arguments,
const std::string& custom_signature_name,
v8::Local<v8::Value> arguments_to_validate);
// Type references. Guaranteed to outlive this object.
const raw_ptr<APITypeReferenceMap, DanglingUntriaged> type_refs_;
// The request handler. Guaranteed to outlive this object.
const raw_ptr<APIRequestHandler, DanglingUntriaged> request_handler_;
// The event handler. Guaranteed to outlive this object.
const raw_ptr<APIEventHandler, DanglingUntriaged> event_handler_;
// The exception handler. Guaranteed to outlive this object.
const raw_ptr<ExceptionHandler, DanglingUntriaged> exception_handler_;
};
} // namespace extensions
#endif // EXTENSIONS_RENDERER_BINDINGS_API_BINDING_JS_UTIL_H_
|