diff --git a/atom/browser/api/atom_api_net.cc b/atom/browser/api/atom_api_net.cc index 1559cee4caff..2464393fcd02 100644 --- a/atom/browser/api/atom_api_net.cc +++ b/atom/browser/api/atom_api_net.cc @@ -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 exports, v8::Local unused, v8::Local context, void* priv) { - v8::Isolate* isolate = context->GetIsolate(); URLRequest::SetConstructor(isolate, base::Bind(URLRequest::New)); @@ -58,9 +57,8 @@ void Initialize(v8::Local exports, v8::Local 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) \ No newline at end of file +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_net, Initialize) diff --git a/atom/browser/api/atom_api_net.h b/atom/browser/api/atom_api_net.h index dd97bb9653dd..640a6445ebc4 100644 --- a/atom/browser/api/atom_api_net.h +++ b/atom/browser/api/atom_api_net.h @@ -12,20 +12,18 @@ namespace atom { namespace api { class Net : public mate::EventEmitter { - -public: + public: static v8::Local Create(v8::Isolate* isolate); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); v8::Local 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_ \ No newline at end of file +#endif // ATOM_BROWSER_API_ATOM_API_NET_H_ diff --git a/atom/browser/api/atom_api_url_request.cc b/atom/browser/api/atom_api_url_request.cc index a98bd58e7429..ad2d66f5fe4f 100644 --- a/atom/browser/api/atom_api_url_request.cc +++ b/atom/browser/api/atom_api_url_request.cc @@ -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 #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> { static v8::Local ToV8( - v8::Isolate* isolate, - scoped_refptr val) { - + v8::Isolate* isolate, + scoped_refptr val) { mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); if (val) { size_t iter = 0; @@ -34,7 +27,7 @@ struct Converter> { std::string value; while (val->EnumerateHeaderLines(&iter, &name, &value)) { dict.Set(name, value); - } + } } return dict.GetHandle(); } @@ -43,29 +36,27 @@ struct Converter> { template<> struct Converter> { static v8::Local ToV8( - v8::Isolate* isolate, - scoped_refptr buffer) { + v8::Isolate* isolate, + scoped_refptr buffer) { return node::Buffer::Copy(isolate, - buffer->data(), - buffer->size()).ToLocalChecked(); + buffer->data(), + buffer->size()).ToLocalChecked(); } static bool FromV8( - v8::Isolate* isolate, - v8::Local val, - scoped_refptr* out) { - + v8::Isolate* isolate, + v8::Local val, + scoped_refptr* 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> { } }; -} -namespace atom { +} // namespace mate +namespace atom { namespace api { URLRequest::URLRequest(v8::Isolate* isolate, v8::Local wrapper) @@ -101,7 +92,6 @@ URLRequest::~URLRequest() { // static mate::WrappableBase* URLRequest::New(mate::Arguments* args) { - v8::Local 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 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 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 \ No newline at end of file +} // namespace atom diff --git a/atom/browser/api/atom_api_url_request.h b/atom/browser/api/atom_api_url_request.h index 9fb07243024c..89702b0d4012 100644 --- a/atom/browser/api/atom_api_url_request.h +++ b/atom/browser/api/atom_api_url_request.h @@ -6,10 +6,11 @@ #define ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_ #include +#include #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 { public: static mate::WrappableBase* New(mate::Arguments* args); - static void BuildPrototype(v8::Isolate* isolate, + static void BuildPrototype( + v8::Isolate* isolate, v8::Local prototype); protected: - URLRequest(v8::Isolate* isolate, + explicit URLRequest(v8::Isolate* isolate, v8::Local wrapper); ~URLRequest() override; -private: + private: bool Write(scoped_refptr 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 - std::array, sizeof...(ArgTypes)> + std::array, sizeof...(ArgTypes)> BuildArgsArray(ArgTypes... args) const; template @@ -70,7 +72,6 @@ private: scoped_refptr atom_request_; v8::Global wrapper_; base::WeakPtrFactory 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_ \ No newline at end of file +#endif // ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_ diff --git a/atom/browser/net/atom_url_request.cc b/atom/browser/net/atom_url_request.cc index d7f509695177..6b07a2e3ef6f 100644 --- a/atom/browser/net/atom_url_request.cc +++ b/atom/browser/net/atom_url_request.cc @@ -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 #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 buffer) : net::UploadBytesElementReader(buffer->data(), buffer->size()) @@ -39,18 +41,18 @@ public: return new UploadOwnedIOBufferElementReader(std::move(buffer)); } -private: + private: scoped_refptr buffer_; DISALLOW_COPY_AND_ASSIGN(UploadOwnedIOBufferElementReader); }; -} +} // namespace internal AtomURLRequest::AtomURLRequest(base::WeakPtr 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::Create( DCHECK(context); - scoped_refptr atom_url_request = + scoped_refptr atom_url_request = new AtomURLRequest(delegate); atom_url_request->request_ = context->CreateRequest(GURL(url), @@ -82,14 +84,13 @@ scoped_refptr AtomURLRequest::Create( atom_url_request->request_->set_method(method); return atom_url_request; - } bool AtomURLRequest::Write( scoped_refptr 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(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 \ No newline at end of file +} // namespace atom diff --git a/atom/browser/net/atom_url_request.h b/atom/browser/net/atom_url_request.h index 7de6e1a40aea..dd6e3d11e089 100644 --- a/atom/browser/net/atom_url_request.h +++ b/atom/browser/net/atom_url_request.h @@ -6,9 +6,11 @@ #ifndef ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_ #define ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_ +#include +#include #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, public net::URLRequest::Delegate { -public: + public: static scoped_refptr Create( AtomBrowserContext* browser_context, const std::string& method, @@ -44,15 +46,15 @@ public: const base::string16& password) const; scoped_refptr 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; void DoWriteBuffer(scoped_refptr buffer, bool is_last); @@ -72,7 +74,7 @@ private: void InformDelegateResponseCompleted() const; void InformDelegateErrorOccured(const std::string& error) const; - AtomURLRequest(base::WeakPtr delegate); + explicit AtomURLRequest(base::WeakPtr delegate); virtual ~AtomURLRequest(); base::WeakPtr delegate_; @@ -81,15 +83,14 @@ private: bool is_chunked_upload_; std::unique_ptr chunked_stream_; std::unique_ptr chunked_stream_writer_; - - std::vector>upload_element_readers_; - + std::vector> + upload_element_readers_; scoped_refptr response_read_buffer_; scoped_refptr response_headers_; - DISALLOW_COPY_AND_ASSIGN(AtomURLRequest); - }; + DISALLOW_COPY_AND_ASSIGN(AtomURLRequest); +}; } // namespace atom -#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_ \ No newline at end of file +#endif // ATOM_BROWSER_NET_ATOM_URL_REQUEST_H_