Use util.inherits instead of CoffeeScript's extend function

This commit is contained in:
Kevin Sawicki 2016-01-15 09:57:36 -08:00
parent 2b95aeba3c
commit 34030d7b2b
3 changed files with 367 additions and 434 deletions

View file

@ -2,27 +2,24 @@ const app = require('electron').app;
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const url = require('url'); const url = require('url');
const squirrelUpdate = require('./squirrel-update-win'); const squirrelUpdate = require('./squirrel-update-win');
const util = require('util');
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, function AutoUpdater() {
hasProp = {}.hasOwnProperty; AutoUpdater.super_.call(this);
}
var AutoUpdater = (function(superClass) { util.inherits(AutoUpdater, EventEmitter);
extend(AutoUpdater, superClass);
function AutoUpdater() { AutoUpdater.prototype.quitAndInstall = function() {
return AutoUpdater.__super__.constructor.apply(this, arguments);
}
AutoUpdater.prototype.quitAndInstall = function() {
squirrelUpdate.processStart(); squirrelUpdate.processStart();
return app.quit(); return app.quit();
}; };
AutoUpdater.prototype.setFeedURL = function(updateURL) { AutoUpdater.prototype.setFeedURL = function(updateURL) {
return this.updateURL = updateURL; return this.updateURL = updateURL;
}; };
AutoUpdater.prototype.checkForUpdates = function() { AutoUpdater.prototype.checkForUpdates = function() {
if (!this.updateURL) { if (!this.updateURL) {
return this.emitError('Update URL is not set'); return this.emitError('Update URL is not set');
} }
@ -55,17 +52,12 @@ var AutoUpdater = (function(superClass) {
}); });
}; };
})(this)); })(this));
}; };
// Private: Emit both error object and message, this is to keep compatibility
// Private: Emit both error object and message, this is to keep compatibility // with Old APIs.
// with Old APIs. AutoUpdater.prototype.emitError = function(message) {
AutoUpdater.prototype.emitError = function(message) {
return this.emit('error', new Error(message), message); return this.emit('error', new Error(message), message);
}; };
return AutoUpdater;
})(EventEmitter);
module.exports = new AutoUpdater; module.exports = new AutoUpdater;

View file

