Merge pull request #6952 from deepak1556/custom_scheme_filesystem_patch
protocol: allow standard schemes to support filesystem api
This commit is contained in:
commit
764c84f569
5 changed files with 56 additions and 4 deletions
|
@ -9,6 +9,7 @@
|
|||
#endif
|
||||
|
||||
#include "atom/browser/api/atom_api_app.h"
|
||||
#include "atom/browser/api/atom_api_protocol.h"
|
||||
#include "atom/browser/atom_access_token_store.h"
|
||||
#include "atom/browser/atom_browser_context.h"
|
||||
#include "atom/browser/atom_browser_main_parts.h"
|
||||
|
@ -23,8 +24,8 @@
|
|||
#include "base/command_line.h"
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/stl_util.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "chrome/browser/printing/printing_message_filter.h"
|
||||
#include "chrome/browser/renderer_host/pepper/chrome_browser_pepper_host_factory.h"
|
||||
#include "chrome/browser/renderer_host/pepper/widevine_cdm_message_filter.h"
|
||||
|
@ -279,6 +280,15 @@ bool AtomBrowserClient::CanCreateWindow(
|
|||
return false;
|
||||
}
|
||||
|
||||
void AtomBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
|
||||
std::vector<std::string>* additional_schemes) {
|
||||
auto schemes_list = api::GetStandardSchemes();
|
||||
if (!schemes_list.empty())
|
||||
additional_schemes->insert(additional_schemes->end(),
|
||||
schemes_list.begin(),
|
||||
schemes_list.end());
|
||||
}
|
||||
|
||||
brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts(
|
||||
const content::MainFunctionParams&) {
|
||||
v8::V8::Initialize(); // Init V8 before creating main parts.
|
||||
|
|
|
@ -94,6 +94,8 @@ class AtomBrowserClient : public brightray::BrowserClient,
|
|||
int opener_render_view_id,
|
||||
int opener_render_frame_id,
|
||||
bool* no_javascript_access) override;
|
||||
void GetAdditionalAllowedSchemesForFileSystem(
|
||||
std::vector<std::string>* schemes) override;
|
||||
|
||||
// brightray::BrowserClient:
|
||||
brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
|
||||
|
|
|
@ -48,8 +48,10 @@ non-standard schemes can not recognize relative URLs:
|
|||
</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 to files through the
|
||||
[FileSystem API][file-system-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 a standard scheme:
|
||||
|
||||
```javascript
|
||||
const {app, protocol} = require('electron')
|
||||
|
@ -228,3 +230,4 @@ which sends a new HTTP request as a response.
|
|||
Remove the interceptor installed for `scheme` and restore its original handler.
|
||||
|
||||
[net-error]: https://code.google.com/p/chromium/codesearch#chromium/src/net/base/net_error_list.h
|
||||
[file-system-api]: https://developer.mozilla.org/en-US/docs/Web/API/LocalFileSystem
|
||||
|
|
|
@ -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 the 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
24
spec/fixtures/pages/filesystem.html
vendored
Normal 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>
|
Loading…
Add table
Reference in a new issue