Implementing URLRequest API, getting response body.

This commit is contained in:
ali.ibrahim 2016-09-21 09:23:00 +02:00
parent 81eab9887b
commit 2d9d4af98d
6 changed files with 313 additions and 76 deletions

View file

@ -5,9 +5,12 @@
#ifndef ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_
#define ATOM_BROWSER_API_ATOM_API_URL_REQUEST_H_
#include <array>
#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 {
@ -38,13 +41,26 @@ private:
friend class AtomURLRequest;
void OnResponseStarted();
void OnResponseData();
void OnResponseEnd();
void OnResponseData(scoped_refptr<net::IOBufferWithSize> data);
void OnResponseCompleted();
int StatusCode();
void StatusMessage();
void ResponseHeaders();
void ResponseHttpVersion();
std::string StatusMessage();
scoped_refptr<net::HttpResponseHeaders> RawResponseHeaders();
uint32_t ResponseHttpVersionMajor();
uint32_t ResponseHttpVersionMinor();
template <typename ... ArgTypes>
std::array<v8::Local<v8::Value>, sizeof...(ArgTypes)>
BuildArgsArray(ArgTypes... args);
template <typename ... ArgTypes>
void EmitRequestEvent(ArgTypes... args);
template <typename ... ArgTypes>
void EmitResponseEvent(ArgTypes... args);
void pin();
@ -53,10 +69,41 @@ private:
scoped_refptr<AtomURLRequest> atom_url_request_;
v8::Global<v8::Object> wrapper_;
base::WeakPtrFactory<URLRequest> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequest);
};
template <typename ... ArgTypes>
std::array<v8::Local<v8::Value>, sizeof...(ArgTypes)>
URLRequest::BuildArgsArray(ArgTypes... args) {
std::array<v8::Local<v8::Value>, sizeof...(ArgTypes)> result
= { mate::ConvertToV8(isolate(), args)... };
return result;
}
template <typename ... ArgTypes>
void URLRequest::EmitRequestEvent(ArgTypes... args) {
auto arguments = BuildArgsArray(args...);
v8::Local<v8::Function> _emitRequestEvent;
auto wrapper = GetWrapper();
if (mate::Dictionary(isolate(), wrapper).Get("_emitRequestEvent", &_emitRequestEvent))
_emitRequestEvent->Call(wrapper, arguments.size(), arguments.data());
}
template <typename ... ArgTypes>
void URLRequest::EmitResponseEvent(ArgTypes... args) {
auto arguments = BuildArgsArray(args...);
v8::Local<v8::Function> _emitResponseEvent;
auto wrapper = GetWrapper();
if (mate::Dictionary(isolate(), wrapper).Get("_emitResponseEvent", &_emitResponseEvent))
_emitResponseEvent->Call(wrapper, arguments.size(), arguments.data());
}
} // namepsace api
} // namepsace atom