// Copyright (c) 2013 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_ #define ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_ #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" namespace atom { class AtomURLRequest; namespace api { class URLRequest : public mate::EventEmitter { public: static mate::WrappableBase* New(mate::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); protected: URLRequest(v8::Isolate* isolate, v8::Local wrapper); ~URLRequest() override; private: bool Write(scoped_refptr buffer, bool is_last); void Abort(); bool SetExtraHeader(const std::string& name, const std::string& value); void RemoveExtraHeader(const std::string& name); void SetChunkedUpload(bool is_chunked_upload); friend class AtomURLRequest; void OnAuthenticationRequired( scoped_refptr auth_info); void OnResponseStarted(); void OnResponseData(scoped_refptr data); void OnResponseCompleted(); void OnError(const std::string& error); int StatusCode() const; std::string StatusMessage() const; scoped_refptr RawResponseHeaders() const; uint32_t ResponseHttpVersionMajor() const; uint32_t ResponseHttpVersionMinor() const; template std::array, sizeof...(ArgTypes)> BuildArgsArray(ArgTypes... args) const; template void EmitRequestEvent(ArgTypes... args); template void EmitResponseEvent(ArgTypes... args); void pin(); void unpin(); scoped_refptr atom_request_; v8::Global wrapper_; base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(URLRequest); }; template std::array, sizeof...(ArgTypes)> URLRequest::BuildArgsArray(ArgTypes... args) const { std::array, sizeof...(ArgTypes)> result = { mate::ConvertToV8(isolate(), args)... }; return result; } template void URLRequest::EmitRequestEvent(ArgTypes... args) { auto arguments = BuildArgsArray(args...); v8::Local _emitRequestEvent; auto wrapper = GetWrapper(); if (mate::Dictionary(isolate(), wrapper) .Get("_emitRequestEvent", &_emitRequestEvent)) _emitRequestEvent->Call(wrapper, arguments.size(), arguments.data()); } template void URLRequest::EmitResponseEvent(ArgTypes... args) { auto arguments = BuildArgsArray(args...); v8::Local _emitResponseEvent; auto wrapper = GetWrapper(); if (mate::Dictionary(isolate(), wrapper) .Get("_emitResponseEvent", &_emitResponseEvent)) _emitResponseEvent->Call(wrapper, arguments.size(), arguments.data()); } } // namepsace api } // namepsace atom #endif // ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_