2019-04-24 04:50:25 +02:00
|
|
|
// Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/net_service/resource_request_handler_wrapper.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_host_base.h"
|
|
|
|
#include "cef/libcef/browser/context.h"
|
|
|
|
#include "cef/libcef/browser/iothread_state.h"
|
|
|
|
#include "cef/libcef/browser/net_service/cookie_helper.h"
|
|
|
|
#include "cef/libcef/browser/net_service/proxy_url_loader_factory.h"
|
|
|
|
#include "cef/libcef/browser/net_service/resource_handler_wrapper.h"
|
|
|
|
#include "cef/libcef/browser/net_service/response_filter_wrapper.h"
|
|
|
|
#include "cef/libcef/browser/prefs/browser_prefs.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/common/app_manager.h"
|
|
|
|
#include "cef/libcef/common/net/scheme_registration.h"
|
|
|
|
#include "cef/libcef/common/net_service/net_service_util.h"
|
|
|
|
#include "cef/libcef/common/request_impl.h"
|
|
|
|
#include "cef/libcef/common/response_impl.h"
|
2020-07-01 02:57:00 +02:00
|
|
|
#include "chrome/browser/profiles/profile.h"
|
2019-05-11 00:14:48 +02:00
|
|
|
#include "components/language/core/browser/pref_names.h"
|
|
|
|
#include "components/prefs/pref_service.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "content/browser/renderer_host/frame_tree_node.h"
|
2022-01-07 01:41:56 +01:00
|
|
|
#include "content/browser/renderer_host/render_frame_host_impl.h"
|
|
|
|
#include "content/browser/storage_partition_impl.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2019-07-29 23:27:12 +02:00
|
|
|
#include "net/base/load_flags.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "net/http/http_status_code.h"
|
2020-03-30 22:13:42 +02:00
|
|
|
#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "ui/base/page_transition_types.h"
|
|
|
|
#include "url/origin.h"
|
|
|
|
|
|
|
|
namespace net_service {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-06-12 17:14:20 +02:00
|
|
|
const int kLoadNoCookiesFlags =
|
|
|
|
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES;
|
|
|
|
|
2021-09-27 11:09:45 +02:00
|
|
|
class RequestCallbackWrapper : public CefCallback {
|
2019-04-24 04:50:25 +02:00
|
|
|
public:
|
|
|
|
using Callback = base::OnceCallback<void(bool /* allow */)>;
|
|
|
|
explicit RequestCallbackWrapper(Callback callback)
|
|
|
|
: callback_(std::move(callback)),
|
2023-01-30 18:43:54 +01:00
|
|
|
work_thread_task_runner_(
|
|
|
|
base::SequencedTaskRunner::GetCurrentDefault()) {}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
RequestCallbackWrapper(const RequestCallbackWrapper&) = delete;
|
|
|
|
RequestCallbackWrapper& operator=(const RequestCallbackWrapper&) = delete;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
~RequestCallbackWrapper() override {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// Make sure it executes on the correct thread.
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(std::move(callback_), true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 11:09:45 +02:00
|
|
|
void Continue() override { ContinueNow(true); }
|
|
|
|
|
|
|
|
void Cancel() override { ContinueNow(false); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ContinueNow(bool allow) {
|
2019-04-24 04:50:25 +02:00
|
|
|
if (!work_thread_task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE,
|
2021-09-27 11:09:45 +02:00
|
|
|
base::BindOnce(&RequestCallbackWrapper::ContinueNow, this, allow));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
std::move(callback_).Run(allow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Callback callback_;
|
|
|
|
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> work_thread_task_runner_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestCallbackWrapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler {
|
|
|
|
public:
|
|
|
|
struct RequestState {
|
2024-01-20 23:48:57 +01:00
|
|
|
RequestState() = default;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
void Reset(CefRefPtr<CefResourceRequestHandler> handler,
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> scheme_factory,
|
|
|
|
CefRefPtr<CefRequestImpl> request,
|
2019-05-18 11:15:04 +02:00
|
|
|
bool request_was_redirected,
|
|
|
|
CancelRequestCallback cancel_callback) {
|
2019-04-24 04:50:25 +02:00
|
|
|
handler_ = handler;
|
|
|
|
scheme_factory_ = scheme_factory;
|
2019-04-26 19:02:47 +02:00
|
|
|
cookie_filter_ = nullptr;
|
2019-04-24 04:50:25 +02:00
|
|
|
pending_request_ = request;
|
|
|
|
pending_response_ = nullptr;
|
|
|
|
request_was_redirected_ = request_was_redirected;
|
2019-04-26 19:02:47 +02:00
|
|
|
was_custom_handled_ = false;
|
2019-05-18 11:15:04 +02:00
|
|
|
cancel_callback_ = std::move(cancel_callback);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefResourceRequestHandler> handler_;
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> scheme_factory_;
|
2019-04-26 19:02:47 +02:00
|
|
|
CefRefPtr<CefCookieAccessFilter> cookie_filter_;
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefRequestImpl> pending_request_;
|
|
|
|
CefRefPtr<CefResponseImpl> pending_response_;
|
|
|
|
bool request_was_redirected_ = false;
|
2019-04-26 19:02:47 +02:00
|
|
|
bool was_custom_handled_ = false;
|
2022-04-20 20:39:43 +02:00
|
|
|
bool accept_language_added_ = false;
|
2019-05-18 11:15:04 +02:00
|
|
|
CancelRequestCallback cancel_callback_;
|
2019-04-24 04:50:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PendingRequest {
|
2021-04-21 00:52:34 +02:00
|
|
|
PendingRequest(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)
|
2021-04-21 00:52:34 +02:00
|
|
|
: id_(request_id),
|
2019-04-24 04:50:25 +02:00
|
|
|
request_(request),
|
|
|
|
request_was_redirected_(request_was_redirected),
|
2019-05-18 11:15:04 +02:00
|
|
|
callback_(std::move(callback)),
|
|
|
|
cancel_callback_(std::move(cancel_callback)) {}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
~PendingRequest() {
|
|
|
|
if (cancel_callback_) {
|
|
|
|
std::move(cancel_callback_).Run(net::ERR_ABORTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
void Run(InterceptedRequestHandlerWrapper* self) {
|
|
|
|
self->OnBeforeRequest(id_, request_, request_was_redirected_,
|
2019-05-18 11:15:04 +02:00
|
|
|
std::move(callback_), std::move(cancel_callback_));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
const int32_t id_;
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<network::ResourceRequest> request_;
|
2019-04-24 04:50:25 +02:00
|
|
|
const bool request_was_redirected_;
|
|
|
|
OnBeforeRequestResultCallback callback_;
|
2019-05-18 11:15:04 +02:00
|
|
|
CancelRequestCallback cancel_callback_;
|
|
|
|
};
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Observer to receive notification of CEF context or associated browser
|
|
|
|
// destruction. Only one of the *Destroyed() methods will be called.
|
2020-09-18 00:24:08 +02:00
|
|
|
class DestructionObserver : public CefBrowserHostBase::Observer,
|
2019-05-31 11:50:12 +02:00
|
|
|
public CefContext::Observer {
|
2019-05-18 11:15:04 +02:00
|
|
|
public:
|
2020-09-18 00:24:08 +02:00
|
|
|
explicit DestructionObserver(CefBrowserHostBase* browser) {
|
2019-07-23 21:10:45 +02:00
|
|
|
if (browser) {
|
|
|
|
browser_info_ = browser->browser_info();
|
|
|
|
browser->AddObserver(this);
|
|
|
|
} else {
|
|
|
|
CefContext::Get()->AddObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
DestructionObserver(const DestructionObserver&) = delete;
|
|
|
|
DestructionObserver& operator=(const DestructionObserver&) = delete;
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
~DestructionObserver() override {
|
2019-07-23 21:10:45 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!registered_) {
|
2019-07-23 21:10:45 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-23 21:10:45 +02:00
|
|
|
|
|
|
|
// Verify that the browser or context still exists before attempting to
|
|
|
|
// remove the observer.
|
|
|
|
if (browser_info_) {
|
|
|
|
auto browser = browser_info_->browser();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser) {
|
2019-07-23 21:10:45 +02:00
|
|
|
browser->RemoveObserver(this);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-23 21:10:45 +02:00
|
|
|
} else if (CefContext::Get()) {
|
|
|
|
// Network requests may be torn down during shutdown, so we can't check
|
|
|
|
// CONTEXT_STATE_VALID() here.
|
|
|
|
CefContext::Get()->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 11:15:04 +02:00
|
|
|
|
|
|
|
void SetWrapper(base::WeakPtr<InterceptedRequestHandlerWrapper> wrapper) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
wrapper_ = wrapper;
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
void OnBrowserDestroyed(CefBrowserHostBase* browser) override {
|
2019-05-18 11:15:04 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
browser->RemoveObserver(this);
|
2019-07-23 21:10:45 +02:00
|
|
|
registered_ = false;
|
|
|
|
browser_info_ = nullptr;
|
2019-05-31 11:50:12 +02:00
|
|
|
NotifyOnDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnContextDestroyed() override {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
CefContext::Get()->RemoveObserver(this);
|
2019-07-23 21:10:45 +02:00
|
|
|
registered_ = false;
|
2019-05-31 11:50:12 +02:00
|
|
|
NotifyOnDestroyed();
|
2019-05-18 11:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-31 11:50:12 +02:00
|
|
|
void NotifyOnDestroyed() {
|
2019-07-23 21:10:45 +02:00
|
|
|
if (wrapper_.MaybeValid()) {
|
|
|
|
// This will be a no-op if the WeakPtr is invalid.
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_IOT,
|
|
|
|
base::BindOnce(&InterceptedRequestHandlerWrapper::OnDestroyed,
|
|
|
|
wrapper_));
|
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 21:10:45 +02:00
|
|
|
scoped_refptr<CefBrowserInfo> browser_info_;
|
|
|
|
bool registered_ = true;
|
|
|
|
|
2019-05-18 11:15:04 +02:00
|
|
|
base::WeakPtr<InterceptedRequestHandlerWrapper> wrapper_;
|
2019-04-24 04:50:25 +02:00
|
|
|
};
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
// Holds state information for InterceptedRequestHandlerWrapper. State is
|
|
|
|
// initialized on the UI thread and later passed to the *Wrapper object on
|
|
|
|
// the IO thread.
|
|
|
|
struct InitState {
|
2024-01-20 23:48:57 +01:00
|
|
|
InitState() = default;
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
~InitState() {
|
|
|
|
if (destruction_observer_) {
|
|
|
|
if (initialized_) {
|
|
|
|
// Clear the reference added in
|
|
|
|
// InterceptedRequestHandlerWrapper::SetInitialized().
|
|
|
|
destruction_observer_->SetWrapper(nullptr);
|
|
|
|
}
|
2019-07-23 21:10:45 +02:00
|
|
|
DeleteDestructionObserver();
|
2019-06-18 14:28:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
void Initialize(content::BrowserContext* browser_context,
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
2019-06-18 14:28:37 +02:00
|
|
|
CefRefPtr<CefFrame> frame,
|
2021-08-19 23:07:44 +02:00
|
|
|
const content::GlobalRenderFrameHostId& global_id,
|
2019-06-18 14:28:37 +02:00
|
|
|
bool is_navigation,
|
|
|
|
bool is_download,
|
2020-09-25 03:40:47 +02:00
|
|
|
const url::Origin& request_initiator,
|
2021-06-04 03:34:56 +02:00
|
|
|
const base::RepeatingClosure& unhandled_request_callback) {
|
2019-06-18 14:28:37 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2021-02-15 22:40:07 +01:00
|
|
|
auto profile = Profile::FromBrowserContext(browser_context);
|
2021-04-07 00:09:45 +02:00
|
|
|
auto cef_browser_context = CefBrowserContext::FromProfile(profile);
|
2022-01-10 22:11:16 +01:00
|
|
|
browser_context_getter_ = cef_browser_context->getter();
|
2020-07-01 02:57:00 +02:00
|
|
|
iothread_state_ = cef_browser_context->iothread_state();
|
2021-04-07 22:58:43 +02:00
|
|
|
CHECK(iothread_state_);
|
2020-09-25 03:40:47 +02:00
|
|
|
cookieable_schemes_ = cef_browser_context->GetCookieableSchemes();
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
// We register to be notified of CEF context or browser destruction so
|
|
|
|
// that we can stop accepting new requests and cancel pending/in-progress
|
|
|
|
// requests in a timely manner (e.g. before we start asserting about
|
|
|
|
// leaked objects during CEF shutdown).
|
2024-01-20 23:48:57 +01:00
|
|
|
destruction_observer_ =
|
|
|
|
std::make_unique<DestructionObserver>(browser.get());
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
if (browser) {
|
|
|
|
// These references will be released in OnDestroyed().
|
|
|
|
browser_ = browser;
|
|
|
|
frame_ = frame;
|
|
|
|
}
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
global_id_ = global_id;
|
2019-06-18 14:28:37 +02:00
|
|
|
is_navigation_ = is_navigation;
|
|
|
|
is_download_ = is_download;
|
|
|
|
request_initiator_ = request_initiator.Serialize();
|
2020-09-25 03:40:47 +02:00
|
|
|
unhandled_request_callback_ = unhandled_request_callback;
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
// Default values for standard headers.
|
2023-10-05 22:54:42 +02:00
|
|
|
accept_language_ = browser_prefs::GetAcceptLanguageList(profile);
|
2019-06-18 14:28:37 +02:00
|
|
|
DCHECK(!accept_language_.empty());
|
2020-06-28 23:05:36 +02:00
|
|
|
user_agent_ =
|
|
|
|
CefAppManager::Get()->GetContentClient()->browser()->GetUserAgent();
|
2019-06-18 14:28:37 +02:00
|
|
|
DCHECK(!user_agent_.empty());
|
2019-05-18 11:15:04 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 21:10:45 +02:00
|
|
|
void DeleteDestructionObserver() {
|
|
|
|
DCHECK(destruction_observer_);
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
|
|
|
base::BindOnce(&InitState::DeleteDestructionObserverOnUIThread,
|
|
|
|
std::move(destruction_observer_)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DeleteDestructionObserverOnUIThread(
|
|
|
|
std::unique_ptr<DestructionObserver> observer) {}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
// Only accessed on the UI thread.
|
2022-01-10 22:11:16 +01:00
|
|
|
CefBrowserContext::Getter browser_context_getter_;
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
bool initialized_ = false;
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browser_;
|
2019-06-18 14:28:37 +02:00
|
|
|
CefRefPtr<CefFrame> frame_;
|
2021-04-07 22:58:43 +02:00
|
|
|
scoped_refptr<CefIOThreadState> iothread_state_;
|
2020-09-04 21:08:55 +02:00
|
|
|
CefBrowserContext::CookieableSchemes cookieable_schemes_;
|
2021-08-19 23:07:44 +02:00
|
|
|
content::GlobalRenderFrameHostId global_id_;
|
2019-06-18 14:28:37 +02:00
|
|
|
bool is_navigation_ = true;
|
|
|
|
bool is_download_ = false;
|
|
|
|
CefString request_initiator_;
|
2021-06-04 03:34:56 +02:00
|
|
|
base::RepeatingClosure unhandled_request_callback_;
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
// Default values for standard headers.
|
|
|
|
std::string accept_language_;
|
|
|
|
std::string user_agent_;
|
|
|
|
|
2022-01-07 01:41:56 +01:00
|
|
|
// Used to route authentication and certificate callbacks through the
|
|
|
|
// associated StoragePartition instance.
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderNetworkServiceObserver>
|
|
|
|
url_loader_network_observer_;
|
|
|
|
bool did_try_create_url_loader_network_observer_ = false;
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
// Used to receive destruction notification.
|
|
|
|
std::unique_ptr<DestructionObserver> destruction_observer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Manages InterceptedRequestHandlerWrapper initialization. The *Wrapper
|
|
|
|
// object is owned by ProxyURLLoaderFactory and may be deleted before
|
|
|
|
// SetInitialized() is called.
|
|
|
|
struct InitHelper : base::RefCountedThreadSafe<InitHelper> {
|
|
|
|
public:
|
|
|
|
explicit InitHelper(InterceptedRequestHandlerWrapper* wrapper)
|
|
|
|
: wrapper_(wrapper) {}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
InitHelper(const InitHelper&) = delete;
|
|
|
|
InitHelper& operator=(const InitHelper&) = delete;
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
void MaybeSetInitialized(std::unique_ptr<InitState> init_state) {
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::BindOnce(&InitHelper::SetInitialized, this,
|
|
|
|
std::move(init_state)));
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
void Disconnect() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
wrapper_ = nullptr;
|
2019-05-18 11:15:04 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
private:
|
|
|
|
void SetInitialized(std::unique_ptr<InitState> init_state) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
// May be nullptr if the InterceptedRequestHandlerWrapper has already
|
|
|
|
// been deleted.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!wrapper_) {
|
2019-06-18 14:28:37 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-06-18 14:28:37 +02:00
|
|
|
wrapper_->SetInitialized(std::move(init_state));
|
|
|
|
wrapper_ = nullptr;
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
base::Lock lock_;
|
2024-05-11 17:48:38 +02:00
|
|
|
raw_ptr<InterceptedRequestHandlerWrapper> wrapper_;
|
2019-06-18 14:28:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
InterceptedRequestHandlerWrapper()
|
|
|
|
: init_helper_(base::MakeRefCounted<InitHelper>(this)),
|
|
|
|
weak_ptr_factory_(this) {}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
InterceptedRequestHandlerWrapper(const InterceptedRequestHandlerWrapper&) =
|
|
|
|
delete;
|
|
|
|
InterceptedRequestHandlerWrapper& operator=(
|
|
|
|
const InterceptedRequestHandlerWrapper&) = delete;
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
~InterceptedRequestHandlerWrapper() override {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
// There should be no in-progress requests during destruction.
|
|
|
|
DCHECK(request_map_.empty());
|
|
|
|
|
|
|
|
// Don't continue with initialization if we get deleted before
|
|
|
|
// SetInitialized is called asynchronously.
|
|
|
|
init_helper_->Disconnect();
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
scoped_refptr<InitHelper> init_helper() const { return init_helper_; }
|
|
|
|
|
|
|
|
void SetInitialized(std::unique_ptr<InitState> init_state) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
2019-06-18 14:28:37 +02:00
|
|
|
DCHECK(!init_state_);
|
|
|
|
init_state_ = std::move(init_state);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Check that the CEF context or associated browser was not destroyed
|
|
|
|
// between the calls to Initialize and SetInitialized, in which case
|
|
|
|
// we won't get an OnDestroyed callback from DestructionObserver.
|
2019-06-18 14:28:37 +02:00
|
|
|
if (init_state_->browser_) {
|
|
|
|
if (!init_state_->browser_->browser_info()->browser()) {
|
2019-05-31 11:50:12 +02:00
|
|
|
OnDestroyed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!CONTEXT_STATE_VALID()) {
|
|
|
|
OnDestroyed();
|
|
|
|
return;
|
2019-05-18 11:15:04 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->initialized_ = true;
|
|
|
|
init_state_->destruction_observer_->SetWrapper(
|
|
|
|
weak_ptr_factory_.GetWeakPtr());
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Continue any pending requests.
|
|
|
|
if (!pending_requests_.empty()) {
|
2023-01-02 23:59:03 +01:00
|
|
|
for (const auto& request : pending_requests_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
request->Run(this);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
pending_requests_.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 01:41:56 +01:00
|
|
|
static void TryCreateURLLoaderNetworkObserver(
|
|
|
|
std::unique_ptr<PendingRequest> pending_request,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2022-01-10 22:11:16 +01:00
|
|
|
const CefBrowserContext::Getter& browser_context_getter,
|
2022-01-07 01:41:56 +01:00
|
|
|
base::WeakPtr<InterceptedRequestHandlerWrapper> self) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderNetworkServiceObserver>
|
|
|
|
url_loader_network_observer;
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
// The request will be associated with this frame/browser if it's valid,
|
|
|
|
// otherwise the request will be canceled.
|
2022-08-09 00:00:16 +02:00
|
|
|
content::RenderFrameHostImpl* rfh =
|
|
|
|
static_cast<content::RenderFrameHostImpl*>(
|
|
|
|
static_cast<CefFrameHostImpl*>(frame.get())
|
|
|
|
->GetRenderFrameHost());
|
2022-01-07 01:41:56 +01:00
|
|
|
if (rfh) {
|
2022-08-09 00:00:16 +02:00
|
|
|
if (rfh->frame_tree_node() &&
|
|
|
|
rfh->frame_tree_node()->navigation_request()) {
|
|
|
|
// Associate the Observer with the current NavigationRequest. This is
|
|
|
|
// necessary for |is_main_frame_request| to report true (the expected
|
|
|
|
// value) in AllowCertificateError.
|
|
|
|
// TODO(cef): This approach for retrieving the NavigationRequest is
|
|
|
|
// deprecated, see https://crbug.com/1179502#c36.
|
|
|
|
url_loader_network_observer =
|
|
|
|
rfh->GetStoragePartition()
|
|
|
|
->CreateURLLoaderNetworkObserverForNavigationRequest(
|
|
|
|
*(rfh->frame_tree_node()->navigation_request()));
|
|
|
|
} else {
|
|
|
|
// Associate the Observer with the RenderFrameHost.
|
|
|
|
url_loader_network_observer = rfh->CreateURLLoaderNetworkObserver();
|
|
|
|
}
|
2022-01-07 01:41:56 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-10 22:11:16 +01:00
|
|
|
auto cef_browser_context = browser_context_getter.Run();
|
|
|
|
auto browser_context = cef_browser_context
|
|
|
|
? cef_browser_context->AsBrowserContext()
|
|
|
|
: nullptr;
|
|
|
|
if (browser_context) {
|
|
|
|
url_loader_network_observer =
|
|
|
|
static_cast<content::StoragePartitionImpl*>(
|
|
|
|
browser_context->GetDefaultStoragePartition())
|
2024-01-26 03:12:43 +01:00
|
|
|
->CreateAuthCertObserverForServiceWorker(
|
|
|
|
content::ChildProcessHost::kInvalidUniqueID);
|
2022-01-10 22:11:16 +01:00
|
|
|
}
|
2022-01-07 01:41:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::BindOnce(&InterceptedRequestHandlerWrapper::
|
|
|
|
ContinueCreateURLLoaderNetworkObserver,
|
|
|
|
self, std::move(pending_request),
|
|
|
|
std::move(url_loader_network_observer)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContinueCreateURLLoaderNetworkObserver(
|
|
|
|
std::unique_ptr<PendingRequest> pending_request,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderNetworkServiceObserver>
|
|
|
|
url_loader_network_observer) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
DCHECK(!init_state_->did_try_create_url_loader_network_observer_);
|
|
|
|
init_state_->did_try_create_url_loader_network_observer_ = true;
|
|
|
|
init_state_->url_loader_network_observer_ =
|
|
|
|
std::move(url_loader_network_observer);
|
|
|
|
pending_request->Run(this);
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// InterceptedRequestHandler methods:
|
2021-04-21 00:52:34 +02:00
|
|
|
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) override {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
if (shutting_down_) {
|
|
|
|
// Abort immediately.
|
|
|
|
std::move(cancel_callback).Run(net::ERR_ABORTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
if (!init_state_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Queue requests until we're initialized.
|
|
|
|
pending_requests_.push_back(std::make_unique<PendingRequest>(
|
2021-04-21 00:52:34 +02:00
|
|
|
request_id, request, request_was_redirected, std::move(callback),
|
2019-05-18 11:15:04 +02:00
|
|
|
std::move(cancel_callback)));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-07 01:41:56 +01:00
|
|
|
if (request->trusted_params &&
|
|
|
|
!request->trusted_params->url_loader_network_observer &&
|
|
|
|
!init_state_->did_try_create_url_loader_network_observer_) {
|
|
|
|
// Restarted/redirected requests won't already have an observer, so we
|
|
|
|
// need to create one.
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
|
|
|
base::BindOnce(&InterceptedRequestHandlerWrapper::
|
|
|
|
TryCreateURLLoaderNetworkObserver,
|
|
|
|
std::make_unique<PendingRequest>(
|
|
|
|
request_id, request, request_was_redirected,
|
|
|
|
std::move(callback), std::move(cancel_callback)),
|
2022-01-10 22:11:16 +01:00
|
|
|
init_state_->frame_,
|
|
|
|
init_state_->browser_context_getter_,
|
2022-01-07 01:41:56 +01:00
|
|
|
weak_ptr_factory_.GetWeakPtr()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// State may already exist for restarted requests.
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetOrCreateState(request_id);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2022-01-07 01:41:56 +01:00
|
|
|
if (init_state_->did_try_create_url_loader_network_observer_) {
|
|
|
|
if (init_state_->url_loader_network_observer_) {
|
|
|
|
request->trusted_params->url_loader_network_observer =
|
|
|
|
std::move(init_state_->url_loader_network_observer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset state so that the observer will be recreated on the next
|
|
|
|
// restart/redirect.
|
|
|
|
init_state_->did_try_create_url_loader_network_observer_ = false;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Add standard headers, if currently unspecified.
|
2022-04-20 20:39:43 +02:00
|
|
|
if (!request->headers.HasHeader(net::HttpRequestHeaders::kAcceptLanguage)) {
|
|
|
|
request->headers.SetHeaderIfMissing(
|
|
|
|
net::HttpRequestHeaders::kAcceptLanguage,
|
|
|
|
init_state_->accept_language_);
|
|
|
|
state->accept_language_added_ = true;
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
request->headers.SetHeaderIfMissing(net::HttpRequestHeaders::kUserAgent,
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->user_agent_);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-25 16:28:03 +02:00
|
|
|
const bool is_external = IsExternalRequest(request);
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// External requests will not have a default handler.
|
2019-06-25 16:28:03 +02:00
|
|
|
bool intercept_only = is_external;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefRequestImpl> requestPtr;
|
|
|
|
CefRefPtr<CefResourceRequestHandler> handler =
|
2021-04-21 00:52:34 +02:00
|
|
|
GetHandler(request_id, request, &intercept_only, requestPtr);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> scheme_factory =
|
2020-07-01 02:57:00 +02:00
|
|
|
init_state_->iothread_state_->GetSchemeHandlerFactory(request->url);
|
2019-04-26 20:22:42 +02:00
|
|
|
if (scheme_factory && !requestPtr) {
|
2021-04-21 00:52:34 +02:00
|
|
|
requestPtr = MakeRequest(request, request_id, true);
|
2019-04-26 20:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// True if there's a possibility that the client might handle the request.
|
|
|
|
const bool maybe_intercept_request = handler || scheme_factory;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!maybe_intercept_request && requestPtr) {
|
2019-04-26 20:22:42 +02:00
|
|
|
requestPtr = nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// May have a handler and/or scheme factory.
|
2019-05-18 11:15:04 +02:00
|
|
|
state->Reset(handler, scheme_factory, requestPtr, request_was_redirected,
|
|
|
|
std::move(cancel_callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
if (handler) {
|
2019-06-18 14:28:37 +02:00
|
|
|
state->cookie_filter_ = handler->GetCookieAccessFilter(
|
|
|
|
init_state_->browser_, init_state_->frame_, requestPtr.get());
|
2019-04-26 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 20:22:42 +02:00
|
|
|
auto exec_callback =
|
|
|
|
base::BindOnce(std::move(callback), maybe_intercept_request,
|
2019-06-25 16:28:03 +02:00
|
|
|
is_external ? true : intercept_only);
|
2019-04-26 20:22:42 +02:00
|
|
|
|
|
|
|
if (!maybe_intercept_request) {
|
|
|
|
// Cookies will be handled by the NetworkService.
|
|
|
|
std::move(exec_callback).Run();
|
|
|
|
return;
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
MaybeLoadCookies(request_id, state, request, std::move(exec_callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void MaybeLoadCookies(int32_t request_id,
|
2019-05-18 11:15:04 +02:00
|
|
|
RequestState* state,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
base::OnceClosure callback) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2020-09-04 21:08:55 +02:00
|
|
|
if (!cookie_helper::IsCookieableScheme(request->url,
|
|
|
|
init_state_->cookieable_schemes_)) {
|
|
|
|
// The scheme does not support cookies.
|
|
|
|
std::move(callback).Run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
// We need to load/save cookies ourselves for custom-handled requests, or
|
|
|
|
// if we're using a cookie filter.
|
2019-04-24 04:50:25 +02:00
|
|
|
auto allow_cookie_callback =
|
2019-05-18 11:15:04 +02:00
|
|
|
state->cookie_filter_
|
|
|
|
? base::BindRepeating(
|
|
|
|
&InterceptedRequestHandlerWrapper::AllowCookieLoad,
|
2021-04-21 00:52:34 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(), request_id)
|
2019-05-18 11:15:04 +02:00
|
|
|
: base::BindRepeating(
|
|
|
|
&InterceptedRequestHandlerWrapper::AllowCookieAlways);
|
2019-04-24 04:50:25 +02:00
|
|
|
auto done_cookie_callback = base::BindOnce(
|
|
|
|
&InterceptedRequestHandlerWrapper::ContinueWithLoadedCookies,
|
2021-04-21 00:52:34 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(), request_id, request,
|
|
|
|
std::move(callback));
|
2022-01-10 22:11:16 +01:00
|
|
|
cookie_helper::LoadCookies(init_state_->browser_context_getter_, *request,
|
2020-09-04 21:08:55 +02:00
|
|
|
allow_cookie_callback,
|
|
|
|
std::move(done_cookie_callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-05-18 11:15:04 +02:00
|
|
|
static void AllowCookieAlways(const net::CanonicalCookie& cookie,
|
|
|
|
bool* allow) {
|
|
|
|
*allow = true;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void AllowCookieLoad(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const net::CanonicalCookie& cookie,
|
|
|
|
bool* allow) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-18 11:15:04 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled while the async callback was
|
|
|
|
// pending.
|
2019-04-26 19:02:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:15:04 +02:00
|
|
|
DCHECK(state->cookie_filter_);
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
CefCookie cef_cookie;
|
|
|
|
if (net_service::MakeCefCookie(cookie, cef_cookie)) {
|
2019-04-26 19:02:47 +02:00
|
|
|
*allow = state->cookie_filter_->CanSendCookie(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), cef_cookie);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void ContinueWithLoadedCookies(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
base::OnceClosure callback,
|
|
|
|
int total_count,
|
|
|
|
net::CookieList allowed_cookies) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-18 11:15:04 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled while the async callback was
|
|
|
|
// pending.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
if (state->cookie_filter_) {
|
|
|
|
// Also add/save cookies ourselves for default-handled network requests
|
|
|
|
// so that we can filter them. This will be a no-op for custom-handled
|
|
|
|
// requests.
|
2019-06-12 17:14:20 +02:00
|
|
|
request->load_flags |= kLoadNoCookiesFlags;
|
2019-04-26 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!allowed_cookies.empty()) {
|
|
|
|
const std::string& cookie_line =
|
|
|
|
net::CanonicalCookie::BuildCookieLine(allowed_cookies);
|
|
|
|
request->headers.SetHeader(net::HttpRequestHeaders::kCookie, cookie_line);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
state->pending_request_->SetReadOnly(false);
|
|
|
|
state->pending_request_->SetHeaderByName(net::HttpRequestHeaders::kCookie,
|
|
|
|
cookie_line, true);
|
|
|
|
state->pending_request_->SetReadOnly(true);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::move(callback).Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
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) override {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-31 11:50:12 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled during destruction.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Must have a handler and/or scheme factory.
|
|
|
|
DCHECK(state->handler_ || state->scheme_factory_);
|
|
|
|
DCHECK(state->pending_request_);
|
|
|
|
|
|
|
|
if (state->handler_) {
|
|
|
|
// The client may modify |pending_request_| before executing the callback.
|
|
|
|
state->pending_request_->SetReadOnly(false);
|
|
|
|
state->pending_request_->SetTrackChanges(true,
|
|
|
|
true /* backup_on_change */);
|
|
|
|
|
|
|
|
CefRefPtr<RequestCallbackWrapper> callbackPtr =
|
|
|
|
new RequestCallbackWrapper(base::BindOnce(
|
|
|
|
&InterceptedRequestHandlerWrapper::ContinueShouldInterceptRequest,
|
2021-04-21 00:52:34 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(), request_id,
|
|
|
|
base::Unretained(request), std::move(callback)));
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
cef_return_value_t retval = state->handler_->OnBeforeResourceLoad(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), callbackPtr.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
if (retval != RV_CONTINUE_ASYNC) {
|
2021-09-27 11:09:45 +02:00
|
|
|
if (retval == RV_CONTINUE) {
|
|
|
|
// Continue the request immediately.
|
|
|
|
callbackPtr->Continue();
|
|
|
|
} else {
|
|
|
|
// Cancel the request immediately.
|
|
|
|
callbackPtr->Cancel();
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The scheme factory may choose to handle it.
|
2021-04-21 00:52:34 +02:00
|
|
|
ContinueShouldInterceptRequest(request_id, request, std::move(callback),
|
|
|
|
true);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContinueShouldInterceptRequest(
|
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,
|
|
|
|
bool allow) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-18 11:15:04 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled while the async callback was
|
|
|
|
// pending.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Must have a handler and/or scheme factory.
|
|
|
|
DCHECK(state->handler_ || state->scheme_factory_);
|
|
|
|
DCHECK(state->pending_request_);
|
|
|
|
|
|
|
|
if (state->handler_) {
|
|
|
|
if (allow) {
|
|
|
|
// Apply any |requestPtr| changes to |request|.
|
|
|
|
state->pending_request_->Get(request, true /* changed_only */);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool redirect =
|
|
|
|
(state->pending_request_->GetChanges() & CefRequestImpl::kChangedUrl);
|
|
|
|
if (redirect) {
|
|
|
|
// Revert any changes for now. We'll get them back after the redirect.
|
|
|
|
state->pending_request_->RevertChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
state->pending_request_->SetReadOnly(true);
|
|
|
|
state->pending_request_->SetTrackChanges(false);
|
|
|
|
|
|
|
|
if (!allow) {
|
|
|
|
// Cancel the request.
|
2019-05-29 18:41:05 +02:00
|
|
|
if (state->cancel_callback_) {
|
|
|
|
std::move(state->cancel_callback_).Run(net::ERR_ABORTED);
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redirect) {
|
|
|
|
// Performing a redirect.
|
2019-05-18 11:15:04 +02:00
|
|
|
std::move(callback).Run(nullptr);
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefResourceHandler> resource_handler;
|
|
|
|
|
|
|
|
if (state->handler_) {
|
|
|
|
// Does the client want to handle the request?
|
|
|
|
resource_handler = state->handler_->GetResourceHandler(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
if (!resource_handler && state->scheme_factory_) {
|
|
|
|
// Does the scheme factory want to handle the request?
|
2019-05-31 11:50:12 +02:00
|
|
|
resource_handler = state->scheme_factory_->Create(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_, request->url.scheme(),
|
2019-05-31 11:50:12 +02:00
|
|
|
state->pending_request_.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ResourceResponse> resource_response;
|
|
|
|
if (resource_handler) {
|
2021-04-21 00:52:34 +02:00
|
|
|
resource_response = CreateResourceResponse(request_id, resource_handler);
|
2019-04-26 19:02:47 +02:00
|
|
|
DCHECK(resource_response);
|
|
|
|
state->was_custom_handled_ = true;
|
2022-04-20 20:39:43 +02:00
|
|
|
} else if (state->accept_language_added_) {
|
2019-11-05 21:29:32 +01:00
|
|
|
// The request will be handled by the NetworkService. Remove the
|
|
|
|
// "Accept-Language" header here so that it can be re-added in
|
|
|
|
// URLRequestHttpJob::AddExtraHeaders with correct ordering applied.
|
|
|
|
request->headers.RemoveHeader(net::HttpRequestHeaders::kAcceptLanguage);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Continue the request.
|
2019-05-18 11:15:04 +02:00
|
|
|
std::move(callback).Run(std::move(resource_response));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
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) override {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-31 11:50:12 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled during destruction.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!state->handler_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!state->pending_response_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
state->pending_response_ = new CefResponseImpl();
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2019-04-24 04:50:25 +02:00
|
|
|
state->pending_response_->SetReadOnly(false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (headers) {
|
2020-03-04 01:29:39 +01:00
|
|
|
state->pending_response_->SetResponseHeaders(*headers);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
state->pending_response_->SetReadOnly(true);
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void OnRequestResponse(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,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<net::RedirectInfo> redirect_info,
|
2019-04-24 04:50:25 +02:00
|
|
|
OnRequestResponseResultCallback callback) override {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-31 11:50:12 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled during destruction.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:50:30 +02:00
|
|
|
if (state->cookie_filter_) {
|
|
|
|
// Remove the flags that were added in ContinueWithLoadedCookies.
|
|
|
|
request->load_flags &= ~kLoadNoCookiesFlags;
|
|
|
|
}
|
|
|
|
|
2019-04-26 20:22:42 +02:00
|
|
|
if (!state->handler_) {
|
2019-05-03 00:23:57 +02:00
|
|
|
// Cookies may come from a scheme handler.
|
|
|
|
MaybeSaveCookies(
|
2021-04-21 00:52:34 +02:00
|
|
|
request_id, state, request, headers,
|
2019-05-03 00:23:57 +02:00
|
|
|
base::BindOnce(
|
|
|
|
std::move(callback), ResponseMode::CONTINUE, nullptr,
|
|
|
|
redirect_info.has_value() ? redirect_info->new_url : GURL()));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
2019-04-26 20:22:42 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
DCHECK(state->pending_request_);
|
|
|
|
DCHECK(state->pending_response_);
|
|
|
|
|
|
|
|
if (redirect_info.has_value()) {
|
2021-04-21 00:52:34 +02:00
|
|
|
HandleRedirect(request_id, state, request, headers, *redirect_info,
|
2019-05-18 11:15:04 +02:00
|
|
|
std::move(callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
} else {
|
2021-04-21 00:52:34 +02:00
|
|
|
HandleResponse(request_id, state, request, headers, std::move(callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void HandleRedirect(int32_t request_id,
|
2019-05-18 11:15:04 +02:00
|
|
|
RequestState* state,
|
2019-04-26 19:02:47 +02:00
|
|
|
network::ResourceRequest* request,
|
2020-03-04 01:29:39 +01:00
|
|
|
net::HttpResponseHeaders* headers,
|
2019-04-24 04:50:25 +02:00
|
|
|
const net::RedirectInfo& redirect_info,
|
|
|
|
OnRequestResponseResultCallback callback) {
|
|
|
|
GURL new_url = redirect_info.new_url;
|
|
|
|
CefString newUrl = redirect_info.new_url.spec();
|
|
|
|
CefString oldUrl = newUrl;
|
|
|
|
bool url_changed = false;
|
2019-06-18 14:28:37 +02:00
|
|
|
state->handler_->OnResourceRedirect(
|
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), state->pending_response_.get(), newUrl);
|
2019-04-24 04:50:25 +02:00
|
|
|
if (newUrl != oldUrl) {
|
|
|
|
// Also support relative URLs.
|
|
|
|
const GURL& url = redirect_info.new_url.Resolve(newUrl.ToString());
|
|
|
|
if (url.is_valid()) {
|
|
|
|
url_changed = true;
|
|
|
|
new_url = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the |pending_request_| object with the new info.
|
|
|
|
state->pending_request_->SetReadOnly(false);
|
|
|
|
state->pending_request_->Set(redirect_info);
|
|
|
|
if (url_changed) {
|
|
|
|
state->pending_request_->SetURL(new_url.spec());
|
|
|
|
}
|
|
|
|
state->pending_request_->SetReadOnly(true);
|
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
auto exec_callback = base::BindOnce(
|
|
|
|
std::move(callback), ResponseMode::CONTINUE, nullptr, new_url);
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
MaybeSaveCookies(request_id, state, request, headers,
|
|
|
|
std::move(exec_callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void HandleResponse(int32_t request_id,
|
2019-05-18 11:15:04 +02:00
|
|
|
RequestState* state,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
2020-03-04 01:29:39 +01:00
|
|
|
net::HttpResponseHeaders* headers,
|
2019-04-24 04:50:25 +02:00
|
|
|
OnRequestResponseResultCallback callback) {
|
|
|
|
// The client may modify |pending_request_| in OnResourceResponse.
|
|
|
|
state->pending_request_->SetReadOnly(false);
|
|
|
|
state->pending_request_->SetTrackChanges(true, true /* backup_on_change */);
|
|
|
|
|
|
|
|
auto response_mode = ResponseMode::CONTINUE;
|
|
|
|
GURL new_url;
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
if (state->handler_->OnResourceResponse(
|
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), state->pending_response_.get())) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// The request may have been modified.
|
|
|
|
const auto changes = state->pending_request_->GetChanges();
|
|
|
|
if (changes) {
|
|
|
|
state->pending_request_->Get(request, true /* changed_only */);
|
|
|
|
|
|
|
|
if (changes & CefRequestImpl::kChangedUrl) {
|
|
|
|
// Redirect to the new URL.
|
|
|
|
new_url = GURL(state->pending_request_->GetURL().ToString());
|
|
|
|
} else {
|
|
|
|
// Restart the request.
|
|
|
|
response_mode = ResponseMode::RESTART;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert any changes for now. We'll get them back after the redirect or
|
|
|
|
// restart.
|
|
|
|
state->pending_request_->RevertChanges();
|
|
|
|
|
|
|
|
state->pending_request_->SetReadOnly(true);
|
|
|
|
state->pending_request_->SetTrackChanges(false);
|
|
|
|
|
|
|
|
auto exec_callback =
|
|
|
|
base::BindOnce(std::move(callback), response_mode, nullptr, new_url);
|
|
|
|
|
|
|
|
if (response_mode == ResponseMode::RESTART) {
|
2019-04-26 19:02:47 +02:00
|
|
|
// Get any cookies after the restart.
|
2019-04-24 04:50:25 +02:00
|
|
|
std::move(exec_callback).Run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
MaybeSaveCookies(request_id, state, request, headers,
|
|
|
|
std::move(exec_callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void MaybeSaveCookies(int32_t request_id,
|
2019-05-18 11:15:04 +02:00
|
|
|
RequestState* state,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
2020-03-04 01:29:39 +01:00
|
|
|
net::HttpResponseHeaders* headers,
|
2019-04-24 04:50:25 +02:00
|
|
|
base::OnceClosure callback) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
if (!state->cookie_filter_ && !state->was_custom_handled_) {
|
|
|
|
// The NetworkService saves the cookies for default-handled requests.
|
|
|
|
std::move(callback).Run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-04 21:08:55 +02:00
|
|
|
if (!cookie_helper::IsCookieableScheme(request->url,
|
|
|
|
init_state_->cookieable_schemes_)) {
|
|
|
|
// The scheme does not support cookies.
|
|
|
|
std::move(callback).Run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-26 19:02:47 +02:00
|
|
|
// We need to load/save cookies ourselves for custom-handled requests, or
|
|
|
|
// if we're using a cookie filter.
|
2019-04-24 04:50:25 +02:00
|
|
|
auto allow_cookie_callback =
|
2019-05-18 11:15:04 +02:00
|
|
|
state->cookie_filter_
|
|
|
|
? base::BindRepeating(
|
|
|
|
&InterceptedRequestHandlerWrapper::AllowCookieSave,
|
2021-04-21 00:52:34 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(), request_id)
|
2019-05-18 11:15:04 +02:00
|
|
|
: base::BindRepeating(
|
|
|
|
&InterceptedRequestHandlerWrapper::AllowCookieAlways);
|
2019-04-24 04:50:25 +02:00
|
|
|
auto done_cookie_callback = base::BindOnce(
|
|
|
|
&InterceptedRequestHandlerWrapper::ContinueWithSavedCookies,
|
2021-04-21 00:52:34 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(), request_id, std::move(callback));
|
2022-01-10 22:11:16 +01:00
|
|
|
cookie_helper::SaveCookies(init_state_->browser_context_getter_, *request,
|
|
|
|
headers, allow_cookie_callback,
|
2020-09-04 21:08:55 +02:00
|
|
|
std::move(done_cookie_callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void AllowCookieSave(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const net::CanonicalCookie& cookie,
|
|
|
|
bool* allow) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-18 11:15:04 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled while the async callback was
|
|
|
|
// pending.
|
2019-04-26 19:02:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:15:04 +02:00
|
|
|
DCHECK(state->cookie_filter_);
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
CefCookie cef_cookie;
|
|
|
|
if (net_service::MakeCefCookie(cookie, cef_cookie)) {
|
2019-04-26 19:02:47 +02:00
|
|
|
*allow = state->cookie_filter_->CanSaveCookie(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), state->pending_response_.get(),
|
|
|
|
cef_cookie);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void ContinueWithSavedCookies(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
base::OnceClosure callback,
|
|
|
|
int total_count,
|
|
|
|
net::CookieList allowed_cookies) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
std::move(callback).Run();
|
|
|
|
}
|
|
|
|
|
2019-05-19 21:21:20 +02:00
|
|
|
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) override {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-31 11:50:12 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled during destruction.
|
|
|
|
return body;
|
|
|
|
}
|
2019-05-19 21:21:20 +02:00
|
|
|
|
|
|
|
if (state->handler_) {
|
|
|
|
auto filter = state->handler_->GetResourceResponseFilter(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), state->pending_response_.get());
|
2019-05-19 21:21:20 +02:00
|
|
|
if (filter) {
|
|
|
|
return CreateResponseFilterHandler(
|
|
|
|
filter, std::move(body),
|
|
|
|
base::BindOnce(&InterceptedRequestHandlerWrapper::OnFilterError,
|
2021-04-21 00:52:34 +02:00
|
|
|
weak_ptr_factory_.GetWeakPtr(), request_id));
|
2019-05-19 21:21:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void OnFilterError(int32_t request_id) {
|
2019-05-19 21:21:20 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-19 21:21:20 +02:00
|
|
|
if (!state) {
|
|
|
|
// The request may have been canceled while the async callback was
|
|
|
|
// pending.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:41:05 +02:00
|
|
|
if (state->cancel_callback_) {
|
|
|
|
std::move(state->cancel_callback_).Run(net::ERR_CONTENT_DECODING_FAILED);
|
|
|
|
}
|
2019-05-19 21:21:20 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
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) override {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* state = GetState(request_id);
|
2019-05-29 18:41:05 +02:00
|
|
|
if (!state) {
|
2019-06-19 15:45:23 +02:00
|
|
|
// The request may have been aborted during initialization or canceled
|
|
|
|
// during destruction. This method will always be called before a request
|
|
|
|
// is deleted, so if the request is currently pending also remove it from
|
|
|
|
// the list.
|
|
|
|
if (!pending_requests_.empty()) {
|
|
|
|
PendingRequests::iterator it = pending_requests_.begin();
|
|
|
|
for (; it != pending_requests_.end(); ++it) {
|
2021-04-21 00:52:34 +02:00
|
|
|
if ((*it)->id_ == request_id) {
|
2019-06-19 15:45:23 +02:00
|
|
|
pending_requests_.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 18:41:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-25 16:28:03 +02:00
|
|
|
const bool is_external = IsExternalRequest(&request);
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Redirection of standard custom schemes is handled with a restart, so we
|
|
|
|
// get completion notifications for both the original (redirected) request
|
|
|
|
// and the final request. Don't report completion of the redirected request.
|
2019-06-25 16:28:03 +02:00
|
|
|
const bool ignore_result = is_external && request.url.IsStandard() &&
|
2019-04-24 04:50:25 +02:00
|
|
|
status.error_code == net::ERR_ABORTED &&
|
|
|
|
state->pending_response_.get() &&
|
|
|
|
net::HttpResponseHeaders::IsRedirectResponseCode(
|
|
|
|
state->pending_response_->GetStatus());
|
|
|
|
|
|
|
|
if (state->handler_ && !ignore_result) {
|
|
|
|
DCHECK(state->pending_request_);
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
CallHandlerOnComplete(state, status);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
if (status.error_code != 0 && status.error_code != ERR_ABORTED &&
|
|
|
|
is_external) {
|
2019-04-24 04:50:25 +02:00
|
|
|
bool allow_os_execution = false;
|
2019-06-18 14:28:37 +02:00
|
|
|
state->handler_->OnProtocolExecution(
|
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), allow_os_execution);
|
2020-09-25 03:40:47 +02:00
|
|
|
if (allow_os_execution && init_state_->unhandled_request_callback_) {
|
|
|
|
init_state_->unhandled_request_callback_.Run();
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RemoveState(request_id);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-31 11:50:12 +02:00
|
|
|
void CallHandlerOnComplete(RequestState* state,
|
|
|
|
const network::URLLoaderCompletionStatus& status) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!state->handler_ || !state->pending_request_) {
|
2019-05-31 11:50:12 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
|
|
|
// The request object may be currently flagged as writable in cases where we
|
|
|
|
// abort a request that is waiting on a pending callack.
|
|
|
|
if (!state->pending_request_->IsReadOnly()) {
|
|
|
|
state->pending_request_->SetReadOnly(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state->pending_response_) {
|
|
|
|
// If the request failed there may not be a response object yet.
|
|
|
|
state->pending_response_ = new CefResponseImpl();
|
|
|
|
} else {
|
|
|
|
state->pending_response_->SetReadOnly(false);
|
|
|
|
}
|
|
|
|
state->pending_response_->SetError(
|
|
|
|
static_cast<cef_errorcode_t>(status.error_code));
|
|
|
|
state->pending_response_->SetReadOnly(true);
|
|
|
|
|
|
|
|
state->handler_->OnResourceLoadComplete(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_,
|
|
|
|
state->pending_request_.get(), state->pending_response_.get(),
|
2019-05-31 11:50:12 +02:00
|
|
|
status.error_code == 0 ? UR_SUCCESS : UR_FAILED,
|
|
|
|
status.encoded_body_length);
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Returns the handler, if any, that should be used for this request.
|
|
|
|
CefRefPtr<CefResourceRequestHandler> GetHandler(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
bool* intercept_only,
|
|
|
|
CefRefPtr<CefRequestImpl>& requestPtr) const {
|
|
|
|
CefRefPtr<CefResourceRequestHandler> handler;
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
if (init_state_->browser_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Maybe the browser's client wants to handle it?
|
2019-06-18 14:28:37 +02:00
|
|
|
CefRefPtr<CefClient> client =
|
|
|
|
init_state_->browser_->GetHost()->GetClient();
|
2019-04-24 04:50:25 +02:00
|
|
|
if (client) {
|
|
|
|
CefRefPtr<CefRequestHandler> request_handler =
|
|
|
|
client->GetRequestHandler();
|
|
|
|
if (request_handler) {
|
|
|
|
requestPtr = MakeRequest(request, request_id, true);
|
|
|
|
|
|
|
|
handler = request_handler->GetResourceRequestHandler(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_, requestPtr.get(),
|
|
|
|
init_state_->is_navigation_, init_state_->is_download_,
|
|
|
|
init_state_->request_initiator_, *intercept_only);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handler) {
|
|
|
|
// Maybe the request context wants to handle it?
|
|
|
|
CefRefPtr<CefRequestContextHandler> context_handler =
|
2020-07-01 02:57:00 +02:00
|
|
|
init_state_->iothread_state_->GetHandler(
|
2021-08-19 23:07:44 +02:00
|
|
|
init_state_->global_id_, /*require_frame_match=*/false);
|
2019-04-24 04:50:25 +02:00
|
|
|
if (context_handler) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!requestPtr) {
|
2019-04-24 04:50:25 +02:00
|
|
|
requestPtr = MakeRequest(request, request_id, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
handler = context_handler->GetResourceRequestHandler(
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_, init_state_->frame_, requestPtr.get(),
|
|
|
|
init_state_->is_navigation_, init_state_->is_download_,
|
|
|
|
init_state_->request_initiator_, *intercept_only);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* GetOrCreateState(int32_t request_id) {
|
|
|
|
RequestState* state = GetState(request_id);
|
2019-04-24 04:50:25 +02:00
|
|
|
if (!state) {
|
|
|
|
state = new RequestState();
|
2021-04-21 00:52:34 +02:00
|
|
|
request_map_.insert(std::make_pair(request_id, base::WrapUnique(state)));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
RequestState* GetState(int32_t request_id) const {
|
|
|
|
RequestMap::const_iterator it = request_map_.find(request_id);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != request_map_.end()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return it->second.get();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void RemoveState(int32_t request_id) {
|
|
|
|
RequestMap::iterator it = request_map_.find(request_id);
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK(it != request_map_.end());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != request_map_.end()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
request_map_.erase(it);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Stop accepting new requests and cancel pending/in-flight requests when the
|
|
|
|
// CEF context or associated browser is destroyed.
|
|
|
|
void OnDestroyed() {
|
2019-05-18 11:15:04 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
2019-06-18 14:28:37 +02:00
|
|
|
DCHECK(init_state_);
|
2019-05-18 11:15:04 +02:00
|
|
|
|
2019-07-23 21:10:45 +02:00
|
|
|
init_state_->DeleteDestructionObserver();
|
2019-05-31 11:50:12 +02:00
|
|
|
|
|
|
|
// Stop accepting new requests.
|
|
|
|
shutting_down_ = true;
|
|
|
|
|
|
|
|
// Stop the delivery of pending callbacks.
|
|
|
|
weak_ptr_factory_.InvalidateWeakPtrs();
|
|
|
|
|
|
|
|
// Take ownership of any pending requests.
|
|
|
|
PendingRequests pending_requests;
|
|
|
|
pending_requests.swap(pending_requests_);
|
|
|
|
|
|
|
|
// Take ownership of any in-progress requests.
|
|
|
|
RequestMap request_map;
|
|
|
|
request_map.swap(request_map_);
|
|
|
|
|
|
|
|
// Notify handlers for in-progress requests.
|
|
|
|
for (const auto& pair : request_map) {
|
|
|
|
CallHandlerOnComplete(
|
|
|
|
pair.second.get(),
|
|
|
|
network::URLLoaderCompletionStatus(net::ERR_ABORTED));
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
if (init_state_->browser_) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// Clear objects that reference the browser.
|
2019-06-18 14:28:37 +02:00
|
|
|
init_state_->browser_ = nullptr;
|
|
|
|
init_state_->frame_ = nullptr;
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute cancel callbacks and delete pending and in-progress requests.
|
|
|
|
// This may result in the request being torn down sooner, or it may be
|
|
|
|
// ignored if the request is already in the process of being torn down. When
|
|
|
|
// the last callback is executed it may result in |this| being deleted.
|
|
|
|
pending_requests.clear();
|
|
|
|
|
|
|
|
for (auto& pair : request_map) {
|
|
|
|
auto state = std::move(pair.second);
|
|
|
|
if (state->cancel_callback_) {
|
|
|
|
std::move(state->cancel_callback_).Run(net::ERR_ABORTED);
|
2019-05-29 18:41:05 +02:00
|
|
|
}
|
2019-05-18 11:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
static CefRefPtr<CefRequestImpl> MakeRequest(
|
|
|
|
const network::ResourceRequest* request,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
bool read_only) {
|
|
|
|
CefRefPtr<CefRequestImpl> requestPtr = new CefRequestImpl();
|
|
|
|
requestPtr->Set(request, request_id);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (read_only) {
|
2019-04-24 04:50:25 +02:00
|
|
|
requestPtr->SetReadOnly(true);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2019-04-24 04:50:25 +02:00
|
|
|
requestPtr->SetTrackChanges(true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
return requestPtr;
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:28:03 +02:00
|
|
|
// Returns true if |request| cannot be handled internally.
|
|
|
|
static bool IsExternalRequest(const network::ResourceRequest* request) {
|
|
|
|
return !scheme::IsInternalHandledScheme(request->url.scheme());
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
scoped_refptr<InitHelper> init_helper_;
|
|
|
|
std::unique_ptr<InitState> init_state_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
bool shutting_down_ = false;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
using RequestMap = std::map<int32_t, std::unique_ptr<RequestState>>;
|
2019-04-24 04:50:25 +02:00
|
|
|
RequestMap request_map_;
|
|
|
|
|
|
|
|
using PendingRequests = std::vector<std::unique_ptr<PendingRequest>>;
|
|
|
|
PendingRequests pending_requests_;
|
|
|
|
|
|
|
|
base::WeakPtrFactory<InterceptedRequestHandlerWrapper> weak_ptr_factory_;
|
|
|
|
};
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<InterceptedRequestHandler> CreateInterceptedRequestHandler(
|
|
|
|
content::BrowserContext* browser_context,
|
|
|
|
content::RenderFrameHost* frame,
|
|
|
|
int render_process_id,
|
|
|
|
bool is_navigation,
|
|
|
|
bool is_download,
|
|
|
|
const url::Origin& request_initiator) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
CHECK(browser_context);
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserHostBase> browserPtr;
|
|
|
|
CefRefPtr<CefFrame> framePtr;
|
|
|
|
|
|
|
|
// Default to handlers for the same process in case |frame| doesn't have an
|
|
|
|
// associated CefBrowserHost.
|
|
|
|
content::GlobalRenderFrameHostId global_id(render_process_id,
|
|
|
|
MSG_ROUTING_NONE);
|
|
|
|
|
|
|
|
// |frame| may be nullptr for service worker requests.
|
|
|
|
if (frame) {
|
|
|
|
browserPtr = CefBrowserHostBase::GetBrowserForHost(frame);
|
|
|
|
if (browserPtr) {
|
2024-04-24 23:23:47 +02:00
|
|
|
// May return nullptr for excluded view requests.
|
2021-08-19 23:07:44 +02:00
|
|
|
framePtr = browserPtr->GetFrameForHost(frame);
|
2024-04-17 18:01:26 +02:00
|
|
|
if (!framePtr) {
|
|
|
|
framePtr = browserPtr->GetMainFrame();
|
|
|
|
}
|
2021-08-19 23:07:44 +02:00
|
|
|
global_id = frame->GetGlobalId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto init_state =
|
|
|
|
std::make_unique<InterceptedRequestHandlerWrapper::InitState>();
|
|
|
|
init_state->Initialize(browser_context, browserPtr, framePtr, global_id,
|
|
|
|
is_navigation, is_download, request_initiator,
|
|
|
|
base::RepeatingClosure());
|
|
|
|
|
|
|
|
auto wrapper = std::make_unique<InterceptedRequestHandlerWrapper>();
|
|
|
|
wrapper->init_helper()->MaybeSetInitialized(std::move(init_state));
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<InterceptedRequestHandler> CreateInterceptedRequestHandler(
|
2019-10-01 15:55:16 +02:00
|
|
|
content::WebContents::Getter web_contents_getter,
|
2019-04-24 04:50:25 +02:00
|
|
|
int frame_tree_node_id,
|
2020-09-25 03:40:47 +02:00
|
|
|
const network::ResourceRequest& request,
|
2021-06-04 03:34:56 +02:00
|
|
|
const base::RepeatingClosure& unhandled_request_callback) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
content::WebContents* web_contents = web_contents_getter.Run();
|
2021-08-19 23:07:44 +02:00
|
|
|
CHECK(web_contents);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
content::BrowserContext* browser_context = web_contents->GetBrowserContext();
|
2021-08-19 23:07:44 +02:00
|
|
|
CHECK(browser_context);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
content::RenderFrameHost* frame = nullptr;
|
|
|
|
|
2022-04-27 21:24:24 +02:00
|
|
|
if (request.is_outermost_main_frame ||
|
2021-04-21 00:52:34 +02:00
|
|
|
static_cast<blink::mojom::ResourceType>(request.resource_type) ==
|
|
|
|
blink::mojom::ResourceType::kMainFrame) {
|
2022-07-21 19:26:10 +02:00
|
|
|
frame = web_contents->GetPrimaryMainFrame();
|
2021-08-19 23:07:44 +02:00
|
|
|
CHECK(frame);
|
2021-04-21 00:52:34 +02:00
|
|
|
} else {
|
2021-08-19 23:07:44 +02:00
|
|
|
// May return nullptr for frames in inner WebContents.
|
|
|
|
auto node = content::FrameTreeNode::GloballyFindByID(frame_tree_node_id);
|
|
|
|
if (node) {
|
|
|
|
frame = node->current_frame_host();
|
|
|
|
|
|
|
|
// RFHs can move between FrameTreeNodes. Make sure this one hasn't. See
|
|
|
|
// documentation on RenderFrameHost::GetFrameTreeNodeId() for background.
|
|
|
|
if (content::WebContents::FromRenderFrameHost(frame) != web_contents) {
|
|
|
|
frame = nullptr;
|
|
|
|
}
|
2021-04-21 00:52:34 +02:00
|
|
|
}
|
2021-08-19 23:07:44 +02:00
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
if (!frame) {
|
|
|
|
// Use the main frame for the CefBrowserHost.
|
2022-07-21 19:26:10 +02:00
|
|
|
frame = web_contents->GetPrimaryMainFrame();
|
2021-08-19 23:07:44 +02:00
|
|
|
CHECK(frame);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:24:08 +02:00
|
|
|
CefRefPtr<CefBrowserHostBase> browserPtr;
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefFrame> framePtr;
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
// Default to handlers for the same process in case |frame| doesn't have an
|
|
|
|
// associated CefBrowserHost.
|
|
|
|
content::GlobalRenderFrameHostId global_id(frame->GetProcess()->GetID(),
|
|
|
|
MSG_ROUTING_NONE);
|
|
|
|
|
|
|
|
browserPtr = CefBrowserHostBase::GetBrowserForHost(frame);
|
|
|
|
if (browserPtr) {
|
2024-04-24 23:23:47 +02:00
|
|
|
// May return nullptr for excluded view requests.
|
2021-08-19 23:07:44 +02:00
|
|
|
framePtr = browserPtr->GetFrameForHost(frame);
|
2024-04-17 18:01:26 +02:00
|
|
|
if (!framePtr) {
|
|
|
|
framePtr = browserPtr->GetMainFrame();
|
|
|
|
}
|
2021-08-19 23:07:44 +02:00
|
|
|
global_id = frame->GetGlobalId();
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool is_navigation = ui::PageTransitionIsNewNavigation(
|
|
|
|
static_cast<ui::PageTransition>(request.transition_type));
|
|
|
|
// TODO(navigation): Can we determine the |is_download| value?
|
|
|
|
const bool is_download = false;
|
|
|
|
url::Origin request_initiator;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (request.request_initiator.has_value()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
request_initiator = *request.request_initiator;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
auto init_state =
|
|
|
|
std::make_unique<InterceptedRequestHandlerWrapper::InitState>();
|
2021-08-19 23:07:44 +02:00
|
|
|
init_state->Initialize(browser_context, browserPtr, framePtr, global_id,
|
|
|
|
is_navigation, is_download, request_initiator,
|
2021-04-21 00:52:34 +02:00
|
|
|
unhandled_request_callback);
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
auto wrapper = std::make_unique<InterceptedRequestHandlerWrapper>();
|
|
|
|
wrapper->init_helper()->MaybeSetInitialized(std::move(init_state));
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace net_service
|