]> khamidou — GitHub mirrors - electron.git/commitdiff
Allow returning file for custom protocol.
authorCheng Zhao <zcbenz@gmail.com>
Sun, 25 Aug 2013 08:06:29 +0000 (16:06 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 25 Aug 2013 08:06:29 +0000 (16:06 +0800)
browser/api/atom_api_protocol.cc
browser/api/lib/protocol.coffee
spec/api/protocol.coffee
spec/main.js

index 40d1d802425661fab9a66cdad2dd80d506b1d154..8fe1571c274bfa2d81b13d367386e8bf984634a5 100644 (file)
@@ -101,6 +101,9 @@ class AdapterRequestJob : public net::URLRequestJob {
       case REQUEST_STRING_JOB:
         return static_cast<URLRequestStringJob*>(real_job_.get())->
             ReadRawData(buf, buf_size, bytes_read);
+      case REQUEST_FILE_JOB:
+        return static_cast<net::URLRequestFileJob*>(real_job_.get())->
+            ReadRawData(buf, buf_size, bytes_read);
       default:
         return net::URLRequestJob::ReadRawData(buf, buf_size, bytes_read);
     }
@@ -178,6 +181,17 @@ class AdapterRequestJob : public net::URLRequestJob {
                        charset,
                        data));
         return;
+      } else if (name == "RequestFileJob") {
+        base::FilePath path = base::FilePath::FromUTF8Unsafe(
+            *v8::String::Utf8Value(obj->Get(v8::String::New("path"))));
+
+        content::BrowserThread::PostTask(
+            content::BrowserThread::IO,
+            FROM_HERE,
+            base::Bind(&AdapterRequestJob::CreateFileJobAndStart,
+                       weak_factory_.GetWeakPtr(),
+                       path));
+        return;
       }
     }
 
@@ -210,6 +224,14 @@ class AdapterRequestJob : public net::URLRequestJob {
     real_job_->Start();
   }
 
+  void CreateFileJobAndStart(const base::FilePath& path) {
+    DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
+
+    type_ = REQUEST_FILE_JOB;
+    real_job_ = new net::URLRequestFileJob(request(), network_delegate(), path);
+    real_job_->Start();
+  }
+
   scoped_refptr<net::URLRequestJob> real_job_;
 
   // Type of the delegated url request job.
index 350a3d609748f65fd51518e484b28fdf9460485b..3fe56a4401be4594ef7ae5edbdbc8d473be403fc 100644 (file)
@@ -9,3 +9,7 @@ class RequestStringJob
     @mimeType = mimeType ? 'text/plain'
     @charset = charset ? 'UTF-8'
     @data = String data
+
+module.exports.RequestFileJob =
+class RequestFileJob
+  constructor: (@path) ->
index 6f20fcf4454bbc047c28618c7c0cde6b508fce4e..9f5e06c097192302f83db3090186a36e4ac1bd3a 100644 (file)
@@ -13,9 +13,9 @@ describe 'protocol API', ->
     it 'calls the callback when scheme is visited', (done) ->
       protocol.registerProtocol 'test2', (url) ->
         assert.equal url, 'test2://test2'
+        protocol.unregisterProtocol 'test2'
         done()
       $.get 'test2://test2', ->
-      protocol.unregisterProtocol 'test2'
 
   describe 'protocol.unregisterProtocol', ->
     it 'throws error when scheme does not exist', ->
@@ -40,3 +40,12 @@ describe 'protocol API', ->
           done()
         error: (xhr, errorType, error) ->
           assert false, 'Got error: ' + errorType + ' ' + error
+
+    it 'returns RequestFileJob should send file', (done) ->
+      $.ajax
+        url: 'atom-file-job://' + __filename
+        success: (data) ->
+          console.log data
+          done()
+        error: (xhr, errorType, error) ->
+          assert false, 'Got error: ' + errorType + ' ' + error
index 851f608177965ca25e54f4a520fe824e6a549bf8..ec41f007e159927fc63f8082644690916c941ae3 100644 (file)
@@ -45,6 +45,10 @@ app.on('will-finish-launching', function() {
   protocol.registerProtocol('atom-string-job', function(url) {
     return new protocol.RequestStringJob({mimeType: 'text/html', data: url});
   });
+
+  protocol.registerProtocol('atom-file-job', function(url) {
+    return new protocol.RequestFileJob(url.substr(16));
+  });
 });
 
 app.on('finish-launching', function() {