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 url = require('url');
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;
function AutoUpdater() {
AutoUpdater.super_.call(this);
}
var AutoUpdater = (function(superClass) {
extend(AutoUpdater, superClass);
util.inherits(AutoUpdater, EventEmitter);
function AutoUpdater() {
return AutoUpdater.__super__.constructor.apply(this, arguments);
}
AutoUpdater.prototype.quitAndInstall = function() {
AutoUpdater.prototype.quitAndInstall = function() {
squirrelUpdate.processStart();
return app.quit();
};
};
AutoUpdater.prototype.setFeedURL = function(updateURL) {
AutoUpdater.prototype.setFeedURL = function(updateURL) {
return this.updateURL = updateURL;
};
};
AutoUpdater.prototype.checkForUpdates = function() {
AutoUpdater.prototype.checkForUpdates = function() {
if (!this.updateURL) {
return this.emitError('Update URL is not set');
}
@ -55,17 +52,12 @@ var AutoUpdater = (function(superClass) {
});
};
})(this));
};
};
// Private: Emit both error object and message, this is to keep compatibility
// with Old APIs.
AutoUpdater.prototype.emitError = function(message) {
// Private: Emit both error object and message, this is to keep compatibility
// with Old APIs.
AutoUpdater.prototype.emitError = function(message) {
return this.emit('error', new Error(message), message);
};
return AutoUpdater;
})(EventEmitter);
};
module.exports = new AutoUpdater;

View file

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

View file

