e70ce89235
* chore: iwyu shell/browser/electron_pdf_document_helper_client.h * chore: iwyu shell/browser/hid/electron_hid_delegate.h * chore: iwyu content/public/browser/web_contents.h * chore: iwyu shell/browser/usb/electron_usb_delegate.h * chore: iwyu shell/browser/browser_observer.h * chore: iwyu shell/browser/bluetooth/electron_bluetooth_delegate.h * chore: iwyu shell/browser/serial/electron_serial_delegate.h * chore: iwyu shell/browser/api/frame_subscriber.h * chore: iwyu mojo/public/cpp/bindings/ * chore: iwyu components/ * chore: iwyu extensions/ * chore: iwyu shell/common/gin_helper/ * chore: iwyu v8/ * chore: iwyu base/containers/linked_list.h * chore: iwyu shell/browser/native_window.h * chore: iwyu shell/browser/api/electron_api_base_window.h * chore: iwyu shell/common/node_includes.h * chore: iwyu gin/handle.h * chore: iwyu base/functional/callback.h * chore: iwyu ui/gfx/ * chore: iwyu content/public/browser/render_frame_host.h * fix: mac * fix: mac * fix: win * chore: iwyu base/files/file_path.h * chore: iwyu base/unguessable_token.h * chore: iwyu ui/display/screen.h * chore: iwyu chrome/browser/predictors/preconnect_manager.h * chore: iwyu base/observer_list_types.h * chore: iwyu content/public/browser/web_contents.h * chore: iwyu chrome/browser/devtools/devtools_eye_dropper.h * chore: iwyu shell/browser/ui/inspectable_web_contents.h * chore: iwyu content/public/browser/keyboard_event_processing_result.h * chore: iwyu net/cookies/canonical_cookie.h * chore: iwyu net/base/address_list.h * chore: iwyu net/cert/x509_certificate.h * chore: iwyu net/cookies/cookie_change_dispatcher.h * chore: iwyu net/dns/public/host_resolver_results.h * fix: mac * Revert "chore: iwyu net/cert/x509_certificate.h" This reverts commit 002896f71146e90f1e29e090a1d6eede48cee11e.
186 lines
6.7 KiB
C++
186 lines
6.7 KiB
C++
// Copyright (c) 2019 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_NET_ELECTRON_URL_LOADER_FACTORY_H_
|
|
#define ELECTRON_SHELL_BROWSER_NET_ELECTRON_URL_LOADER_FACTORY_H_
|
|
|
|
#include <map>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
|
#include "mojo/public/cpp/bindings/receiver.h"
|
|
#include "mojo/public/cpp/bindings/remote.h"
|
|
#include "net/url_request/url_request_job_factory.h"
|
|
#include "services/network/public/cpp/resource_request.h"
|
|
#include "services/network/public/cpp/self_deleting_url_loader_factory.h"
|
|
#include "services/network/public/mojom/url_loader.mojom.h"
|
|
#include "services/network/public/mojom/url_loader_factory.mojom.h"
|
|
#include "services/network/public/mojom/url_response_head.mojom.h"
|
|
#include "v8/include/v8-array-buffer.h"
|
|
|
|
namespace gin {
|
|
class Arguments;
|
|
} // namespace gin
|
|
|
|
namespace gin_helper {
|
|
class Dictionary;
|
|
} // namespace gin_helper
|
|
|
|
namespace mojo {
|
|
template <typename T>
|
|
class PendingReceiver;
|
|
} // namespace mojo
|
|
|
|
namespace electron {
|
|
|
|
// 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.
|
|
};
|
|
|
|
using StartLoadingCallback = base::OnceCallback<void(gin::Arguments*)>;
|
|
using ProtocolHandler =
|
|
base::RepeatingCallback<void(const network::ResourceRequest&,
|
|
StartLoadingCallback)>;
|
|
|
|
// scheme => (type, handler).
|
|
using HandlersMap =
|
|
std::map<std::string, std::pair<ProtocolType, ProtocolHandler>>;
|
|
|
|
// Implementation of URLLoaderFactory.
|
|
class ElectronURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {
|
|
public:
|
|
// This class binds a URLLoader receiver in the case of a redirect, waiting
|
|
// for |FollowRedirect| to be called at which point the new request will be
|
|
// started, and the receiver will be unbound letting a new URLLoader bind it
|
|
class RedirectedRequest : public network::mojom::URLLoader {
|
|
public:
|
|
RedirectedRequest(
|
|
const net::RedirectInfo& redirect_info,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
|
|
int32_t request_id,
|
|
uint32_t options,
|
|
const network::ResourceRequest& request,
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory>
|
|
target_factory_remote);
|
|
~RedirectedRequest() override;
|
|
|
|
// disable copy
|
|
RedirectedRequest(const RedirectedRequest&) = delete;
|
|
RedirectedRequest& operator=(const RedirectedRequest&) = delete;
|
|
|
|
// network::mojom::URLLoader:
|
|
void FollowRedirect(
|
|
const std::vector<std::string>& removed_headers,
|
|
const net::HttpRequestHeaders& modified_headers,
|
|
const net::HttpRequestHeaders& modified_cors_exempt_headers,
|
|
const std::optional<GURL>& new_url) override;
|
|
void SetPriority(net::RequestPriority priority,
|
|
int32_t intra_priority_value) override {}
|
|
void PauseReadingBodyFromNet() override {}
|
|
void ResumeReadingBodyFromNet() override {}
|
|
|
|
void OnTargetFactoryError();
|
|
void DeleteThis();
|
|
|
|
private:
|
|
net::RedirectInfo redirect_info_;
|
|
|
|
mojo::Receiver<network::mojom::URLLoader> loader_receiver_{this};
|
|
int32_t request_id_;
|
|
uint32_t options_;
|
|
network::ResourceRequest request_;
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client_;
|
|
net::MutableNetworkTrafficAnnotationTag traffic_annotation_;
|
|
|
|
mojo::Remote<network::mojom::URLLoaderFactory> target_factory_remote_;
|
|
};
|
|
|
|
static mojo::PendingRemote<network::mojom::URLLoaderFactory> Create(
|
|
ProtocolType type,
|
|
const ProtocolHandler& handler);
|
|
|
|
// network::mojom::URLLoaderFactory:
|
|
void CreateLoaderAndStart(
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
int32_t request_id,
|
|
uint32_t options,
|
|
const network::ResourceRequest& request,
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
|
|
override;
|
|
|
|
static void StartLoading(
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
int32_t request_id,
|
|
uint32_t options,
|
|
const network::ResourceRequest& request,
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory,
|
|
ProtocolType type,
|
|
gin::Arguments* args);
|
|
|
|
// disable copy
|
|
ElectronURLLoaderFactory(const ElectronURLLoaderFactory&) = delete;
|
|
ElectronURLLoaderFactory& operator=(const ElectronURLLoaderFactory&) = delete;
|
|
|
|
private:
|
|
ElectronURLLoaderFactory(
|
|
ProtocolType type,
|
|
const ProtocolHandler& handler,
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver);
|
|
~ElectronURLLoaderFactory() override;
|
|
|
|
static void OnComplete(
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
int32_t request_id,
|
|
const network::URLLoaderCompletionStatus& status);
|
|
|
|
static void StartLoadingBuffer(
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
network::mojom::URLResponseHeadPtr head,
|
|
v8::Local<v8::ArrayBufferView> buffer);
|
|
static void StartLoadingFile(
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
network::mojom::URLResponseHeadPtr head,
|
|
const network::ResourceRequest& original_request,
|
|
const base::FilePath& path,
|
|
const gin_helper::Dictionary& opts);
|
|
static void StartLoadingHttp(
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
const network::ResourceRequest& original_request,
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
|
const gin_helper::Dictionary& dict);
|
|
static void StartLoadingStream(
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
network::mojom::URLResponseHeadPtr head,
|
|
const gin_helper::Dictionary& dict);
|
|
|
|
// Helper to send string as response.
|
|
static void SendContents(
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
network::mojom::URLResponseHeadPtr head,
|
|
std::string data);
|
|
|
|
ProtocolType type_;
|
|
ProtocolHandler handler_;
|
|
};
|
|
|
|
} // namespace electron
|
|
|
|
#endif // ELECTRON_SHELL_BROWSER_NET_ELECTRON_URL_LOADER_FACTORY_H_
|