Browse Source

add spec and docs

deepak1556 8 years ago
parent
commit
ae297760af
3 changed files with 42 additions and 4 deletions
  1. 4 3
      docs/api/protocol.md
  2. 14 1
      spec/api-protocol-spec.js
  3. 24 0
      spec/fixtures/pages/filesystem.html

+ 4 - 3
docs/api/protocol.md

@@ -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')

+ 14 - 1
spec/api-protocol-spec.js

@@ -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 - 0
spec/fixtures/pages/filesystem.html

@@ -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>