refactor: reduce coupling in electron::api::Protocol (#46183)

* refactor: decouple api::Protocol from ElectronBrowserContext

now they do not know about each other

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: make electron::api::ProtocolError private

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: remove unused isolate arg in Protocol constructor

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: use =default for trivial destructor

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-03-21 11:08:59 -05:00 committed by GitHub
parent 998de7aa6c
commit a1d8676e9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 54 additions and 58 deletions

View file

@ -22,7 +22,6 @@ class Handle;
namespace electron {
class ElectronBrowserContext;
class ProtocolRegistry;
namespace api {
@ -35,21 +34,12 @@ void AddServiceWorkerScheme(const std::string& scheme);
void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> val);
// Possible errors.
enum class ProtocolError {
kOK, // no error
kRegistered,
kNotRegistered,
kIntercepted,
kNotIntercepted,
};
// Protocol implementation based on network services.
class Protocol final : public gin::Wrappable<Protocol>,
public gin_helper::Constructible<Protocol> {
public:
static gin::Handle<Protocol> Create(v8::Isolate* isolate,
ElectronBrowserContext* browser_context);
ProtocolRegistry* protocol_registry);
// gin_helper::Constructible
static gin::Handle<Protocol> New(gin_helper::ErrorThrower thrower);
@ -63,23 +53,34 @@ class Protocol final : public gin::Wrappable<Protocol>,
const char* GetTypeName() override;
private:
Protocol(v8::Isolate* isolate, ProtocolRegistry* protocol_registry);
~Protocol() override;
// Possible errors.
enum class Error {
kOK, // no error
kRegistered,
kNotRegistered,
kIntercepted,
kNotIntercepted,
};
// Callback types.
using CompletionCallback =
base::RepeatingCallback<void(v8::Local<v8::Value>)>;
explicit Protocol(ProtocolRegistry* protocol_registry);
~Protocol() override = default;
[[nodiscard]] static std::string_view ErrorCodeToString(Error error);
// JS APIs.
ProtocolError RegisterProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
Error RegisterProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
bool UnregisterProtocol(const std::string& scheme, gin::Arguments* args);
bool IsProtocolRegistered(const std::string& scheme);
ProtocolError InterceptProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
Error InterceptProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
bool UninterceptProtocol(const std::string& scheme, gin::Arguments* args);
bool IsProtocolIntercepted(const std::string& scheme);
@ -92,21 +93,21 @@ class Protocol final : public gin::Wrappable<Protocol>,
bool RegisterProtocolFor(const std::string& scheme,
const ProtocolHandler& handler,
gin::Arguments* args) {
auto result = RegisterProtocol(type, scheme, handler);
const auto result = RegisterProtocol(type, scheme, handler);
HandleOptionalCallback(args, result);
return result == ProtocolError::kOK;
return result == Error::kOK;
}
template <ProtocolType type>
bool InterceptProtocolFor(const std::string& scheme,
const ProtocolHandler& handler,
gin::Arguments* args) {
auto result = InterceptProtocol(type, scheme, handler);
const auto result = InterceptProtocol(type, scheme, handler);
HandleOptionalCallback(args, result);
return result == ProtocolError::kOK;
return result == Error::kOK;
}
// Be compatible with old interface, which accepts optional callback.
void HandleOptionalCallback(gin::Arguments* args, ProtocolError error);
void HandleOptionalCallback(gin::Arguments* args, Error error);
// Weak pointer; the lifetime of the ProtocolRegistry is guaranteed to be
// longer than the lifetime of this JS interface.