Implement protocol.registerHttpProtocol

This commit is contained in:
Cheng Zhao 2015-08-12 21:09:44 +08:00
parent 1f2d7d1cd8
commit f493eb34ae
6 changed files with 52 additions and 38 deletions

View file

@ -9,6 +9,7 @@
#include "atom/browser/atom_browser_main_parts.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_fetch_job.h"
#include "atom/browser/net/url_request_string_job.h"
#include "atom/common/native_mate_converters/callback.h"
#include "native_mate/dictionary.h"
@ -52,7 +53,9 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
.SetMethod("registerBufferProtocol",
&Protocol::RegisterProtocol<URLRequestBufferJob>)
.SetMethod("registerFileProtocol",
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>);
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>)
.SetMethod("registerHttpProtocol",
&Protocol::RegisterProtocol<URLRequestFetchJob>);
}
void Protocol::RegisterStandardSchemes(

View file

@ -47,7 +47,9 @@ void HandlerCallback(v8::Isolate* isolate,
V8ValueConverter converter;
v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
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, ...).

View file

@ -47,9 +47,7 @@ class JsAsker : public RequestJob {
weak_factory_(this) {}
// Subclass should do initailze work here.
virtual void StartAsync(scoped_ptr<base::Value> options) {
RequestJob::Start();
}
virtual void StartAsync(scoped_ptr<base::Value> options) = 0;
private:
// RequestJob:

View file

@ -8,7 +8,6 @@
#include <string>
#include "atom/browser/net/js_asker.h"
#include "atom/common/node_includes.h"
#include "base/memory/ref_counted_memory.h"
#include "net/url_request/url_request_simple_job.h"

View file

@ -75,42 +75,53 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace
URLRequestFetchJob::URLRequestFetchJob(
scoped_refptr<net::URLRequestContextGetter> request_context_getter,
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,
const std::string& method,
const std::string& referrer)
: net::URLRequestJob(request, network_delegate),
v8::Isolate* isolate,
const JavaScriptHandler& handler)
: JsAsker<net::URLRequestJob>(request, network_delegate, isolate, handler),
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.
net::URLFetcher::RequestType request_type;
if (method.empty())
request_type = GetRequestType(request->method());
request_type = GetRequestType(request()->method());
else
request_type = GetRequestType(method);
fetcher_ = net::URLFetcher::Create(url, request_type, this);
// Use request context if provided else create one.
if (request_context_getter)
fetcher_->SetRequestContext(request_context_getter.get());
else
fetcher_->SetRequestContext(GetRequestContext());
fetcher_ = net::URLFetcher::Create(GURL(url), request_type, this);
fetcher_->SetRequestContext(CreateRequestContext());
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
// Use |request|'s referrer if |referrer| is not specified.
if (referrer.empty()) {
fetcher_->SetReferrer(request->referrer());
} else {
if (referrer.empty())
fetcher_->SetReferrer(request()->referrer());
else
fetcher_->SetReferrer(referrer);
}
// 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()) {
auto task_runner = base::ThreadTaskRunnerHandle::Get();
net::URLRequestContextBuilder builder;
@ -150,12 +161,8 @@ int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) {
return bytes_read;
}
void URLRequestFetchJob::Start() {
fetcher_->Start();
}
void URLRequestFetchJob::Kill() {
URLRequestJob::Kill();
JsAsker<URLRequestJob>::Kill();
fetcher_.reset();
}

View file

@ -7,6 +7,7 @@
#include <string>
#include "atom/browser/net/js_asker.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_job.h"
@ -15,22 +16,23 @@ namespace atom {
class AtomBrowserContext;
class URLRequestFetchJob : public net::URLRequestJob,
class URLRequestFetchJob : public JsAsker<net::URLRequestJob>,
public net::URLFetcherDelegate {
public:
URLRequestFetchJob(scoped_refptr<net::URLRequestContextGetter> context_getter,
net::URLRequest* request,
URLRequestFetchJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& url,
const std::string& method,
const std::string& referrer);
v8::Isolate* isolate,
const JavaScriptHandler& handler);
net::URLRequestContextGetter* GetRequestContext();
// Called by response writer.
void HeadersCompleted();
int DataAvailable(net::IOBuffer* buffer, int num_bytes);
protected:
// JsAsker:
void StartAsync(scoped_ptr<base::Value> options) override;
// net::URLRequestJob:
void Start() override;
void Kill() override;
bool ReadRawData(net::IOBuffer* buf,
int buf_size,
@ -43,6 +45,9 @@ class URLRequestFetchJob : public net::URLRequestJob,
void OnURLFetchComplete(const net::URLFetcher* source) override;
private:
// Create a independent request context.
net::URLRequestContextGetter* CreateRequestContext();
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
scoped_ptr<net::URLFetcher> fetcher_;
scoped_refptr<net::IOBuffer> pending_buffer_;