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_handler_wrapper.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/net_service/proxy_url_loader_factory.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/common/net_service/net_service_util.h"
|
|
|
|
#include "cef/libcef/common/request_impl.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "net/http/http_status_code.h"
|
|
|
|
|
|
|
|
namespace net_service {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SkipCallbackWrapper : public CefResourceSkipCallback {
|
|
|
|
public:
|
|
|
|
explicit SkipCallbackWrapper(InputStream::SkipCallback 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
|
|
|
SkipCallbackWrapper(const SkipCallbackWrapper&) = delete;
|
|
|
|
SkipCallbackWrapper& operator=(const SkipCallbackWrapper&) = delete;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
~SkipCallbackWrapper() override {
|
|
|
|
if (!callback_.is_null()) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// Make sure it executes on the correct thread.
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(std::move(callback_), net::ERR_FAILED));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void Continue(int64_t bytes_skipped) override {
|
2019-04-24 04:50:25 +02:00
|
|
|
if (!work_thread_task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE,
|
2019-05-31 11:50:12 +02:00
|
|
|
base::BindOnce(&SkipCallbackWrapper::Continue, this, bytes_skipped));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
std::move(callback_).Run(bytes_skipped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() { callback_.Reset(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
InputStream::SkipCallback callback_;
|
|
|
|
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> work_thread_task_runner_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SkipCallbackWrapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadCallbackWrapper : public CefResourceReadCallback {
|
|
|
|
public:
|
|
|
|
explicit ReadCallbackWrapper(InputStream::ReadCallback 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
|
|
|
ReadCallbackWrapper(const ReadCallbackWrapper&) = delete;
|
|
|
|
ReadCallbackWrapper& operator=(const ReadCallbackWrapper&) = delete;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
~ReadCallbackWrapper() override {
|
|
|
|
if (!callback_.is_null()) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// Make sure it executes on the correct thread.
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(std::move(callback_), net::ERR_FAILED));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue(int bytes_read) override {
|
|
|
|
if (!work_thread_task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE,
|
2019-05-31 11:50:12 +02:00
|
|
|
base::BindOnce(&ReadCallbackWrapper::Continue, this, bytes_read));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
std::move(callback_).Run(bytes_read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() { callback_.Reset(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
InputStream::ReadCallback callback_;
|
|
|
|
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> work_thread_task_runner_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ReadCallbackWrapper);
|
|
|
|
};
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Helper for accessing a CefResourceHandler without creating reference loops.
|
|
|
|
class HandlerProvider : public base::RefCountedThreadSafe<HandlerProvider> {
|
|
|
|
public:
|
|
|
|
explicit HandlerProvider(CefRefPtr<CefResourceHandler> handler)
|
|
|
|
: handler_(handler) {
|
|
|
|
DCHECK(handler_);
|
|
|
|
}
|
|
|
|
virtual ~HandlerProvider() {
|
|
|
|
// Detach should have been called.
|
|
|
|
DCHECK(!handler_);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefResourceHandler> handler() const {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Detach() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!handler_) {
|
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
|
|
|
|
|
|
|
// Execute on the expected thread.
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::BindOnce(&CefResourceHandler::Cancel, handler_));
|
|
|
|
|
|
|
|
handler_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable base::Lock lock_;
|
|
|
|
CefRefPtr<CefResourceHandler> handler_;
|
|
|
|
};
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
class ReadResponseCallbackWrapper : public CefCallback {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
ReadResponseCallbackWrapper(const ReadResponseCallbackWrapper&) = delete;
|
|
|
|
ReadResponseCallbackWrapper& operator=(const ReadResponseCallbackWrapper&) =
|
|
|
|
delete;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
~ReadResponseCallbackWrapper() override {
|
|
|
|
if (callback_) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// This will post to the correct thread if necessary.
|
2019-04-24 04:50:25 +02:00
|
|
|
callback_->Continue(net::ERR_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue() override {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
2019-05-31 11:50:12 +02:00
|
|
|
base::BindOnce(&ReadResponseCallbackWrapper::DoRead, this));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
2019-05-31 11:50:12 +02:00
|
|
|
base::BindOnce(&ReadResponseCallbackWrapper::DoCancel, this));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
static void ReadResponse(scoped_refptr<HandlerProvider> handler_provider,
|
2019-04-24 04:50:25 +02:00
|
|
|
net::IOBuffer* dest,
|
|
|
|
int length,
|
|
|
|
CefRefPtr<ReadCallbackWrapper> callback) {
|
|
|
|
CEF_POST_TASK(
|
2019-05-31 11:50:12 +02:00
|
|
|
CEF_IOT,
|
|
|
|
base::BindOnce(ReadResponseCallbackWrapper::ReadResponseOnIOThread,
|
|
|
|
handler_provider, base::Unretained(dest), length,
|
|
|
|
callback));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-31 11:50:12 +02:00
|
|
|
ReadResponseCallbackWrapper(scoped_refptr<HandlerProvider> handler_provider,
|
2019-04-24 04:50:25 +02:00
|
|
|
net::IOBuffer* dest,
|
|
|
|
int length,
|
|
|
|
CefRefPtr<ReadCallbackWrapper> callback)
|
2019-05-31 11:50:12 +02:00
|
|
|
: handler_provider_(handler_provider),
|
|
|
|
dest_(dest),
|
|
|
|
length_(length),
|
|
|
|
callback_(callback) {}
|
|
|
|
|
|
|
|
static void ReadResponseOnIOThread(
|
|
|
|
scoped_refptr<HandlerProvider> handler_provider,
|
|
|
|
net::IOBuffer* dest,
|
|
|
|
int length,
|
|
|
|
CefRefPtr<ReadCallbackWrapper> callback) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
CefRefPtr<ReadResponseCallbackWrapper> callbackWrapper =
|
2019-05-31 11:50:12 +02:00
|
|
|
new ReadResponseCallbackWrapper(handler_provider, dest, length,
|
|
|
|
callback);
|
2019-04-24 04:50:25 +02:00
|
|
|
callbackWrapper->DoRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoRead() {
|
|
|
|
CEF_REQUIRE_IOT();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!callback_) {
|
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
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
auto handler = handler_provider_->handler();
|
|
|
|
if (!handler) {
|
|
|
|
DoCancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
int bytes_read = 0;
|
|
|
|
bool result =
|
2019-05-31 11:50:12 +02:00
|
|
|
handler->ReadResponse(dest_->data(), length_, bytes_read, this);
|
2019-04-24 04:50:25 +02:00
|
|
|
if (result) {
|
|
|
|
if (bytes_read > 0) {
|
|
|
|
// Continue immediately.
|
|
|
|
callback_->Continue(bytes_read);
|
|
|
|
callback_ = nullptr;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal response completion immediately.
|
|
|
|
callback_->Continue(0);
|
|
|
|
callback_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoCancel() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
if (callback_) {
|
|
|
|
callback_->Continue(net::ERR_FAILED);
|
|
|
|
callback_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
scoped_refptr<HandlerProvider> handler_provider_;
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<net::IOBuffer> dest_;
|
2019-04-24 04:50:25 +02:00
|
|
|
int length_;
|
|
|
|
CefRefPtr<ReadCallbackWrapper> callback_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ReadResponseCallbackWrapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
class InputStreamWrapper : public InputStream {
|
|
|
|
public:
|
2019-05-31 11:50:12 +02:00
|
|
|
explicit InputStreamWrapper(scoped_refptr<HandlerProvider> handler_provider)
|
|
|
|
: handler_provider_(handler_provider) {}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
InputStreamWrapper(const InputStreamWrapper&) = delete;
|
|
|
|
InputStreamWrapper& operator=(const InputStreamWrapper&) = delete;
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
~InputStreamWrapper() override { Cancel(); }
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// InputStream methods:
|
|
|
|
bool Skip(int64_t n, int64_t* bytes_skipped, SkipCallback callback) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
auto handler = handler_provider_->handler();
|
|
|
|
if (!handler) {
|
|
|
|
// Cancel immediately.
|
|
|
|
*bytes_skipped = net::ERR_FAILED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<SkipCallbackWrapper> callbackWrapper =
|
|
|
|
new SkipCallbackWrapper(std::move(callback));
|
2019-05-31 11:50:12 +02:00
|
|
|
bool result = handler->Skip(n, *bytes_skipped, callbackWrapper.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
if (result) {
|
|
|
|
if (*bytes_skipped > 0) {
|
|
|
|
// Continue immediately.
|
|
|
|
callbackWrapper->Disconnect();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel immediately.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Read(net::IOBuffer* dest,
|
|
|
|
int length,
|
|
|
|
int* bytes_read,
|
|
|
|
ReadCallback callback) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
auto handler = handler_provider_->handler();
|
|
|
|
if (!handler) {
|
|
|
|
// Cancel immediately.
|
|
|
|
*bytes_read = net::ERR_FAILED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<ReadCallbackWrapper> callbackWrapper =
|
|
|
|
new ReadCallbackWrapper(std::move(callback));
|
2019-05-31 11:50:12 +02:00
|
|
|
bool result =
|
|
|
|
handler->Read(dest->data(), length, *bytes_read, callbackWrapper.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
if (result) {
|
|
|
|
if (*bytes_read > 0) {
|
|
|
|
// Continue immediately.
|
|
|
|
callbackWrapper->Disconnect();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*bytes_read == -1) {
|
|
|
|
// Call ReadResponse on the IO thread.
|
2019-05-31 11:50:12 +02:00
|
|
|
ReadResponseCallbackWrapper::ReadResponse(handler_provider_, dest, length,
|
2019-04-24 04:50:25 +02:00
|
|
|
callbackWrapper);
|
|
|
|
*bytes_read = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete or cancel immediately.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
void Cancel() {
|
|
|
|
// Triggers a call to Cancel on the handler.
|
|
|
|
handler_provider_->Detach();
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
private:
|
2019-05-31 11:50:12 +02:00
|
|
|
scoped_refptr<HandlerProvider> handler_provider_;
|
2019-04-24 04:50:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class OpenCallbackWrapper : public CefCallback {
|
|
|
|
public:
|
|
|
|
OpenCallbackWrapper(ResourceResponse::OpenCallback callback,
|
|
|
|
std::unique_ptr<InputStreamWrapper> stream)
|
|
|
|
: callback_(std::move(callback)),
|
|
|
|
stream_(std::move(stream)),
|
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
|
|
|
OpenCallbackWrapper(const OpenCallbackWrapper&) = delete;
|
|
|
|
OpenCallbackWrapper& operator=(const OpenCallbackWrapper&) = delete;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
~OpenCallbackWrapper() override {
|
|
|
|
if (!callback_.is_null()) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// Make sure it executes on the correct thread.
|
|
|
|
work_thread_task_runner_->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::BindOnce(&OpenCallbackWrapper::Execute, std::move(callback_),
|
|
|
|
std::move(stream_), false));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Continue() override {
|
|
|
|
if (!work_thread_task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
work_thread_task_runner_->PostTask(
|
2019-05-31 11:50:12 +02:00
|
|
|
FROM_HERE, base::BindOnce(&OpenCallbackWrapper::Continue, this));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
Execute(std::move(callback_), std::move(stream_), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
if (!work_thread_task_runner_->RunsTasksInCurrentSequence()) {
|
|
|
|
work_thread_task_runner_->PostTask(
|
2019-05-31 11:50:12 +02:00
|
|
|
FROM_HERE, base::BindOnce(&OpenCallbackWrapper::Cancel, this));
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
Execute(std::move(callback_), std::move(stream_), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void Execute(ResourceResponse::OpenCallback callback,
|
|
|
|
std::unique_ptr<InputStreamWrapper> stream,
|
|
|
|
bool cont) {
|
|
|
|
std::move(callback).Run(cont ? std::move(stream) : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceResponse::OpenCallback callback_;
|
|
|
|
std::unique_ptr<InputStreamWrapper> stream_;
|
|
|
|
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> work_thread_task_runner_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(OpenCallbackWrapper);
|
|
|
|
};
|
|
|
|
|
|
|
|
void CallProcessRequestOnIOThread(
|
2019-05-31 11:50:12 +02:00
|
|
|
scoped_refptr<HandlerProvider> handler_provider,
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefRequestImpl> request,
|
|
|
|
CefRefPtr<OpenCallbackWrapper> callbackWrapper) {
|
|
|
|
CEF_REQUIRE_IOT();
|
2019-05-31 11:50:12 +02:00
|
|
|
auto handler = handler_provider->handler();
|
|
|
|
if (!handler) {
|
|
|
|
callbackWrapper->Cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
if (!handler->ProcessRequest(request.get(), callbackWrapper.get())) {
|
|
|
|
callbackWrapper->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResourceResponseWrapper : public ResourceResponse {
|
|
|
|
public:
|
2021-04-21 00:52:34 +02:00
|
|
|
ResourceResponseWrapper(const int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefResourceHandler> handler)
|
2019-05-31 11:50:12 +02:00
|
|
|
: request_id_(request_id),
|
|
|
|
handler_provider_(new HandlerProvider(handler)) {}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
ResourceResponseWrapper(const ResourceResponseWrapper&) = delete;
|
|
|
|
ResourceResponseWrapper& operator=(const ResourceResponseWrapper&) = delete;
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
~ResourceResponseWrapper() override {
|
|
|
|
// Triggers a call to Cancel on the handler.
|
|
|
|
handler_provider_->Detach();
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceResponse methods:
|
2021-04-21 00:52:34 +02:00
|
|
|
bool OpenInputStream(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
OpenCallback callback) override {
|
|
|
|
DCHECK_EQ(request_id, request_id_);
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
auto handler = handler_provider_->handler();
|
|
|
|
if (!handler) {
|
|
|
|
// Cancel immediately.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// May be recreated on redirect.
|
|
|
|
request_ = new CefRequestImpl();
|
2021-04-21 00:52:34 +02:00
|
|
|
request_->Set(&request, request_id);
|
2019-04-24 04:50:25 +02:00
|
|
|
request_->SetReadOnly(true);
|
|
|
|
|
|
|
|
CefRefPtr<OpenCallbackWrapper> callbackWrapper = new OpenCallbackWrapper(
|
2019-05-31 11:50:12 +02:00
|
|
|
std::move(callback),
|
|
|
|
std::make_unique<InputStreamWrapper>(handler_provider_));
|
2019-04-24 04:50:25 +02:00
|
|
|
bool handle_request = false;
|
|
|
|
bool result =
|
2019-05-31 11:50:12 +02:00
|
|
|
handler->Open(request_.get(), handle_request, callbackWrapper.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
if (result) {
|
|
|
|
if (handle_request) {
|
|
|
|
// Continue immediately.
|
|
|
|
callbackWrapper->Continue();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handle_request) {
|
|
|
|
// Cancel immediately.
|
|
|
|
callbackWrapper->Cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call ProcessRequest on the IO thread.
|
2019-05-31 11:50:12 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_IOT, base::BindOnce(CallProcessRequestOnIOThread, handler_provider_,
|
|
|
|
request_, callbackWrapper));
|
2019-04-24 04:50:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void GetResponseHeaders(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
int* status_code,
|
|
|
|
std::string* reason_phrase,
|
|
|
|
std::string* mime_type,
|
|
|
|
std::string* charset,
|
|
|
|
int64_t* content_length,
|
|
|
|
HeaderMap* extra_headers) override {
|
|
|
|
DCHECK_EQ(request_id, request_id_);
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
auto handler = handler_provider_->handler();
|
|
|
|
if (!handler) {
|
|
|
|
// Cancel immediately.
|
|
|
|
*status_code = net::ERR_FAILED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefResponse> response = CefResponse::Create();
|
|
|
|
int64_t response_length = -1;
|
|
|
|
CefString redirect_url;
|
2019-05-31 11:50:12 +02:00
|
|
|
handler->GetResponseHeaders(response, response_length, redirect_url);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
const auto error_code = response->GetError();
|
|
|
|
if (error_code != ERR_NONE) {
|
|
|
|
// Early exit if the handler reported an error.
|
|
|
|
*status_code = error_code;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!redirect_url.empty()) {
|
|
|
|
// Perform a redirect.
|
|
|
|
*status_code = net::HTTP_TEMPORARY_REDIRECT;
|
|
|
|
*reason_phrase = std::string();
|
|
|
|
extra_headers->insert(
|
|
|
|
std::make_pair(kHTTPLocationHeaderName, redirect_url));
|
|
|
|
} else {
|
|
|
|
*status_code = response->GetStatus();
|
|
|
|
*reason_phrase = response->GetStatusText();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reason_phrase->empty() && *status_code > 0) {
|
|
|
|
*reason_phrase = net::GetHttpReasonPhrase(
|
|
|
|
static_cast<net::HttpStatusCode>(*status_code));
|
|
|
|
}
|
|
|
|
|
|
|
|
*mime_type = response->GetMimeType();
|
|
|
|
*charset = response->GetCharset();
|
|
|
|
|
|
|
|
// A |content_length| value may already be specified if the request included
|
|
|
|
// a Range header.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (response_length >= 0 && *content_length == -1) {
|
2019-04-24 04:50:25 +02:00
|
|
|
*content_length = response_length;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
response->GetHeaderMap(headerMap);
|
|
|
|
for (const auto& value : headerMap) {
|
|
|
|
extra_headers->insert(std::make_pair(value.first, value.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-04-21 00:52:34 +02:00
|
|
|
const int32_t request_id_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefRequestImpl> request_;
|
2019-05-31 11:50:12 +02:00
|
|
|
scoped_refptr<HandlerProvider> handler_provider_;
|
2019-04-24 04:50:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<ResourceResponse> CreateResourceResponse(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
CefRefPtr<CefResourceHandler> handler) {
|
|
|
|
return std::make_unique<ResourceResponseWrapper>(request_id, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace net_service
|