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,17 +2,14 @@ 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; },
hasProp = {}.hasOwnProperty;
var AutoUpdater = (function(superClass) {
extend(AutoUpdater, superClass);
function AutoUpdater() { function AutoUpdater() {
return AutoUpdater.__super__.constructor.apply(this, arguments); AutoUpdater.super_.call(this);
} }
util.inherits(AutoUpdater, EventEmitter);
AutoUpdater.prototype.quitAndInstall = function() { AutoUpdater.prototype.quitAndInstall = function() {
squirrelUpdate.processStart(); squirrelUpdate.processStart();
return app.quit(); return app.quit();
@ -57,15 +54,10 @@ 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; };
var hasProp = {}.hasOwnProperty;
var ObjectsRegistry = (function(superClass) {
extend(ObjectsRegistry, superClass);
function ObjectsRegistry() { function ObjectsRegistry() {
ObjectsRegistry.super_.call(this);
this.setMaxListeners(Number.MAX_VALUE); this.setMaxListeners(Number.MAX_VALUE);
this.nextId = 0; this.nextId = 0;
@ -20,6 +17,8 @@ var ObjectsRegistry = (function(superClass) {
this.owners = {}; this.owners = {};
} }
util.inherits(ObjectsRegistry, EventEmitter);
// Register a new object, the object would be kept referenced until you release // Register a new object, the object would be kept referenced until you release
// it explicitly. // it explicitly.
ObjectsRegistry.prototype.add = function(webContentsId, obj) { ObjectsRegistry.prototype.add = function(webContentsId, obj) {
@ -108,8 +107,4 @@ var ObjectsRegistry = (function(superClass) {
} }
}; };
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,7 +14,6 @@ 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] || '';
@ -63,18 +59,13 @@ var WebViewAttribute = (function() {
// 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) {
extend(BooleanAttribute, superClass);
function BooleanAttribute(name, webViewImpl) { function BooleanAttribute(name, webViewImpl) {
BooleanAttribute.__super__.constructor.call(this, name, webViewImpl); BooleanAttribute.super_.call(this, name, webViewImpl)
} }
util.inherits(BooleanAttribute, WebViewAttribute);
BooleanAttribute.prototype.getValue = function() { BooleanAttribute.prototype.getValue = function() {
return this.webViewImpl.webviewNode.hasAttribute(this.name); return this.webViewImpl.webviewNode.hasAttribute(this.name);
}; };
@ -87,18 +78,13 @@ var BooleanAttribute = (function(superClass) {
} }
}; };
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) {
extend(AllowTransparencyAttribute, superClass);
function AllowTransparencyAttribute(webViewImpl) { function AllowTransparencyAttribute(webViewImpl) {
AllowTransparencyAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl); AllowTransparencyAttribute.super_.call(this, webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
} }
util.inherits(AllowTransparencyAttribute, BooleanAttribute);
AllowTransparencyAttribute.prototype.handleMutation = function(oldValue, newValue) { AllowTransparencyAttribute.prototype.handleMutation = function(oldValue, newValue) {
if (!this.webViewImpl.guestInstanceId) { if (!this.webViewImpl.guestInstanceId) {
return; return;
@ -106,18 +92,13 @@ var AllowTransparencyAttribute = (function(superClass) {
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) {
extend(AutosizeDimensionAttribute, superClass);
function AutosizeDimensionAttribute(name, webViewImpl) { function AutosizeDimensionAttribute(name, webViewImpl) {
AutosizeDimensionAttribute.__super__.constructor.call(this, name, webViewImpl); AutosizeDimensionAttribute.super_.call(this, name, webViewImpl);
} }
util.inherits(AutosizeDimensionAttribute, WebViewAttribute);
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;
}; };
@ -139,33 +120,23 @@ var AutosizeDimensionAttribute = (function(superClass) {
}); });
}; };
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) {
extend(AutosizeAttribute, superClass);
function AutosizeAttribute(webViewImpl) { function AutosizeAttribute(webViewImpl) {
AutosizeAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl); AutosizeAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
} }
util.inherits(AutosizeAttribute, BooleanAttribute);
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) {
extend(PartitionAttribute, superClass);
function PartitionAttribute(webViewImpl) { function PartitionAttribute(webViewImpl) {
PartitionAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl); PartitionAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
this.validPartitionId = true; this.validPartitionId = true;
} }
util.inherits(PartitionAttribute, WebViewAttribute);
PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) { PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
newValue = newValue || ''; newValue = newValue || '';
@ -181,19 +152,14 @@ var PartitionAttribute = (function(superClass) {
} }
}; };
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) {
extend(SrcAttribute, superClass);
function SrcAttribute(webViewImpl) { function SrcAttribute(webViewImpl) {
SrcAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl); SrcAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
this.setupMutationObserver(); this.setupMutationObserver();
} }
util.inherits(SrcAttribute, WebViewAttribute);
SrcAttribute.prototype.getValue = function() { 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));
@ -228,7 +194,6 @@ var SrcAttribute = (function(superClass) {
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
@ -284,42 +249,27 @@ var SrcAttribute = (function(superClass) {
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) {
extend(HttpReferrerAttribute, superClass);
function HttpReferrerAttribute(webViewImpl) { function HttpReferrerAttribute(webViewImpl) {
HttpReferrerAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl); HttpReferrerAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl);
} }
return HttpReferrerAttribute; util.inherits(HttpReferrerAttribute, WebViewAttribute);
})(WebViewAttribute);
// Attribute specifies user agent // Attribute specifies user agent
var UserAgentAttribute = (function(superClass) {
extend(UserAgentAttribute, superClass);
function UserAgentAttribute(webViewImpl) { function UserAgentAttribute(webViewImpl) {
UserAgentAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl); UserAgentAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl);
} }
return UserAgentAttribute; util.inherits(UserAgentAttribute, WebViewAttribute);
})(WebViewAttribute);
// Attribute that set preload script. // Attribute that set preload script.
var PreloadAttribute = (function(superClass) {
extend(PreloadAttribute, superClass);
function PreloadAttribute(webViewImpl) { function PreloadAttribute(webViewImpl) {
PreloadAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl); PreloadAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl);
} }
util.inherits(PreloadAttribute, WebViewAttribute);
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)) {
@ -334,10 +284,6 @@ var PreloadAttribute = (function(superClass) {
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() {
var attribute, autosizeAttributes, i, len, results; var attribute, autosizeAttributes, i, len, results;