2019-04-23 21:39:21 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_PROTOCOL_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_PROTOCOL_H_
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
#include <string>
|
2019-08-02 23:56:46 +00:00
|
|
|
#include <vector>
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
#include "content/public/browser/content_browser_client.h"
|
2019-10-15 01:15:23 +00:00
|
|
|
#include "gin/handle.h"
|
2020-03-26 17:34:32 +00:00
|
|
|
#include "gin/wrappable.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/net/electron_url_loader_factory.h"
|
2023-03-27 17:00:55 +00:00
|
|
|
#include "shell/common/gin_helper/constructible.h"
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
class ElectronBrowserContext;
|
2020-03-26 17:34:32 +00:00
|
|
|
class ProtocolRegistry;
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2019-08-02 23:56:46 +00:00
|
|
|
std::vector<std::string> GetStandardSchemes();
|
|
|
|
|
2021-06-01 01:45:23 +00:00
|
|
|
void AddServiceWorkerScheme(const std::string& scheme);
|
|
|
|
|
2019-10-15 01:15:23 +00:00
|
|
|
void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> val);
|
2019-08-02 23:56:46 +00:00
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
// Possible errors.
|
2019-05-03 18:11:41 +00:00
|
|
|
enum class ProtocolError {
|
2020-10-27 17:51:45 +00:00
|
|
|
kOK, // no error
|
|
|
|
kRegistered,
|
|
|
|
kNotRegistered,
|
|
|
|
kIntercepted,
|
|
|
|
kNotIntercepted,
|
2019-04-23 21:39:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Protocol implementation based on network services.
|
2023-03-27 17:00:55 +00:00
|
|
|
class Protocol : public gin::Wrappable<Protocol>,
|
|
|
|
public gin_helper::Constructible<Protocol> {
|
2019-04-23 21:39:21 +00:00
|
|
|
public:
|
2019-10-24 00:51:06 +00:00
|
|
|
static gin::Handle<Protocol> Create(v8::Isolate* isolate,
|
2020-02-04 20:19:40 +00:00
|
|
|
ElectronBrowserContext* browser_context);
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2023-03-27 17:00:55 +00:00
|
|
|
static gin::Handle<Protocol> New(gin_helper::ErrorThrower thrower);
|
|
|
|
|
2020-03-26 17:34:32 +00:00
|
|
|
// gin::Wrappable
|
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
2023-03-27 17:00:55 +00:00
|
|
|
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> tmpl);
|
2020-03-26 17:34:32 +00:00
|
|
|
const char* GetTypeName() override;
|
2019-05-24 02:28:00 +00:00
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
private:
|
2020-03-26 17:34:32 +00:00
|
|
|
Protocol(v8::Isolate* isolate, ProtocolRegistry* protocol_registry);
|
2019-10-24 00:51:06 +00:00
|
|
|
~Protocol() override;
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
// Callback types.
|
2019-05-29 20:02:15 +00:00
|
|
|
using CompletionCallback =
|
|
|
|
base::RepeatingCallback<void(v8::Local<v8::Value>)>;
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
// JS APIs.
|
2019-04-29 02:37:45 +00:00
|
|
|
ProtocolError RegisterProtocol(ProtocolType type,
|
|
|
|
const std::string& scheme,
|
|
|
|
const ProtocolHandler& handler);
|
2020-06-02 16:46:18 +00:00
|
|
|
bool UnregisterProtocol(const std::string& scheme, gin::Arguments* args);
|
2019-04-23 21:39:21 +00:00
|
|
|
bool IsProtocolRegistered(const std::string& scheme);
|
2019-05-24 02:28:00 +00:00
|
|
|
|
|
|
|
ProtocolError InterceptProtocol(ProtocolType type,
|
|
|
|
const std::string& scheme,
|
|
|
|
const ProtocolHandler& handler);
|
2020-06-02 16:46:18 +00:00
|
|
|
bool UninterceptProtocol(const std::string& scheme, gin::Arguments* args);
|
2019-05-24 02:28:00 +00:00
|
|
|
bool IsProtocolIntercepted(const std::string& scheme);
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
// Old async version of IsProtocolRegistered.
|
2019-06-28 07:25:30 +00:00
|
|
|
v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme,
|
2019-10-15 01:15:23 +00:00
|
|
|
gin::Arguments* args);
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2019-04-29 02:37:45 +00:00
|
|
|
// Helper for converting old registration APIs to new RegisterProtocol API.
|
|
|
|
template <ProtocolType type>
|
2020-06-02 16:46:18 +00:00
|
|
|
bool RegisterProtocolFor(const std::string& scheme,
|
2019-04-29 02:37:45 +00:00
|
|
|
const ProtocolHandler& handler,
|
2019-10-15 01:15:23 +00:00
|
|
|
gin::Arguments* args) {
|
2020-06-02 16:46:18 +00:00
|
|
|
auto result = RegisterProtocol(type, scheme, handler);
|
|
|
|
HandleOptionalCallback(args, result);
|
2020-10-27 17:51:45 +00:00
|
|
|
return result == ProtocolError::kOK;
|
2019-04-29 02:37:45 +00:00
|
|
|
}
|
2019-05-24 02:28:00 +00:00
|
|
|
template <ProtocolType type>
|
2020-06-02 16:46:18 +00:00
|
|
|
bool InterceptProtocolFor(const std::string& scheme,
|
2019-05-24 02:28:00 +00:00
|
|
|
const ProtocolHandler& handler,
|
2019-10-15 01:15:23 +00:00
|
|
|
gin::Arguments* args) {
|
2020-06-02 16:46:18 +00:00
|
|
|
auto result = InterceptProtocol(type, scheme, handler);
|
|
|
|
HandleOptionalCallback(args, result);
|
2020-10-27 17:51:45 +00:00
|
|
|
return result == ProtocolError::kOK;
|
2019-05-24 02:28:00 +00:00
|
|
|
}
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
// Be compatible with old interface, which accepts optional callback.
|
2019-10-15 01:15:23 +00:00
|
|
|
void HandleOptionalCallback(gin::Arguments* args, ProtocolError error);
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2020-03-26 17:34:32 +00:00
|
|
|
// Weak pointer; the lifetime of the ProtocolRegistry is guaranteed to be
|
|
|
|
// longer than the lifetime of this JS interface.
|
|
|
|
ProtocolRegistry* protocol_registry_;
|
2019-04-23 21:39:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_PROTOCOL_H_
|