Allow returning file for custom protocol.
This commit is contained in:
parent
1ed77371c0
commit
244d7eaf17
4 changed files with 40 additions and 1 deletions
|
@ -101,6 +101,9 @@ class AdapterRequestJob : public net::URLRequestJob {
|
||||||
case REQUEST_STRING_JOB:
|
case REQUEST_STRING_JOB:
|
||||||
return static_cast<URLRequestStringJob*>(real_job_.get())->
|
return static_cast<URLRequestStringJob*>(real_job_.get())->
|
||||||
ReadRawData(buf, buf_size, bytes_read);
|
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:
|
default:
|
||||||
return net::URLRequestJob::ReadRawData(buf, buf_size, bytes_read);
|
return net::URLRequestJob::ReadRawData(buf, buf_size, bytes_read);
|
||||||
}
|
}
|
||||||
|
@ -178,6 +181,17 @@ class AdapterRequestJob : public net::URLRequestJob {
|
||||||
charset,
|
charset,
|
||||||
data));
|
data));
|
||||||
return;
|
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();
|
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_;
|
scoped_refptr<net::URLRequestJob> real_job_;
|
||||||
|
|
||||||
// Type of the delegated url request job.
|
// Type of the delegated url request job.
|
||||||
|
|
|
@ -9,3 +9,7 @@ class RequestStringJob
|
||||||
@mimeType = mimeType ? 'text/plain'
|
@mimeType = mimeType ? 'text/plain'
|
||||||
@charset = charset ? 'UTF-8'
|
@charset = charset ? 'UTF-8'
|
||||||
@data = String data
|
@data = String data
|
||||||
|
|
||||||
|
module.exports.RequestFileJob =
|
||||||
|
class RequestFileJob
|
||||||
|
constructor: (@path) ->
|
||||||
|
|
|
@ -13,9 +13,9 @@ describe 'protocol API', ->
|
||||||
it 'calls the callback when scheme is visited', (done) ->
|
it 'calls the callback when scheme is visited', (done) ->
|
||||||
protocol.registerProtocol 'test2', (url) ->
|
protocol.registerProtocol 'test2', (url) ->
|
||||||
assert.equal url, 'test2://test2'
|
assert.equal url, 'test2://test2'
|
||||||
|
protocol.unregisterProtocol 'test2'
|
||||||
done()
|
done()
|
||||||
$.get 'test2://test2', ->
|
$.get 'test2://test2', ->
|
||||||
protocol.unregisterProtocol 'test2'
|
|
||||||
|
|
||||||
describe 'protocol.unregisterProtocol', ->
|
describe 'protocol.unregisterProtocol', ->
|
||||||
it 'throws error when scheme does not exist', ->
|
it 'throws error when scheme does not exist', ->
|
||||||
|
@ -40,3 +40,12 @@ describe 'protocol API', ->
|
||||||
done()
|
done()
|
||||||
error: (xhr, errorType, error) ->
|
error: (xhr, errorType, error) ->
|
||||||
assert false, 'Got error: ' + 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
|
||||||
|
|
|
@ -45,6 +45,10 @@ app.on('will-finish-launching', function() {
|
||||||
protocol.registerProtocol('atom-string-job', function(url) {
|
protocol.registerProtocol('atom-string-job', function(url) {
|
||||||
return new protocol.RequestStringJob({mimeType: 'text/html', data: 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() {
|
app.on('finish-launching', function() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue