2016-03-24 13:15:04 -07:00
|
|
|
'use strict'
|
2016-01-13 11:55:49 +08:00
|
|
|
|
2019-03-18 12:37:06 -07:00
|
|
|
const features = process.electronBinding('features')
|
2018-09-14 02:10:51 +10:00
|
|
|
const { EventEmitter } = require('events')
|
2016-09-06 11:31:14 -07:00
|
|
|
const electron = require('electron')
|
2018-01-04 11:38:56 +13:00
|
|
|
const path = require('path')
|
|
|
|
const url = require('url')
|
2018-12-03 17:13:09 +01:00
|
|
|
const { app, ipcMain, session, deprecate } = electron
|
2016-06-01 14:57:35 +09:00
|
|
|
|
2019-12-16 02:30:25 +01:00
|
|
|
const { internalWindowOpen } = require('@electron/internal/browser/guest-window-manager')
|
2018-12-03 17:13:09 +01:00
|
|
|
const NavigationController = require('@electron/internal/browser/navigation-controller')
|
2019-02-04 14:49:53 -08:00
|
|
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
2019-03-13 20:03:17 +01:00
|
|
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
2018-09-11 11:56:00 +02:00
|
|
|
|
2016-06-01 14:57:35 +09:00
|
|
|
// session is not used here, the purpose is to make sure session is initalized
|
|
|
|
// before the webContents module.
|
2017-11-23 13:42:48 -08:00
|
|
|
// eslint-disable-next-line
|
2016-06-01 14:57:35 +09:00
|
|
|
session
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-03-24 13:15:04 -07:00
|
|
|
let nextId = 0
|
2016-06-01 15:08:51 +09:00
|
|
|
const getNextId = function () {
|
2016-03-24 13:15:04 -07:00
|
|
|
return ++nextId
|
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-06-01 15:24:53 +09:00
|
|
|
// Stock page sizes
|
|
|
|
const PDFPageSizes = {
|
2016-01-11 18:40:23 -08:00
|
|
|
A5: {
|
2016-03-24 13:15:04 -07:00
|
|
|
custom_display_name: 'A5',
|
2016-01-11 18:40:23 -08:00
|
|
|
height_microns: 210000,
|
2016-03-24 13:15:04 -07:00
|
|
|
name: 'ISO_A5',
|
2016-01-11 18:40:23 -08:00
|
|
|
width_microns: 148000
|
|
|
|
},
|
|
|
|
A4: {
|
2016-03-24 13:15:04 -07:00
|
|
|
custom_display_name: 'A4',
|
2016-01-11 18:40:23 -08:00
|
|
|
height_microns: 297000,
|
2016-03-24 13:15:04 -07:00
|
|
|
name: 'ISO_A4',
|
|
|
|
is_default: 'true',
|
2016-01-11 18:40:23 -08:00
|
|
|
width_microns: 210000
|
|
|
|
},
|
|
|
|
A3: {
|
2016-03-24 13:15:04 -07:00
|
|
|
custom_display_name: 'A3',
|
2016-01-11 18:40:23 -08:00
|
|
|
height_microns: 420000,
|
2016-03-24 13:15:04 -07:00
|
|
|
name: 'ISO_A3',
|
2016-01-11 18:40:23 -08:00
|
|
|
width_microns: 297000
|
|
|
|
},
|
|
|
|
Legal: {
|
2016-03-24 13:15:04 -07:00
|
|
|
custom_display_name: 'Legal',
|
2016-01-11 18:40:23 -08:00
|
|
|
height_microns: 355600,
|
2016-03-24 13:15:04 -07:00
|
|
|
name: 'NA_LEGAL',
|
2016-01-11 18:40:23 -08:00
|
|
|
width_microns: 215900
|
|
|
|
},
|
|
|
|
Letter: {
|
2016-03-24 13:15:04 -07:00
|
|
|
custom_display_name: 'Letter',
|
2016-01-11 18:40:23 -08:00
|
|
|
height_microns: 279400,
|
2016-03-24 13:15:04 -07:00
|
|
|
name: 'NA_LETTER',
|
2016-01-11 18:40:23 -08:00
|
|
|
width_microns: 215900
|
|
|
|
},
|
|
|
|
Tabloid: {
|
|
|
|
height_microns: 431800,
|
2016-03-24 13:15:04 -07:00
|
|
|
name: 'NA_LEDGER',
|
2016-01-11 18:40:23 -08:00
|
|
|
width_microns: 279400,
|
2016-03-24 13:15:04 -07:00
|
|
|
custom_display_name: 'Tabloid'
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-06-01 15:24:53 +09:00
|
|
|
// Default printing setting
|
|
|
|
const defaultPrintingSetting = {
|
|
|
|
pageRage: [],
|
|
|
|
mediaSize: {},
|
|
|
|
landscape: false,
|
|
|
|
color: 2,
|
|
|
|
headerFooterEnabled: false,
|
|
|
|
marginsType: 0,
|
|
|
|
isFirstRequest: false,
|
2018-11-09 09:12:34 +05:30
|
|
|
previewUIID: 0,
|
2016-06-01 15:24:53 +09:00
|
|
|
previewModifiable: true,
|
|
|
|
printToPDF: true,
|
|
|
|
printWithCloudPrint: false,
|
|
|
|
printWithPrivet: false,
|
|
|
|
printWithExtension: false,
|
2019-01-27 21:32:56 +05:30
|
|
|
pagesPerSheet: 1,
|
2016-06-01 15:24:53 +09:00
|
|
|
deviceName: 'Save as PDF',
|
|
|
|
generateDraftData: true,
|
|
|
|
fitToPageEnabled: false,
|
2019-10-18 08:40:19 +08:00
|
|
|
scaleFactor: 100,
|
2017-08-22 13:54:49 +09:00
|
|
|
dpiHorizontal: 72,
|
|
|
|
dpiVertical: 72,
|
2017-01-31 17:41:05 +09:00
|
|
|
rasterizePDF: false,
|
2016-06-01 15:24:53 +09:00
|
|
|
duplex: 0,
|
|
|
|
copies: 1,
|
|
|
|
collate: true,
|
|
|
|
shouldPrintBackgrounds: false,
|
|
|
|
shouldPrintSelectionOnly: false
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
// JavaScript implementations of WebContents.
|
2019-03-18 12:37:06 -07:00
|
|
|
const binding = process.electronBinding('web_contents')
|
2018-09-14 02:10:51 +10:00
|
|
|
const { WebContents } = binding
|
2016-08-02 20:38:35 +09:00
|
|
|
|
2016-08-02 20:55:56 +09:00
|
|
|
Object.setPrototypeOf(NavigationController.prototype, EventEmitter.prototype)
|
|
|
|
Object.setPrototypeOf(WebContents.prototype, NavigationController.prototype)
|
2016-08-02 20:38:35 +09:00
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
// WebContents::send(channel, args..)
|
|
|
|
// WebContents::sendToAll(channel, args..)
|
|
|
|
WebContents.prototype.send = function (channel, ...args) {
|
2018-10-06 13:48:00 +02:00
|
|
|
if (typeof channel !== 'string') {
|
|
|
|
throw new Error('Missing required channel argument')
|
|
|
|
}
|
|
|
|
|
|
|
|
const internal = false
|
|
|
|
const sendToAll = false
|
|
|
|
|
|
|
|
return this._send(internal, sendToAll, channel, args)
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
2019-01-20 23:40:27 -08:00
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
WebContents.prototype.sendToAll = function (channel, ...args) {
|
2018-10-06 13:48:00 +02:00
|
|
|
if (typeof channel !== 'string') {
|
|
|
|
throw new Error('Missing required channel argument')
|
|
|
|
}
|
|
|
|
|
|
|
|
const internal = false
|
|
|
|
const sendToAll = true
|
|
|
|
|
|
|
|
return this._send(internal, sendToAll, channel, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
WebContents.prototype._sendInternal = function (channel, ...args) {
|
|
|
|
if (typeof channel !== 'string') {
|
|
|
|
throw new Error('Missing required channel argument')
|
|
|
|
}
|
|
|
|
|
|
|
|
const internal = true
|
|
|
|
const sendToAll = false
|
|
|
|
|
|
|
|
return this._send(internal, sendToAll, channel, args)
|
|
|
|
}
|
|
|
|
WebContents.prototype._sendInternalToAll = function (channel, ...args) {
|
|
|
|
if (typeof channel !== 'string') {
|
|
|
|
throw new Error('Missing required channel argument')
|
|
|
|
}
|
|
|
|
|
|
|
|
const internal = true
|
|
|
|
const sendToAll = true
|
|
|
|
|
|
|
|
return this._send(internal, sendToAll, channel, args)
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
2019-01-22 11:24:46 -08:00
|
|
|
WebContents.prototype.sendToFrame = function (frameId, channel, ...args) {
|
|
|
|
if (typeof channel !== 'string') {
|
|
|
|
throw new Error('Missing required channel argument')
|
|
|
|
} else if (typeof frameId !== 'number') {
|
|
|
|
throw new Error('Missing required frameId argument')
|
|
|
|
}
|
|
|
|
|
|
|
|
const internal = false
|
|
|
|
const sendToAll = false
|
|
|
|
|
|
|
|
return this._sendToFrame(internal, sendToAll, frameId, channel, args)
|
|
|
|
}
|
2018-12-10 09:37:42 +09:00
|
|
|
WebContents.prototype._sendToFrameInternal = function (frameId, channel, ...args) {
|
|
|
|
if (typeof channel !== 'string') {
|
|
|
|
throw new Error('Missing required channel argument')
|
|
|
|
} else if (typeof frameId !== 'number') {
|
|
|
|
throw new Error('Missing required frameId argument')
|
|
|
|
}
|
|
|
|
|
|
|
|
const internal = true
|
|
|
|
const sendToAll = false
|
|
|
|
|
|
|
|
return this._sendToFrame(internal, sendToAll, frameId, channel, args)
|
|
|
|
}
|
2016-08-02 20:52:07 +09:00
|
|
|
|
2016-01-13 11:55:49 +08:00
|
|
|
// Following methods are mapped to webFrame.
|
|
|
|
const webFrameMethods = [
|
2016-12-19 15:50:47 -08:00
|
|
|
'insertCSS',
|
2016-01-13 11:55:49 +08:00
|
|
|
'insertText',
|
2019-06-17 17:39:36 +02:00
|
|
|
'removeInsertedCSS',
|
2018-02-20 08:57:48 -05:00
|
|
|
'setVisualZoomLevelLimits'
|
2016-03-24 13:15:04 -07:00
|
|
|
]
|
2017-11-18 00:51:14 -08:00
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
for (const method of webFrameMethods) {
|
|
|
|
WebContents.prototype[method] = function (...args) {
|
2019-06-17 11:10:02 +02:00
|
|
|
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-11-22 15:33:55 -08:00
|
|
|
const waitTillCanExecuteJavaScript = async (webContents) => {
|
|
|
|
if (webContents.getURL() && !webContents.isLoadingMainFrame()) return
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
webContents.once('did-stop-loading', () => {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
2019-03-14 20:08:54 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
// Make sure WebContents::executeJavaScript would run the code only when the
|
|
|
|
// WebContents has been loaded.
|
2019-11-22 15:33:55 -08:00
|
|
|
WebContents.prototype.executeJavaScript = async function (code, hasUserGesture) {
|
|
|
|
await waitTillCanExecuteJavaScript(this)
|
|
|
|
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScript', code, hasUserGesture)
|
|
|
|
}
|
|
|
|
WebContents.prototype.executeJavaScriptInIsolatedWorld = async function (code, hasUserGesture) {
|
|
|
|
await waitTillCanExecuteJavaScript(this)
|
|
|
|
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', 'executeJavaScriptInIsolatedWorld', code, hasUserGesture)
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
2016-08-02 13:02:55 +10:00
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
// Translate the options of printToPDF.
|
2019-02-11 20:20:04 +01:00
|
|
|
WebContents.prototype.printToPDF = function (options) {
|
2019-10-29 03:36:29 +01:00
|
|
|
const printingSetting = {
|
|
|
|
...defaultPrintingSetting,
|
|
|
|
requestID: getNextId()
|
|
|
|
}
|
2016-08-02 20:52:07 +09:00
|
|
|
if (options.landscape) {
|
|
|
|
printingSetting.landscape = options.landscape
|
|
|
|
}
|
2019-10-18 08:40:19 +08:00
|
|
|
if (options.fitToPageEnabled) {
|
|
|
|
printingSetting.fitToPageEnabled = options.fitToPageEnabled
|
|
|
|
}
|
|
|
|
if (options.scaleFactor) {
|
|
|
|
printingSetting.scaleFactor = options.scaleFactor
|
|
|
|
}
|
2016-08-02 20:52:07 +09:00
|
|
|
if (options.marginsType) {
|
|
|
|
printingSetting.marginsType = options.marginsType
|
|
|
|
}
|
|
|
|
if (options.printSelectionOnly) {
|
|
|
|
printingSetting.shouldPrintSelectionOnly = options.printSelectionOnly
|
|
|
|
}
|
|
|
|
if (options.printBackground) {
|
|
|
|
printingSetting.shouldPrintBackgrounds = options.printBackground
|
2016-01-13 11:55:49 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
if (options.pageSize) {
|
|
|
|
const pageSize = options.pageSize
|
|
|
|
if (typeof pageSize === 'object') {
|
|
|
|
if (!pageSize.height || !pageSize.width) {
|
2019-02-11 20:20:04 +01:00
|
|
|
return Promise.reject(new Error('Must define height and width for pageSize'))
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
|
|
|
// Dimensions in Microns
|
|
|
|
// 1 meter = 10^6 microns
|
|
|
|
printingSetting.mediaSize = {
|
|
|
|
name: 'CUSTOM',
|
|
|
|
custom_display_name: 'Custom',
|
2017-05-06 19:19:31 -04:00
|
|
|
height_microns: Math.ceil(pageSize.height),
|
|
|
|
width_microns: Math.ceil(pageSize.width)
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
|
|
|
} else if (PDFPageSizes[pageSize]) {
|
|
|
|
printingSetting.mediaSize = PDFPageSizes[pageSize]
|
|
|
|
} else {
|
2019-02-11 20:20:04 +01:00
|
|
|
return Promise.reject(new Error(`Does not support pageSize with ${pageSize}`))
|
2016-08-02 13:02:55 +10:00
|
|
|
}
|
2016-08-02 20:52:07 +09:00
|
|
|
} else {
|
|
|
|
printingSetting.mediaSize = PDFPageSizes['A4']
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2016-02-22 19:30:21 +05:30
|
|
|
|
2018-10-13 03:57:04 +02:00
|
|
|
// Chromium expects this in a 0-100 range number, not as float
|
2019-10-18 08:40:19 +08:00
|
|
|
printingSetting.scaleFactor = Math.ceil(printingSetting.scaleFactor) % 100
|
2019-10-28 18:12:35 -04:00
|
|
|
// PrinterType enum from //printing/print_job_constants.h
|
|
|
|
printingSetting.printerType = 2
|
2018-11-09 09:12:34 +05:30
|
|
|
if (features.isPrintingEnabled()) {
|
2019-02-11 20:20:04 +01:00
|
|
|
return this._printToPDF(printingSetting)
|
2018-11-09 09:12:34 +05:30
|
|
|
} else {
|
2019-02-11 20:20:04 +01:00
|
|
|
return Promise.reject(new Error('Printing feature is disabled'))
|
2018-11-09 09:12:34 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebContents.prototype.print = function (...args) {
|
|
|
|
if (features.isPrintingEnabled()) {
|
2019-02-25 13:30:22 +00:00
|
|
|
this._print(...args)
|
2018-11-09 09:12:34 +05:30
|
|
|
} else {
|
|
|
|
console.error('Error: Printing feature is disabled.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebContents.prototype.getPrinters = function () {
|
|
|
|
if (features.isPrintingEnabled()) {
|
|
|
|
return this._getPrinters()
|
|
|
|
} else {
|
|
|
|
console.error('Error: Printing feature is disabled.')
|
2019-12-06 12:36:35 -08:00
|
|
|
return []
|
2018-11-09 09:12:34 +05:30
|
|
|
}
|
2016-08-02 20:52:07 +09:00
|
|
|
}
|
|
|
|
|
2018-09-11 09:56:49 +02:00
|
|
|
WebContents.prototype.loadFile = function (filePath, options = {}) {
|
2018-01-04 11:38:56 +13:00
|
|
|
if (typeof filePath !== 'string') {
|
|
|
|
throw new Error('Must pass filePath as a string')
|
|
|
|
}
|
2018-09-14 02:10:51 +10:00
|
|
|
const { query, search, hash } = options
|
2018-09-11 09:56:49 +02:00
|
|
|
|
2018-01-04 11:38:56 +13:00
|
|
|
return this.loadURL(url.format({
|
|
|
|
protocol: 'file',
|
|
|
|
slashes: true,
|
2018-09-11 09:56:49 +02:00
|
|
|
pathname: path.resolve(app.getAppPath(), filePath),
|
|
|
|
query,
|
|
|
|
search,
|
|
|
|
hash
|
2018-01-04 11:38:56 +13:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:24:46 -08:00
|
|
|
const addReplyToEvent = (event) => {
|
|
|
|
event.reply = (...args) => {
|
|
|
|
event.sender.sendToFrame(event.frameId, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const addReplyInternalToEvent = (event) => {
|
|
|
|
Object.defineProperty(event, '_replyInternal', {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: false,
|
|
|
|
value: (...args) => {
|
|
|
|
event.sender._sendToFrameInternal(event.frameId, ...args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:24:57 +01:00
|
|
|
const addReturnValueToEvent = (event) => {
|
|
|
|
Object.defineProperty(event, 'returnValue', {
|
|
|
|
set: (value) => event.sendReply([value]),
|
|
|
|
get: () => {}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
// Add JavaScript wrappers for WebContents class.
|
|
|
|
WebContents.prototype._init = function () {
|
2016-08-02 20:55:56 +09:00
|
|
|
// The navigation controller.
|
|
|
|
NavigationController.call(this, this)
|
|
|
|
|
2018-12-10 08:13:09 -08:00
|
|
|
// Every remote callback from renderer process would add a listener to the
|
2018-11-27 23:50:53 -05:00
|
|
|
// render-view-deleted event, so ignore the listeners warning.
|
2016-08-02 20:52:07 +09:00
|
|
|
this.setMaxListeners(0)
|
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Dispatch IPC messages to the ipc module.
|
2019-01-23 17:24:57 +01:00
|
|
|
this.on('-ipc-message', function (event, internal, channel, args) {
|
|
|
|
if (internal) {
|
|
|
|
addReplyInternalToEvent(event)
|
|
|
|
ipcMainInternal.emit(channel, event, ...args)
|
|
|
|
} else {
|
|
|
|
addReplyToEvent(event)
|
|
|
|
this.emit('ipc-message', event, channel, ...args)
|
|
|
|
ipcMain.emit(channel, event, ...args)
|
|
|
|
}
|
2018-10-06 13:48:00 +02:00
|
|
|
})
|
2018-11-27 23:50:53 -05:00
|
|
|
|
2019-08-24 00:45:50 +02:00
|
|
|
this.on('-ipc-invoke', function (event, internal, channel, args) {
|
2019-05-31 10:25:19 -07:00
|
|
|
event._reply = (result) => event.sendReply({ result })
|
|
|
|
event._throw = (error) => {
|
|
|
|
console.error(`Error occurred in handler for '${channel}':`, error)
|
|
|
|
event.sendReply({ error: error.toString() })
|
|
|
|
}
|
2019-08-24 00:45:50 +02:00
|
|
|
const target = internal ? ipcMainInternal : ipcMain
|
|
|
|
if (target._invokeHandlers.has(channel)) {
|
|
|
|
target._invokeHandlers.get(channel)(event, ...args)
|
2019-05-31 10:25:19 -07:00
|
|
|
} else {
|
|
|
|
event._throw(`No handler registered for '${channel}'`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-01-23 17:24:57 +01:00
|
|
|
this.on('-ipc-message-sync', function (event, internal, channel, args) {
|
|
|
|
addReturnValueToEvent(event)
|
|
|
|
if (internal) {
|
|
|
|
addReplyInternalToEvent(event)
|
|
|
|
ipcMainInternal.emit(channel, event, ...args)
|
|
|
|
} else {
|
|
|
|
addReplyToEvent(event)
|
|
|
|
this.emit('ipc-message-sync', event, channel, ...args)
|
|
|
|
ipcMain.emit(channel, event, ...args)
|
|
|
|
}
|
2018-10-06 13:48:00 +02:00
|
|
|
})
|
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// Handle context menu action request from pepper plugin.
|
2017-11-27 00:24:25 +01:00
|
|
|
this.on('pepper-context-menu', function (event, params, callback) {
|
2017-12-20 18:48:09 +09:00
|
|
|
// Access Menu via electron.Menu to prevent circular require.
|
2016-09-06 11:31:14 -07:00
|
|
|
const menu = electron.Menu.buildFromTemplate(params.menu)
|
2018-02-20 14:02:24 -06:00
|
|
|
menu.popup({
|
|
|
|
window: event.sender.getOwnerBrowserWindow(),
|
|
|
|
x: params.x,
|
|
|
|
y: params.y,
|
2018-02-20 19:20:11 -06:00
|
|
|
callback
|
2018-02-20 14:02:24 -06:00
|
|
|
})
|
2016-03-24 13:15:04 -07:00
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-03-12 00:17:24 +01:00
|
|
|
this.on('crashed', (event, ...args) => {
|
|
|
|
app.emit('renderer-process-crashed', event, this, ...args)
|
|
|
|
})
|
|
|
|
|
2016-04-12 16:36:12 +09:00
|
|
|
// The devtools requests the webContents to reload.
|
2016-08-02 20:38:35 +09:00
|
|
|
this.on('devtools-reload-page', function () {
|
|
|
|
this.reload()
|
2016-04-12 16:36:12 +09:00
|
|
|
})
|
|
|
|
|
2018-10-04 14:00:34 +03:00
|
|
|
// Handle window.open for BrowserWindow and BrowserView.
|
2019-01-30 23:07:08 -06:00
|
|
|
if (['browserView', 'window'].includes(this.getType())) {
|
2018-10-04 14:00:34 +03:00
|
|
|
// Make new windows requested by links behave like "window.open".
|
|
|
|
this.on('-new-window', (event, url, frameName, disposition,
|
|
|
|
additionalFeatures, postData,
|
|
|
|
referrer) => {
|
|
|
|
const options = {
|
|
|
|
show: true,
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
|
|
|
}
|
2019-12-16 02:30:25 +01:00
|
|
|
internalWindowOpen(event, url, referrer, frameName, disposition, options, additionalFeatures, postData)
|
2018-10-04 14:00:34 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// Create a new browser window for the native implementation of
|
|
|
|
// "window.open", used in sandbox and nativeWindowOpen mode.
|
|
|
|
this.on('-add-new-contents', (event, webContents, disposition,
|
|
|
|
userGesture, left, top, width, height, url, frameName) => {
|
|
|
|
if ((disposition !== 'foreground-tab' && disposition !== 'new-window' &&
|
|
|
|
disposition !== 'background-tab')) {
|
|
|
|
event.preventDefault()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
show: true,
|
|
|
|
x: left,
|
|
|
|
y: top,
|
|
|
|
width: width || 800,
|
|
|
|
height: height || 600,
|
|
|
|
webContents
|
|
|
|
}
|
|
|
|
const referrer = { url: '', policy: 'default' }
|
2019-12-16 02:30:25 +01:00
|
|
|
internalWindowOpen(event, url, referrer, frameName, disposition, options)
|
2018-10-04 14:00:34 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-11 09:47:01 -08:00
|
|
|
this.on('login', (event, ...args) => {
|
|
|
|
app.emit('login', event, this, ...args)
|
|
|
|
})
|
|
|
|
|
2019-07-29 13:54:37 -07:00
|
|
|
const event = process.electronBinding('event').createEmpty()
|
|
|
|
app.emit('web-contents-created', event, this)
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-07-03 08:57:10 -07:00
|
|
|
// Deprecations
|
|
|
|
deprecate.fnToProperty(WebContents.prototype, 'audioMuted', '_isAudioMuted', '_setAudioMuted')
|
|
|
|
deprecate.fnToProperty(WebContents.prototype, 'userAgent', '_getUserAgent', '_setUserAgent')
|
|
|
|
deprecate.fnToProperty(WebContents.prototype, 'zoomLevel', '_getZoomLevel', '_setZoomLevel')
|
|
|
|
deprecate.fnToProperty(WebContents.prototype, 'zoomFactor', '_getZoomFactor', '_setZoomFactor')
|
|
|
|
deprecate.fnToProperty(WebContents.prototype, 'frameRate', '_getFrameRate', '_setFrameRate')
|
|
|
|
|
2016-08-02 20:52:07 +09:00
|
|
|
// JavaScript wrapper of Debugger.
|
2019-03-18 12:37:06 -07:00
|
|
|
const { Debugger } = process.electronBinding('debugger')
|
2016-08-02 20:38:35 +09:00
|
|
|
Object.setPrototypeOf(Debugger.prototype, EventEmitter.prototype)
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-08-02 20:38:35 +09:00
|
|
|
// Public APIs.
|
2016-06-13 08:59:03 -07:00
|
|
|
module.exports = {
|
2016-06-13 09:06:42 -07:00
|
|
|
create (options = {}) {
|
2016-06-13 08:59:03 -07:00
|
|
|
return binding.create(options)
|
|
|
|
},
|
|
|
|
|
|
|
|
fromId (id) {
|
|
|
|
return binding.fromId(id)
|
2016-07-13 08:54:40 -07:00
|
|
|
},
|
2016-07-13 12:15:30 -07:00
|
|
|
|
|
|
|
getFocusedWebContents () {
|
|
|
|
let focused = null
|
2018-10-02 03:56:31 +02:00
|
|
|
for (const contents of binding.getAllWebContents()) {
|
2016-07-13 12:15:30 -07:00
|
|
|
if (!contents.isFocused()) continue
|
2016-07-13 14:49:25 -07:00
|
|
|
if (focused == null) focused = contents
|
2016-07-13 12:15:30 -07:00
|
|
|
// Return webview web contents which may be embedded inside another
|
|
|
|
// web contents that is also reporting as focused
|
|
|
|
if (contents.getType() === 'webview') return contents
|
|
|
|
}
|
|
|
|
return focused
|
2016-07-14 08:59:49 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getAllWebContents () {
|
|
|
|
return binding.getAllWebContents()
|
2016-06-13 08:59:03 -07:00
|
|
|
}
|
|
|
|
}
|