]> khamidou — GitHub mirrors - electron.git/commitdiff
add protocol.RequestBuffer api
authordeepak1556 <hop2deep@gmail.com>
Mon, 16 Mar 2015 12:53:45 +0000 (18:23 +0530)
committerdeepak1556 <hop2deep@gmail.com>
Thu, 19 Mar 2015 04:06:57 +0000 (09:36 +0530)
atom.gyp
atom/browser/api/atom_api_protocol.cc
atom/browser/api/lib/protocol.coffee
atom/browser/net/adapter_request_job.cc
atom/browser/net/adapter_request_job.h
atom/browser/net/url_request_buffer_job.cc [new file with mode: 0644]
atom/browser/net/url_request_buffer_job.h [new file with mode: 0644]
docs/api/protocol.md
spec/api-protocol-spec.coffee

index 898c6135271c901bf96cb4082dc9a0d563abb78d..7465d42ea36837bbdfc4ffd77fd62a44bb90f735 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       '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',
index 0d0adb3eb018cb8a0bb4d69471c1576ac18d42e0..ecb9d416875aab16fa62d066e15cce80b104cf28 100644 (file)
@@ -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<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") {
         base::FilePath path;
         dict.Get("path", &path);
index 045dc9568839bee39493d1e387093827ca4c094c..6eae01462b424398e92d644564a4eae7d66153e8 100644 (file)
@@ -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) ->
index ebefa14f123ef39bd57090e891525448ff2b60af..0d873d4a7da4d26878b98e14a051d0f7915cb1e2 100644 (file)
@@ -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<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) {
   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
 
index 557b8d61e9079a08061194f7efb59519cf5592ae..406214e3c162daa57514ac101a9395645dd98dc1 100644 (file)
@@ -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<v8::Object> 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 (file)
index 0000000..4aeb328
--- /dev/null
@@ -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
diff --git a/atom/browser/net/url_request_buffer_job.h b/atom/browser/net/url_request_buffer_job.h
new file mode 100644 (file)
index 0000000..936d964
--- /dev/null
@@ -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_
index 1ac402170ee9028d8c7cadca6cd46c0d8241afb8..0a80397ed6803e1494c451944a911597edc86c1f 100644 (file)
@@ -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.
index bf7408df2b26953a22229443a00473bdc9be7587..b87dc7510f6f1ae5e753fda49c0aff3cb9eb5d7f 100644 (file)
@@ -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