Formatting C++ code using ClangFormat.

This commit is contained in:
ali.ibrahim 2016-10-14 11:50:47 +02:00
parent 9cc8bfae1c
commit 4347ce4a53
6 changed files with 93 additions and 150 deletions

View file

@ -15,9 +15,7 @@ Net::Net(v8::Isolate* isolate) {
Init(isolate);
}
Net::~Net() {
}
Net::~Net() {}
// static
v8::Local<v8::Value> Net::Create(v8::Isolate* isolate) {
@ -38,25 +36,24 @@ v8::Local<v8::Value> Net::URLRequest(v8::Isolate* isolate) {
return URLRequest::GetConstructor(isolate)->GetFunction();
}
void Net::RequestGarbageCollectionForTesting() {
isolate()->RequestGarbageCollectionForTesting(
v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
}
} // namespace api
} // namespace atom
namespace {
using atom::api::Net;
using atom::api::URLRequest;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
URLRequest::SetConstructor(isolate, base::Bind(URLRequest::New));

View file

@ -19,6 +19,7 @@ class Net : public mate::EventEmitter<Net> {
v8::Local<v8::FunctionTemplate> prototype);
v8::Local<v8::Value> URLRequest(v8::Isolate* isolate);
protected:
explicit Net(v8::Isolate* isolate);
~Net() override;
@ -32,5 +33,4 @@ class Net : public mate::EventEmitter<Net> {
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_NET_H_

View file

@ -2,9 +2,9 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_url_request.h"
#include <string>
#include "atom/browser/api/atom_api_session.h"
#include "atom/browser/api/atom_api_url_request.h"
#include "atom/browser/net/atom_url_request.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/net_converter.h"
@ -12,8 +12,6 @@
#include "atom/common/node_includes.h"
#include "native_mate/dictionary.h"
namespace mate {
template <>
@ -21,13 +19,11 @@ struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
scoped_refptr<const net::IOBufferWithSize> buffer) {
return node::Buffer::Copy(isolate,
buffer->data(),
buffer->size()).ToLocalChecked();
return node::Buffer::Copy(isolate, buffer->data(), buffer->size())
.ToLocalChecked();
}
static bool FromV8(
v8::Isolate* isolate,
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<const net::IOBufferWithSize>* out) {
auto size = node::Buffer::Length(val);
@ -65,16 +61,14 @@ struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
namespace atom {
namespace api {
template <typename Flags>
URLRequest::StateBase<Flags>::StateBase(Flags initialState)
: state_(initialState) {
}
: state_(initialState) {}
template <typename Flags>
void URLRequest::StateBase<Flags>::SetFlag(Flags flag) {
state_ = static_cast<Flags>(static_cast<int>(state_) |
static_cast<int>(flag));
state_ =
static_cast<Flags>(static_cast<int>(state_) | static_cast<int>(flag));
}
template <typename Flags>
@ -88,8 +82,7 @@ bool URLRequest::StateBase<Flags>::IsFlagSet(Flags flag) const {
}
URLRequest::RequestState::RequestState()
: StateBase(RequestStateFlags::kNotStarted) {
}
: StateBase(RequestStateFlags::kNotStarted) {}
bool URLRequest::RequestState::NotStarted() const {
return *this == RequestStateFlags::kNotStarted;
@ -116,8 +109,7 @@ bool URLRequest::RequestState::Closed() const {
}
URLRequest::ResponseState::ResponseState()
: StateBase(ResponseStateFlags::kNotStarted) {
}
: StateBase(ResponseStateFlags::kNotStarted) {}
bool URLRequest::ResponseState::NotStarted() const {
return *this == ResponseStateFlags::kNotStarted;
@ -131,7 +123,6 @@ bool URLRequest::ResponseState::Ended() const {
return IsFlagSet(ResponseStateFlags::kEnded);
}
bool URLRequest::ResponseState::Failed() const {
return IsFlagSet(ResponseStateFlags::kFailed);
}
@ -141,8 +132,7 @@ URLRequest::URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
InitWith(isolate, wrapper);
}
URLRequest::~URLRequest() {
}
URLRequest::~URLRequest() {}
// static
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
@ -161,8 +151,8 @@ mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
auto browser_context = session->browser_context();
auto api_url_request = new URLRequest(args->isolate(), args->GetThis());
auto weak_ptr = api_url_request->weak_ptr_factory_.GetWeakPtr();
auto atom_url_request = AtomURLRequest::Create(browser_context, method, url,
weak_ptr);
auto atom_url_request =
AtomURLRequest::Create(browser_context, method, url, weak_ptr);
api_url_request->atom_request_ = atom_url_request;
@ -203,13 +193,10 @@ bool URLRequest::Canceled() const {
return request_state_.Canceled();
}
bool URLRequest::Write(
scoped_refptr<const net::IOBufferWithSize> buffer,
bool URLRequest::Write(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last) {
if (request_state_.Canceled() ||
request_state_.Failed() ||
request_state_.Finished() ||
request_state_.Closed()) {
if (request_state_.Canceled() || request_state_.Failed() ||
request_state_.Finished() || request_state_.Closed()) {
return false;
}
@ -231,10 +218,8 @@ bool URLRequest::Write(
return false;
}
void URLRequest::Cancel() {
if (request_state_.Canceled() ||
request_state_.Closed()) {
if (request_state_.Canceled() || request_state_.Closed()) {
// Cancel only once.
return;
}
@ -304,8 +289,7 @@ void URLRequest::SetChunkedUpload(bool is_chunked_upload) {
void URLRequest::OnAuthenticationRequired(
scoped_refptr<const net::AuthChallengeInfo> auth_info) {
if (request_state_.Canceled() ||
request_state_.Closed()) {
if (request_state_.Canceled() || request_state_.Closed()) {
return;
}
@ -315,16 +299,13 @@ void URLRequest::OnAuthenticationRequired(
}
EmitRequestEvent(
false,
"login",
auth_info.get(),
false, "login", auth_info.get(),
base::Bind(&AtomURLRequest::PassLoginInformation, atom_request_));
}
void URLRequest::OnResponseStarted(
scoped_refptr<net::HttpResponseHeaders> response_headers) {
if (request_state_.Canceled() ||
request_state_.Failed() ||
if (request_state_.Canceled() || request_state_.Failed() ||
request_state_.Closed()) {
// Don't emit any event after request cancel.
return;
@ -336,10 +317,8 @@ void URLRequest::OnResponseStarted(
void URLRequest::OnResponseData(
scoped_refptr<const net::IOBufferWithSize> buffer) {
if (request_state_.Canceled() ||
request_state_.Closed() ||
request_state_.Failed() ||
response_state_.Failed()) {
if (request_state_.Canceled() || request_state_.Closed() ||
request_state_.Failed() || response_state_.Failed()) {
// In case we received an unexpected event from Chromium net,
// don't emit any data event after request cancel/error/close.
return;
@ -351,10 +330,8 @@ void URLRequest::OnResponseData(
}
void URLRequest::OnResponseCompleted() {
if (request_state_.Canceled() ||
request_state_.Closed() ||
request_state_.Failed() ||
response_state_.Failed()) {
if (request_state_.Canceled() || request_state_.Closed() ||
request_state_.Failed() || response_state_.Failed()) {
// In case we received an unexpected event from Chromium net,
// don't emit any data event after request cancel/error/close.
return;
@ -378,7 +355,6 @@ void URLRequest::OnResponseError(const std::string& error) {
Close();
}
int URLRequest::StatusCode() const {
if (response_headers_) {
return response_headers_->response_code();
@ -394,8 +370,7 @@ std::string URLRequest::StatusMessage() const {
return result;
}
scoped_refptr<net::HttpResponseHeaders>
URLRequest::RawResponseHeaders() const {
scoped_refptr<net::HttpResponseHeaders> URLRequest::RawResponseHeaders() const {
return response_headers_;
}

View file

@ -175,7 +175,6 @@ class URLRequest : public mate::EventEmitter<URLRequest> {
void RemoveExtraHeader(const std::string& name);
void SetChunkedUpload(bool is_chunked_upload);
bool CanReadHeaders() const;
int StatusCode() const;
std::string StatusMessage() const;
scoped_refptr<net::HttpResponseHeaders> RawResponseHeaders() const;

View file

@ -3,17 +3,16 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/atom_url_request.h"
#include <string>
#include "atom/browser/api/atom_api_url_request.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/net/atom_url_request.h"
#include "base/callback.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/elements_upload_data_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/upload_bytes_element_reader.h"
namespace {
const int kBufferSize = 4096;
} // namespace
@ -27,11 +26,9 @@ class UploadOwnedIOBufferElementReader : public net::UploadBytesElementReader {
explicit UploadOwnedIOBufferElementReader(
scoped_refptr<const net::IOBufferWithSize> buffer)
: net::UploadBytesElementReader(buffer->data(), buffer->size()),
buffer_(buffer) {
}
buffer_(buffer) {}
~UploadOwnedIOBufferElementReader() override {
}
~UploadOwnedIOBufferElementReader() override {}
static UploadOwnedIOBufferElementReader* CreateWithBuffer(
scoped_refptr<const net::IOBufferWithSize> buffer) {
@ -49,11 +46,9 @@ class UploadOwnedIOBufferElementReader : public net::UploadBytesElementReader {
AtomURLRequest::AtomURLRequest(base::WeakPtr<api::URLRequest> delegate)
: delegate_(delegate),
is_chunked_upload_(false),
response_read_buffer_(new net::IOBuffer(kBufferSize)) {
}
response_read_buffer_(new net::IOBuffer(kBufferSize)) {}
AtomURLRequest::~AtomURLRequest() {
}
AtomURLRequest::~AtomURLRequest() {}
scoped_refptr<AtomURLRequest> AtomURLRequest::Create(
AtomBrowserContext* browser_context,
@ -71,8 +66,7 @@ scoped_refptr<AtomURLRequest> AtomURLRequest::Create(
auto request_context_getter = browser_context->url_request_context_getter();
scoped_refptr<AtomURLRequest> atom_url_request(new AtomURLRequest(delegate));
if (content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
content::BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequest::DoInitialize, atom_url_request,
request_context_getter, method, url))) {
return atom_url_request;
@ -90,15 +84,13 @@ void AtomURLRequest::DoInitialize(
auto context = request_context_getter->GetURLRequestContext();
DCHECK(context);
request_ = context->CreateRequest(GURL(url),
net::RequestPriority::DEFAULT_PRIORITY,
this);
request_ = context->CreateRequest(
GURL(url), net::RequestPriority::DEFAULT_PRIORITY, this);
request_->set_method(method);
}
bool AtomURLRequest::Write(
scoped_refptr<const net::IOBufferWithSize> buffer,
bool AtomURLRequest::Write(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return content::BrowserThread::PostTask(
@ -117,9 +109,7 @@ void AtomURLRequest::SetChunkedUpload(bool is_chunked_upload) {
void AtomURLRequest::Cancel() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequest::DoCancel, this));
}
@ -127,16 +117,14 @@ void AtomURLRequest::SetExtraHeader(const std::string& name,
const std::string& value) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
content::BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequest::DoSetExtraHeader, this, name, value));
}
void AtomURLRequest::RemoveExtraHeader(const std::string& name) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
content::BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequest::DoRemoveExtraHeader, this, name));
}
@ -174,14 +162,11 @@ void AtomURLRequest::DoWriteBuffer(
if (buffer)
// Non-empty buffer.
chunked_stream_writer_->AppendData(buffer->data(),
buffer->size(),
chunked_stream_writer_->AppendData(buffer->data(), buffer->size(),
is_last);
else if (is_last)
// Empty buffer and last chunk, i.e. request.end().
chunked_stream_writer_->AppendData(nullptr,
0,
true);
chunked_stream_writer_->AppendData(nullptr, 0, true);
if (first_call) {
request_->Start();
@ -190,8 +175,8 @@ void AtomURLRequest::DoWriteBuffer(
if (buffer) {
// Handling potential empty buffers.
using internal::UploadOwnedIOBufferElementReader;
auto element_reader = UploadOwnedIOBufferElementReader::CreateWithBuffer(
std::move(buffer));
auto element_reader =
UploadOwnedIOBufferElementReader::CreateWithBuffer(std::move(buffer));
upload_element_readers_.push_back(
std::unique_ptr<net::UploadElementReader>(element_reader));
}
@ -221,8 +206,7 @@ void AtomURLRequest::DoRemoveExtraHeader(const std::string& name) const {
request_->RemoveRequestHeaderByName(name);
}
void AtomURLRequest::DoSetAuth(
const base::string16& username,
void AtomURLRequest::DoSetAuth(const base::string16& username,
const base::string16& password) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
request_->SetAuth(net::AuthCredentials(username, password));
@ -233,15 +217,13 @@ void AtomURLRequest::DoCancelAuth() const {
request_->CancelAuth();
}
void AtomURLRequest::OnAuthRequired(
net::URLRequest* request,
void AtomURLRequest::OnAuthRequired(net::URLRequest* request,
net::AuthChallengeInfo* auth_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateAuthenticationRequired,
this,
base::Bind(&AtomURLRequest::InformDelegateAuthenticationRequired, this,
scoped_refptr<net::AuthChallengeInfo>(auth_info)));
}
@ -256,8 +238,7 @@ void AtomURLRequest::OnResponseStarted(net::URLRequest* request) {
// Success or pending trigger a Read.
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateResponseStarted,
this,
base::Bind(&AtomURLRequest::InformDelegateResponseStarted, this,
response_headers));
ReadResponse();
@ -267,8 +248,7 @@ void AtomURLRequest::OnResponseStarted(net::URLRequest* request) {
auto error = net::ErrorToString(status.ToNetError());
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateRequestErrorOccured,
this,
base::Bind(&AtomURLRequest::InformDelegateRequestErrorOccured, this,
std::move(error)));
}
// We don't report an error is the request is canceled.
@ -283,9 +263,7 @@ void AtomURLRequest::ReadResponse() {
}
}
void AtomURLRequest::OnReadCompleted(net::URLRequest* request,
int bytes_read) {
void AtomURLRequest::OnReadCompleted(net::URLRequest* request, int bytes_read) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK_EQ(request, request_.get());
@ -308,16 +286,14 @@ void AtomURLRequest::OnReadCompleted(net::URLRequest* request,
data_transfer_error = true;
break;
}
} while (request_->Read(response_read_buffer_.get(),
kBufferSize,
&bytes_read));
} while (
request_->Read(response_read_buffer_.get(), kBufferSize, &bytes_read));
if (response_error) {
DoCancel();
auto error = net::ErrorToString(status.ToNetError());
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateResponseErrorOccured,
this,
base::Bind(&AtomURLRequest::InformDelegateResponseErrorOccured, this,
std::move(error)));
} else if (data_ended) {
content::BrowserThread::PostTask(
@ -328,8 +304,7 @@ void AtomURLRequest::OnReadCompleted(net::URLRequest* request,
DoCancel();
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateResponseErrorOccured,
this,
base::Bind(&AtomURLRequest::InformDelegateResponseErrorOccured, this,
"Failed to transfer data from IO to UI thread."));
}
}
@ -344,12 +319,10 @@ bool AtomURLRequest::CopyAndPostBuffer(int bytes_read) {
return content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateResponseData,
this,
base::Bind(&AtomURLRequest::InformDelegateResponseData, this,
buffer_copy));
}
void AtomURLRequest::InformDelegateAuthenticationRequired(
scoped_refptr<net::AuthChallengeInfo> auth_info) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

View file

@ -30,8 +30,7 @@ class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
const std::string& url,
base::WeakPtr<api::URLRequest> delegate);
bool Write(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last);
bool Write(scoped_refptr<const net::IOBufferWithSize> buffer, bool is_last);
void SetChunkedUpload(bool is_chunked_upload);
void Cancel() const;
void SetExtraHeader(const std::string& name, const std::string& value) const;