From 244d7eaf17a269160f2f90e0a9103bc8ed6bef69 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 25 Aug 2013 16:06:29 +0800 Subject: [PATCH] Allow returning file for custom protocol. --- browser/api/atom_api_protocol.cc | 22 ++++++++++++++++++++++ browser/api/lib/protocol.coffee | 4 ++++ spec/api/protocol.coffee | 11 ++++++++++- spec/main.js | 4 ++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/browser/api/atom_api_protocol.cc b/browser/api/atom_api_protocol.cc index 40d1d8024256..8fe1571c274b 100644 --- a/browser/api/atom_api_protocol.cc +++ b/browser/api/atom_api_protocol.cc @@ -101,6 +101,9 @@ class AdapterRequestJob : public net::URLRequestJob { case REQUEST_STRING_JOB: return static_cast(real_job_.get())-> ReadRawData(buf, buf_size, bytes_read); + case REQUEST_FILE_JOB: + return static_cast(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 real_job_; // Type of the delegated url request job. diff --git a/browser/api/lib/protocol.coffee b/browser/api/lib/protocol.coffee index 350a3d609748..3fe56a4401be 100644 --- a/browser/api/lib/protocol.coffee +++ b/browser/api/lib/protocol.coffee @@ -9,3 +9,7 @@ class RequestStringJob @mimeType = mimeType ? 'text/plain' @charset = charset ? 'UTF-8' @data = String data + +module.exports.RequestFileJob = +class RequestFileJob + constructor: (@path) -> diff --git a/spec/api/protocol.coffee b/spec/api/protocol.coffee index 6f20fcf4454b..9f5e06c09719 100644 --- a/spec/api/protocol.coffee +++ b/spec/api/protocol.coffee @@ -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 diff --git a/spec/main.js b/spec/main.js index 851f60817796..ec41f007e159 100644 --- a/spec/main.js +++ b/spec/main.js @@ -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() {