add features module to detect availability of build time features at runtime
This commit is contained in:
parent
4b39d17e5f
commit
e24c0dda5d
11 changed files with 62 additions and 44 deletions
|
@ -2,10 +2,6 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ENABLE_PDF_VIEWER
|
||||
#error("This header can only be used when enable_pdf_viewer gyp flag is enabled")
|
||||
#endif // defined(ENABLE_PDF_VIEWER)
|
||||
|
||||
#include "atom/browser/ui/webui/pdf_viewer_handler.h"
|
||||
|
||||
#include "atom/common/atom_constants.h"
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ENABLE_PDF_VIEWER
|
||||
#error("This header can only be used when enable_pdf_viewer gyp flag is enabled")
|
||||
#endif // defined(ENABLE_PDF_VIEWER)
|
||||
|
||||
#ifndef ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_
|
||||
#define ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_HANDLER_H_
|
||||
|
||||
#ifndef ENABLE_PDF_VIEWER
|
||||
#error("This header can only be used when enable_pdf_viewer gyp flag is enabled") // NOLINT
|
||||
#endif // defined(ENABLE_PDF_VIEWER)
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/browser/web_contents_zoom_controller.h"
|
||||
|
|
|
@ -2,10 +2,6 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ENABLE_PDF_VIEWER
|
||||
#error("This header can only be used when enable_pdf_viewer gyp flag is enabled")
|
||||
#endif // defined(ENABLE_PDF_VIEWER)
|
||||
|
||||
#include "atom/browser/ui/webui/pdf_viewer_ui.h"
|
||||
|
||||
#include <map>
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ENABLE_PDF_VIEWER
|
||||
#error("This header can only be used when enable_pdf_viewer gyp flag is enabled")
|
||||
#endif // defined(ENABLE_PDF_VIEWER)
|
||||
|
||||
#ifndef ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_UI_H_
|
||||
#define ATOM_BROWSER_UI_WEBUI_PDF_VIEWER_UI_H_
|
||||
|
||||
#ifndef ENABLE_PDF_VIEWER
|
||||
#error("This header can only be used when enable_pdf_viewer gyp flag is enabled") // NOLINT
|
||||
#endif // defined(ENABLE_PDF_VIEWER)
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/macros.h"
|
||||
|
|
28
atom/common/api/features.cc
Normal file
28
atom/common/api/features.cc
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "atom/common/node_includes.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool IsPDFViewerEnabled() {
|
||||
#if defined(ENABLE_PDF_VIEWER)
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Initialize(v8::Local<v8::Object> exports,
|
||||
v8::Local<v8::Value> unused,
|
||||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||
dict.SetMethod("isPDFViewerEnabled", &IsPDFViewerEnabled);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_common_features, Initialize)
|
|
@ -53,6 +53,7 @@
|
|||
V(atom_common_asar) \
|
||||
V(atom_common_clipboard) \
|
||||
V(atom_common_crash_reporter) \
|
||||
V(atom_common_features) \
|
||||
V(atom_common_native_image) \
|
||||
V(atom_common_notification) \
|
||||
V(atom_common_screen) \
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
'variables': {
|
||||
'variables': {
|
||||
'enable_osr%': 0, # FIXME(alexeykuzmin)
|
||||
'enable_pdf_viewer%': 0,
|
||||
'enable_pdf_viewer%': 1,
|
||||
'enable_run_as_node%': 1,
|
||||
},
|
||||
'enable_osr%': '<(enable_osr)',
|
||||
|
|
|
@ -407,6 +407,7 @@
|
|||
'atom/common/api/atom_bindings.h',
|
||||
'atom/common/api/event_emitter_caller.cc',
|
||||
'atom/common/api/event_emitter_caller.h',
|
||||
'atom/common/api/features.cc',
|
||||
'atom/common/api/locker.cc',
|
||||
'atom/common/api/locker.h',
|
||||
'atom/common/api/object_life_monitor.cc',
|
||||
|
|
|
@ -189,13 +189,9 @@ def execute_stdout(argv, env=os.environ, cwd=None):
|
|||
def electron_gyp():
|
||||
SOURCE_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', '..'))
|
||||
gyp = os.path.join(SOURCE_ROOT, 'electron.gyp')
|
||||
features_gyp = os.path.join(SOURCE_ROOT, 'features.gypi')
|
||||
obj = {}
|
||||
with open(gyp) as f:
|
||||
obj = eval(f.read())['variables']
|
||||
with open(features_gyp) as g:
|
||||
obj.update(eval(g.read())['variables']['variables'])
|
||||
return obj
|
||||
obj = eval(f.read());
|
||||
return obj['variables']
|
||||
|
||||
|
||||
def get_electron_version():
|
||||
|
|
|
@ -28,7 +28,6 @@ SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
|||
|
||||
PROJECT_NAME = electron_gyp()['project_name%']
|
||||
PRODUCT_NAME = electron_gyp()['product_name%']
|
||||
PDF_VIEWER_ENABLED = electron_gyp()['enable_pdf_viewer%']
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -36,7 +35,6 @@ def main():
|
|||
|
||||
args = parse_args()
|
||||
config = args.configuration
|
||||
os.environ['PDF_VIEWER_ENABLED'] = str(PDF_VIEWER_ENABLED)
|
||||
|
||||
if args.verbose:
|
||||
enable_verbose_mode()
|
||||
|
|
|
@ -7,10 +7,9 @@ const url = require('url')
|
|||
const ChildProcess = require('child_process')
|
||||
const {ipcRenderer, remote} = require('electron')
|
||||
const {closeWindow} = require('./window-helpers')
|
||||
|
||||
const {app, BrowserWindow, ipcMain, protocol, session, webContents} = remote
|
||||
|
||||
const isCI = remote.getGlobal('isCi')
|
||||
const features = process.atomBinding('features')
|
||||
|
||||
/* Most of the APIs here don't use standard callbacks */
|
||||
/* eslint-disable standard/no-callback-literal */
|
||||
|
@ -1019,16 +1018,19 @@ describe('chromium feature', () => {
|
|||
|
||||
describe('PDF Viewer', () => {
|
||||
before(function () {
|
||||
if (!parseInt(process.env.PDF_VIEWER_ENABLED)) {
|
||||
if (!features.isPDFViewerEnabled()) {
|
||||
return this.skip()
|
||||
}
|
||||
})
|
||||
|
||||
const pdfSource = url.format({
|
||||
beforeEach(() => {
|
||||
this.pdfSource = url.format({
|
||||
pathname: path.join(fixtures, 'assets', 'cat.pdf').replace(/\\/g, '/'),
|
||||
protocol: 'file',
|
||||
slashes: true
|
||||
})
|
||||
const pdfSourceWithParams = url.format({
|
||||
|
||||
this.pdfSourceWithParams = url.format({
|
||||
pathname: path.join(fixtures, 'assets', 'cat.pdf').replace(/\\/g, '/'),
|
||||
query: {
|
||||
a: 1,
|
||||
|
@ -1038,7 +1040,7 @@ describe('chromium feature', () => {
|
|||
slashes: true
|
||||
})
|
||||
|
||||
function createBrowserWindow ({plugins, preload}) {
|
||||
this.createBrowserWindow = ({plugins, preload}) => {
|
||||
w = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
|
@ -1048,14 +1050,14 @@ describe('chromium feature', () => {
|
|||
})
|
||||
}
|
||||
|
||||
function testPDFIsLoadedInSubFrame (page, preloadFile, done) {
|
||||
this.testPDFIsLoadedInSubFrame = (page, preloadFile, done) => {
|
||||
const pagePath = url.format({
|
||||
pathname: path.join(fixtures, 'pages', page).replace(/\\/g, '/'),
|
||||
protocol: 'file',
|
||||
slashes: true
|
||||
})
|
||||
|
||||
createBrowserWindow({plugins: true, preload: preloadFile})
|
||||
this.createBrowserWindow({plugins: true, preload: preloadFile})
|
||||
ipcMain.once('pdf-loaded', (event, state) => {
|
||||
assert.equal(state, 'success')
|
||||
done()
|
||||
|
@ -1072,7 +1074,7 @@ describe('chromium feature', () => {
|
|||
})
|
||||
|
||||
it('opens when loading a pdf resource as top level navigation', (done) => {
|
||||
createBrowserWindow({plugins: true, preload: 'preload-pdf-loaded.js'})
|
||||
this.createBrowserWindow({plugins: true, preload: 'preload-pdf-loaded.js'})
|
||||
ipcMain.once('pdf-loaded', (event, state) => {
|
||||
assert.equal(state, 'success')
|
||||
done()
|
||||
|
@ -1081,14 +1083,14 @@ describe('chromium feature', () => {
|
|||
const parsedURL = url.parse(w.webContents.getURL(), true)
|
||||
assert.equal(parsedURL.protocol, 'chrome:')
|
||||
assert.equal(parsedURL.hostname, 'pdf-viewer')
|
||||
assert.equal(parsedURL.query.src, pdfSource)
|
||||
assert.equal(parsedURL.query.src, this.pdfSource)
|
||||
assert.equal(w.webContents.getTitle(), 'cat.pdf')
|
||||
})
|
||||
w.webContents.loadURL(pdfSource)
|
||||
w.webContents.loadURL(this.pdfSource)
|
||||
})
|
||||
|
||||
it('opens a pdf link given params, the query string should be escaped', (done) => {
|
||||
createBrowserWindow({plugins: true, preload: 'preload-pdf-loaded.js'})
|
||||
this.createBrowserWindow({plugins: true, preload: 'preload-pdf-loaded.js'})
|
||||
ipcMain.once('pdf-loaded', (event, state) => {
|
||||
assert.equal(state, 'success')
|
||||
done()
|
||||
|
@ -1097,16 +1099,16 @@ describe('chromium feature', () => {
|
|||
const parsedURL = url.parse(w.webContents.getURL(), true)
|
||||
assert.equal(parsedURL.protocol, 'chrome:')
|
||||
assert.equal(parsedURL.hostname, 'pdf-viewer')
|
||||
assert.equal(parsedURL.query.src, pdfSourceWithParams)
|
||||
assert.equal(parsedURL.query.src, this.pdfSourceWithParams)
|
||||
assert.equal(parsedURL.query.b, undefined)
|
||||
assert(parsedURL.search.endsWith('%3Fa%3D1%26b%3D2'))
|
||||
assert.equal(w.webContents.getTitle(), 'cat.pdf')
|
||||
})
|
||||
w.webContents.loadURL(pdfSourceWithParams)
|
||||
w.webContents.loadURL(this.pdfSourceWithParams)
|
||||
})
|
||||
|
||||
it('should download a pdf when plugins are disabled', (done) => {
|
||||
createBrowserWindow({plugins: false, preload: 'preload-pdf-loaded.js'})
|
||||
this.createBrowserWindow({plugins: false, preload: 'preload-pdf-loaded.js'})
|
||||
ipcRenderer.sendSync('set-download-option', false, false)
|
||||
ipcRenderer.once('download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) => {
|
||||
assert.equal(state, 'completed')
|
||||
|
@ -1115,11 +1117,11 @@ describe('chromium feature', () => {
|
|||
fs.unlinkSync(path.join(fixtures, 'mock.pdf'))
|
||||
done()
|
||||
})
|
||||
w.webContents.loadURL(pdfSource)
|
||||
w.webContents.loadURL(this.pdfSource)
|
||||
})
|
||||
|
||||
it('should not open when pdf is requested as sub resource', (done) => {
|
||||
fetch(pdfSource).then((res) => {
|
||||
fetch(this.pdfSource).then((res) => {
|
||||
assert.equal(res.status, 200)
|
||||
assert.notEqual(document.title, 'cat.pdf')
|
||||
done()
|
||||
|
@ -1127,11 +1129,11 @@ describe('chromium feature', () => {
|
|||
})
|
||||
|
||||
it('opens when loading a pdf resource in a iframe', (done) => {
|
||||
testPDFIsLoadedInSubFrame('pdf-in-iframe.html', 'preload-pdf-loaded-in-subframe.js', done)
|
||||
this.testPDFIsLoadedInSubFrame('pdf-in-iframe.html', 'preload-pdf-loaded-in-subframe.js', done)
|
||||
})
|
||||
|
||||
it('opens when loading a pdf resource in a nested iframe', (done) => {
|
||||
testPDFIsLoadedInSubFrame('pdf-in-nested-iframe.html', 'preload-pdf-loaded-in-nested-subframe.js', done)
|
||||
this.testPDFIsLoadedInSubFrame('pdf-in-nested-iframe.html', 'preload-pdf-loaded-in-nested-subframe.js', done)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue