add spec and docs

This commit is contained in:
deepak1556 2016-08-24 06:53:14 +05:30
parent ad0c86db7a
commit ae297760af
3 changed files with 42 additions and 4 deletions

View file

@ -47,9 +47,10 @@ non-standard schemes can not recognize relative URLs:
<img src='test.png'>
</body>
```
So if you want to register a custom protocol to replace the `http` protocol, you
have to register it as standard scheme:
Registering a scheme as standard, will allow access of files through
the FileSystem API. Otherwise the renderer will throw a security error for the
scheme. So in general if you want to register a custom protocol to replace the
`http` protocol, you have to register it as standard scheme:
```javascript
const {app, protocol} = require('electron')

View file

@ -4,7 +4,7 @@ const path = require('path')
const qs = require('querystring')
const {closeWindow} = require('./window-helpers')
const remote = require('electron').remote
const {BrowserWindow, protocol, webContents} = remote
const {BrowserWindow, ipcMain, protocol, webContents} = remote
describe('protocol module', function () {
var protocolName = 'sp'
@ -965,5 +965,18 @@ describe('protocol module', function () {
w.loadURL(origin)
})
})
it('can access files through FileSystem API', function (done) {
let filePath = path.join(__dirname, 'fixtures', 'pages', 'filesystem.html')
const handler = function (request, callback) {
callback({path: filePath})
}
protocol.registerFileProtocol(standardScheme, handler, function (error) {
if (error) return done(error)
w.loadURL(origin)
})
ipcMain.once('file-system-error', (event, err) => done(err))
ipcMain.once('file-system-write-end', () => done())
})
})
})

24
spec/fixtures/pages/filesystem.html vendored Normal file
View file

@ -0,0 +1,24 @@
<script>
const {ipcRenderer} = require('electron')
function onInitFs (fs) {
fs.root.getFile('log.txt', {create: true}, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});
fileWriter.onwriteend = function() {
ipcRenderer.send('file-system-write-end')
};
fileWriter.onerror = errorHandler
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}
navigator.webkitPersistentStorage.requestQuota(5 * 1024 * 1024, function (granted) {
webkitRequestFileSystem(TEMPORARY, granted, onInitFs, errorHandler);
}, errorHandler)
function errorHandler(e) {
ipcRenderer.send('file-system-error', e)
}
</script>