]> khamidou — GitHub mirrors - electron.git/commitdiff
Don't read Buffer in IO thread
authorCheng Zhao <zcbenz@gmail.com>
Fri, 22 May 2015 14:15:13 +0000 (22:15 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 22 May 2015 14:15:13 +0000 (22:15 +0800)
atom/browser/api/atom_api_protocol.cc
atom/browser/net/adapter_request_job.cc
atom/browser/net/adapter_request_job.h
atom/browser/net/url_request_buffer_job.cc
atom/browser/net/url_request_buffer_job.h
vendor/native_mate

index 0d56a6fb1c492b475833c88320281d33d32ae746..323d282b19a9c80c894602fe34c1001df574f580 100644 (file)
@@ -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,
@@ -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;
index 8b562bcbf248cf955ca7ab5f836c3e1ccfc35a88..af3b02f150f42092ee4a4e75ea00653accaeb72a 100644 (file)
@@ -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();
@@ -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(),
@@ -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())
index 406214e3c162daa57514ac101a9395645dd98dc1..d87207464c089c43fec36ef0f449c5ae833ef569 100644 (file)
@@ -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"
@@ -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();
 
index 4aeb3284a4e95f393da414caf720e9fe5788ef7e..a8233b88da460417ac665329131067838246cd3d 100644 (file)
 
 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(
index 936d964094289048bc1b443bb745a7b389780958..cbdfbfa10d0bf9a79eb0d101e3102ae535687a3e 100644 (file)
@@ -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,
index 269be869988bda9a7e8ad0ef56af178a741af09c..1696237a3f444f0e33a8947749b4d70f6feb511b 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 269be869988bda9a7e8ad0ef56af178a741af09c
+Subproject commit 1696237a3f444f0e33a8947749b4d70f6feb511b