add protocol.RequestBuffer api
This commit is contained in:
parent
b19cfba98e
commit
9eac8ef266
9 changed files with 144 additions and 0 deletions
2
atom.gyp
2
atom.gyp
|
@ -149,6 +149,8 @@
|
||||||
'atom/browser/net/atom_url_request_job_factory.h',
|
'atom/browser/net/atom_url_request_job_factory.h',
|
||||||
'atom/browser/net/url_request_string_job.cc',
|
'atom/browser/net/url_request_string_job.cc',
|
||||||
'atom/browser/net/url_request_string_job.h',
|
'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.cc',
|
||||||
'atom/browser/node_debugger.h',
|
'atom/browser/node_debugger.h',
|
||||||
'atom/browser/ui/accelerator_util.cc',
|
'atom/browser/ui/accelerator_util.cc',
|
||||||
|
|
|
@ -86,6 +86,17 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
|
||||||
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
||||||
GetWeakPtr(), mime_type, charset, data));
|
GetWeakPtr(), mime_type, charset, data));
|
||||||
return;
|
return;
|
||||||
|
} else if (name == "RequestBufferJob") {
|
||||||
|
std::string mime_type, encoding;
|
||||||
|
v8::Handle<v8::Value> 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") {
|
} else if (name == "RequestFileJob") {
|
||||||
base::FilePath path;
|
base::FilePath path;
|
||||||
dict.Get("path", &path);
|
dict.Get("path", &path);
|
||||||
|
|
|
@ -16,6 +16,16 @@ class RequestStringJob
|
||||||
@charset = charset ? 'UTF-8'
|
@charset = charset ? 'UTF-8'
|
||||||
@data = String data
|
@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 =
|
protocol.RequestFileJob =
|
||||||
class RequestFileJob
|
class RequestFileJob
|
||||||
constructor: (@path) ->
|
constructor: (@path) ->
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include "atom/browser/net/url_request_string_job.h"
|
#include "atom/browser/net/url_request_string_job.h"
|
||||||
#include "atom/browser/net/asar/url_request_asar_job.h"
|
#include "atom/browser/net/asar/url_request_asar_job.h"
|
||||||
#include "atom/common/asar/asar_util.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 "content/public/browser/browser_thread.h"
|
||||||
#include "net/base/net_errors.h"
|
#include "net/base/net_errors.h"
|
||||||
#include "net/url_request/url_request_error_job.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();
|
real_job_->Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AdapterRequestJob::CreateBufferJobAndStart(const std::string& mime_type,
|
||||||
|
const std::string& charset,
|
||||||
|
v8::Local<v8::Object> 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) {
|
void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
|
||||||
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "base/memory/weak_ptr.h"
|
#include "base/memory/weak_ptr.h"
|
||||||
#include "net/url_request/url_request_job.h"
|
#include "net/url_request/url_request_job.h"
|
||||||
#include "net/url_request/url_request_job_factory.h"
|
#include "net/url_request/url_request_job_factory.h"
|
||||||
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
class FilePath;
|
class FilePath;
|
||||||
|
@ -50,6 +51,9 @@ class AdapterRequestJob : public net::URLRequestJob {
|
||||||
void CreateStringJobAndStart(const std::string& mime_type,
|
void CreateStringJobAndStart(const std::string& mime_type,
|
||||||
const std::string& charset,
|
const std::string& charset,
|
||||||
const std::string& data);
|
const std::string& data);
|
||||||
|
void CreateBufferJobAndStart(const std::string& mime_type,
|
||||||
|
const std::string& charset,
|
||||||
|
v8::Local<v8::Object> buffer);
|
||||||
void CreateFileJobAndStart(const base::FilePath& path);
|
void CreateFileJobAndStart(const base::FilePath& path);
|
||||||
void CreateJobFromProtocolHandlerAndStart();
|
void CreateJobFromProtocolHandlerAndStart();
|
||||||
|
|
||||||
|
|
38
atom/browser/net/url_request_buffer_job.cc
Normal file
38
atom/browser/net/url_request_buffer_job.cc
Normal file
|
@ -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 <string>
|
||||||
|
|
||||||
|
#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<v8::Object> data)
|
||||||
|
: net::URLRequestSimpleJob(request, network_delegate),
|
||||||
|
mime_type_(mime_type),
|
||||||
|
charset_(charset),
|
||||||
|
buffer_data_(new base::RefCountedBytes()) {
|
||||||
|
auto input = reinterpret_cast<const unsigned char*>(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<base::RefCountedMemory>* data,
|
||||||
|
const net::CompletionCallback& callback) const {
|
||||||
|
*mime_type = mime_type_;
|
||||||
|
*charset = charset_;
|
||||||
|
*data = buffer_data_;
|
||||||
|
return net::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace atom
|
40
atom/browser/net/url_request_buffer_job.h
Normal file
40
atom/browser/net/url_request_buffer_job.h
Normal file
|
@ -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 <string>
|
||||||
|
|
||||||
|
#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<v8::Object> buffer);
|
||||||
|
|
||||||
|
// URLRequestSimpleJob:
|
||||||
|
int GetRefCountedData(std::string* mime_type,
|
||||||
|
std::string* charset,
|
||||||
|
scoped_refptr<base::RefCountedMemory>* data,
|
||||||
|
const net::CompletionCallback& callback) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string mime_type_;
|
||||||
|
std::string charset_;
|
||||||
|
scoped_refptr<base::RefCountedBytes> buffer_data_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(URLRequestBufferJob);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace atom
|
||||||
|
|
||||||
|
#endif // ATOM_BROWSER_NET_URL_REQUEST_BUFFER_JOB_H_
|
|
@ -74,3 +74,12 @@ mime types.
|
||||||
* `data` String
|
* `data` String
|
||||||
|
|
||||||
Create a request job which sends a string as response.
|
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.
|
||||||
|
|
|
@ -58,6 +58,25 @@ describe 'protocol module', ->
|
||||||
assert false, 'Got error: ' + errorType + ' ' + error
|
assert false, 'Got error: ' + errorType + ' ' + error
|
||||||
protocol.unregisterProtocol 'atom-string-job'
|
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) ->
|
it 'returns RequestFileJob should send file', (done) ->
|
||||||
job = new protocol.RequestFileJob(__filename)
|
job = new protocol.RequestFileJob(__filename)
|
||||||
handler = remote.createFunctionWithReturnValue job
|
handler = remote.createFunctionWithReturnValue job
|
||||||
|
|
Loading…
Reference in a new issue