@ -2,10 +2,7 @@ const WebViewImpl = require('./web-view');
const guestViewInternal = require('./guest-view-internal');
const webViewConstants = require('./web-view-constants');
const remote = require('electron').remote;
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 util = require('util');
// Helper function to resolve url set in attribute.
var a = document.createElement('a');
@ -17,34 +14,33 @@ var resolveURL = function(url) {
// Attribute objects.
// Default implementation of a WebView attribute.
var WebViewAttribute = (function() {
function WebViewAttribute(name, webViewImpl) {
function WebViewAttribute(name, webViewImpl) {
this.name = name;
this.value = webViewImpl.webviewNode[name] || '';
this.webViewImpl = webViewImpl;
this.ignoreMutation = false;
this.defineProperty();
}
}
// Retrieves and returns the attribute's value.
WebViewAttribute.prototype.getValue = function() {
// Retrieves and returns the attribute's value.
WebViewAttribute.prototype.getValue = function() {
return this.webViewImpl.webviewNode.getAttribute(this.name) || this.value;
};
};
// Sets the attribute's value.
WebViewAttribute.prototype.setValue = function(value) {
// Sets the attribute's value.
WebViewAttribute.prototype.setValue = function(value) {
return this.webViewImpl.webviewNode.setAttribute(this.name, value || '');
};
};
// Changes the attribute's value without triggering its mutation handler.
WebViewAttribute.prototype.setValueIgnoreMutation = function(value) {
// Changes the attribute's value without triggering its mutation handler.
WebViewAttribute.prototype.setValueIgnoreMutation = function(value) {
this.ignoreMutation = true;
this.setValue(value);
return this.ignoreMutation = false;
};
};
// Defines this attribute as a property on the webview node.
WebViewAttribute.prototype.defineProperty = function() {
// Defines this attribute as a property on the webview node.
WebViewAttribute.prototype.defineProperty = function() {
return Object.defineProperty(this.webViewImpl.webviewNode, this.name, {
get: (function(_this) {
return function() {
@ -58,71 +54,56 @@ var WebViewAttribute = (function() {
})(this),
enumerable: true
});
};
};
// Called when the attribute's value changes.
WebViewAttribute.prototype.handleMutation = function() {};
return WebViewAttribute;
})();
// Called when the attribute's value changes.
WebViewAttribute.prototype.handleMutation = function() {};
// An attribute that is treated as a Boolean.
var BooleanAttribute = (function(superClass) {
extend(BooleanAttribute, superClass);
function BooleanAttribute(name, webViewImpl) {
BooleanAttribute.super_.call(this, name, webViewImpl)
}
function BooleanAttribute(name, webViewImpl) {
BooleanAttribute.__super__.constructor.call(this, name, webViewImpl);
}
util.inherits(BooleanAttribute, WebViewAttribute);
BooleanAttribute.prototype.getValue = function() {
BooleanAttribute.prototype.getValue = function() {
return this.webViewImpl.webviewNode.hasAttribute(this.name);
};
};
BooleanAttribute.prototype.setValue = function(value) {
BooleanAttribute.prototype.setValue = function(value) {
if (!value) {
return this.webViewImpl.webviewNode.removeAttribute(this.name);
} else {
return this.webViewImpl.webviewNode.setAttribute(this.name, '');
}
};
return BooleanAttribute;
})(WebViewAttribute);
};
// Attribute that specifies whether transparency is allowed in the webview.
var AllowTransparencyAttribute = (function(superClass) {
extend(AllowTransparencyAttribute, superClass);
function AllowTransparencyAttribute(webViewImpl) {
AllowTransparencyAttribute.super_.call(this, webViewConstants.ATTRIBUTE_ALLOWTRANSPARENCY, webViewImpl);
}
function AllowTransparencyAttribute(webViewImpl) {
AllowTransparencyAttribute.__super__.constructor.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) {
return;
}
return guestViewInternal.setAllowTransparency(this.webViewImpl.guestInstanceId, this.getValue());
};
return AllowTransparencyAttribute;
})(BooleanAttribute);
};
// Attribute used to define the demension limits of autosizing.
var AutosizeDimensionAttribute = (function(superClass) {
extend(AutosizeDimensionAttribute, superClass);
function AutosizeDimensionAttribute(name, webViewImpl) {
AutosizeDimensionAttribute.super_.call(this, name, webViewImpl);
}
function AutosizeDimensionAttribute(name, webViewImpl) {
AutosizeDimensionAttribute.__super__.constructor.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;
};
};
AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) {
AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) {
if (!this.webViewImpl.guestInstanceId) {
return;
}
@ -137,36 +118,26 @@ var AutosizeDimensionAttribute = (function(superClass) {
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0)
}
});
};
return AutosizeDimensionAttribute;
})(WebViewAttribute);
};
// Attribute that specifies whether the webview should be autosized.
var AutosizeAttribute = (function(superClass) {
extend(AutosizeAttribute, superClass);
function AutosizeAttribute(webViewImpl) {
AutosizeAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
}
function AutosizeAttribute(webViewImpl) {
AutosizeAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_AUTOSIZE, webViewImpl);
}
util.inherits(AutosizeAttribute, BooleanAttribute);
AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation;
return AutosizeAttribute;
})(BooleanAttribute);
AutosizeAttribute.prototype.handleMutation = AutosizeDimensionAttribute.prototype.handleMutation;
// Attribute representing the state of the storage partition.
var PartitionAttribute = (function(superClass) {
extend(PartitionAttribute, superClass);
function PartitionAttribute(webViewImpl) {
PartitionAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
function PartitionAttribute(webViewImpl) {
PartitionAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
this.validPartitionId = true;
}
}
PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
util.inherits(PartitionAttribute, WebViewAttribute);
PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
newValue = newValue || '';
// The partition cannot change if the webview has already navigated.
@ -179,30 +150,25 @@ var PartitionAttribute = (function(superClass) {
this.validPartitionId = false;
return window.console.error(webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE);
}
};
return PartitionAttribute;
})(WebViewAttribute);
};
// Attribute that handles the location and navigation of the webview.
var SrcAttribute = (function(superClass) {
extend(SrcAttribute, superClass);
function SrcAttribute(webViewImpl) {
SrcAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
function SrcAttribute(webViewImpl) {
SrcAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
this.setupMutationObserver();
}
}
SrcAttribute.prototype.getValue = function() {
util.inherits(SrcAttribute, WebViewAttribute);
SrcAttribute.prototype.getValue = function() {
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name));
} else {
return this.value;
}
};
};
SrcAttribute.prototype.setValueIgnoreMutation = function(value) {
SrcAttribute.prototype.setValueIgnoreMutation = function(value) {
WebViewAttribute.prototype.setValueIgnoreMutation.call(this, value);
// 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
// want to handle this mutation.
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 <webview> enters a navigated state, it cannot return to a
@ -226,14 +192,13 @@ var SrcAttribute = (function(superClass) {
return;
}
return this.parse();
};
};
// 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
// where the webview guest has crashed and navigating to the same address
// spawns off a new process.
SrcAttribute.prototype.setupMutationObserver = function() {
// 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
// where the webview guest has crashed and navigating to the same address
// spawns off a new process.
SrcAttribute.prototype.setupMutationObserver = function() {
var params;
this.observer = new MutationObserver((function(_this) {
return function(mutations) {
@ -255,9 +220,9 @@ var SrcAttribute = (function(superClass) {
attributeFilter: [this.name]
};
return this.observer.observe(this.webViewImpl.webviewNode, params);
};
};
SrcAttribute.prototype.parse = function() {
SrcAttribute.prototype.parse = function() {
var guestContents, httpreferrer, opts, useragent;
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) {
return;
@ -282,45 +247,30 @@ var SrcAttribute = (function(superClass) {
}
guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId);
return guestContents.loadURL(this.getValue(), opts);
};
return SrcAttribute;
})(WebViewAttribute);
};
// Attribute specifies HTTP referrer.
var HttpReferrerAttribute = (function(superClass) {
extend(HttpReferrerAttribute, superClass);
function HttpReferrerAttribute(webViewImpl) {
HttpReferrerAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl);
}
function HttpReferrerAttribute(webViewImpl) {
HttpReferrerAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_HTTPREFERRER, webViewImpl);
}
return HttpReferrerAttribute;
})(WebViewAttribute);
util.inherits(HttpReferrerAttribute, WebViewAttribute);
// Attribute specifies user agent
var UserAgentAttribute = (function(superClass) {
extend(UserAgentAttribute, superClass);
function UserAgentAttribute(webViewImpl) {
UserAgentAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl);
}
function UserAgentAttribute(webViewImpl) {
UserAgentAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_USERAGENT, webViewImpl);
}
return UserAgentAttribute;
})(WebViewAttribute);
util.inherits(UserAgentAttribute, WebViewAttribute);
// Attribute that set preload script.
var PreloadAttribute = (function(superClass) {
extend(PreloadAttribute, superClass);
function PreloadAttribute(webViewImpl) {
PreloadAttribute.super_.constructor.call(this, webViewConstants.ATTRIBUTE_PRELOAD, webViewImpl);
}
function PreloadAttribute(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;
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return this.value;
@ -332,11 +282,7 @@ var PreloadAttribute = (function(superClass) {
preload = '';
}
return preload;
};
return PreloadAttribute;
})(WebViewAttribute);
};
// Sets up all of the webview attributes.
WebViewImpl.prototype.setupWebViewAttributes = function() {