2015-11-13 08:03:40 +00:00
|
|
|
{deprecate, webFrame, remote} = require 'electron'
|
2014-10-22 14:55:13 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2015-11-12 10:28:04 +00:00
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
guestViewInternal = require './guest-view-internal'
|
2014-12-08 21:36:30 +00:00
|
|
|
webViewConstants = require './web-view-constants'
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2014-12-08 21:36:30 +00:00
|
|
|
# ID generator.
|
|
|
|
nextId = 0
|
|
|
|
getNextId = -> ++nextId
|
2014-10-22 14:55:13 +00:00
|
|
|
|
|
|
|
# Represents the internal state of the WebView node.
|
2014-12-09 05:10:59 +00:00
|
|
|
class WebViewImpl
|
2014-10-22 14:55:13 +00:00
|
|
|
constructor: (@webviewNode) ->
|
2014-10-22 15:37:27 +00:00
|
|
|
v8Util.setHiddenValue @webviewNode, 'internal', this
|
2014-10-22 14:55:13 +00:00
|
|
|
@attached = false
|
|
|
|
@elementAttached = false
|
|
|
|
|
|
|
|
@beforeFirstNavigation = true
|
|
|
|
|
|
|
|
# on* Event handlers.
|
|
|
|
@on = {}
|
|
|
|
|
|
|
|
@browserPluginNode = @createBrowserPluginNode()
|
|
|
|
shadowRoot = @webviewNode.createShadowRoot()
|
2014-12-09 00:14:12 +00:00
|
|
|
@setupWebViewAttributes()
|
2014-10-22 14:55:13 +00:00
|
|
|
@setupFocusPropagation()
|
|
|
|
|
|
|
|
@viewInstanceId = getNextId()
|
2014-12-08 16:05:34 +00:00
|
|
|
|
2014-10-22 14:55:13 +00:00
|
|
|
shadowRoot.appendChild @browserPluginNode
|
|
|
|
|
|
|
|
createBrowserPluginNode: ->
|
|
|
|
# We create BrowserPlugin as a custom element in order to observe changes
|
|
|
|
# to attributes synchronously.
|
2014-12-09 05:10:59 +00:00
|
|
|
browserPluginNode = new WebViewImpl.BrowserPlugin()
|
2014-10-22 14:55:13 +00:00
|
|
|
v8Util.setHiddenValue browserPluginNode, 'internal', this
|
|
|
|
browserPluginNode
|
|
|
|
|
|
|
|
# Resets some state upon reattaching <webview> element to the DOM.
|
|
|
|
reset: ->
|
|
|
|
# If guestInstanceId is defined then the <webview> has navigated and has
|
|
|
|
# already picked up a partition ID. Thus, we need to reset the initialization
|
|
|
|
# state. However, it may be the case that beforeFirstNavigation is false BUT
|
|
|
|
# guestInstanceId has yet to be initialized. This means that we have not
|
|
|
|
# heard back from createGuest yet. We will not reset the flag in this case so
|
|
|
|
# that we don't end up allocating a second guest.
|
|
|
|
if @guestInstanceId
|
2014-10-23 09:54:52 +00:00
|
|
|
guestViewInternal.destroyGuest @guestInstanceId
|
2015-09-30 08:58:37 +00:00
|
|
|
@webContents = null
|
2014-10-22 14:55:13 +00:00
|
|
|
@guestInstanceId = undefined
|
|
|
|
@beforeFirstNavigation = true
|
2014-12-09 00:14:12 +00:00
|
|
|
@attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId = true
|
2014-10-22 14:55:13 +00:00
|
|
|
@internalInstanceId = 0
|
|
|
|
|
|
|
|
# Sets the <webview>.request property.
|
|
|
|
setRequestPropertyOnWebViewNode: (request) ->
|
|
|
|
Object.defineProperty @webviewNode, 'request', value: request, enumerable: true
|
|
|
|
|
|
|
|
setupFocusPropagation: ->
|
|
|
|
unless @webviewNode.hasAttribute 'tabIndex'
|
|
|
|
# <webview> needs a tabIndex in order to be focusable.
|
|
|
|
# TODO(fsamuel): It would be nice to avoid exposing a tabIndex attribute
|
|
|
|
# to allow <webview> to be focusable.
|
|
|
|
# See http://crbug.com/231664.
|
|
|
|
@webviewNode.setAttribute 'tabIndex', -1
|
|
|
|
@webviewNode.addEventListener 'focus', (e) =>
|
|
|
|
# Focus the BrowserPlugin when the <webview> takes focus.
|
|
|
|
@browserPluginNode.focus()
|
|
|
|
@webviewNode.addEventListener 'blur', (e) =>
|
|
|
|
# Blur the BrowserPlugin when the <webview> loses focus.
|
|
|
|
@browserPluginNode.blur()
|
|
|
|
|
|
|
|
# This observer monitors mutations to attributes of the <webview> and
|
|
|
|
# updates the BrowserPlugin properties accordingly. In turn, updating
|
|
|
|
# a BrowserPlugin property will update the corresponding BrowserPlugin
|
|
|
|
# attribute, if necessary. See BrowserPlugin::UpdateDOMAttribute for more
|
|
|
|
# details.
|
2014-12-09 00:56:14 +00:00
|
|
|
handleWebviewAttributeMutation: (attributeName, oldValue, newValue) ->
|
2014-12-09 05:06:23 +00:00
|
|
|
if not @attributes[attributeName] or @attributes[attributeName].ignoreMutation
|
2014-12-09 00:56:14 +00:00
|
|
|
return
|
|
|
|
|
2014-12-09 05:06:23 +00:00
|
|
|
# Let the changed attribute handle its own mutation;
|
|
|
|
@attributes[attributeName].handleMutation oldValue, newValue
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
handleBrowserPluginAttributeMutation: (attributeName, oldValue, newValue) ->
|
|
|
|
if attributeName is webViewConstants.ATTRIBUTE_INTERNALINSTANCEID and !oldValue and !!newValue
|
2014-12-09 00:14:12 +00:00
|
|
|
@browserPluginNode.removeAttribute webViewConstants.ATTRIBUTE_INTERNALINSTANCEID
|
2014-12-08 16:05:34 +00:00
|
|
|
@internalInstanceId = parseInt newValue
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2015-05-29 05:47:09 +00:00
|
|
|
# Track when the element resizes using the element resize callback.
|
|
|
|
webFrame.registerElementResizeCallback @internalInstanceId, @onElementResize.bind(this)
|
|
|
|
|
2014-12-09 01:15:50 +00:00
|
|
|
return unless @guestInstanceId
|
|
|
|
|
2015-05-29 03:44:49 +00:00
|
|
|
guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, @buildParams()
|
2014-10-22 14:55:13 +00:00
|
|
|
|
|
|
|
onSizeChanged: (webViewEvent) ->
|
|
|
|
newWidth = webViewEvent.newWidth
|
|
|
|
newHeight = webViewEvent.newHeight
|
|
|
|
|
|
|
|
node = @webviewNode
|
|
|
|
|
|
|
|
width = node.offsetWidth
|
|
|
|
height = node.offsetHeight
|
|
|
|
|
|
|
|
# Check the current bounds to make sure we do not resize <webview>
|
|
|
|
# outside of current constraints.
|
2014-12-09 05:15:18 +00:00
|
|
|
maxWidth = @attributes[webViewConstants.ATTRIBUTE_MAXWIDTH].getValue() | width
|
|
|
|
maxHeight = @attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() | width
|
|
|
|
minWidth = @attributes[webViewConstants.ATTRIBUTE_MINWIDTH].getValue() | width
|
|
|
|
minHeight = @attributes[webViewConstants.ATTRIBUTE_MINHEIGHT].getValue() | width
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2015-05-29 03:12:55 +00:00
|
|
|
minWidth = Math.min minWidth, maxWidth
|
|
|
|
minHeight = Math.min minHeight, maxHeight
|
|
|
|
|
2014-12-09 00:56:14 +00:00
|
|
|
if not @attributes[webViewConstants.ATTRIBUTE_AUTOSIZE].getValue() or
|
2014-10-22 14:55:13 +00:00
|
|
|
(newWidth >= minWidth and
|
|
|
|
newWidth <= maxWidth and
|
|
|
|
newHeight >= minHeight and
|
|
|
|
newHeight <= maxHeight)
|
|
|
|
node.style.width = newWidth + 'px'
|
|
|
|
node.style.height = newHeight + 'px'
|
|
|
|
# Only fire the DOM event if the size of the <webview> has actually
|
|
|
|
# changed.
|
|
|
|
@dispatchEvent webViewEvent
|
|
|
|
|
2015-08-04 09:07:31 +00:00
|
|
|
onElementResize: (newSize) ->
|
2015-05-29 05:47:09 +00:00
|
|
|
# Dispatch the 'resize' event.
|
|
|
|
resizeEvent = new Event('resize', bubbles: true)
|
|
|
|
resizeEvent.newWidth = newSize.width
|
|
|
|
resizeEvent.newHeight = newSize.height
|
|
|
|
@dispatchEvent resizeEvent
|
|
|
|
|
|
|
|
if @guestInstanceId
|
|
|
|
guestViewInternal.setSize @guestInstanceId, normal: newSize
|
|
|
|
|
2014-10-22 14:55:13 +00:00
|
|
|
createGuest: ->
|
2015-11-10 07:29:43 +00:00
|
|
|
guestViewInternal.createGuest @buildParams(), (event, guestInstanceId) =>
|
2014-12-09 01:15:50 +00:00
|
|
|
@attachWindow guestInstanceId
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2014-10-25 07:48:59 +00:00
|
|
|
dispatchEvent: (webViewEvent) ->
|
2014-10-22 14:55:13 +00:00
|
|
|
@webviewNode.dispatchEvent webViewEvent
|
|
|
|
|
|
|
|
# Adds an 'on<event>' property on the webview, which can be used to set/unset
|
|
|
|
# an event handler.
|
|
|
|
setupEventProperty: (eventName) ->
|
|
|
|
propertyName = 'on' + eventName.toLowerCase()
|
|
|
|
Object.defineProperty @webviewNode, propertyName,
|
|
|
|
get: => @on[propertyName]
|
|
|
|
set: (value) =>
|
|
|
|
if @on[propertyName]
|
|
|
|
@webviewNode.removeEventListener eventName, @on[propertyName]
|
|
|
|
@on[propertyName] = value
|
|
|
|
if value
|
|
|
|
@webviewNode.addEventListener eventName, value
|
|
|
|
enumerable: true
|
|
|
|
|
|
|
|
# Updates state upon loadcommit.
|
2015-08-05 11:50:23 +00:00
|
|
|
onLoadCommit: (webViewEvent) ->
|
2014-12-09 00:14:12 +00:00
|
|
|
oldValue = @webviewNode.getAttribute webViewConstants.ATTRIBUTE_SRC
|
2015-08-05 11:50:23 +00:00
|
|
|
newValue = webViewEvent.url
|
|
|
|
if webViewEvent.isMainFrame and (oldValue != newValue)
|
2014-10-22 14:55:13 +00:00
|
|
|
# Touching the src attribute triggers a navigation. To avoid
|
|
|
|
# triggering a page reload on every guest-initiated navigation,
|
2014-12-09 05:06:23 +00:00
|
|
|
# we do not handle this mutation
|
|
|
|
@attributes[webViewConstants.ATTRIBUTE_SRC].setValueIgnoreMutation newValue
|
2014-10-22 14:55:13 +00:00
|
|
|
|
|
|
|
onAttach: (storagePartitionId) ->
|
2014-12-09 00:14:12 +00:00
|
|
|
@attributes[webViewConstants.ATTRIBUTE_PARTITION].setValue storagePartitionId
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2015-05-29 03:44:49 +00:00
|
|
|
buildParams: ->
|
2014-12-09 05:06:23 +00:00
|
|
|
params =
|
|
|
|
instanceId: @viewInstanceId
|
|
|
|
userAgentOverride: @userAgentOverride
|
2015-06-09 23:17:00 +00:00
|
|
|
for own attributeName, attribute of @attributes
|
2014-12-09 05:06:23 +00:00
|
|
|
params[attributeName] = attribute.getValue()
|
2015-05-29 03:44:49 +00:00
|
|
|
# When the WebView is not participating in layout (display:none)
|
|
|
|
# then getBoundingClientRect() would report a width and height of 0.
|
|
|
|
# However, in the case where the WebView has a fixed size we can
|
|
|
|
# use that value to initially size the guest so as to avoid a relayout of
|
|
|
|
# the on display:block.
|
|
|
|
css = window.getComputedStyle @webviewNode, null
|
|
|
|
elementRect = @webviewNode.getBoundingClientRect()
|
2015-05-29 05:47:09 +00:00
|
|
|
params.elementWidth = parseInt(elementRect.width) ||
|
|
|
|
parseInt(css.getPropertyValue('width'))
|
|
|
|
params.elementHeight = parseInt(elementRect.height) ||
|
|
|
|
parseInt(css.getPropertyValue('height'))
|
2014-12-09 05:06:23 +00:00
|
|
|
params
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2014-12-09 01:15:50 +00:00
|
|
|
attachWindow: (guestInstanceId) ->
|
2014-10-22 14:55:13 +00:00
|
|
|
@guestInstanceId = guestInstanceId
|
2015-09-30 08:58:37 +00:00
|
|
|
@webContents = remote.getGuestWebContents @guestInstanceId
|
2014-12-09 05:15:18 +00:00
|
|
|
return true unless @internalInstanceId
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2015-05-29 03:44:49 +00:00
|
|
|
guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, @buildParams()
|
2014-10-22 14:55:13 +00:00
|
|
|
|
|
|
|
# Registers browser plugin <object> custom element.
|
|
|
|
registerBrowserPluginElement = ->
|
|
|
|
proto = Object.create HTMLObjectElement.prototype
|
|
|
|
|
|
|
|
proto.createdCallback = ->
|
|
|
|
@setAttribute 'type', 'application/browser-plugin'
|
|
|
|
@setAttribute 'id', 'browser-plugin-' + getNextId()
|
|
|
|
# The <object> node fills in the <webview> container.
|
2015-02-21 22:34:57 +00:00
|
|
|
@style.display = 'block'
|
2014-10-22 14:55:13 +00:00
|
|
|
@style.width = '100%'
|
|
|
|
@style.height = '100%'
|
|
|
|
|
|
|
|
proto.attributeChangedCallback = (name, oldValue, newValue) ->
|
2014-10-22 15:37:27 +00:00
|
|
|
internal = v8Util.getHiddenValue this, 'internal'
|
2014-10-22 14:55:13 +00:00
|
|
|
return unless internal
|
|
|
|
internal.handleBrowserPluginAttributeMutation name, oldValue, newValue
|
|
|
|
|
|
|
|
proto.attachedCallback = ->
|
|
|
|
# Load the plugin immediately.
|
|
|
|
unused = this.nonExistentAttribute
|
|
|
|
|
2014-12-09 05:10:59 +00:00
|
|
|
WebViewImpl.BrowserPlugin = webFrame.registerEmbedderCustomElement 'browserplugin',
|
2014-10-22 14:55:13 +00:00
|
|
|
extends: 'object', prototype: proto
|
|
|
|
|
|
|
|
delete proto.createdCallback
|
|
|
|
delete proto.attachedCallback
|
|
|
|
delete proto.detachedCallback
|
|
|
|
delete proto.attributeChangedCallback
|
|
|
|
|
|
|
|
# Registers <webview> custom element.
|
|
|
|
registerWebViewElement = ->
|
|
|
|
proto = Object.create HTMLObjectElement.prototype
|
|
|
|
|
|
|
|
proto.createdCallback = ->
|
2014-12-09 05:10:59 +00:00
|
|
|
new WebViewImpl(this)
|
2014-10-22 14:55:13 +00:00
|
|
|
|
|
|
|
proto.attributeChangedCallback = (name, oldValue, newValue) ->
|
2014-10-22 15:37:27 +00:00
|
|
|
internal = v8Util.getHiddenValue this, 'internal'
|
2014-10-22 14:55:13 +00:00
|
|
|
return unless internal
|
|
|
|
internal.handleWebviewAttributeMutation name, oldValue, newValue
|
|
|
|
|
|
|
|
proto.detachedCallback = ->
|
2014-10-22 15:37:27 +00:00
|
|
|
internal = v8Util.getHiddenValue this, 'internal'
|
2014-10-22 14:55:13 +00:00
|
|
|
return unless internal
|
2015-03-23 15:07:53 +00:00
|
|
|
guestViewInternal.deregisterEvents internal.viewInstanceId
|
2014-10-22 14:55:13 +00:00
|
|
|
internal.elementAttached = false
|
|
|
|
internal.reset()
|
|
|
|
|
|
|
|
proto.attachedCallback = ->
|
2014-10-22 15:37:27 +00:00
|
|
|
internal = v8Util.getHiddenValue this, 'internal'
|
2014-10-22 14:55:13 +00:00
|
|
|
return unless internal
|
|
|
|
unless internal.elementAttached
|
2015-03-23 15:07:53 +00:00
|
|
|
guestViewInternal.registerEvents internal, internal.viewInstanceId
|
2014-10-22 14:55:13 +00:00
|
|
|
internal.elementAttached = true
|
2014-12-09 05:35:10 +00:00
|
|
|
internal.attributes[webViewConstants.ATTRIBUTE_SRC].parse()
|
2014-10-22 14:55:13 +00:00
|
|
|
|
2014-10-24 12:49:51 +00:00
|
|
|
# Public-facing API methods.
|
|
|
|
methods = [
|
2015-11-13 08:03:40 +00:00
|
|
|
'getURL'
|
|
|
|
'getTitle'
|
|
|
|
'isLoading'
|
|
|
|
'isWaitingForResponse'
|
|
|
|
'stop'
|
|
|
|
'reload'
|
|
|
|
'reloadIgnoringCache'
|
|
|
|
'canGoBack'
|
|
|
|
'canGoForward'
|
|
|
|
'canGoToOffset'
|
|
|
|
'clearHistory'
|
|
|
|
'goBack'
|
|
|
|
'goForward'
|
|
|
|
'goToIndex'
|
|
|
|
'goToOffset'
|
|
|
|
'isCrashed'
|
|
|
|
'setUserAgent'
|
|
|
|
'getUserAgent'
|
|
|
|
'executeJavaScript'
|
|
|
|
'insertCSS'
|
|
|
|
'openDevTools'
|
|
|
|
'closeDevTools'
|
|
|
|
'isDevToolsOpened'
|
|
|
|
'inspectElement'
|
|
|
|
'setAudioMuted'
|
|
|
|
'isAudioMuted'
|
|
|
|
'undo'
|
|
|
|
'redo'
|
|
|
|
'cut'
|
|
|
|
'copy'
|
|
|
|
'paste'
|
|
|
|
'pasteAndMatchStyle'
|
|
|
|
'delete'
|
|
|
|
'selectAll'
|
|
|
|
'unselect'
|
|
|
|
'replace'
|
|
|
|
'replaceMisspelling'
|
|
|
|
'send'
|
|
|
|
'getId'
|
|
|
|
'inspectServiceWorker'
|
|
|
|
'print'
|
|
|
|
'printToPDF'
|
|
|
|
'sendInputEvent'
|
2014-10-24 12:49:51 +00:00
|
|
|
]
|
|
|
|
|
2014-12-09 05:10:59 +00:00
|
|
|
# Forward proto.foo* method calls to WebViewImpl.foo*.
|
2014-10-24 12:49:51 +00:00
|
|
|
createHandler = (m) ->
|
|
|
|
(args...) ->
|
|
|
|
internal = v8Util.getHiddenValue this, 'internal'
|
2015-09-30 08:58:37 +00:00
|
|
|
internal.webContents[m] args...
|
2014-10-24 12:49:51 +00:00
|
|
|
proto[m] = createHandler m for m in methods
|
|
|
|
|
2015-11-13 08:03:40 +00:00
|
|
|
# Deprecated.
|
|
|
|
deprecate.rename proto, 'getUrl', 'getURL'
|
|
|
|
|
2014-10-24 10:44:15 +00:00
|
|
|
window.WebView = webFrame.registerEmbedderCustomElement 'webview',
|
2014-10-22 14:55:13 +00:00
|
|
|
prototype: proto
|
|
|
|
|
|
|
|
# Delete the callbacks so developers cannot call them and produce unexpected
|
|
|
|
# behavior.
|
|
|
|
delete proto.createdCallback
|
|
|
|
delete proto.attachedCallback
|
|
|
|
delete proto.detachedCallback
|
|
|
|
delete proto.attributeChangedCallback
|
|
|
|
|
|
|
|
useCapture = true
|
|
|
|
listener = (event) ->
|
|
|
|
return if document.readyState == 'loading'
|
|
|
|
registerBrowserPluginElement()
|
|
|
|
registerWebViewElement()
|
|
|
|
window.removeEventListener event.type, listener, useCapture
|
|
|
|
window.addEventListener 'readystatechange', listener, true
|
2014-12-09 00:14:12 +00:00
|
|
|
|
2014-12-09 05:10:59 +00:00
|
|
|
module.exports = WebViewImpl
|