2019-04-24 04:50:25 +02:00
|
|
|
// Copyright (c) 2019 The Chromium Embedded Framework Authors. Portions
|
|
|
|
// Copyright (c) 2018 The Chromium Authors. All rights reserved. Use of this
|
|
|
|
// source code is governed by a BSD-style license that can be found in the
|
|
|
|
// LICENSE file.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_NET_SERVICE_PROXY_URL_LOADER_FACTORY_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_NET_SERVICE_PROXY_URL_LOADER_FACTORY_H_
|
|
|
|
|
|
|
|
#include "libcef/browser/net_service/stream_reader_url_loader.h"
|
|
|
|
|
|
|
|
#include "base/callback.h"
|
|
|
|
#include "base/containers/unique_ptr_adapters.h"
|
2019-06-05 16:15:45 +02:00
|
|
|
#include "base/hash/hash.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/macros.h"
|
|
|
|
#include "base/strings/string_piece.h"
|
|
|
|
#include "content/public/browser/content_browser_client.h"
|
2019-10-01 15:55:16 +02:00
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "mojo/public/cpp/bindings/pending_receiver.h"
|
2021-03-04 23:36:57 +01:00
|
|
|
#include "mojo/public/cpp/bindings/receiver_set.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "services/network/public/mojom/network_context.mojom.h"
|
|
|
|
#include "services/network/public/mojom/url_loader_factory.mojom.h"
|
2021-06-04 03:34:56 +02:00
|
|
|
#include "third_party/abseil-cpp/absl/types/optional.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-12-02 23:31:49 +01:00
|
|
|
namespace content {
|
|
|
|
class ResourceContext;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
namespace net_service {
|
|
|
|
|
|
|
|
class InterceptedRequest;
|
|
|
|
class ResourceContextData;
|
|
|
|
|
|
|
|
// Implement this interface to to evaluate requests. All methods are called on
|
2019-05-18 11:15:04 +02:00
|
|
|
// the IO thread, and all callbacks must be executed on the IO thread.
|
2019-04-24 04:50:25 +02:00
|
|
|
class InterceptedRequestHandler {
|
|
|
|
public:
|
|
|
|
InterceptedRequestHandler();
|
|
|
|
virtual ~InterceptedRequestHandler();
|
|
|
|
|
2019-05-18 11:15:04 +02:00
|
|
|
// Optionally modify |request| and execute |callback| to continue the request.
|
|
|
|
// Set |intercept_request| to false if the request will not be intercepted.
|
|
|
|
// Set |intercept_only| to true if the loader should not proceed unless the
|
|
|
|
// request is intercepted. Keep a reference to |cancel_callback| and execute
|
|
|
|
// at any time to cancel the request.
|
2019-04-24 04:50:25 +02:00
|
|
|
using OnBeforeRequestResultCallback =
|
|
|
|
base::OnceCallback<void(bool /* intercept_request */,
|
|
|
|
bool /* intercept_only */)>;
|
2019-05-19 21:21:20 +02:00
|
|
|
using CancelRequestCallback = base::OnceCallback<void(int /* error_code */)>;
|
2021-04-21 00:52:34 +02:00
|
|
|
virtual void OnBeforeRequest(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
bool request_was_redirected,
|
2019-05-18 11:15:04 +02:00
|
|
|
OnBeforeRequestResultCallback callback,
|
|
|
|
CancelRequestCallback cancel_callback);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-05-18 11:15:04 +02:00
|
|
|
// Optionally modify |request| and execute |callback| after determining if the
|
|
|
|
// request hould be intercepted.
|
2019-04-24 04:50:25 +02:00
|
|
|
using ShouldInterceptRequestResultCallback =
|
2019-05-18 11:15:04 +02:00
|
|
|
base::OnceCallback<void(std::unique_ptr<ResourceResponse>)>;
|
2019-04-24 04:50:25 +02:00
|
|
|
virtual void ShouldInterceptRequest(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
ShouldInterceptRequestResultCallback callback);
|
|
|
|
|
|
|
|
// Called to evaluate and optionally modify request headers. |request| is the
|
|
|
|
// current request information. |redirect_url| will be non-empty if this
|
|
|
|
// method is called in response to a redirect. The |modified_headers| and
|
|
|
|
// |removed_headers| may be modified. If non-empty the |modified_headers|
|
|
|
|
// values will be merged first, and then any |removed_headers| values will be
|
|
|
|
// removed. This comparison is case sensitive.
|
|
|
|
virtual void ProcessRequestHeaders(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
const GURL& redirect_url,
|
|
|
|
net::HttpRequestHeaders* modified_headers,
|
|
|
|
std::vector<std::string>* removed_headers) {}
|
|
|
|
|
|
|
|
// Called to evaluate and optionally modify response headers. |request| is the
|
|
|
|
// current request information. |redirect_url| will be non-empty if this
|
|
|
|
// method is called in response to a redirect. Even though |head| is const the
|
|
|
|
// |head.headers| value is non-const and any changes will be passed to the
|
|
|
|
// client.
|
2021-04-21 00:52:34 +02:00
|
|
|
virtual void ProcessResponseHeaders(int32_t request_id,
|
2020-03-04 01:29:39 +01:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
const GURL& redirect_url,
|
|
|
|
net::HttpResponseHeaders* headers) {}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
enum class ResponseMode {
|
|
|
|
// Continue the request.
|
|
|
|
CONTINUE,
|
|
|
|
// Restart the request.
|
|
|
|
RESTART,
|
|
|
|
// Cancel the request.
|
|
|
|
CANCEL
|
|
|
|
};
|
|
|
|
|
|
|
|
// Called on response. |request| is the current request information.
|
|
|
|
// |redirect_info| will be non-empty for redirect responses.
|
|
|
|
// Optionally modify |request| and execute the callback as appropriate.
|
|
|
|
using OnRequestResponseResultCallback = base::OnceCallback<void(
|
|
|
|
ResponseMode /* response_mode */,
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> /* override_headers */,
|
|
|
|
const GURL& /* redirect_url */)>;
|
|
|
|
virtual void OnRequestResponse(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
2020-03-04 01:29:39 +01:00
|
|
|
net::HttpResponseHeaders* headers,
|
2021-06-04 03:34:56 +02:00
|
|
|
absl::optional<net::RedirectInfo> redirect_info,
|
2019-04-24 04:50:25 +02:00
|
|
|
OnRequestResponseResultCallback callback);
|
|
|
|
|
2019-05-19 21:21:20 +02:00
|
|
|
// Called to optionally filter the response body.
|
|
|
|
virtual mojo::ScopedDataPipeConsumerHandle OnFilterResponseBody(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-05-19 21:21:20 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
mojo::ScopedDataPipeConsumerHandle body);
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Called on completion notification from the loader (successful or not).
|
|
|
|
virtual void OnRequestComplete(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
const network::URLLoaderCompletionStatus& status) {}
|
|
|
|
|
|
|
|
// Called on error.
|
2021-04-21 00:52:34 +02:00
|
|
|
virtual void OnRequestError(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
int error_code,
|
|
|
|
bool safebrowsing_hit) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(InterceptedRequestHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
// URL Loader Factory that supports request/response interception, processing
|
|
|
|
// and callback invocation.
|
|
|
|
// Based on android_webview/browser/network_service/
|
|
|
|
// aw_proxying_url_loader_factory.cc
|
|
|
|
class ProxyURLLoaderFactory
|
|
|
|
: public network::mojom::URLLoaderFactory,
|
|
|
|
public network::mojom::TrustedURLLoaderHeaderClient {
|
|
|
|
public:
|
|
|
|
~ProxyURLLoaderFactory() override;
|
|
|
|
|
|
|
|
// Create a proxy object on the UI thread.
|
|
|
|
static void CreateProxy(
|
|
|
|
content::BrowserContext* browser_context,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory>* factory_receiver,
|
|
|
|
mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>*
|
|
|
|
header_client,
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler);
|
|
|
|
|
|
|
|
// Create a proxy object on the IO thread.
|
2020-09-25 03:40:47 +02:00
|
|
|
static void CreateProxy(
|
2019-10-01 15:55:16 +02:00
|
|
|
content::WebContents::Getter web_contents_getter,
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader_request,
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler);
|
|
|
|
|
|
|
|
// mojom::URLLoaderFactory methods:
|
2020-02-10 18:10:17 +01:00
|
|
|
void CreateLoaderAndStart(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
|
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
|
|
|
|
override;
|
2019-11-12 17:11:44 +01:00
|
|
|
void Clone(
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory) override;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// network::mojom::TrustedURLLoaderHeaderClient:
|
|
|
|
void OnLoaderCreated(
|
|
|
|
int32_t request_id,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver)
|
|
|
|
override;
|
2019-11-12 17:11:44 +01:00
|
|
|
void OnLoaderForCorsPreflightCreated(
|
2020-09-11 18:52:35 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver)
|
2019-11-12 17:11:44 +01:00
|
|
|
override;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class InterceptedRequest;
|
|
|
|
friend class ResourceContextData;
|
|
|
|
|
|
|
|
ProxyURLLoaderFactory(
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::mojom::URLLoaderFactoryPtrInfo target_factory_info,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>
|
|
|
|
header_client_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler);
|
|
|
|
|
|
|
|
static void CreateOnIOThread(
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::mojom::URLLoaderFactoryPtrInfo target_factory_info,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>
|
|
|
|
header_client_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
content::ResourceContext* resource_context,
|
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler);
|
|
|
|
|
|
|
|
using DisconnectCallback = base::OnceCallback<void(ProxyURLLoaderFactory*)>;
|
|
|
|
void SetDisconnectCallback(DisconnectCallback on_disconnect);
|
|
|
|
|
|
|
|
void OnTargetFactoryError();
|
|
|
|
void OnProxyBindingError();
|
|
|
|
void RemoveRequest(InterceptedRequest* request);
|
|
|
|
void MaybeDestroySelf();
|
|
|
|
|
2021-03-04 23:36:57 +01:00
|
|
|
mojo::ReceiverSet<network::mojom::URLLoaderFactory> proxy_receivers_;
|
|
|
|
mojo::Remote<network::mojom::URLLoaderFactory> target_factory_;
|
|
|
|
mojo::Receiver<network::mojom::TrustedURLLoaderHeaderClient>
|
2019-10-01 15:55:16 +02:00
|
|
|
url_loader_header_client_receiver_{this};
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler_;
|
|
|
|
|
2019-06-14 18:52:28 +02:00
|
|
|
bool destroyed_ = false;
|
2019-04-24 04:50:25 +02:00
|
|
|
DisconnectCallback on_disconnect_;
|
|
|
|
|
|
|
|
// Map of request ID to request object.
|
|
|
|
std::map<int32_t, std::unique_ptr<InterceptedRequest>> requests_;
|
|
|
|
|
|
|
|
base::WeakPtrFactory<ProxyURLLoaderFactory> weak_factory_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ProxyURLLoaderFactory);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace net_service
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_NET_SERVICE_PROXY_URL_LOADER_FACTORY_H_
|