You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2020/06/12 15:22:03 UTC
[cordova-electron] branch master updated: feature: support custom
scheme & hostname (#154)
This is an automated email from the ASF dual-hosted git repository.
erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-electron.git
The following commit(s) were added to refs/heads/master by this push:
new cc03aff feature: support custom scheme & hostname (#154)
cc03aff is described below
commit cc03aff0541487c5c6ef0fc85c89bb2073c19a24
Author: エリス <er...@users.noreply.github.com>
AuthorDate: Sat Jun 13 00:21:57 2020 +0900
feature: support custom scheme & hostname (#154)
* feature: support custom scheme & hostname
* chore: apply suggestions from code review
* chore: ignore standard/no-callback-literal for Electron main js
* chore: remove cb prefix and remove error cb to fix ProtocolDeprecateCallback
Co-authored-by: Tim Brust <gi...@timbrust.de>
---
.eslintrc.yml | 4 ++
bin/templates/platform_www/cdv-electron-main.js | 52 ++++++++++++++++++++--
.../platform_www/cdv-reserved-scheme.json | 43 ++++++++++++++++++
lib/SettingJsonParser.js | 6 +++
tests/spec/fixtures/test-config-custom-scheme.xml | 28 ++++++++++++
tests/spec/unit/lib/SettingJsonParser.spec.js | 47 ++++++++++++++++++-
6 files changed, 176 insertions(+), 4 deletions(-)
diff --git a/.eslintrc.yml b/.eslintrc.yml
index 679d9c0..0133d6f 100644
--- a/.eslintrc.yml
+++ b/.eslintrc.yml
@@ -24,3 +24,7 @@ overrides:
- files: [cordova-js-src/**/*.js]
extends: '@cordova/eslint-config/browser'
+
+ - files: [bin/templates/platform_www/cdv-electron-main.js]
+ rules:
+ standard/no-callback-literal: off
diff --git a/bin/templates/platform_www/cdv-electron-main.js b/bin/templates/platform_www/cdv-electron-main.js
index 32baa95..155dbc2 100644
--- a/bin/templates/platform_www/cdv-electron-main.js
+++ b/bin/templates/platform_www/cdv-electron-main.js
@@ -18,10 +18,37 @@
*/
const fs = require('fs');
+const path = require('path');
// Module to control application life, browser window and tray.
-const { app, BrowserWindow } = require('electron');
+const {
+ app,
+ BrowserWindow,
+ protocol
+} = require('electron');
// Electron settings from .json file.
const cdvElectronSettings = require('./cdv-electron-settings.json');
+const reservedScheme = require('./cdv-reserved-scheme.json');
+
+const scheme = cdvElectronSettings.scheme;
+const hostname = cdvElectronSettings.hostname;
+const isFileProtocol = scheme === 'file';
+
+/**
+ * The base url path.
+ * E.g:
+ * When scheme is defined as "file" the base path is "file://path-to-the-app-root-directory"
+ * When scheme is anything except "file", for example "app", the base path will be "app://localhost"
+ * The hostname "localhost" can be changed but only set when scheme is not "file"
+ */
+const basePath = (() => isFileProtocol ? `file://${__dirname}` : `${scheme}://${hostname}`)();
+
+if (reservedScheme.includes(scheme)) throw new Error(`The scheme "${scheme}" can not be registered. Please use a non-reserved scheme.`);
+
+if (!isFileProtocol) {
+ protocol.registerSchemesAsPrivileged([
+ { scheme, privileges: { standard: true, secure: true } }
+ ]);
+}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
@@ -43,7 +70,7 @@ function createWindow () {
// Load a local HTML file or a remote URL.
const cdvUrl = cdvElectronSettings.browserWindowInstance.loadURL.url;
- const loadUrl = cdvUrl.includes('://') ? cdvUrl : `file://${__dirname}/${cdvUrl}`;
+ const loadUrl = cdvUrl.includes('://') ? cdvUrl : `${basePath}/${cdvUrl}`;
const loadUrlOpts = Object.assign({}, cdvElectronSettings.browserWindowInstance.loadURL.options);
mainWindow.loadURL(loadUrl, loadUrlOpts);
@@ -62,10 +89,25 @@ function createWindow () {
});
}
+function configureProtocol () {
+ protocol.registerFileProtocol(scheme, (request, cb) => {
+ const url = request.url.substr(basePath.length + 1);
+ cb({ path: path.normalize(`${__dirname}/${url}`) });
+ });
+
+ protocol.interceptFileProtocol('file', (_, cb) => { cb(null); });
+}
+
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
-app.on('ready', createWindow);
+app.on('ready', () => {
+ if (!isFileProtocol) {
+ configureProtocol();
+ }
+
+ createWindow();
+});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
@@ -80,6 +122,10 @@ app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
+ if (!isFileProtocol) {
+ configureProtocol();
+ }
+
createWindow();
}
});
diff --git a/bin/templates/platform_www/cdv-reserved-scheme.json b/bin/templates/platform_www/cdv-reserved-scheme.json
new file mode 100644
index 0000000..c77ae9f
--- /dev/null
+++ b/bin/templates/platform_www/cdv-reserved-scheme.json
@@ -0,0 +1,43 @@
+[
+ "about",
+ "data",
+ "dav",
+ "dns",
+ "example",
+ "ftp",
+ "geo",
+ "http",
+ "https",
+ "im",
+ "imap",
+ "info",
+ "ldap",
+ "mailto",
+ "news",
+ "nfs",
+ "pkcs11",
+ "pop",
+ "reload",
+ "rtsp",
+ "s3",
+ "service",
+ "session",
+ "sftp",
+ "shttp",
+ "sip",
+ "sips",
+ "sms",
+ "svn",
+ "tag",
+ "tel",
+ "telnet",
+ "tftp",
+ "tip",
+ "turn",
+ "turns",
+ "tv",
+ "vnc",
+ "ws",
+ "wss",
+ "xmpp"
+]
diff --git a/lib/SettingJsonParser.js b/lib/SettingJsonParser.js
index 87a4db6..7997edf 100644
--- a/lib/SettingJsonParser.js
+++ b/lib/SettingJsonParser.js
@@ -32,12 +32,18 @@ class SettingJsonParser {
const contentNode = config.doc.find('content');
const contentSrc = (contentNode && contentNode.attrib.src) || 'index.html';
+ const scheme = config.getPreference('scheme', 'electron');
+ const hostname = config.getPreference('hostname', 'electron');
+
this.package.browserWindowInstance = {
loadURL: {
url: contentSrc
}
};
+ this.package.scheme = scheme || 'file';
+ this.package.hostname = hostname || 'localhost';
+
// Apply user settings ontop of defaults.
if (userElectronSettingsPath) {
const userElectronSettings = require(userElectronSettingsPath);
diff --git a/tests/spec/fixtures/test-config-custom-scheme.xml b/tests/spec/fixtures/test-config-custom-scheme.xml
new file mode 100644
index 0000000..93d6a5b
--- /dev/null
+++ b/tests/spec/fixtures/test-config-custom-scheme.xml
@@ -0,0 +1,28 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+
+<widget>
+ <name />
+ <description />
+ <author />
+ <content />
+ <preference name="scheme" value="app" />
+ <preference name="hostname" value="cordova" />
+</widget>
diff --git a/tests/spec/unit/lib/SettingJsonParser.spec.js b/tests/spec/unit/lib/SettingJsonParser.spec.js
index f25e8d4..6be292c 100644
--- a/tests/spec/unit/lib/SettingJsonParser.spec.js
+++ b/tests/spec/unit/lib/SettingJsonParser.spec.js
@@ -27,6 +27,7 @@ const fixturesDir = path.join(rootDir, 'tests/spec/fixtures');
// Create a real config object before mocking out everything.
const cfg = new ConfigParser(path.join(fixturesDir, 'test-config-1.xml'));
const cfgEmpty = new ConfigParser(path.join(fixturesDir, 'test-config-empty.xml'));
+const cfgCustomSchemeHostname = new ConfigParser(path.join(fixturesDir, 'test-config-custom-scheme.xml'));
describe('Testing SettingJsonParser.js:', () => {
let SettingJsonParser;
@@ -100,6 +101,50 @@ describe('Testing SettingJsonParser.js:', () => {
expect(settingJsonParser.package.browserWindow.webPreferences.nodeIntegration).toBe(true);
});
+ it('should set scheme and hostname to default when not defined.', () => {
+ options = { options: { release: true, argv: [] } };
+
+ SettingJsonParser.__set__('require', (file) => {
+ // return defaults
+ if (file.includes('cdv-electron-settings.json')) {
+ return {
+ browserWindow: {
+ webPreferences: { }
+ }
+ };
+ }
+
+ return require(file);
+ });
+
+ settingJsonParser = new SettingJsonParser(locations.www).configure(cfgEmpty, options.options, false);
+
+ expect(settingJsonParser.package.scheme).toBe('file');
+ expect(settingJsonParser.package.hostname).toBe('localhost');
+ });
+
+ it('should set custom scheme and hostname from config.xml.', () => {
+ options = { options: { release: true, argv: [] } };
+
+ SettingJsonParser.__set__('require', (file) => {
+ // return defaults
+ if (file.includes('cdv-electron-settings.json')) {
+ return {
+ browserWindow: {
+ webPreferences: { }
+ }
+ };
+ }
+
+ return require(file);
+ });
+
+ settingJsonParser = new SettingJsonParser(locations.www).configure(cfgCustomSchemeHostname, options.options, false);
+
+ expect(settingJsonParser.package.scheme).toBe('app');
+ expect(settingJsonParser.package.hostname).toBe('cordova');
+ });
+
it('should use default when users settings files does not exist.', () => {
options = {};
@@ -191,7 +236,7 @@ describe('Testing SettingJsonParser.js:', () => {
// get settings json file content and remove white spaces
let settingsFile = writeFileSyncSpy.calls.argsFor(0)[1];
settingsFile = settingsFile.replace(/\s+/g, '');
- expect(settingsFile).toEqual('{"browserWindow":{"webPreferences":{"devTools":true,"nodeIntegration":true}},"browserWindowInstance":{"loadURL":{"url":"index.html"}}}');
+ expect(settingsFile).toEqual('{"browserWindow":{"webPreferences":{"devTools":true,"nodeIntegration":true}},"browserWindowInstance":{"loadURL":{"url":"index.html"}},"scheme":"file","hostname":"localhost"}');
const settingsFormat = writeFileSyncSpy.calls.argsFor(0)[2];
expect(settingsFormat).toEqual('utf8');
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org