Skip to content

Commit

Permalink
Don't read Buffer in IO thread
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed May 22, 2015
1 parent d78efe7 commit b202bba
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
11 changes: 10 additions & 1 deletion atom/browser/api/atom_api_protocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ namespace {

typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;

scoped_refptr<base::RefCountedBytes> BufferToRefCountedBytes(
v8::Local<v8::Value> buf) {
scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes);
auto start = reinterpret_cast<const unsigned char*>(node::Buffer::Data(buf));
data->data().assign(start, start + node::Buffer::Length(buf));
return data;
}

class CustomProtocolRequestJob : public AdapterRequestJob {
public:
CustomProtocolRequestJob(Protocol* registry,
Expand Down Expand Up @@ -95,7 +103,8 @@ class CustomProtocolRequestJob : public AdapterRequestJob {

BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&AdapterRequestJob::CreateBufferJobAndStart,
GetWeakPtr(), mime_type, encoding, buffer->ToObject()));
GetWeakPtr(), mime_type, encoding,
BufferToRefCountedBytes(buffer)));
return;
} else if (name == "RequestFileJob") {
base::FilePath path;
Expand Down
18 changes: 5 additions & 13 deletions atom/browser/net/adapter_request_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ base::WeakPtr<AdapterRequestJob> AdapterRequestJob::GetWeakPtr() {
}

void AdapterRequestJob::CreateErrorJobAndStart(int error_code) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

real_job_ = new net::URLRequestErrorJob(
request(), network_delegate(), error_code);
real_job_->Start();
Expand All @@ -81,25 +79,21 @@ void AdapterRequestJob::CreateErrorJobAndStart(int error_code) {
void AdapterRequestJob::CreateStringJobAndStart(const std::string& mime_type,
const std::string& charset,
const std::string& data) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

real_job_ = new URLRequestStringJob(
request(), network_delegate(), mime_type, charset, data);
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));

void AdapterRequestJob::CreateBufferJobAndStart(
const std::string& mime_type,
const std::string& charset,
scoped_refptr<base::RefCountedBytes> data) {
real_job_ = new URLRequestBufferJob(
request(), network_delegate(), mime_type, charset, buffer);
request(), network_delegate(), mime_type, charset, data);
real_job_->Start();
}

void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
real_job_ = asar::CreateJobFromPath(
path,
request(),
Expand All @@ -111,8 +105,6 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
}

void AdapterRequestJob::CreateJobFromProtocolHandlerAndStart() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
DCHECK(protocol_handler_);
real_job_ = protocol_handler_->MaybeCreateJob(request(),
network_delegate());
if (!real_job_.get())
Expand Down
3 changes: 2 additions & 1 deletion atom/browser/net/adapter_request_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <string>

#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_job_factory.h"
Expand Down Expand Up @@ -53,7 +54,7 @@ class AdapterRequestJob : public net::URLRequestJob {
const std::string& data);
void CreateBufferJobAndStart(const std::string& mime_type,
const std::string& charset,
v8::Local<v8::Object> buffer);
scoped_refptr<base::RefCountedBytes> data);
void CreateFileJobAndStart(const base::FilePath& path);
void CreateJobFromProtocolHandlerAndStart();

Expand Down
16 changes: 7 additions & 9 deletions atom/browser/net/url_request_buffer_job.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@

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)
URLRequestBufferJob::URLRequestBufferJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const std::string& mime_type,
const std::string& charset,
scoped_refptr<base::RefCountedBytes> 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);
buffer_data_(data) {
}

int URLRequestBufferJob::GetRefCountedData(
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/net/url_request_buffer_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class URLRequestBufferJob : public net::URLRequestSimpleJob {
net::NetworkDelegate* network_delegate,
const std::string& mime_type,
const std::string& charset,
v8::Local<v8::Object> buffer);
scoped_refptr<base::RefCountedBytes> data);

// URLRequestSimpleJob:
int GetRefCountedData(std::string* mime_type,
Expand Down
2 changes: 1 addition & 1 deletion vendor/native_mate

0 comments on commit b202bba

Please sign in to comment.