Fixing cpplint issues.

This commit is contained in:
ali.ibrahim 2016-09-29 11:31:08 +02:00
parent 08947682b0
commit 0588435882
6 changed files with 83 additions and 100 deletions

View file

@ -4,8 +4,8 @@
#include "atom/browser/api/atom_api_net.h"
#include "atom/browser/api/atom_api_url_request.h"
#include "native_mate/dictionary.h"
#include "atom/common/node_includes.h"
#include "native_mate/dictionary.h"
namespace atom {
@ -50,7 +50,6 @@ using atom::api::URLRequest;
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));
@ -58,9 +57,8 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
mate::Dictionary dict(isolate, exports);
dict.Set("net", Net::Create(isolate));
dict.Set("Net", Net::GetConstructor(isolate)->GetFunction());
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_net, Initialize)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_net, Initialize)

View file

@ -12,20 +12,18 @@ namespace atom {
namespace api {
class Net : public mate::EventEmitter<Net> {
public:
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
v8::Local<v8::Value> URLRequest(v8::Isolate* isolate);
protected:
Net(v8::Isolate* isolate);
protected:
explicit Net(v8::Isolate* isolate);
~Net() override;
private:
private:
DISALLOW_COPY_AND_ASSIGN(Net);
};
@ -34,4 +32,4 @@ private:
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_NET_H_
#endif // ATOM_BROWSER_API_ATOM_API_NET_H_

View file

@ -2,31 +2,24 @@
// 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 "native_mate/dictionary.h"
#include "atom/browser/api/atom_api_url_request.h"
#include "atom/browser/net/atom_url_request.h"
#include "atom/common/node_includes.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/node_includes.h"
#include "native_mate/dictionary.h"
namespace {
const char* const kResponse = "response";
const char* const kData = "data";
const char* const kEnd = "end";
}
namespace mate {
template<>
struct Converter<scoped_refptr<const net::HttpResponseHeaders>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
scoped_refptr<const net::HttpResponseHeaders> val) {
v8::Isolate* isolate,
scoped_refptr<const net::HttpResponseHeaders> val) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
if (val) {
size_t iter = 0;
@ -34,7 +27,7 @@ struct Converter<scoped_refptr<const net::HttpResponseHeaders>> {
std::string value;
while (val->EnumerateHeaderLines(&iter, &name, &value)) {
dict.Set(name, value);
}
}
}
return dict.GetHandle();
}
@ -43,29 +36,27 @@ struct Converter<scoped_refptr<const net::HttpResponseHeaders>> {
template<>
struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
scoped_refptr<const net::IOBufferWithSize> buffer) {
v8::Isolate* isolate,
scoped_refptr<const net::IOBufferWithSize> buffer) {
return node::Buffer::Copy(isolate,
buffer->data(),
buffer->size()).ToLocalChecked();
buffer->data(),
buffer->size()).ToLocalChecked();
}
static bool FromV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<const net::IOBufferWithSize>* out) {
v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<const net::IOBufferWithSize>* out) {
auto size = node::Buffer::Length(val);
if (size == 0) {
// Support conversoin from empty buffer. A use case is
// a GET request without body.
// Support conversoin from empty buffer. A use case is
// a GET request without body.
// Since zero-sized IOBuffer(s) are not supported, we set the
// out pointer to null.
*out = nullptr;
return true;
}
auto data = node::Buffer::Data(val);
if (!data) {
// This is an error as size is positif but data is null.
@ -86,9 +77,9 @@ struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
}
};
}
namespace atom {
} // namespace mate
namespace atom {
namespace api {
URLRequest::URLRequest(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
@ -101,7 +92,6 @@ URLRequest::~URLRequest() {
// static
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
v8::Local<v8::Object> options;
args->GetNext(&options);
mate::Dictionary dict(args->isolate(), options);
@ -122,7 +112,7 @@ mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
method,
url,
weak_ptr);
api_url_request->atom_request_ = atom_url_request;
return api_url_request;
@ -146,15 +136,12 @@ void URLRequest::BuildPrototype(v8::Isolate* isolate,
.SetProperty("rawResponseHeaders", &URLRequest::RawResponseHeaders)
.SetProperty("httpVersionMajor", &URLRequest::ResponseHttpVersionMajor)
.SetProperty("httpVersionMinor", &URLRequest::ResponseHttpVersionMinor);
}
bool URLRequest::Write(
scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last) {
return atom_request_->Write(buffer, is_last);
}
@ -191,7 +178,7 @@ void URLRequest::OnAuthenticationRequired(
auth_info.get(),
base::Bind(&AtomURLRequest::PassLoginInformation, atom_request_));
}
void URLRequest::OnResponseStarted() {
Emit("response");
@ -202,7 +189,6 @@ void URLRequest::OnResponseData(
if (!buffer || !buffer->data() || !buffer->size()) {
return;
}
EmitResponseEvent("data", buffer);
}
@ -234,7 +220,7 @@ std::string URLRequest::StatusMessage() const {
scoped_refptr<const net::HttpResponseHeaders>
URLRequest::RawResponseHeaders() const {
return atom_request_->GetResponseHeaders();
return atom_request_->GetResponseHeaders();
}
uint32_t URLRequest::ResponseHttpVersionMajor() const {
@ -261,6 +247,6 @@ void URLRequest::unpin() {
wrapper_.Reset();
}
} // namespace mate
} // namespace api
} // namepsace mate
} // namespace atom

View file

@ -6,10 +6,11 @@
#define ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_
#include <array>
#include <string>
#include "atom/browser/api/trackable_object.h"
#include "native_mate/handle.h"
#include "net/url_request/url_request_context.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request_context.h"
namespace atom {
@ -22,18 +23,19 @@ class URLRequest : public mate::EventEmitter<URLRequest> {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
static void BuildPrototype(
v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
protected:
URLRequest(v8::Isolate* isolate,
explicit URLRequest(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper);
~URLRequest() override;
private:
private:
bool Write(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last);
bool is_last);
void Abort();
bool SetExtraHeader(const std::string& name, const std::string& value);
void RemoveExtraHeader(const std::string& name);
@ -55,7 +57,7 @@ private:
template <typename ... ArgTypes>
std::array<v8::Local<v8::Value>, sizeof...(ArgTypes)>
std::array<v8::Local<v8::Value>, sizeof...(ArgTypes)>
BuildArgsArray(ArgTypes... args) const;
template <typename ... ArgTypes>
@ -70,7 +72,6 @@ private:
scoped_refptr<AtomURLRequest> atom_request_;
v8::Global<v8::Object> wrapper_;
base::WeakPtrFactory<URLRequest> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequest);
};
@ -107,8 +108,8 @@ void URLRequest::EmitResponseEvent(ArgTypes... args) {
} // namepsace api
} // namespace api
} // namepsace atom
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_
#endif // ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_

