Implement protocol.registerHttpProtocol
This commit is contained in:
parent
1f2d7d1cd8
commit
f493eb34ae
6 changed files with 52 additions and 38 deletions
|
@ -9,6 +9,7 @@
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
#include "atom/browser/net/url_request_async_asar_job.h"
|
#include "atom/browser/net/url_request_async_asar_job.h"
|
||||||
#include "atom/browser/net/url_request_buffer_job.h"
|
#include "atom/browser/net/url_request_buffer_job.h"
|
||||||
|
#include "atom/browser/net/url_request_fetch_job.h"
|
||||||
#include "atom/browser/net/url_request_string_job.h"
|
#include "atom/browser/net/url_request_string_job.h"
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
@ -52,7 +53,9 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
|
||||||
.SetMethod("registerBufferProtocol",
|
.SetMethod("registerBufferProtocol",
|
||||||
&Protocol::RegisterProtocol<URLRequestBufferJob>)
|
&Protocol::RegisterProtocol<URLRequestBufferJob>)
|
||||||
.SetMethod("registerFileProtocol",
|
.SetMethod("registerFileProtocol",
|
||||||
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>);
|
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>)
|
||||||
|
.SetMethod("registerHttpProtocol",
|
||||||
|
&Protocol::RegisterProtocol<URLRequestFetchJob>);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::RegisterStandardSchemes(
|
void Protocol::RegisterStandardSchemes(
|
||||||
|
|
|
@ -47,7 +47,9 @@ void HandlerCallback(v8::Isolate* isolate,
|
||||||
V8ValueConverter converter;
|
V8ValueConverter converter;
|
||||||
v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
|
v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
|
||||||
scoped_ptr<base::Value> options(converter.FromV8Value(value, context));
|
scoped_ptr<base::Value> options(converter.FromV8Value(value, context));
|
||||||
holder->callback.Run(true, options.Pass());
|
content::BrowserThread::PostTask(
|
||||||
|
content::BrowserThread::IO, FROM_HERE,
|
||||||
|
base::Bind(holder->callback, true, base::Passed(&options)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// func.bind(func, ...).
|
// func.bind(func, ...).
|
||||||
|
|
|
@ -47,9 +47,7 @@ class JsAsker : public RequestJob {
|
||||||
weak_factory_(this) {}
|
weak_factory_(this) {}
|
||||||
|
|
||||||
// Subclass should do initailze work here.
|
// Subclass should do initailze work here.
|
||||||
virtual void StartAsync(scoped_ptr<base::Value> options) {
|
virtual void StartAsync(scoped_ptr<base::Value> options) = 0;
|
||||||
RequestJob::Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// RequestJob:
|
// RequestJob:
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/net/js_asker.h"
|
#include "atom/browser/net/js_asker.h"
|
||||||
#include "atom/common/node_includes.h"
|
|
||||||
#include "base/memory/ref_counted_memory.h"
|
#include "base/memory/ref_counted_memory.h"
|
||||||
#include "net/url_request/url_request_simple_job.h"
|
#include "net/url_request/url_request_simple_job.h"
|
||||||
|
|
||||||
|
|
|
@ -75,42 +75,53 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
URLRequestFetchJob::URLRequestFetchJob(
|
URLRequestFetchJob::URLRequestFetchJob(
|
||||||
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
|
|
||||||
net::URLRequest* request,
|
net::URLRequest* request,
|
||||||
net::NetworkDelegate* network_delegate,
|
net::NetworkDelegate* network_delegate,
|
||||||
const GURL& url,
|
v8::Isolate* isolate,
|
||||||
const std::string& method,
|
const JavaScriptHandler& handler)
|
||||||
const std::string& referrer)
|
: JsAsker<net::URLRequestJob>(request, network_delegate, isolate, handler),
|
||||||
: net::URLRequestJob(request, network_delegate),
|
|
||||||
pending_buffer_size_(0) {
|
pending_buffer_size_(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void URLRequestFetchJob::StartAsync(scoped_ptr<base::Value> options) {
|
||||||
|
if (!options->IsType(base::Value::TYPE_DICTIONARY)) {
|
||||||
|
NotifyStartError(net::URLRequestStatus(
|
||||||
|
net::URLRequestStatus::FAILED, net::ERR_NOT_IMPLEMENTED));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string url, method, referrer;
|
||||||
|
base::DictionaryValue* dict =
|
||||||
|
static_cast<base::DictionaryValue*>(options.get());
|
||||||
|
dict->GetString("url", &url);
|
||||||
|
dict->GetString("method", &method);
|
||||||
|
dict->GetString("referrer", &referrer);
|
||||||
|
|
||||||
// Use |request|'s method if |method| is not specified.
|
// Use |request|'s method if |method| is not specified.
|
||||||
net::URLFetcher::RequestType request_type;
|
net::URLFetcher::RequestType request_type;
|
||||||
if (method.empty())
|
if (method.empty())
|
||||||
request_type = GetRequestType(request->method());
|
request_type = GetRequestType(request()->method());
|
||||||
else
|
else
|
||||||
request_type = GetRequestType(method);
|
request_type = GetRequestType(method);
|
||||||
|
|
||||||
fetcher_ = net::URLFetcher::Create(url, request_type, this);
|
fetcher_ = net::URLFetcher::Create(GURL(url), request_type, this);
|
||||||
// Use request context if provided else create one.
|
fetcher_->SetRequestContext(CreateRequestContext());
|
||||||
if (request_context_getter)
|
|
||||||
fetcher_->SetRequestContext(request_context_getter.get());
|
|
||||||
else
|
|
||||||
fetcher_->SetRequestContext(GetRequestContext());
|
|
||||||
|
|
||||||
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
|
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
|
||||||
|
|
||||||
// Use |request|'s referrer if |referrer| is not specified.
|
// Use |request|'s referrer if |referrer| is not specified.
|
||||||
if (referrer.empty()) {
|
if (referrer.empty())
|
||||||
fetcher_->SetReferrer(request->referrer());
|
fetcher_->SetReferrer(request()->referrer());
|
||||||
} else {
|
else
|
||||||
fetcher_->SetReferrer(referrer);
|
fetcher_->SetReferrer(referrer);
|
||||||
}
|
|
||||||
|
|
||||||
// Use |request|'s headers.
|
// Use |request|'s headers.
|
||||||
fetcher_->SetExtraRequestHeaders(request->extra_request_headers().ToString());
|
fetcher_->SetExtraRequestHeaders(
|
||||||
|
request()->extra_request_headers().ToString());
|
||||||
|
|
||||||
|
fetcher_->Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
net::URLRequestContextGetter* URLRequestFetchJob::GetRequestContext() {
|
net::URLRequestContextGetter* URLRequestFetchJob::CreateRequestContext() {
|
||||||
if (!url_request_context_getter_.get()) {
|
if (!url_request_context_getter_.get()) {
|
||||||
auto task_runner = base::ThreadTaskRunnerHandle::Get();
|
auto task_runner = base::ThreadTaskRunnerHandle::Get();
|
||||||
net::URLRequestContextBuilder builder;
|
net::URLRequestContextBuilder builder;
|
||||||
|
@ -150,12 +161,8 @@ int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) {
|
||||||
return bytes_read;
|
return bytes_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
void URLRequestFetchJob::Start() {
|
|
||||||
fetcher_->Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void URLRequestFetchJob::Kill() {
|
void URLRequestFetchJob::Kill() {
|
||||||
URLRequestJob::Kill();
|
JsAsker<URLRequestJob>::Kill();
|
||||||
fetcher_.reset();
|
fetcher_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "atom/browser/net/js_asker.h"
|
||||||
#include "net/url_request/url_request_context_getter.h"
|
#include "net/url_request/url_request_context_getter.h"
|
||||||
#include "net/url_request/url_fetcher_delegate.h"
|
#include "net/url_request/url_fetcher_delegate.h"
|
||||||
#include "net/url_request/url_request_job.h"
|
#include "net/url_request/url_request_job.h"
|
||||||
|
@ -15,22 +16,23 @@ namespace atom {
|
||||||
|
|
||||||
class AtomBrowserContext;
|
class AtomBrowserContext;
|
||||||
|
|
||||||
class URLRequestFetchJob : public net::URLRequestJob,
|
class URLRequestFetchJob : public JsAsker<net::URLRequestJob>,
|
||||||
public net::URLFetcherDelegate {
|
public net::URLFetcherDelegate {
|
||||||
public:
|
public:
|
||||||
URLRequestFetchJob(scoped_refptr<net::URLRequestContextGetter> context_getter,
|
URLRequestFetchJob(net::URLRequest* request,
|
||||||
net::URLRequest* request,
|
|
||||||
net::NetworkDelegate* network_delegate,
|
net::NetworkDelegate* network_delegate,
|
||||||
const GURL& url,
|
v8::Isolate* isolate,
|
||||||
const std::string& method,
|
const JavaScriptHandler& handler);
|
||||||
const std::string& referrer);
|
|
||||||
|
|
||||||
net::URLRequestContextGetter* GetRequestContext();
|
// Called by response writer.
|
||||||
void HeadersCompleted();
|
void HeadersCompleted();
|
||||||
int DataAvailable(net::IOBuffer* buffer, int num_bytes);
|
int DataAvailable(net::IOBuffer* buffer, int num_bytes);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// JsAsker:
|
||||||
|
void StartAsync(scoped_ptr<base::Value> options) override;
|
||||||
|
|
||||||
// net::URLRequestJob:
|
// net::URLRequestJob:
|
||||||
void Start() override;
|
|
||||||
void Kill() override;
|
void Kill() override;
|
||||||
bool ReadRawData(net::IOBuffer* buf,
|
bool ReadRawData(net::IOBuffer* buf,
|
||||||
int buf_size,
|
int buf_size,
|
||||||
|
@ -43,6 +45,9 @@ class URLRequestFetchJob : public net::URLRequestJob,
|
||||||
void OnURLFetchComplete(const net::URLFetcher* source) override;
|
void OnURLFetchComplete(const net::URLFetcher* source) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Create a independent request context.
|
||||||
|
net::URLRequestContextGetter* CreateRequestContext();
|
||||||
|
|
||||||
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
|
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
|
||||||
scoped_ptr<net::URLFetcher> fetcher_;
|
scoped_ptr<net::URLFetcher> fetcher_;
|
||||||
scoped_refptr<net::IOBuffer> pending_buffer_;
|
scoped_refptr<net::IOBuffer> pending_buffer_;
|
||||||
|
|
Loading…
Reference in a new issue