From 9eac8ef266806b360430576e73d9939b26e583c3 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Mon, 16 Mar 2015 18:23:45 +0530 Subject: [PATCH] add protocol.RequestBuffer api --- atom.gyp | 2 ++ atom/browser/api/atom_api_protocol.cc | 11 ++++++ atom/browser/api/lib/protocol.coffee | 10 ++++++ atom/browser/net/adapter_request_job.cc | 11 ++++++ atom/browser/net/adapter_request_job.h | 4 +++ atom/browser/net/url_request_buffer_job.cc | 38 ++++++++++++++++++++ atom/browser/net/url_request_buffer_job.h | 40 ++++++++++++++++++++++ docs/api/protocol.md | 9 +++++ spec/api-protocol-spec.coffee | 19 ++++++++++ 9 files changed, 144 insertions(+) create mode 100644 atom/browser/net/url_request_buffer_job.cc create mode 100644 atom/browser/net/url_request_buffer_job.h diff --git a/atom.gyp b/atom.gyp index 898c6135271c..7465d42ea368 100644 --- a/atom.gyp +++ b/atom.gyp @@ -149,6 +149,8 @@ 'atom/browser/net/atom_url_request_job_factory.h', 'atom/browser/net/url_request_string_job.cc', 'atom/browser/net/url_request_string_job.h', + 'atom/browser/net/url_request_buffer_job.cc', + 'atom/browser/net/url_request_buffer_job.h', 'atom/browser/node_debugger.cc', 'atom/browser/node_debugger.h', 'atom/browser/ui/accelerator_util.cc', diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 0d0adb3eb018..ecb9d416875a 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -86,6 +86,17 @@ class CustomProtocolRequestJob : public AdapterRequestJob { base::Bind(&AdapterRequestJob::CreateStringJobAndStart, GetWeakPtr(), mime_type, charset, data)); return; + } else if (name == "RequestBufferJob") { + std::string mime_type, encoding; + v8::Handle buffer; + dict.Get("mimeType", &mime_type); + dict.Get("encoding", &encoding); + dict.Get("data", &buffer); + + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, + base::Bind(&AdapterRequestJob::CreateBufferJobAndStart, + GetWeakPtr(), mime_type, encoding, buffer->ToObject())); + return; } else if (name == "RequestFileJob") { base::FilePath path; dict.Get("path", &path); diff --git a/atom/browser/api/lib/protocol.coffee b/atom/browser/api/lib/protocol.coffee index 045dc9568839..6eae01462b42 100644 --- a/atom/browser/api/lib/protocol.coffee +++ b/atom/browser/api/lib/protocol.coffee @@ -16,6 +16,16 @@ class RequestStringJob @charset = charset ? 'UTF-8' @data = String data +protocol.RequestBufferJob = +class RequestBufferJob + constructor: ({mimeType, encoding, data}) -> + if not data instanceof Buffer + throw new TypeError('Data should be Buffer') + + @mimeType = mimeType ? 'application/octet-stream' + @encoding = encoding ? 'utf8' + @data = new Buffer(data) + protocol.RequestFileJob = class RequestFileJob constructor: (@path) -> diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index ebefa14f123e..0d873d4a7da4 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -8,6 +8,7 @@ #include "atom/browser/net/url_request_string_job.h" #include "atom/browser/net/asar/url_request_asar_job.h" #include "atom/common/asar/asar_util.h" +#include "atom/browser/net/url_request_buffer_job.h" #include "content/public/browser/browser_thread.h" #include "net/base/net_errors.h" #include "net/url_request/url_request_error_job.h" @@ -87,6 +88,16 @@ void AdapterRequestJob::CreateStringJobAndStart(const std::string& mime_type, real_job_->Start(); } +void AdapterRequestJob::CreateBufferJobAndStart(const std::string& mime_type, + const std::string& charset, + v8::Local buffer) { + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); + + real_job_ = new URLRequestBufferJob( + request(), network_delegate(), mime_type, charset, buffer); + real_job_->Start(); +} + void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index 557b8d61e907..406214e3c162 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -10,6 +10,7 @@ #include "base/memory/weak_ptr.h" #include "net/url_request/url_request_job.h" #include "net/url_request/url_request_job_factory.h" +#include "v8/include/v8.h" namespace base { class FilePath; @@ -50,6 +51,9 @@ class AdapterRequestJob : public net::URLRequestJob { void CreateStringJobAndStart(const std::string& mime_type, const std::string& charset, const std::string& data); + void CreateBufferJobAndStart(const std::string& mime_type, + const std::string& charset, + v8::Local buffer); void CreateFileJobAndStart(const base::FilePath& path); void CreateJobFromProtocolHandlerAndStart(); diff --git a/atom/browser/net/url_request_buffer_job.cc b/atom/browser/net/url_request_buffer_job.cc new file mode 100644 index 000000000000..4aeb3284a4e9 --- /dev/null +++ b/atom/browser/net/url_request_buffer_job.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2013 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/net/url_request_buffer_job.h" + +#include + +#include "net/base/net_errors.h" + +namespace atom { + +URLRequestBufferJob::URLRequestBufferJob(net::URLRequest* request, + net::NetworkDelegate* network_delegate, + const std::string& mime_type, + const std::string& charset, + v8::Local data) + : net::URLRequestSimpleJob(request, network_delegate), + mime_type_(mime_type), + charset_(charset), + buffer_data_(new base::RefCountedBytes()) { + auto input = reinterpret_cast(node::Buffer::Data(data)); + size_t length = node::Buffer::Length(data); + buffer_data_->data().assign(input, input + length); +} + +int URLRequestBufferJob::GetRefCountedData( + std::string* mime_type, + std::string* charset, + scoped_refptr* data, + const net::CompletionCallback& callback) const { + *mime_type = mime_type_; + *charset = charset_; + *data = buffer_data_; + return net::OK; +} + +} // namespace atom diff --git a/atom/browser/net/url_request_buffer_job.h b/atom/browser/net/url_request_buffer_job.h new file mode 100644 index 000000000000..936d96409428 --- /dev/null +++ b/atom/browser/net/url_request_buffer_job.h @@ -0,0 +1,40 @@ +// 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_NET_URL_REQUEST_BUFFER_JOB_H_ +#define ATOM_BROWSER_NET_URL_REQUEST_BUFFER_JOB_H_ + +#include + +#include "base/memory/ref_counted_memory.h" +#include "net/url_request/url_request_simple_job.h" +#include "atom/common/node_includes.h" + +namespace atom { + +class URLRequestBufferJob : public net::URLRequestSimpleJob { + public: + URLRequestBufferJob(net::URLRequest* request, + net::NetworkDelegate* network_delegate, + const std::string& mime_type, + const std::string& charset, + v8::Local buffer); + + // URLRequestSimpleJob: + int GetRefCountedData(std::string* mime_type, + std::string* charset, + scoped_refptr* data, + const net::CompletionCallback& callback) const override; + + private: + std::string mime_type_; + std::string charset_; + scoped_refptr buffer_data_; + + DISALLOW_COPY_AND_ASSIGN(URLRequestBufferJob); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_NET_URL_REQUEST_BUFFER_JOB_H_ diff --git a/docs/api/protocol.md b/docs/api/protocol.md index 1ac402170ee9..0a80397ed680 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -74,3 +74,12 @@ mime types. * `data` String Create a request job which sends a string as response. + +## Class: protocol.RequestBufferJob(options) + +* `options` Object + * `mimeType` String - Default is `application/octet-stream` + * `encoding` String - Default is `UTF-8` + * `data` Buffer + +Create a request job which accepts a buffer and sends a string as response. diff --git a/spec/api-protocol-spec.coffee b/spec/api-protocol-spec.coffee index bf7408df2b26..b87dc7510f6f 100644 --- a/spec/api-protocol-spec.coffee +++ b/spec/api-protocol-spec.coffee @@ -58,6 +58,25 @@ describe 'protocol module', -> assert false, 'Got error: ' + errorType + ' ' + error protocol.unregisterProtocol 'atom-string-job' + it 'returns RequestBufferJob should send buffer', (done) -> + data = new Buffer("hello") + job = new protocol.RequestBufferJob(data: data) + handler = remote.createFunctionWithReturnValue job + protocol.registerProtocol 'atom-buffer-job', handler + + $.ajax + url: 'atom-buffer-job://fake-host' + success: (response) -> + assert.equal response.length, data.length + buf = new Buffer(response.length) + buf.write(response) + assert buf.equals(data) + protocol.unregisterProtocol 'atom-buffer-job' + done() + error: (xhr, errorType, error) -> + assert false, 'Got error: ' + errorType + ' ' + error + protocol.unregisterProtocol 'atom-buffer-job' + it 'returns RequestFileJob should send file', (done) -> job = new protocol.RequestFileJob(__filename) handler = remote.createFunctionWithReturnValue job