Use ES6 style class

This commit is contained in:
Kevin Sawicki 2016-01-15 14:28:12 -08:00
parent aab1568682
commit aab2821122

View file

@ -1,8 +1,9 @@
'use strict';
const WebViewImpl = require('./web-view'); const WebViewImpl = require('./web-view');
const guestViewInternal = require('./guest-view-internal'); const guestViewInternal = require('./guest-view-internal');
const webViewConstants = require('./web-view-constants'); const webViewConstants = require('./web-view-constants');
const remote = require('electron').remote; const remote = require('electron').remote;
const util = require('util');
// Helper function to resolve url set in attribute. // Helper function to resolve url set in attribute.
var a = document.createElement('a'); var a = document.createElement('a');
@ -14,33 +15,34 @@ var resolveURL = function(url) {
// Attribute objects. // Attribute objects.
// Default implementation of a WebView attribute. // Default implementation of a WebView attribute.
function WebViewAttribute(name, webViewImpl) { class WebViewAttribute {
constructor(name, webViewImpl) {
this.name = name; this.name = name;
this.value = webViewImpl.webviewNode[name] || ''; this.value = webViewImpl.webviewNode[name] || '';
this.webViewImpl = webViewImpl; this.webViewImpl = webViewImpl;
this.ignoreMutation = false; this.ignoreMutation = false;
this.defineProperty(); this.defineProperty();
} }
// Retrieves and returns the attribute's value. // Retrieves and returns the attribute's value.
WebViewAttribute.prototype.getValue = function() { getValue() {
return this.webViewImpl.webviewNode.getAttribute(this.name) || this.value; return this.webViewImpl.webviewNode.getAttribute(this.name) || this.value;
}; }
// Sets the attribute's value. // Sets the attribute's value.
WebViewAttribute.prototype.setValue = function(value) { setValue(value) {
return this.webViewImpl.webviewNode.setAttribute(this.name, value || ''); return this.webViewImpl.webviewNode.setAttribute(this.name, value || '');
}; }
// Changes the attribute's value without triggering its mutation handler. // Changes the attribute's value without triggering its mutation handler.
WebViewAttribute.prototype.setValueIgnoreMutation = function(value) { setValueIgnoreMutation(value) {
this.ignoreMutation = true; this.ignoreMutation = true;
this.setValue(value); this.setValue(value);
return this.ignoreMutation = false; return this.ignoreMutation = false;
}; }
// Defines this attribute as a property on the webview node. // Defines this attribute as a property on the webview node.
WebViewAttribute.prototype.defineProperty = function() { defineProperty() {
return Object.defineProperty(this.webViewImpl.webviewNode, this.name, { return Object.defineProperty(this.webViewImpl.webviewNode, this.name, {
get: (function(_this) { get: (function(_this) {
return function() { return function() {
@ -54,56 +56,56 @@ WebViewAttribute.prototype.defineProperty = function() {
})(this), })(this),
enumerable: true enumerable: true
}); });
}; };
// Called when the attribute's value changes. // Called when the attribute's value changes.
WebViewAttribute.prototype.handleMutation = function() {}; handleMutation() {}
// An attribute that is treated as a Boolean.
function BooleanAttribute(name, webViewImpl) {
BooleanAttribute.super_.call(this, name, webViewImpl)
} }
util.inherits(BooleanAttribute, WebViewAttribute); // An attribute that is treated as a Boolean.
class BooleanAttribute extends WebViewAttribute {
constructor(name, webViewImpl) {
super(name, webViewImpl);
}
BooleanAttribute.prototype.getValue = function() { getValue() {
return this.webViewImpl.webviewNode.hasAttribute(this.name); return this.webViewImpl.webviewNode.hasAttribute(this.name);
}; }
BooleanAttribute.prototype.setValue = function(value) { setValue(value) {
if (!value) { if (!value) {
return this.webViewImpl.webviewNode.removeAttribute(this.name); return this.webViewImpl.webviewNode.removeAttribute(this.name);
} else { } else {
return this.webViewImpl.webviewNode.setAttribute(this.name, ''); return this.webViewImpl.webviewNode.setAttribute(this.name, '');
} }
}; }
// Attribute that specifies whether transparency is allowed in the webview.
function AllowTransparencyAttribute(webViewImpl) {
AllowTransparencyAttribute.super_.call(this, webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
} }
util.inherits(AllowTransparencyAttribute, BooleanAttribute); // Attribute that specifies whether transparency is allowed in the webview.
class AllowTransparencyAttribute extends BooleanAttribute {
constructor(webViewImpl) {
super(webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
}
AllowTransparencyAttribute.prototype.handleMutation = function(oldValue, newValue) { handleMutation(oldValue, newValue) {
if (!this.webViewImpl.guestInstanceId) { if (!this.webViewImpl.guestInstanceId) {
return; return;
} }
return guestViewInternal.setAllowTransparency(this.webViewImpl.guestInstanceId, this.getValue()); return guestViewInternal.setAllowTransparency(this.webViewImpl.guestInstanceId, this.getValue());
}; }
// Attribute used to define the demension limits of autosizing.
function AutosizeDimensionAttribute(name, webViewImpl) {
AutosizeDimensionAttribute.super_.call(this, name, webViewImpl);
} }
util.inherits(AutosizeDimensionAttribute, WebViewAttribute); // Attribute used to define the demension limits of autosizing.
class AutosizeDimensionAttribute extends WebViewAttribute {
constructor(name, webViewImpl) {
super(name, webViewImpl);
}
AutosizeDimensionAttribute.prototype.getValue = function() { getValue() {
return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0; return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0;
}; }
AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) { handleMutation(oldValue, newValue) {
if (!this.webViewImpl.guestInstanceId) { if (!this.webViewImpl.guestInstanceId) {
return; return;
} }
@ -118,26 +120,27 @@ AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValu
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0) height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0)
} }
}); });
}; }
// Attribute that specifies whether the webview should be autosized.
function AutosizeAttribute(webViewImpl) {
AutosizeAttribute.super_.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
} }
util.inherits(AutosizeAttribute, BooleanAttribute);
// Attribute that specifies whether the webview should be autosized.
class AutosizeAttribute extends BooleanAttribute {
constructor(webViewImpl) {
super(webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
}
}
AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation; AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation;
// Attribute representing the state of the storage partition. // Attribute representing the state of the storage partition.
function PartitionAttribute(webViewImpl) { class PartitionAttribute extends WebViewAttribute {
PartitionAttribute.super_.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl); constructor(webViewImpl) {
super(webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
this.validPartitionId = true; this.validPartitionId = true;
} }
util.inherits(PartitionAttribute, WebViewAttribute); handleMutation(oldValue, newValue) {
PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
newValue = newValue || ''; newValue = newValue || '';
// The partition cannot change if the webview has already navigated. // The partition cannot change if the webview has already navigated.
@ -150,35 +153,35 @@ PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
this.validPartitionId = false; this.validPartitionId = false;
return window.console.error(webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE); return window.console.error(webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE);
} }
}; }
// Attribute that handles the location and navigation of the webview.
function SrcAttribute(webViewImpl) {
SrcAttribute.super_.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
this.setupMutationObserver();
} }
util.inherits(SrcAttribute, WebViewAttribute); // Attribute that handles the location and navigation of the webview.
class SrcAttribute extends WebViewAttribute {
constructor(webViewImpl) {
super(webViewConstants.ATTRIBUTE_SRC, webViewImpl);
this.setupMutationObserver();
}
SrcAttribute.prototype.getValue = function() { getValue() {
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) { if (this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name)); return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name));
} else { } else {
return this.value; return this.value;
} }
}; }
SrcAttribute.prototype.setValueIgnoreMutation = function(value) { setValueIgnoreMutation(value) {
WebViewAttribute.prototype.setValueIgnoreMutation.call(this, value); super.setValueIgnoreMutation(value);
// takeRecords() is needed to clear queued up src mutations. Without it, it // takeRecords() is needed to clear queued up src mutations. Without it, it
// is possible for this change to get picked up asyncronously by src's // is possible for this change to get picked up asyncronously by src's
// mutation observer |observer|, and then get handled even though we do not // mutation observer |observer|, and then get handled even though we do not
// want to handle this mutation. // want to handle this mutation.
return this.observer.takeRecords(); return this.observer.takeRecords();
}; }
SrcAttribute.prototype.handleMutation = function(oldValue, newValue) { handleMutation(oldValue, newValue) {
// Once we have navigated, we don't allow clearing the src attribute. // Once we have navigated, we don't allow clearing the src attribute.
// Once <webview> enters a navigated state, it cannot return to a // Once <webview> enters a navigated state, it cannot return to a
@ -192,13 +195,13 @@ SrcAttribute.prototype.handleMutation = function(oldValue, newValue) {
return; return;
} }
return this.parse(); return this.parse();
}; }
// The purpose of this mutation observer is to catch assignment to the src // The purpose of this mutation observer is to catch assignment to the src
// attribute without any changes to its value. This is useful in the case // attribute without any changes to its value. This is useful in the case
// where the webview guest has crashed and navigating to the same address // where the webview guest has crashed and navigating to the same address
// spawns off a new process. // spawns off a new process.
SrcAttribute.prototype.setupMutationObserver = function() { setupMutationObserver() {
var params; var params;
this.observer = new MutationObserver((function(_this) { this.observer = new MutationObserver((function(_this) {
return function(mutations) { return function(mutations) {
@ -220,9 +223,9 @@ SrcAttribute.prototype.setupMutationObserver = function() {
attributeFilter: [this.name] attributeFilter: [this.name]
}; };
return this.observer.observe(this.webViewImpl.webviewNode, params); return this.observer.observe(this.webViewImpl.webviewNode, params);
}; }
SrcAttribute.prototype.parse = function() { parse() {
var guestContents, httpreferrer, opts, useragent; var guestContents, httpreferrer, opts, useragent;
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) { if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) {
return; return;
@ -247,30 +250,30 @@ SrcAttribute.prototype.parse = function() {
} }
guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId); guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId);
return guestContents.loadURL(this.getValue(), opts); return guestContents.loadURL(this.getValue(), opts);
}; }
}
// Attribute specifies HTTP referrer. // Attribute specifies HTTP referrer.
function HttpReferrerAttribute(webViewImpl) { class HttpReferrerAttribute extends WebViewAttribute {
HttpReferrerAttribute.super_.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl); constructor(webViewImpl) {
super(webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl);
}
} }
util.inherits(HttpReferrerAttribute, WebViewAttribute);
// Attribute specifies user agent // Attribute specifies user agent
function UserAgentAttribute(webViewImpl) { class UserAgentAttribute extends WebViewAttribute {
UserAgentAttribute.super_.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl); constructor(webViewImpl) {
super(webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl);
}
} }
util.inherits(UserAgentAttribute, WebViewAttribute);
// Attribute that set preload script. // Attribute that set preload script.
function PreloadAttribute(webViewImpl) { class PreloadAttribute extends WebViewAttribute
PreloadAttribute.super_.call(this, webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl); constructor(webViewImpl) {
} super(webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl);
}
util.inherits(PreloadAttribute, WebViewAttribute); getValue() {
PreloadAttribute.prototype.getValue = function() {
var preload, protocol; var preload, protocol;
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) { if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return this.value; return this.value;
@ -282,7 +285,8 @@ PreloadAttribute.prototype.getValue = function() {
preload = ''; preload = '';
} }
return preload; return preload;
}; }
}
// Sets up all of the webview attributes. // Sets up all of the webview attributes.
WebViewImpl.prototype.setupWebViewAttributes = function() { WebViewImpl.prototype.setupWebViewAttributes = function() {