View file

@ -3,19 +3,21 @@
// 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/io_buffer.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
} // namespace
namespace atom {
@ -23,7 +25,7 @@ namespace internal {
class UploadOwnedIOBufferElementReader : public net::UploadBytesElementReader {
public:
public:
explicit UploadOwnedIOBufferElementReader(
scoped_refptr<const net::IOBufferWithSize> buffer)
: net::UploadBytesElementReader(buffer->data(), buffer->size())
@ -39,18 +41,18 @@ public:
return new UploadOwnedIOBufferElementReader(std::move(buffer));
}
private:
private:
scoped_refptr<const net::IOBuffer> buffer_;
DISALLOW_COPY_AND_ASSIGN(UploadOwnedIOBufferElementReader);
};
}
} // namespace internal
AtomURLRequest::AtomURLRequest(base::WeakPtr<api::URLRequest> delegate)
: delegate_(delegate)
, response_read_buffer_(new net::IOBuffer(kBufferSize))
, is_chunked_upload_(is_chunked_upload_) {
, is_chunked_upload_(false) {
}
AtomURLRequest::~AtomURLRequest() {
@ -73,7 +75,7 @@ scoped_refptr<AtomURLRequest> AtomURLRequest::Create(
DCHECK(context);
scoped_refptr<AtomURLRequest> atom_url_request =
scoped_refptr<AtomURLRequest> atom_url_request =
new AtomURLRequest(delegate);
atom_url_request->request_ = context->CreateRequest(GURL(url),
@ -82,14 +84,13 @@ scoped_refptr<AtomURLRequest> AtomURLRequest::Create(
atom_url_request->request_->set_method(method);
return atom_url_request;
}
bool AtomURLRequest::Write(
scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequest::DoWriteBuffer, this, buffer, is_last));
@ -154,7 +155,6 @@ void AtomURLRequest::DoWriteBuffer(
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (is_chunked_upload_) {
// Chunked encoding case.
bool first_call = false;
@ -181,9 +181,7 @@ void AtomURLRequest::DoWriteBuffer(
if (first_call)
request_->Start();
}
else {
} else {
if (buffer) {
// Handling potential empty buffers.
using internal::UploadOwnedIOBufferElementReader;
@ -223,10 +221,10 @@ void AtomURLRequest::DoCancelAuth() const {
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,
base::Bind(&AtomURLRequest::InformDelegateAuthenticationRequired,
this,
scoped_refptr<net::AuthChallengeInfo>(auth_info)));
}
@ -247,12 +245,13 @@ void AtomURLRequest::OnResponseStarted(net::URLRequest* request) {
base::Bind(&AtomURLRequest::InformDelegateResponseStarted, this));
ReadResponse();
}
else {
} else {
auto error = net::ErrorToString(status.ToNetError());
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateErrorOccured, this, std::move(error)));
base::Bind(&AtomURLRequest::InformDelegateErrorOccured,
this,
std::move(error)));
}
}
@ -264,10 +263,10 @@ void AtomURLRequest::ReadResponse() {
// completed immediately, without trying to read any data back (all we care
// about is the response code and headers, which we already have).
int bytes_read = 0;
if (request_->status().is_success()
if (request_->status().is_success()
/* TODO && (request_type_ != URLFetcher::HEAD)*/)
if (!request_->Read(response_read_buffer_.get(), kBufferSize, &bytes_read))
bytes_read = -1;
bytes_read = -1;
OnReadCompleted(request_.get(), bytes_read);
}
@ -284,7 +283,9 @@ void AtomURLRequest::OnReadCompleted(net::URLRequest* request,
auto error = net::ErrorToString(status.ToNetError());
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateErrorOccured, this, std::move(error)));
base::Bind(&AtomURLRequest::InformDelegateErrorOccured,
this,
std::move(error)));
break;
}
@ -295,15 +296,13 @@ void AtomURLRequest::OnReadCompleted(net::URLRequest* request,
} while (request_->Read(response_read_buffer_.get(),
kBufferSize,
&bytes_read));
if (!status.is_io_pending()
if (!status.is_io_pending()
/* TODO || request_type_ == URLFetcher::HEAD*/ )
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateResponseCompleted, this));
}
bool AtomURLRequest::CopyAndPostBuffer(int bytes_read) {
@ -361,4 +360,4 @@ void AtomURLRequest::InformDelegateErrorOccured(
}
} // namespace atom
} // namespace atom

