electron/atom/browser/net/atom_url_request.cc

522 lines
17 KiB
C++
Raw Normal View History

2016-10-25 10:41:01 +00:00
// Copyright (c) 2016 GitHub, Inc.
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
2016-10-14 09:50:47 +00:00
#include "atom/browser/net/atom_url_request.h"
#include <memory>
#include <string>
#include <utility>
#include "atom/browser/api/atom_api_url_request.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "base/callback.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
2016-09-26 12:03:49 +00:00
#include "net/base/elements_upload_data_stream.h"
2016-09-29 09:31:08 +00:00
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
2016-09-26 12:03:49 +00:00
#include "net/base/upload_bytes_element_reader.h"
2017-03-23 19:37:54 +00:00
#include "net/url_request/redirect_info.h"
namespace {
const int kBufferSize = 4096;
2016-09-29 09:31:08 +00:00
} // namespace
namespace atom {
2016-09-26 12:03:49 +00:00
namespace internal {
class UploadOwnedIOBufferElementReader : public net::UploadBytesElementReader {
2016-09-29 09:31:08 +00:00
public:
2016-09-26 12:03:49 +00:00
explicit UploadOwnedIOBufferElementReader(
2016-10-14 08:58:16 +00:00
scoped_refptr<const net::IOBufferWithSize> buffer)
: net::UploadBytesElementReader(buffer->data(), buffer->size()),
2016-10-14 09:50:47 +00:00
buffer_(buffer) {}
2016-09-26 12:59:53 +00:00
2016-10-14 09:50:47 +00:00
~UploadOwnedIOBufferElementReader() override {}
2016-09-26 12:03:49 +00:00
static UploadOwnedIOBufferElementReader* CreateWithBuffer(
2016-10-14 08:58:16 +00:00
scoped_refptr<const net::IOBufferWithSize> buffer) {
2016-09-26 12:03:49 +00:00
return new UploadOwnedIOBufferElementReader(std::move(buffer));
}
2016-09-29 09:31:08 +00:00
private:
2016-09-26 12:03:49 +00:00
scoped_refptr<const net::IOBuffer> buffer_;
DISALLOW_COPY_AND_ASSIGN(UploadOwnedIOBufferElementReader);
};
2016-09-29 09:31:08 +00:00
} // namespace internal
2016-09-26 12:03:49 +00:00
AtomURLRequest::AtomURLRequest(api::URLRequest* delegate)
: delegate_(delegate),
2016-10-14 09:50:47 +00:00
response_read_buffer_(new net::IOBuffer(kBufferSize)) {}
AtomURLRequest::~AtomURLRequest() {
DCHECK(!request_context_getter_);
DCHECK(!request_);
}
2016-09-21 15:35:03 +00:00
scoped_refptr<AtomURLRequest> AtomURLRequest::Create(
2016-09-26 12:59:53 +00:00
AtomBrowserContext* browser_context,
const std::string& method,
const std::string& url,
2017-03-23 19:37:54 +00:00
const std::string& redirect_policy,
api::URLRequest* delegate) {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(browser_context);
DCHECK(!url.empty());
DCHECK(delegate);
if (!browser_context || url.empty() || !delegate) {
return nullptr;
}
scoped_refptr<net::URLRequestContextGetter> request_context_getter(
browser_context->GetRequestContext());
DCHECK(request_context_getter);
scoped_refptr<AtomURLRequest> atom_url_request(new AtomURLRequest(delegate));
if (base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoInitialize, atom_url_request,
request_context_getter, method, url,
redirect_policy))) {
return atom_url_request;
}
return nullptr;
}
void AtomURLRequest::Terminate() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
delegate_ = nullptr;
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoTerminate, this));
}
void AtomURLRequest::DoInitialize(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
const std::string& method,
2017-03-23 19:37:54 +00:00
const std::string& url,
const std::string& redirect_policy) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(request_context_getter);
2017-03-23 19:37:54 +00:00
redirect_policy_ = redirect_policy;
request_context_getter_ = request_context_getter;
request_context_getter_->AddObserver(this);
auto* context = request_context_getter_->GetURLRequestContext();
if (!context) {
// Called after shutdown.
DoCancelWithError("Cannot start a request after shutdown.", true);
return;
}
DCHECK(context);
2016-10-14 09:50:47 +00:00
request_ = context->CreateRequest(
GURL(url), net::RequestPriority::DEFAULT_PRIORITY, this);
if (!request_) {
DoCancelWithError("Failed to create a net::URLRequest.", true);
return;
}
request_->set_method(method);
// Do not send cookies from the cookie store.
DoSetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES);
// Set a flag to stop custom protocol from intercepting this request.
request_->SetUserData(DisableProtocolInterceptFlagKey(),
base::WrapUnique(new base::SupportsUserData::Data()));
}
void AtomURLRequest::DoTerminate() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
request_.reset();
if (request_context_getter_) {
request_context_getter_->RemoveObserver(this);
request_context_getter_ = nullptr;
}
}
2016-10-14 09:50:47 +00:00
bool AtomURLRequest::Write(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last) {
2016-09-29 09:31:08 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoWriteBuffer, this, buffer, is_last));
}
void AtomURLRequest::SetChunkedUpload(bool is_chunked_upload) {
2016-09-26 12:03:49 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// The method can be called only before switching to multi-threaded mode,
// i.e. before the first call to write.
// So it is safe to change the object in the UI thread.
is_chunked_upload_ = is_chunked_upload;
2016-09-26 12:03:49 +00:00
}
void AtomURLRequest::Cancel() {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoCancel, this));
}
2017-03-23 19:37:54 +00:00
void AtomURLRequest::FollowRedirect() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoFollowRedirect, this));
2017-03-23 19:37:54 +00:00
}
void AtomURLRequest::SetExtraHeader(const std::string& name,
2016-10-14 08:58:16 +00:00
const std::string& value) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoSetExtraHeader, this, name, value));
}
void AtomURLRequest::RemoveExtraHeader(const std::string& name) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoRemoveExtraHeader, this, name));
}
2016-10-14 08:58:16 +00:00
void AtomURLRequest::PassLoginInformation(
const base::string16& username,
const base::string16& password) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (username.empty() || password.empty()) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoCancelAuth, this));
} else {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoSetAuth, this, username, password));
}
}
void AtomURLRequest::SetLoadFlags(int flags) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&AtomURLRequest::DoSetLoadFlags, this, flags));
}
2016-09-26 12:59:53 +00:00
void AtomURLRequest::DoWriteBuffer(
2016-10-14 08:58:16 +00:00
scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last) {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
2016-09-26 12:03:49 +00:00
if (is_chunked_upload_) {
// Chunked encoding case.
bool first_call = false;
if (!chunked_stream_writer_) {
std::unique_ptr<net::ChunkedUploadDataStream> chunked_stream(
new net::ChunkedUploadDataStream(0));
2016-09-26 12:03:49 +00:00
chunked_stream_writer_ = chunked_stream->CreateWriter();
request_->set_upload(std::move(chunked_stream));
first_call = true;
}
if (buffer)
2016-09-26 12:03:49 +00:00
// Non-empty buffer.
2016-10-14 09:50:47 +00:00
chunked_stream_writer_->AppendData(buffer->data(), buffer->size(),
is_last);
else if (is_last)
// Empty buffer and last chunk, i.e. request.end().
2016-10-14 09:50:47 +00:00
chunked_stream_writer_->AppendData(nullptr, 0, true);
2016-09-26 12:03:49 +00:00
if (first_call) {
2016-09-26 12:03:49 +00:00
request_->Start();
}
2016-09-29 09:31:08 +00:00
} else {
2016-09-26 12:03:49 +00:00
if (buffer) {
// Handling potential empty buffers.
2016-09-26 12:59:53 +00:00
using internal::UploadOwnedIOBufferElementReader;
auto* element_reader =
2016-10-14 09:50:47 +00:00
UploadOwnedIOBufferElementReader::CreateWithBuffer(std::move(buffer));
2016-09-26 12:59:53 +00:00
upload_element_readers_.push_back(
std::unique_ptr<net::UploadElementReader>(element_reader));
2016-09-26 12:03:49 +00:00
}
if (is_last) {
auto* elements_upload_data_stream = new net::ElementsUploadDataStream(
std::move(upload_element_readers_), 0);
2016-09-26 12:59:53 +00:00
request_->set_upload(
std::unique_ptr<net::UploadDataStream>(elements_upload_data_stream));
2016-09-26 12:03:49 +00:00
request_->Start();
}
}
2016-09-21 15:35:03 +00:00
}
void AtomURLRequest::DoCancel() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (request_) {
request_->Cancel();
}
DoTerminate();
}
2017-03-23 19:37:54 +00:00
void AtomURLRequest::DoFollowRedirect() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (request_ && request_->is_redirecting() && redirect_policy_ == "manual") {
request_->FollowDeferredRedirect(base::nullopt /* removed_headers */,
base::nullopt /* modified_headers */);
2017-03-23 19:37:54 +00:00
}
}
void AtomURLRequest::DoSetExtraHeader(const std::string& name,
const std::string& value) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
request_->SetExtraRequestHeaderByName(name, value, true);
}
void AtomURLRequest::DoRemoveExtraHeader(const std::string& name) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
request_->RemoveRequestHeaderByName(name);
}
2016-10-14 09:50:47 +00:00
void AtomURLRequest::DoSetAuth(const base::string16& username,
const base::string16& password) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
2016-09-21 15:35:03 +00:00
request_->SetAuth(net::AuthCredentials(username, password));
}
2016-09-21 15:35:03 +00:00
void AtomURLRequest::DoCancelAuth() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
2016-09-21 15:35:03 +00:00
request_->CancelAuth();
}
void AtomURLRequest::DoCancelWithError(const std::string& error,
bool isRequestError) {
DoCancel();
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&AtomURLRequest::InformDelegateErrorOccured, this, error,
isRequestError));
}
void AtomURLRequest::DoSetLoadFlags(int flags) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
request_->SetLoadFlags(request_->load_flags() | flags);
}
2017-03-23 19:37:54 +00:00
void AtomURLRequest::OnReceivedRedirect(net::URLRequest* request,
const net::RedirectInfo& info,
bool* defer_redirect) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_ || redirect_policy_ == "follow")
return;
if (redirect_policy_ == "error") {
request->Cancel();
DoCancelWithError(
"Request cannot follow redirect with the current redirect mode", true);
} else if (redirect_policy_ == "manual") {
*defer_redirect = true;
scoped_refptr<net::HttpResponseHeaders> response_headers =
request->response_headers();
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&AtomURLRequest::InformDelegateReceivedRedirect, this,
info.status_code, info.new_method, info.new_url,
response_headers));
2017-03-23 19:37:54 +00:00
}
}
2016-10-14 09:50:47 +00:00
void AtomURLRequest::OnAuthRequired(net::URLRequest* request,
chore: bump chromium to 1e9f9a24aa12 (master) (#17880) * chore: bump chromium in DEPS to 1e9f9a24aa12bea9cf194a82a7e249bd1242ec4f * chore: update patches * Make WebContents' theme color a base::Optional<SkColor> https://chromium-review.googlesource.com/c/chromium/src/+/1540022 * update autofill patch for incorrect header includes * Move Shell messages to web_test and rename to BlinkTest. https://chromium-review.googlesource.com/c/chromium/src/+/1525181 * Make PlatformNotificationServiceImpl a KeyedService. https://chromium-review.googlesource.com/c/chromium/src/+/1336150 * Move MediaPlayerId to its own file. https://chromium-review.googlesource.com/c/chromium/src/+/1547057 * Remove net/base/completion_callback.h, which is no longer used https://chromium-review.googlesource.com/c/chromium/src/+/1552821 * AW NS: support file scheme cookies https://chromium-review.googlesource.com/c/chromium/src/+/1533486 * Remove SecurityInfo and adapt remaining consumers https://chromium-review.googlesource.com/c/chromium/src/+/1509455 * Remove deprecated type-specific number to string conversion functions https://chromium-review.googlesource.com/c/chromium/src/+/1545881 * DevTools: Adding new performance histograms for launch of top 4 tools https://chromium-review.googlesource.com/c/chromium/src/+/1506388 * Update include paths for //base/hash/hash.h https://chromium-review.googlesource.com/c/chromium/src/+/1544630 * build: Disable ensure_gn_version gclient hook for mac CI checkout * update patches * use maybe version of v8::String::NewFromTwoByte * bump appveyor image version * fix mac ci hopefully * Convert enum to enum class for MenuAnchorPosition https://chromium-review.googlesource.com/c/chromium/src/+/1530508 * use maybe version of ToObject * RenderViewHost::GetProcess is no longer const * Unrefcount AuthChallengeInfo https://chromium-review.googlesource.com/c/chromium/src/+/1550631 * MenuButtonController takes Button rather than MenuButton https://chromium-review.googlesource.com/c/chromium/src/+/1500935 * add //ui/views_bridge_mac to deps to fix link error * forward declare views::Button in atom::MenuDelegate * more v8 patches * base/{=> hash}/md5.h https://chromium-review.googlesource.com/c/chromium/src/+/1535124 * gfx::{PlatformFontWin => win}::* https://chromium-review.googlesource.com/c/chromium/src/+/1534178 * fix v8 patches * [base] Rename TaskScheduler to ThreadPool https://chromium-review.googlesource.com/c/chromium/src/+/1561552 * use internal_config_base for bytecode_builtins_list_generator avoids windows link errors * FIXME: temporarily disable v8/breakpad integration * FIXME: temporarily disable prevent-will-redirect test * FIXME: disable neon on aarch64 pending crbug.com/953815 * update to account for WebCursor refactor https://chromium-review.googlesource.com/c/chromium/src/+/1562755 * enable stack dumping on appveyor * Revert "FIXME: disable neon on aarch64 pending crbug.com/953815" This reverts commit 57f082026be3d83069f2a2814684abf4dc9e7b53. * fix: remove const qualifiers to match upstream * fix: remove const qualifiers to match upstream in cc files as well * don't throw an error when testing if an object is an object * use non-deprecated Buffer constructor * Remove net::CookieSameSite::DEFAULT_MODE enum value https://chromium-review.googlesource.com/c/chromium/src/+/1567955 * depend on modded dbus-native to work around buffer deprecation https://github.com/sidorares/dbus-native/pull/262 * revert clang roll to fix arm build on linux * fixup! depend on modded dbus-native to work around buffer deprecation need more coffee * update coffee-script * robustify verify-mksnapshot w.r.t. command-line parameters * Revert "robustify verify-mksnapshot w.r.t. command-line parameters" This reverts commit a49af01411f684f6025528d604895c3696e0bc57. * fix mksnapshot by matching args * update patches * TMP: enable rdp on appveyor * Changed ContentBrowserClient::CreateQuotaPermissionContext() to return scoped_refptr. https://chromium-review.googlesource.com/c/chromium/src/+/1569376 * Make content::ResourceType an enum class. https://chromium-review.googlesource.com/c/chromium/src/+/1569345 * fixup! Make content::ResourceType an enum class. * turn off rdp * use net::CompletionRepeatingCallback instead of base::Callback<void(int)> * remove disable_ensure_gn_version_gclient_hook.patch * copy repeating callback instead of std::move * fix lint * add completion_repeating_callback.h include
2019-04-20 17:20:37 +00:00
const net::AuthChallengeInfo& auth_info) {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
2016-09-29 09:31:08 +00:00
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&AtomURLRequest::InformDelegateAuthenticationRequired,
chore: bump chromium to 1e9f9a24aa12 (master) (#17880) * chore: bump chromium in DEPS to 1e9f9a24aa12bea9cf194a82a7e249bd1242ec4f * chore: update patches * Make WebContents' theme color a base::Optional<SkColor> https://chromium-review.googlesource.com/c/chromium/src/+/1540022 * update autofill patch for incorrect header includes * Move Shell messages to web_test and rename to BlinkTest. https://chromium-review.googlesource.com/c/chromium/src/+/1525181 * Make PlatformNotificationServiceImpl a KeyedService. https://chromium-review.googlesource.com/c/chromium/src/+/1336150 * Move MediaPlayerId to its own file. https://chromium-review.googlesource.com/c/chromium/src/+/1547057 * Remove net/base/completion_callback.h, which is no longer used https://chromium-review.googlesource.com/c/chromium/src/+/1552821 * AW NS: support file scheme cookies https://chromium-review.googlesource.com/c/chromium/src/+/1533486 * Remove SecurityInfo and adapt remaining consumers https://chromium-review.googlesource.com/c/chromium/src/+/1509455 * Remove deprecated type-specific number to string conversion functions https://chromium-review.googlesource.com/c/chromium/src/+/1545881 * DevTools: Adding new performance histograms for launch of top 4 tools https://chromium-review.googlesource.com/c/chromium/src/+/1506388 * Update include paths for //base/hash/hash.h https://chromium-review.googlesource.com/c/chromium/src/+/1544630 * build: Disable ensure_gn_version gclient hook for mac CI checkout * update patches * use maybe version of v8::String::NewFromTwoByte * bump appveyor image version * fix mac ci hopefully * Convert enum to enum class for MenuAnchorPosition https://chromium-review.googlesource.com/c/chromium/src/+/1530508 * use maybe version of ToObject * RenderViewHost::GetProcess is no longer const * Unrefcount AuthChallengeInfo https://chromium-review.googlesource.com/c/chromium/src/+/1550631 * MenuButtonController takes Button rather than MenuButton https://chromium-review.googlesource.com/c/chromium/src/+/1500935 * add //ui/views_bridge_mac to deps to fix link error * forward declare views::Button in atom::MenuDelegate * more v8 patches * base/{=> hash}/md5.h https://chromium-review.googlesource.com/c/chromium/src/+/1535124 * gfx::{PlatformFontWin => win}::* https://chromium-review.googlesource.com/c/chromium/src/+/1534178 * fix v8 patches * [base] Rename TaskScheduler to ThreadPool https://chromium-review.googlesource.com/c/chromium/src/+/1561552 * use internal_config_base for bytecode_builtins_list_generator avoids windows link errors * FIXME: temporarily disable v8/breakpad integration * FIXME: temporarily disable prevent-will-redirect test * FIXME: disable neon on aarch64 pending crbug.com/953815 * update to account for WebCursor refactor https://chromium-review.googlesource.com/c/chromium/src/+/1562755 * enable stack dumping on appveyor * Revert "FIXME: disable neon on aarch64 pending crbug.com/953815" This reverts commit 57f082026be3d83069f2a2814684abf4dc9e7b53. * fix: remove const qualifiers to match upstream * fix: remove const qualifiers to match upstream in cc files as well * don't throw an error when testing if an object is an object * use non-deprecated Buffer constructor * Remove net::CookieSameSite::DEFAULT_MODE enum value https://chromium-review.googlesource.com/c/chromium/src/+/1567955 * depend on modded dbus-native to work around buffer deprecation https://github.com/sidorares/dbus-native/pull/262 * revert clang roll to fix arm build on linux * fixup! depend on modded dbus-native to work around buffer deprecation need more coffee * update coffee-script * robustify verify-mksnapshot w.r.t. command-line parameters * Revert "robustify verify-mksnapshot w.r.t. command-line parameters" This reverts commit a49af01411f684f6025528d604895c3696e0bc57. * fix mksnapshot by matching args * update patches * TMP: enable rdp on appveyor * Changed ContentBrowserClient::CreateQuotaPermissionContext() to return scoped_refptr. https://chromium-review.googlesource.com/c/chromium/src/+/1569376 * Make content::ResourceType an enum class. https://chromium-review.googlesource.com/c/chromium/src/+/1569345 * fixup! Make content::ResourceType an enum class. * turn off rdp * use net::CompletionRepeatingCallback instead of base::Callback<void(int)> * remove disable_ensure_gn_version_gclient_hook.patch * copy repeating callback instead of std::move * fix lint * add completion_repeating_callback.h include
2019-04-20 17:20:37 +00:00
this, auth_info));
}
void AtomURLRequest::OnResponseStarted(net::URLRequest* request,
int net_error) {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
2016-09-21 15:35:03 +00:00
DCHECK_EQ(request, request_.get());
scoped_refptr<net::HttpResponseHeaders> response_headers =
2016-10-14 09:50:47 +00:00
request->response_headers();
const auto& status = request_->status();
if (status.is_success()) {
// Success or pending trigger a Read.
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&AtomURLRequest::InformDelegateResponseStarted, this,
response_headers));
ReadResponse();
} else if (status.status() == net::URLRequestStatus::Status::FAILED) {
// Report error on Start.
DoCancelWithError(net::ErrorToString(net_error), true);
}
// We don't report an error is the request is canceled.
}
void AtomURLRequest::ReadResponse() {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
int bytes_read = -1;
if (request_->Read(response_read_buffer_.get(), kBufferSize, &bytes_read)) {
OnReadCompleted(request_.get(), bytes_read);
}
}
2016-10-14 09:50:47 +00:00
void AtomURLRequest::OnReadCompleted(net::URLRequest* request, int bytes_read) {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request_) {
return;
}
2016-09-21 15:35:03 +00:00
DCHECK_EQ(request, request_.get());
const auto status = request_->status();
if (status.error() == bytes_read &&
bytes_read == net::ERR_CONTENT_DECODING_INIT_FAILED) {
// When the request job is unable to create a source stream for the
// content encoding, we fail the request.
DoCancelWithError(net::ErrorToString(net::ERR_CONTENT_DECODING_INIT_FAILED),
true);
return;
}
bool response_error = false;
bool data_ended = false;
bool data_transfer_error = false;
do {
if (!status.is_success()) {
response_error = true;
break;
}
if (bytes_read == 0) {
data_ended = true;
break;
}
if (bytes_read < 0 || !CopyAndPostBuffer(bytes_read)) {
data_transfer_error = true;
break;
}
2016-10-14 09:50:47 +00:00
} while (
request_->Read(response_read_buffer_.get(), kBufferSize, &bytes_read));
if (response_error) {
DoCancelWithError(net::ErrorToString(status.ToNetError()), false);
} else if (data_ended) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&AtomURLRequest::InformDelegateResponseCompleted, this));
DoTerminate();
} else if (data_transfer_error) {
// We abort the request on corrupted data transfer.
DoCancelWithError("Failed to transfer data from IO to UI thread.", false);
}
}
void AtomURLRequest::OnContextShuttingDown() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DoCancel();
}
bool AtomURLRequest::CopyAndPostBuffer(int bytes_read) {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// data is only a wrapper for the asynchronous response_read_buffer_.
// Make a deep copy of payload and transfer ownership to the UI thread.
2017-12-18 00:23:02 +00:00
auto buffer_copy = WrapRefCounted(new net::IOBufferWithSize(bytes_read));
2016-09-26 12:03:49 +00:00
memcpy(buffer_copy->data(), response_read_buffer_->data(), bytes_read);
return base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&AtomURLRequest::InformDelegateResponseData, this,
buffer_copy));
}
2017-03-23 19:37:54 +00:00
void AtomURLRequest::InformDelegateReceivedRedirect(
int status_code,
const std::string& method,
const GURL& url,
scoped_refptr<net::HttpResponseHeaders> response_headers) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_)
delegate_->OnReceivedRedirect(status_code, method, url, response_headers);
}
2016-09-21 15:35:03 +00:00
void AtomURLRequest::InformDelegateAuthenticationRequired(
chore: bump chromium to 1e9f9a24aa12 (master) (#17880) * chore: bump chromium in DEPS to 1e9f9a24aa12bea9cf194a82a7e249bd1242ec4f * chore: update patches * Make WebContents' theme color a base::Optional<SkColor> https://chromium-review.googlesource.com/c/chromium/src/+/1540022 * update autofill patch for incorrect header includes * Move Shell messages to web_test and rename to BlinkTest. https://chromium-review.googlesource.com/c/chromium/src/+/1525181 * Make PlatformNotificationServiceImpl a KeyedService. https://chromium-review.googlesource.com/c/chromium/src/+/1336150 * Move MediaPlayerId to its own file. https://chromium-review.googlesource.com/c/chromium/src/+/1547057 * Remove net/base/completion_callback.h, which is no longer used https://chromium-review.googlesource.com/c/chromium/src/+/1552821 * AW NS: support file scheme cookies https://chromium-review.googlesource.com/c/chromium/src/+/1533486 * Remove SecurityInfo and adapt remaining consumers https://chromium-review.googlesource.com/c/chromium/src/+/1509455 * Remove deprecated type-specific number to string conversion functions https://chromium-review.googlesource.com/c/chromium/src/+/1545881 * DevTools: Adding new performance histograms for launch of top 4 tools https://chromium-review.googlesource.com/c/chromium/src/+/1506388 * Update include paths for //base/hash/hash.h https://chromium-review.googlesource.com/c/chromium/src/+/1544630 * build: Disable ensure_gn_version gclient hook for mac CI checkout * update patches * use maybe version of v8::String::NewFromTwoByte * bump appveyor image version * fix mac ci hopefully * Convert enum to enum class for MenuAnchorPosition https://chromium-review.googlesource.com/c/chromium/src/+/1530508 * use maybe version of ToObject * RenderViewHost::GetProcess is no longer const * Unrefcount AuthChallengeInfo https://chromium-review.googlesource.com/c/chromium/src/+/1550631 * MenuButtonController takes Button rather than MenuButton https://chromium-review.googlesource.com/c/chromium/src/+/1500935 * add //ui/views_bridge_mac to deps to fix link error * forward declare views::Button in atom::MenuDelegate * more v8 patches * base/{=> hash}/md5.h https://chromium-review.googlesource.com/c/chromium/src/+/1535124 * gfx::{PlatformFontWin => win}::* https://chromium-review.googlesource.com/c/chromium/src/+/1534178 * fix v8 patches * [base] Rename TaskScheduler to ThreadPool https://chromium-review.googlesource.com/c/chromium/src/+/1561552 * use internal_config_base for bytecode_builtins_list_generator avoids windows link errors * FIXME: temporarily disable v8/breakpad integration * FIXME: temporarily disable prevent-will-redirect test * FIXME: disable neon on aarch64 pending crbug.com/953815 * update to account for WebCursor refactor https://chromium-review.googlesource.com/c/chromium/src/+/1562755 * enable stack dumping on appveyor * Revert "FIXME: disable neon on aarch64 pending crbug.com/953815" This reverts commit 57f082026be3d83069f2a2814684abf4dc9e7b53. * fix: remove const qualifiers to match upstream * fix: remove const qualifiers to match upstream in cc files as well * don't throw an error when testing if an object is an object * use non-deprecated Buffer constructor * Remove net::CookieSameSite::DEFAULT_MODE enum value https://chromium-review.googlesource.com/c/chromium/src/+/1567955 * depend on modded dbus-native to work around buffer deprecation https://github.com/sidorares/dbus-native/pull/262 * revert clang roll to fix arm build on linux * fixup! depend on modded dbus-native to work around buffer deprecation need more coffee * update coffee-script * robustify verify-mksnapshot w.r.t. command-line parameters * Revert "robustify verify-mksnapshot w.r.t. command-line parameters" This reverts commit a49af01411f684f6025528d604895c3696e0bc57. * fix mksnapshot by matching args * update patches * TMP: enable rdp on appveyor * Changed ContentBrowserClient::CreateQuotaPermissionContext() to return scoped_refptr. https://chromium-review.googlesource.com/c/chromium/src/+/1569376 * Make content::ResourceType an enum class. https://chromium-review.googlesource.com/c/chromium/src/+/1569345 * fixup! Make content::ResourceType an enum class. * turn off rdp * use net::CompletionRepeatingCallback instead of base::Callback<void(int)> * remove disable_ensure_gn_version_gclient_hook.patch * copy repeating callback instead of std::move * fix lint * add completion_repeating_callback.h include
2019-04-20 17:20:37 +00:00
const net::AuthChallengeInfo& auth_info) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_)
2016-09-21 15:35:03 +00:00
delegate_->OnAuthenticationRequired(auth_info);
}
void AtomURLRequest::InformDelegateResponseStarted(
scoped_refptr<net::HttpResponseHeaders> response_headers) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_)
delegate_->OnResponseStarted(response_headers);
}
2016-09-21 15:35:03 +00:00
void AtomURLRequest::InformDelegateResponseData(
2016-10-14 08:58:16 +00:00
scoped_refptr<net::IOBufferWithSize> data) const {
2016-09-21 15:35:03 +00:00
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Transfer ownership of the data buffer, data will be released
// by the delegate's OnResponseData.
if (delegate_)
delegate_->OnResponseData(data);
}
2016-09-21 15:35:03 +00:00
void AtomURLRequest::InformDelegateResponseCompleted() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_)
delegate_->OnResponseCompleted();
}
void AtomURLRequest::InformDelegateErrorOccured(const std::string& error,
bool isRequestError) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_)
delegate_->OnError(error, isRequestError);
}
void AtomURLRequest::GetUploadProgress(mate::Dictionary* progress) const {
net::UploadProgress upload_progress;
if (request_) {
progress->Set("started", true);
upload_progress = request_->GetUploadProgress();
} else {
progress->Set("started", false);
}
progress->Set("current", upload_progress.position());
progress->Set("total", upload_progress.size());
}
2016-09-29 09:31:08 +00:00
} // namespace atom