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.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#ifndef SHELL_BROWSER_NET_ELECTRON_URL_LOADER_FACTORY_H_
|
|
|
|
#define SHELL_BROWSER_NET_ELECTRON_URL_LOADER_FACTORY_H_
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2019-05-24 02:28:00 +00:00
|
|
|
#include <map>
|
2019-04-29 02:37:45 +00:00
|
|
|
#include <string>
|
2019-05-24 02:28:00 +00:00
|
|
|
#include <utility>
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_receiver.h"
|
2019-12-11 00:22:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
2019-10-28 22:12:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/receiver_set.h"
|
2019-12-11 00:22:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/remote.h"
|
2019-04-23 21:39:21 +00:00
|
|
|
#include "net/url_request/url_request_job_factory.h"
|
2021-03-04 17:27:05 +00:00
|
|
|
#include "services/network/public/cpp/self_deleting_url_loader_factory.h"
|
2019-04-23 21:39:21 +00:00
|
|
|
#include "services/network/public/mojom/url_loader_factory.mojom.h"
|
2019-12-13 20:13:12 +00:00
|
|
|
#include "services/network/public/mojom/url_response_head.mojom.h"
|
2019-10-15 01:15:23 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.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
|
|
|
|
2019-04-29 02:37:45 +00:00
|
|
|
// Old Protocol API can only serve one type of response for one scheme.
|
|
|
|
enum class ProtocolType {
|
|
|
|
kBuffer,
|
|
|
|
kString,
|
|
|
|
kFile,
|
|
|
|
kHttp,
|
|
|
|
kStream,
|
|
|
|
kFree, // special type for returning arbitrary type of response.
|
|
|
|
};
|
|
|
|
|
2019-10-15 01:15:23 +00:00
|
|
|
using StartLoadingCallback = base::OnceCallback<void(gin::Arguments*)>;
|
2019-04-29 02:37:45 +00:00
|
|
|
using ProtocolHandler =
|
2019-05-03 00:48:51 +00:00
|
|
|
base::Callback<void(const network::ResourceRequest&, StartLoadingCallback)>;
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-05-24 02:28:00 +00:00
|
|
|
// scheme => (type, handler).
|
|
|
|
using HandlersMap =
|
|
|
|
std::map<std::string, std::pair<ProtocolType, ProtocolHandler>>;
|
|
|
|
|
2019-04-23 21:39:21 +00:00
|
|
|
// Implementation of URLLoaderFactory.
|
2021-03-04 17:27:05 +00:00
|
|
|
class ElectronURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {
|
2019-04-23 21:39:21 +00:00
|
|
|
public:
|
2020-09-21 08:00:36 +00:00
|
|
|
static mojo::PendingRemote<network::mojom::URLLoaderFactory> Create(
|
|
|
|
ProtocolType type,
|
|
|
|
const ProtocolHandler& handler);
|
2019-04-23 21:39:21 +00:00
|
|
|
|
|
|
|
// network::mojom::URLLoaderFactory:
|
2019-11-05 23:41:20 +00:00
|
|
|
void CreateLoaderAndStart(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
2019-12-11 00:22:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-11-05 23:41:20 +00:00
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
|
|
|
|
override;
|
2019-04-23 21:39:21 +00:00
|
|
|
|
2019-05-03 00:48:51 +00:00
|
|
|
static void StartLoading(
|
2019-11-05 23:41:20 +00:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
2019-05-03 00:48:51 +00:00
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
2019-12-11 00:22:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-05-03 00:48:51 +00:00
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
2020-10-06 07:20:56 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> proxy_factory,
|
2019-05-03 00:48:51 +00:00
|
|
|
ProtocolType type,
|
2019-10-15 01:15:23 +00:00
|
|
|
gin::Arguments* args);
|
2019-05-24 02:28:00 +00:00
|
|
|
|
|
|
|
private:
|
2020-09-21 08:00:36 +00:00
|
|
|
ElectronURLLoaderFactory(
|
|
|
|
ProtocolType type,
|
|
|
|
const ProtocolHandler& handler,
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver);
|
|
|
|
~ElectronURLLoaderFactory() override;
|
|
|
|
|
2020-10-06 07:20:56 +00:00
|
|
|
static void OnComplete(
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
|
|
int32_t request_id,
|
|
|
|
const network::URLLoaderCompletionStatus& status);
|
2019-12-11 00:22:35 +00:00
|
|
|
static void StartLoadingBuffer(
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-12-13 20:13:12 +00:00
|
|
|
network::mojom::URLResponseHeadPtr head,
|
2019-12-11 00:22:35 +00:00
|
|
|
const gin_helper::Dictionary& dict);
|
|
|
|
static void StartLoadingString(
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-12-13 20:13:12 +00:00
|
|
|
network::mojom::URLResponseHeadPtr head,
|
2019-12-11 00:22:35 +00:00
|
|
|
const gin_helper::Dictionary& dict,
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> response);
|
2019-11-05 23:41:20 +00:00
|
|
|
static void StartLoadingFile(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
|
|
network::ResourceRequest request,
|
2019-12-11 00:22:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-12-13 20:13:12 +00:00
|
|
|
network::mojom::URLResponseHeadPtr head,
|
2019-11-05 23:41:20 +00:00
|
|
|
const gin_helper::Dictionary& dict,
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> response);
|
2019-05-03 00:48:51 +00:00
|
|
|
static void StartLoadingHttp(
|
2019-11-05 23:41:20 +00:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
2019-04-30 00:47:04 +00:00
|
|
|
const network::ResourceRequest& original_request,
|
2019-12-11 00:22:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-04-30 00:47:04 +00:00
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
2019-10-15 01:15:23 +00:00
|
|
|
const gin_helper::Dictionary& dict);
|
2019-11-05 23:41:20 +00:00
|
|
|
static void StartLoadingStream(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
2019-12-11 00:22:35 +00:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-12-13 20:13:12 +00:00
|
|
|
network::mojom::URLResponseHeadPtr head,
|
2019-11-05 23:41:20 +00:00
|
|
|
const gin_helper::Dictionary& dict);
|
2019-04-29 02:37:45 +00:00
|
|
|
|
2019-05-03 00:48:51 +00:00
|
|
|
// Helper to send string as response.
|
2019-12-11 00:22:35 +00:00
|
|
|
static void SendContents(
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-12-13 20:13:12 +00:00
|
|
|
network::mojom::URLResponseHeadPtr head,
|
2019-12-11 00:22:35 +00:00
|
|
|
std::string data);
|
2019-04-29 02:37:45 +00:00
|
|
|
|
|
|
|
ProtocolType type_;
|
|
|
|
ProtocolHandler handler_;
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(ElectronURLLoaderFactory);
|
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
|
|
|
#endif // SHELL_BROWSER_NET_ELECTRON_URL_LOADER_FACTORY_H_
|