@ -1,13 +1,10 @@
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const util = require('util');
const v8Util = process.atomBinding('v8_util'); const v8Util = process.atomBinding('v8_util');
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; function ObjectsRegistry() {
var hasProp = {}.hasOwnProperty; ObjectsRegistry.super_.call(this);
var ObjectsRegistry = (function(superClass) {
extend(ObjectsRegistry, superClass);
function ObjectsRegistry() {
this.setMaxListeners(Number.MAX_VALUE); this.setMaxListeners(Number.MAX_VALUE);
this.nextId = 0; this.nextId = 0;
@ -18,11 +15,13 @@ var ObjectsRegistry = (function(superClass) {
// Stores the IDs of objects referenced by WebContents. // Stores the IDs of objects referenced by WebContents.
// (webContentsId) => {(id) => (count)} // (webContentsId) => {(id) => (count)}
this.owners = {}; this.owners = {};
} }
// Register a new object, the object would be kept referenced until you release util.inherits(ObjectsRegistry, EventEmitter);
// it explicitly.
ObjectsRegistry.prototype.add = function(webContentsId, obj) { // Register a new object, the object would be kept referenced until you release
// it explicitly.
ObjectsRegistry.prototype.add = function(webContentsId, obj) {
var base, base1, id; var base, base1, id;
id = this.saveToStorage(obj); id = this.saveToStorage(obj);
@ -37,18 +36,18 @@ var ObjectsRegistry = (function(superClass) {
// Returns object's id // Returns object's id
return id; return id;
}; };
// Get an object according to its ID. // Get an object according to its ID.
ObjectsRegistry.prototype.get = function(id) { ObjectsRegistry.prototype.get = function(id) {
var ref; var ref;
return (ref = this.storage[id]) != null ? ref.object : void 0; return (ref = this.storage[id]) != null ? ref.object : void 0;
}; };
// Dereference an object according to its ID. // Dereference an object according to its ID.
ObjectsRegistry.prototype.remove = function(webContentsId, id) { ObjectsRegistry.prototype.remove = function(webContentsId, id) {
var pointer; var pointer;
this.dereference(id, 1); this.dereference(id, 1);
@ -61,10 +60,10 @@ var ObjectsRegistry = (function(superClass) {
if (pointer[id] === 0) { if (pointer[id] === 0) {
return delete pointer[id]; return delete pointer[id];
} }
}; };
// Clear all references to objects refrenced by the WebContents. // Clear all references to objects refrenced by the WebContents.
ObjectsRegistry.prototype.clear = function(webContentsId) { ObjectsRegistry.prototype.clear = function(webContentsId) {
var count, id, ref; var count, id, ref;
this.emit("clear-" + webContentsId); this.emit("clear-" + webContentsId);
if (this.owners[webContentsId] == null) { if (this.owners[webContentsId] == null) {
@ -76,10 +75,10 @@ var ObjectsRegistry = (function(superClass) {
this.dereference(id, count); this.dereference(id, count);
} }
return delete this.owners[webContentsId]; return delete this.owners[webContentsId];
}; };
// Private: Saves the object into storage and assigns an ID for it. // Private: Saves the object into storage and assigns an ID for it.
ObjectsRegistry.prototype.saveToStorage = function(object) { ObjectsRegistry.prototype.saveToStorage = function(object) {
var id; var id;
id = v8Util.getHiddenValue(object, 'atomId'); id = v8Util.getHiddenValue(object, 'atomId');
if (!id) { if (!id) {
@ -92,10 +91,10 @@ var ObjectsRegistry = (function(superClass) {
} }
++this.storage[id].count; ++this.storage[id].count;
return id; return id;
}; };
// Private: Dereference the object from store. // Private: Dereference the object from store.
ObjectsRegistry.prototype.dereference = function(id, count) { ObjectsRegistry.prototype.dereference = function(id, count) {
var pointer; var pointer;
pointer = this.storage[id]; pointer = this.storage[id];
if (pointer == null) { if (pointer == null) {
@ -106,10 +105,6 @@ var ObjectsRegistry = (function(superClass) {
v8Util.deleteHiddenValue(pointer.object, 'atomId'); v8Util.deleteHiddenValue(pointer.object, 'atomId');
return delete this.storage[id]; return delete this.storage[id];
} }
}; };
return ObjectsRegistry;
})(EventEmitter);
module.exports = new ObjectsRegistry; module.exports = new ObjectsRegistry;

View file

@ -2,10 +2,7 @@ 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;
var util = require('util');
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
var hasProp = {}.hasOwnProperty;
// 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');
@ -17,34 +14,33 @@ var resolveURL = function(url) {
// Attribute objects. // Attribute objects.
// Default implementation of a WebView attribute. // Default implementation of a WebView attribute.
var WebViewAttribute = (function() { function WebViewAttribute(name, webViewImpl) {
function WebViewAttribute(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() { WebViewAttribute.prototype.getValue = function() {
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) { WebViewAttribute.prototype.setValue = function(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) { WebViewAttribute.prototype.setValueIgnoreMutation = function(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() { WebViewAttribute.prototype.defineProperty = function() {
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() {
@ -58,71 +54,56 @@ var WebViewAttribute = (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() {}; WebViewAttribute.prototype.handleMutation = function() {};
return WebViewAttribute;
})();
// An attribute that is treated as a Boolean. // An attribute that is treated as a Boolean.
var BooleanAttribute = (function(superClass) { function BooleanAttribute(name, webViewImpl) {
extend(BooleanAttribute, superClass); BooleanAttribute.super_.call(this, name, webViewImpl)
}
function BooleanAttribute(name, webViewImpl) { util.inherits(BooleanAttribute, WebViewAttribute);
BooleanAttribute.__super__.constructor.call(this, name, webViewImpl);
}
BooleanAttribute.prototype.getValue = function() { BooleanAttribute.prototype.getValue = function() {
return this.webViewImpl.webviewNode.hasAttribute(this.name); return this.webViewImpl.webviewNode.hasAttribute(this.name);
}; };
BooleanAttribute.prototype.setValue = function(value) { BooleanAttribute.prototype.setValue = function(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, '');
} }
}; };
return BooleanAttribute;
})(WebViewAttribute);
// Attribute that specifies whether transparency is allowed in the webview. // Attribute that specifies whether transparency is allowed in the webview.
var AllowTransparencyAttribute = (function(superClass) { function AllowTransparencyAttribute(webViewImpl) {
extend(AllowTransparencyAttribute, superClass); AllowTransparencyAttribute.super_.call(this, webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
}
function AllowTransparencyAttribute(webViewImpl) { util.inherits(AllowTransparencyAttribute, BooleanAttribute);
AllowTransparencyAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
}
AllowTransparencyAttribute.prototype.handleMutation = function(oldValue, newValue) { AllowTransparencyAttribute.prototype.handleMutation = function(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());
}; };
return AllowTransparencyAttribute;
})(BooleanAttribute);
// Attribute used to define the demension limits of autosizing. // Attribute used to define the demension limits of autosizing.
var AutosizeDimensionAttribute = (function(superClass) { function AutosizeDimensionAttribute(name, webViewImpl) {
extend(AutosizeDimensionAttribute, superClass); AutosizeDimensionAttribute.super_.call(this, name, webViewImpl);
}
function AutosizeDimensionAttribute(name, webViewImpl) { util.inherits(AutosizeDimensionAttribute, WebViewAttribute);
AutosizeDimensionAttribute.__super__.constructor.call(this, name, webViewImpl);
}
AutosizeDimensionAttribute.prototype.getValue = function() { AutosizeDimensionAttribute.prototype.getValue = function() {
return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0; return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0;
}; };
AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) { AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) {
if (!this.webViewImpl.guestInstanceId) { if (!this.webViewImpl.guestInstanceId) {
return; return;
} }
@ -137,36 +118,26 @@ var AutosizeDimensionAttribute = (function(superClass) {
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0) height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0)
} }
}); });
}; };
return AutosizeDimensionAttribute;
})(WebViewAttribute);
// Attribute that specifies whether the webview should be autosized. // Attribute that specifies whether the webview should be autosized.
var AutosizeAttribute = (function(superClass) { function AutosizeAttribute(webViewImpl) {
extend(AutosizeAttribute, superClass); AutosizeAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
}
function AutosizeAttribute(webViewImpl) { util.inherits(AutosizeAttribute, BooleanAttribute);
AutosizeAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
}
AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation; AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation;
return AutosizeAttribute;
})(BooleanAttribute);
// Attribute representing the state of the storage partition. // Attribute representing the state of the storage partition.
var PartitionAttribute = (function(superClass) { function PartitionAttribute(webViewImpl) {
extend(PartitionAttribute, superClass); PartitionAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
function PartitionAttribute(webViewImpl) {
PartitionAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
this.validPartitionId = true; this.validPartitionId = true;
} }
PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) { util.inherits(PartitionAttribute, WebViewAttribute);
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.
@ -179,30 +150,25 @@ var PartitionAttribute = (function(superClass) {
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);
} }
}; };
return PartitionAttribute;
})(WebViewAttribute);
// Attribute that handles the location and navigation of the webview. // Attribute that handles the location and navigation of the webview.
var SrcAttribute = (function(superClass) { function SrcAttribute(webViewImpl) {
extend(SrcAttribute, superClass); SrcAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
function SrcAttribute(webViewImpl) {
SrcAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
this.setupMutationObserver(); this.setupMutationObserver();
} }
SrcAttribute.prototype.getValue = function() { util.inherits(SrcAttribute, WebViewAttribute);
SrcAttribute.prototype.getValue = function() {
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) { SrcAttribute.prototype.setValueIgnoreMutation = function(value) {
WebViewAttribute.prototype.setValueIgnoreMutation.call(this, value); WebViewAttribute.prototype.setValueIgnoreMutation.call(this, 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
@ -210,9 +176,9 @@ var SrcAttribute = (function(superClass) {
// 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) { SrcAttribute.prototype.handleMutation = function(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
@ -226,14 +192,13 @@ var SrcAttribute = (function(superClass) {
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() {
SrcAttribute.prototype.setupMutationObserver = function() {
var params; var params;
this.observer = new MutationObserver((function(_this) { this.observer = new MutationObserver((function(_this) {
return function(mutations) { return function(mutations) {
@ -255,9 +220,9 @@ var SrcAttribute = (function(superClass) {
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() { SrcAttribute.prototype.parse = function() {
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;
@ -282,45 +247,30 @@ var SrcAttribute = (function(superClass) {
} }
guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId); guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId);
return guestContents.loadURL(this.getValue(), opts); return guestContents.loadURL(this.getValue(), opts);
}; };
return SrcAttribute;
})(WebViewAttribute);
// Attribute specifies HTTP referrer. // Attribute specifies HTTP referrer.
var HttpReferrerAttribute = (function(superClass) { function HttpReferrerAttribute(webViewImpl) {
extend(HttpReferrerAttribute, superClass); HttpReferrerAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl);
}
function HttpReferrerAttribute(webViewImpl) { util.inherits(HttpReferrerAttribute, WebViewAttribute);
HttpReferrerAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl);
}
return HttpReferrerAttribute;
})(WebViewAttribute);
// Attribute specifies user agent // Attribute specifies user agent
var UserAgentAttribute = (function(superClass) { function UserAgentAttribute(webViewImpl) {
extend(UserAgentAttribute, superClass); UserAgentAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl);
}
function UserAgentAttribute(webViewImpl) { util.inherits(UserAgentAttribute, WebViewAttribute);
UserAgentAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl);
}
return UserAgentAttribute;
})(WebViewAttribute);
// Attribute that set preload script. // Attribute that set preload script.
var PreloadAttribute = (function(superClass) { function PreloadAttribute(webViewImpl) {
extend(PreloadAttribute, superClass); PreloadAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl);
}
function PreloadAttribute(webViewImpl) { util.inherits(PreloadAttribute, WebViewAttribute);
PreloadAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl);
}
PreloadAttribute.prototype.getValue = function() { 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;
@ -332,11 +282,7 @@ var PreloadAttribute = (function(superClass) {
preload = ''; preload = '';
} }
return preload; return preload;
}; };
return PreloadAttribute;
})(WebViewAttribute);
// Sets up all of the webview attributes. // Sets up all of the webview attributes.
WebViewImpl.prototype.setupWebViewAttributes = function() { WebViewImpl.prototype.setupWebViewAttributes = function() {