Allow returning file for custom protocol.

This commit is contained in:
Cheng Zhao 2013-08-25 16:06:29 +08:00
parent 1ed77371c0
commit 244d7eaf17
4 changed files with 40 additions and 1 deletions

View 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.

View 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) ->

View 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

View 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() {