From f493eb34aedb5660990f03e5ebc6401ddad17bf0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 12 Aug 2015 21:09:44 +0800 Subject: [PATCH] Implement protocol.registerHttpProtocol --- atom/browser/api/atom_api_protocol.cc | 5 ++- atom/browser/net/js_asker.cc | 4 +- atom/browser/net/js_asker.h | 4 +- atom/browser/net/url_request_buffer_job.h | 1 - atom/browser/net/url_request_fetch_job.cc | 55 +++++++++++++---------- atom/browser/net/url_request_fetch_job.h | 21 +++++---- 6 files changed, 52 insertions(+), 38 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 366b6732d9..c0c2e5b512 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -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) .SetMethod("registerFileProtocol", - &Protocol::RegisterProtocol); + &Protocol::RegisterProtocol) + .SetMethod("registerHttpProtocol", + &Protocol::RegisterProtocol); } void Protocol::RegisterStandardSchemes( diff --git a/atom/browser/net/js_asker.cc b/atom/browser/net/js_asker.cc index b53799b06f..aa6192b057 100644 --- a/atom/browser/net/js_asker.cc +++ b/atom/browser/net/js_asker.cc @@ -47,7 +47,9 @@ void HandlerCallback(v8::Isolate* isolate, V8ValueConverter converter; v8::Local context = args->isolate()->GetCurrentContext(); scoped_ptr 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, ...). diff --git a/atom/browser/net/js_asker.h b/atom/browser/net/js_asker.h index 0f5f05856c..4d7e888e15 100644 --- a/atom/browser/net/js_asker.h +++ b/atom/browser/net/js_asker.h @@ -47,9 +47,7 @@ class JsAsker : public RequestJob { weak_factory_(this) {} // Subclass should do initailze work here. - virtual void StartAsync(scoped_ptr options) { - RequestJob::Start(); - } + virtual void StartAsync(scoped_ptr options) = 0; private: // RequestJob: diff --git a/atom/browser/net/url_request_buffer_job.h b/atom/browser/net/url_request_buffer_job.h index c72e5b8385..de1a41234f 100644 --- a/atom/browser/net/url_request_buffer_job.h +++ b/atom/browser/net/url_request_buffer_job.h @@ -8,7 +8,6 @@ #include #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" diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 47ca0633c6..7e51c26ba2 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -75,42 +75,53 @@ class ResponsePiper : public net::URLFetcherResponseWriter { } // namespace URLRequestFetchJob::URLRequestFetchJob( - scoped_refptr 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(request, network_delegate, isolate, handler), pending_buffer_size_(0) { +} + +void URLRequestFetchJob::StartAsync(scoped_ptr 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(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::Kill(); fetcher_.reset(); } diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index a14e8dd1aa..f2086c6366 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -7,6 +7,7 @@ #include +#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, public net::URLFetcherDelegate { public: - URLRequestFetchJob(scoped_refptr 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 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 url_request_context_getter_; scoped_ptr fetcher_; scoped_refptr pending_buffer_;