2012-06-19 18:29:49 +02:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
|
|
|
#include "libcef/renderer/render_urlrequest_impl.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
#include "libcef/common/request_impl.h"
|
|
|
|
#include "libcef/common/response_impl.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "third_party/WebKit/public/platform/Platform.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebString.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURL.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURLError.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURLLoader.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURLRequest.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURLResponse.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
using blink::WebString;
|
|
|
|
using blink::WebURL;
|
|
|
|
using blink::WebURLError;
|
|
|
|
using blink::WebURLLoader;
|
|
|
|
using blink::WebURLRequest;
|
|
|
|
using blink::WebURLResponse;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
class CefWebURLLoaderClient : public blink::WebURLLoaderClient {
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
|
|
|
CefWebURLLoaderClient(CefRenderURLRequest::Context* context,
|
|
|
|
int request_flags);
|
2014-11-12 20:25:15 +01:00
|
|
|
~CefWebURLLoaderClient() override;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
// blink::WebURLLoaderClient methods.
|
2017-05-17 11:29:28 +02:00
|
|
|
bool WillFollowRedirect(WebURLRequest& newRequest,
|
|
|
|
const WebURLResponse& redirectResponse) override;
|
|
|
|
void DidSendData(unsigned long long bytesSent,
|
|
|
|
unsigned long long totalBytesToBeSent) override;
|
|
|
|
void DidReceiveResponse(const WebURLResponse& response) override;
|
|
|
|
void DidDownloadData(int dataLength, int encodedDataLength) override;
|
|
|
|
void DidReceiveData(const char* data, int dataLength) override;
|
|
|
|
void DidReceiveCachedMetadata(const char* data, int dataLength) override;
|
2017-05-31 17:33:30 +02:00
|
|
|
void DidFinishLoading(double finish_time,
|
|
|
|
int64_t total_encoded_data_length,
|
|
|
|
int64_t total_encoded_body_length,
|
|
|
|
int64_t total_decoded_body_length) override;
|
|
|
|
void DidFail(const WebURLError&,
|
|
|
|
int64_t total_encoded_data_length,
|
|
|
|
int64_t total_encoded_body_length,
|
|
|
|
int64_t total_decoded_body_length) override;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// The context_ pointer will outlive this object.
|
|
|
|
CefRenderURLRequest::Context* context_;
|
|
|
|
int request_flags_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// CefRenderURLRequest::Context -----------------------------------------------
|
|
|
|
|
|
|
|
class CefRenderURLRequest::Context
|
|
|
|
: public base::RefCountedThreadSafe<CefRenderURLRequest::Context> {
|
|
|
|
public:
|
|
|
|
Context(CefRefPtr<CefRenderURLRequest> url_request,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefURLRequestClient> client)
|
2017-05-17 11:29:28 +02:00
|
|
|
: url_request_(url_request),
|
|
|
|
request_(request),
|
|
|
|
client_(client),
|
|
|
|
task_runner_(base::MessageLoop::current()->task_runner()),
|
|
|
|
status_(UR_IO_PENDING),
|
|
|
|
error_code_(ERR_NONE),
|
|
|
|
upload_data_size_(0),
|
|
|
|
got_upload_progress_complete_(false),
|
|
|
|
download_data_received_(0),
|
|
|
|
download_data_total_(-1) {
|
2012-06-19 18:29:49 +02:00
|
|
|
// Mark the request as read-only.
|
|
|
|
static_cast<CefRequestImpl*>(request_.get())->SetReadOnly(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool CalledOnValidThread() {
|
2015-07-24 02:06:56 +02:00
|
|
|
return task_runner_->RunsTasksOnCurrentThread();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Start() {
|
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
|
|
|
|
GURL url = GURL(request_->GetURL().ToString());
|
|
|
|
if (!url.is_valid())
|
|
|
|
return false;
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
loader_ = blink::Platform::Current()->CreateURLLoader();
|
2012-06-19 18:29:49 +02:00
|
|
|
url_client_.reset(new CefWebURLLoaderClient(this, request_->GetFlags()));
|
|
|
|
|
|
|
|
WebURLRequest urlRequest;
|
2017-05-17 11:29:28 +02:00
|
|
|
static_cast<CefRequestImpl*>(request_.get())
|
|
|
|
->Get(urlRequest, upload_data_size_);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
loader_->LoadAsynchronously(urlRequest, url_client_.get());
|
2012-06-19 18:29:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() {
|
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
|
|
|
|
// The request may already be complete.
|
|
|
|
if (!loader_.get() || status_ != UR_IO_PENDING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
status_ = UR_CANCELED;
|
|
|
|
error_code_ = ERR_ABORTED;
|
|
|
|
|
|
|
|
// Will result in a call to OnError().
|
2017-04-20 21:28:17 +02:00
|
|
|
loader_->Cancel();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnResponse(const WebURLResponse& response) {
|
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
|
|
|
|
response_ = CefResponse::Create();
|
|
|
|
CefResponseImpl* responseImpl =
|
|
|
|
static_cast<CefResponseImpl*>(response_.get());
|
|
|
|
responseImpl->Set(response);
|
|
|
|
responseImpl->SetReadOnly(true);
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
download_data_total_ = response.ExpectedContentLength();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnError(const WebURLError& error) {
|
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
|
|
|
|
if (status_ == UR_IO_PENDING) {
|
|
|
|
status_ = UR_FAILED;
|
|
|
|
error_code_ = static_cast<CefURLRequest::ErrorCode>(error.reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
OnComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnComplete() {
|
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
|
|
|
|
if (status_ == UR_IO_PENDING) {
|
|
|
|
status_ = UR_SUCCESS;
|
|
|
|
NotifyUploadProgressIfNecessary();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loader_.get())
|
|
|
|
loader_.reset(NULL);
|
|
|
|
|
|
|
|
DCHECK(url_request_.get());
|
|
|
|
client_->OnRequestComplete(url_request_.get());
|
|
|
|
|
|
|
|
// This may result in the Context object being deleted.
|
|
|
|
url_request_ = NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
void OnDownloadProgress(int64_t current) {
|
2012-06-19 18:29:49 +02:00
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
DCHECK(url_request_.get());
|
|
|
|
|
|
|
|
NotifyUploadProgressIfNecessary();
|
|
|
|
|
|
|
|
download_data_received_ += current;
|
|
|
|
client_->OnDownloadProgress(url_request_.get(), download_data_received_,
|
2017-05-17 11:29:28 +02:00
|
|
|
download_data_total_);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnDownloadData(const char* data, int dataLength) {
|
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
DCHECK(url_request_.get());
|
|
|
|
client_->OnDownloadData(url_request_.get(), data, dataLength);
|
|
|
|
}
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
void OnUploadProgress(int64_t current, int64_t total) {
|
2012-06-19 18:29:49 +02:00
|
|
|
DCHECK(CalledOnValidThread());
|
|
|
|
DCHECK(url_request_.get());
|
|
|
|
if (current == total)
|
|
|
|
got_upload_progress_complete_ = true;
|
|
|
|
client_->OnUploadProgress(url_request_.get(), current, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequest> request() { return request_; }
|
|
|
|
CefRefPtr<CefURLRequestClient> client() { return client_; }
|
|
|
|
CefURLRequest::Status status() { return status_; }
|
|
|
|
CefURLRequest::ErrorCode error_code() { return error_code_; }
|
|
|
|
CefRefPtr<CefResponse> response() { return response_; }
|
|
|
|
|
|
|
|
private:
|
2014-11-12 20:25:15 +01:00
|
|
|
friend class base::RefCountedThreadSafe<CefRenderURLRequest::Context>;
|
|
|
|
|
|
|
|
virtual ~Context() {}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
void NotifyUploadProgressIfNecessary() {
|
|
|
|
if (!got_upload_progress_complete_ && upload_data_size_ > 0) {
|
|
|
|
// URLFetcher sends upload notifications using a timer and will not send
|
|
|
|
// a notification if the request completes too quickly. We therefore
|
|
|
|
// send the notification here if necessary.
|
|
|
|
client_->OnUploadProgress(url_request_.get(), upload_data_size_,
|
|
|
|
upload_data_size_);
|
|
|
|
got_upload_progress_complete_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Members only accessed on the initialization thread.
|
|
|
|
CefRefPtr<CefRenderURLRequest> url_request_;
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
CefRefPtr<CefURLRequestClient> client_;
|
2015-07-24 02:06:56 +02:00
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner_;
|
2012-06-19 18:29:49 +02:00
|
|
|
CefURLRequest::Status status_;
|
|
|
|
CefURLRequest::ErrorCode error_code_;
|
|
|
|
CefRefPtr<CefResponse> response_;
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<blink::WebURLLoader> loader_;
|
|
|
|
std::unique_ptr<CefWebURLLoaderClient> url_client_;
|
2016-01-06 20:20:54 +01:00
|
|
|
int64_t upload_data_size_;
|
2012-06-19 18:29:49 +02:00
|
|
|
bool got_upload_progress_complete_;
|
2016-01-06 20:20:54 +01:00
|
|
|
int64_t download_data_received_;
|
|
|
|
int64_t download_data_total_;
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// CefWebURLLoaderClient --------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
CefWebURLLoaderClient::CefWebURLLoaderClient(
|
|
|
|
CefRenderURLRequest::Context* context,
|
|
|
|
int request_flags)
|
2017-05-17 11:29:28 +02:00
|
|
|
: context_(context), request_flags_(request_flags) {}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefWebURLLoaderClient::~CefWebURLLoaderClient() {}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
bool CefWebURLLoaderClient::WillFollowRedirect(
|
2012-06-19 18:29:49 +02:00
|
|
|
WebURLRequest& newRequest,
|
2016-10-21 21:52:29 +02:00
|
|
|
const WebURLResponse& redirectResponse) {
|
|
|
|
return true;
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefWebURLLoaderClient::DidSendData(unsigned long long bytesSent,
|
|
|
|
unsigned long long totalBytesToBeSent) {
|
2012-06-19 18:29:49 +02:00
|
|
|
if (request_flags_ & UR_FLAG_REPORT_UPLOAD_PROGRESS)
|
|
|
|
context_->OnUploadProgress(bytesSent, totalBytesToBeSent);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefWebURLLoaderClient::DidReceiveResponse(const WebURLResponse& response) {
|
2012-06-19 18:29:49 +02:00
|
|
|
context_->OnResponse(response);
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void CefWebURLLoaderClient::DidDownloadData(int dataLength,
|
2017-05-17 11:29:28 +02:00
|
|
|
int encodedDataLength) {}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefWebURLLoaderClient::DidReceiveData(const char* data, int dataLength) {
|
2012-06-19 18:29:49 +02:00
|
|
|
context_->OnDownloadProgress(dataLength);
|
|
|
|
|
|
|
|
if (!(request_flags_ & UR_FLAG_NO_DOWNLOAD_DATA))
|
|
|
|
context_->OnDownloadData(data, dataLength);
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void CefWebURLLoaderClient::DidReceiveCachedMetadata(const char* data,
|
2017-05-17 11:29:28 +02:00
|
|
|
int dataLength) {}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
void CefWebURLLoaderClient::DidFinishLoading(
|
|
|
|
double finishTime,
|
|
|
|
int64_t total_encoded_data_length,
|
|
|
|
int64_t total_encoded_body_length,
|
|
|
|
int64_t total_decoded_body_length) {
|
2012-06-19 18:29:49 +02:00
|
|
|
context_->OnComplete();
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void CefWebURLLoaderClient::DidFail(const WebURLError& error,
|
2017-05-31 17:33:30 +02:00
|
|
|
int64_t total_encoded_data_length,
|
|
|
|
int64_t total_encoded_body_length,
|
|
|
|
int64_t total_decoded_body_length) {
|
2012-06-19 18:29:49 +02:00
|
|
|
context_->OnError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// CefRenderURLRequest --------------------------------------------------------
|
|
|
|
|
|
|
|
CefRenderURLRequest::CefRenderURLRequest(
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefURLRequestClient> client) {
|
|
|
|
context_ = new Context(this, request, client);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRenderURLRequest::~CefRenderURLRequest() {}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
bool CefRenderURLRequest::Start() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return false;
|
|
|
|
return context_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequest> CefRenderURLRequest::GetRequest() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
return context_->request();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefURLRequestClient> CefRenderURLRequest::GetClient() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
return context_->client();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefURLRequest::Status CefRenderURLRequest::GetRequestStatus() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return UR_UNKNOWN;
|
|
|
|
return context_->status();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefURLRequest::ErrorCode CefRenderURLRequest::GetRequestError() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return ERR_NONE;
|
|
|
|
return context_->error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefResponse> CefRenderURLRequest::GetResponse() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return NULL;
|
|
|
|
return context_->response();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefRenderURLRequest::Cancel() {
|
|
|
|
if (!VerifyContext())
|
|
|
|
return;
|
|
|
|
return context_->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefRenderURLRequest::VerifyContext() {
|
|
|
|
DCHECK(context_.get());
|
|
|
|
if (!context_->CalledOnValidThread()) {
|
|
|
|
NOTREACHED() << "called on invalid thread";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|