View file

@ -6,9 +6,11 @@
#ifndef ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
#define ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
#include <string>
#include <vector>
#include "base/memory/ref_counted.h"
#include "net/url_request/url_request.h"
#include "net/base/chunked_upload_data_stream.h"
#include "net/url_request/url_request.h"
namespace net {
@ -27,7 +29,7 @@ class URLRequest;
class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
public net::URLRequest::Delegate {
public:
public:
static scoped_refptr<AtomURLRequest> Create(
AtomBrowserContext* browser_context,
const std::string& method,
@ -44,15 +46,15 @@ public:
const base::string16& password) const;
scoped_refptr<const net::HttpResponseHeaders> GetResponseHeaders() const;
protected:
protected:
// Overrides of net::URLRequest::Delegate
virtual void OnAuthRequired(net::URLRequest* request,
void OnAuthRequired(net::URLRequest* request,
net::AuthChallengeInfo* auth_info) override;
virtual void OnResponseStarted(net::URLRequest* request) override;
virtual void OnReadCompleted(net::URLRequest* request,
void OnResponseStarted(net::URLRequest* request) override;
void OnReadCompleted(net::URLRequest* request,
int bytes_read) override;
private:
private:
friend class base::RefCountedThreadSafe<AtomURLRequest>;
void DoWriteBuffer(scoped_refptr<const net::IOBufferWithSize> buffer,
bool is_last);
@ -72,7 +74,7 @@ private:
void InformDelegateResponseCompleted() const;
void InformDelegateErrorOccured(const std::string& error) const;
AtomURLRequest(base::WeakPtr<api::URLRequest> delegate);
explicit AtomURLRequest(base::WeakPtr<api::URLRequest> delegate);
virtual ~AtomURLRequest();
base::WeakPtr<api::URLRequest> delegate_;
@ -81,15 +83,14 @@ private:
bool is_chunked_upload_;
std::unique_ptr<net::ChunkedUploadDataStream> chunked_stream_;
std::unique_ptr<net::ChunkedUploadDataStream::Writer> chunked_stream_writer_;
std::vector<std::unique_ptr<net::UploadElementReader>>upload_element_readers_;
std::vector<std::unique_ptr<net::UploadElementReader>>
upload_element_readers_;
scoped_refptr<net::IOBuffer> response_read_buffer_;
scoped_refptr<net::HttpResponseHeaders> response_headers_;
DISALLOW_COPY_AND_ASSIGN(AtomURLRequest);
};
DISALLOW_COPY_AND_ASSIGN(AtomURLRequest);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_
#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_