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,70 +2,62 @@ 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); squirrelUpdate.processStart();
return app.quit();
};
AutoUpdater.prototype.setFeedURL = function(updateURL) {
return this.updateURL = updateURL;
};
AutoUpdater.prototype.checkForUpdates = function() {
if (!this.updateURL) {
return this.emitError('Update URL is not set');
} }
if (!squirrelUpdate.supported()) {
AutoUpdater.prototype.quitAndInstall = function() { return this.emitError('Can not find Squirrel');
squirrelUpdate.processStart(); }
return app.quit(); this.emit('checking-for-update');
}; return squirrelUpdate.download(this.updateURL, (function(_this) {
return function(error, update) {
AutoUpdater.prototype.setFeedURL = function(updateURL) { if (error != null) {
return this.updateURL = updateURL; return _this.emitError(error);
}; }
if (update == null) {
AutoUpdater.prototype.checkForUpdates = function() { return _this.emit('update-not-available');
if (!this.updateURL) { }
return this.emitError('Update URL is not set'); _this.emit('update-available');
} return squirrelUpdate.update(_this.updateURL, function(error) {
if (!squirrelUpdate.supported()) { var date, releaseNotes, version;
return this.emitError('Can not find Squirrel');
}
this.emit('checking-for-update');
return squirrelUpdate.download(this.updateURL, (function(_this) {
return function(error, update) {
if (error != null) { if (error != null) {
return _this.emitError(error); return _this.emitError(error);
} }
if (update == null) { releaseNotes = update.releaseNotes, version = update.version;
return _this.emit('update-not-available');
}
_this.emit('update-available');
return squirrelUpdate.update(_this.updateURL, function(error) {
var date, releaseNotes, version;
if (error != null) {
return _this.emitError(error);
}
releaseNotes = update.releaseNotes, version = update.version;
// Following information is not available on Windows, so fake them. // Following information is not available on Windows, so fake them.
date = new Date; date = new Date;
url = _this.updateURL; url = _this.updateURL;
return _this.emit('update-downloaded', {}, releaseNotes, version, date, url, function() { return _this.emit('update-downloaded', {}, releaseNotes, version, date, url, function() {
return _this.quitAndInstall(); return _this.quitAndInstall();
});
}); });
}; });
})(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,115 +1,110 @@
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) { this.setMaxListeners(Number.MAX_VALUE);
extend(ObjectsRegistry, superClass); this.nextId = 0;
function ObjectsRegistry() { // Stores all objects by ref-counting.
this.setMaxListeners(Number.MAX_VALUE); // (id) => {object, count}
this.nextId = 0; this.storage = {};
// Stores all objects by ref-counting. // Stores the IDs of objects referenced by WebContents.
// (id) => {object, count} // (webContentsId) => {(id) => (count)}
this.storage = {}; this.owners = {};
}
// Stores the IDs of objects referenced by WebContents. util.inherits(ObjectsRegistry, EventEmitter);
// (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) {
var base, base1, id;
id = this.saveToStorage(obj);
// Remember the owner.
if ((base = this.owners)[webContentsId] == null) {
base[webContentsId] = {};
} }
if ((base1 = this.owners[webContentsId])[id] == null) {
base1[id] = 0;
}
this.owners[webContentsId][id]++;
// Register a new object, the object would be kept referenced until you release // Returns object's id
// it explicitly. return id;
ObjectsRegistry.prototype.add = function(webContentsId, obj) { };
var base, base1, id;
id = this.saveToStorage(obj);
// Remember the owner.
if ((base = this.owners)[webContentsId] == null) {
base[webContentsId] = {};
}
if ((base1 = this.owners[webContentsId])[id] == null) {
base1[id] = 0;
}
this.owners[webContentsId][id]++;
// Returns object's 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);
// Also reduce the count in owner. // Also reduce the count in owner.
pointer = this.owners[webContentsId]; pointer = this.owners[webContentsId];
if (pointer == null) { if (pointer == null) {
return; return;
} }
--pointer[id]; --pointer[id];
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) {
return; return;
} }
ref = this.owners[webContentsId]; ref = this.owners[webContentsId];
for (id in ref) { for (id in ref) {
count = ref[id]; count = ref[id];
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) {
id = ++this.nextId; id = ++this.nextId;
this.storage[id] = { this.storage[id] = {
count: 0, count: 0,
object: object object: object
}; };
v8Util.setHiddenValue(object, 'atomId', id); v8Util.setHiddenValue(object, 'atomId', id);
} }
++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) {
return; return;
} }
pointer.count -= count; pointer.count -= count;
if (pointer.count === 0) { if (pointer.count === 0) {
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,326 +14,275 @@ 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() {
return _this.getValue(); return _this.getValue();
}; };
})(this), })(this),
set: (function(_this) { set: (function(_this) {
return function(value) { return function(value) {
return _this.setValue(value); return _this.setValue(value);
}; };
})(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() {
return this.webViewImpl.webviewNode.hasAttribute(this.name);
};
BooleanAttribute.prototype.setValue = function(value) {
if (!value) {
return this.webViewImpl.webviewNode.removeAttribute(this.name);
} else {
return this.webViewImpl.webviewNode.setAttribute(this.name, '');
} }
};
BooleanAttribute.prototype.getValue = function() {
return this.webViewImpl.webviewNode.hasAttribute(this.name);
};
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. // 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) {
if (!this.webViewImpl.guestInstanceId) {
return;
} }
return guestViewInternal.setAllowTransparency(this.webViewImpl.guestInstanceId, this.getValue());
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. // 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() {
return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0;
};
AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) {
if (!this.webViewImpl.guestInstanceId) {
return;
} }
return guestViewInternal.setSize(this.webViewImpl.guestInstanceId, {
AutosizeDimensionAttribute.prototype.getValue = function() { enableAutoSize: this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_AUTOSIZE].getValue(),
return parseInt(this.webViewImpl.webviewNode.getAttribute(this.name)) || 0; min: {
}; width: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MINWIDTH].getValue() || 0),
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MINHEIGHT].getValue() || 0)
AutosizeDimensionAttribute.prototype.handleMutation = function(oldValue, newValue) { },
if (!this.webViewImpl.guestInstanceId) { max: {
return; width: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXWIDTH].getValue() || 0),
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXHEIGHT].getValue() || 0)
} }
return guestViewInternal.setSize(this.webViewImpl.guestInstanceId, { });
enableAutoSize: this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_AUTOSIZE].getValue(), };
min: {
width: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MINWIDTH].getValue() || 0),
height: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MINHEIGHT].getValue() || 0)
},
max: {
width: parseInt(this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_MAXWIDTH].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);
this.validPartitionId = true;
}
function PartitionAttribute(webViewImpl) { util.inherits(PartitionAttribute, WebViewAttribute);
PartitionAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_PARTITION, webViewImpl);
this.validPartitionId = true; PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) {
newValue = newValue || '';
// The partition cannot change if the webview has already navigated.
if (!this.webViewImpl.beforeFirstNavigation) {
window.console.error(webViewConstants.ERROR_MSG_ALREADY_NAVIGATED);
this.setValueIgnoreMutation(oldValue);
return;
} }
if (newValue === 'persist:') {
PartitionAttribute.prototype.handleMutation = function(oldValue, newValue) { this.validPartitionId = false;
newValue = newValue || ''; return window.console.error(webViewConstants.ERROR_MSG_INVALID_PARTITION_ATTRIBUTE);
}
// The partition cannot change if the webview has already navigated. };
if (!this.webViewImpl.beforeFirstNavigation) {
window.console.error(webViewConstants.ERROR_MSG_ALREADY_NAVIGATED);
this.setValueIgnoreMutation(oldValue);
return;
}
if (newValue === 'persist:') {
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. // 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);
this.setupMutationObserver();
}
function SrcAttribute(webViewImpl) { util.inherits(SrcAttribute, WebViewAttribute);
SrcAttribute.__super__.constructor.call(this, webViewConstants.ATTRIBUTE_SRC, webViewImpl);
this.setupMutationObserver(); 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) {
WebViewAttribute.prototype.setValueIgnoreMutation.call(this, value);
// 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
// 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) {
// Once we have navigated, we don't allow clearing the src attribute.
// Once <webview> enters a navigated state, it cannot return to a
// placeholder state.
if (!newValue && oldValue) {
// src attribute changes normally initiate a navigation. We suppress
// the next src attribute handler call to avoid reloading the page
// on every guest-initiated navigation.
this.setValueIgnoreMutation(oldValue);
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() {
var params;
this.observer = new MutationObserver((function(_this) {
return function(mutations) {
var i, len, mutation, newValue, oldValue;
for (i = 0, len = mutations.length; i < len; i++) {
mutation = mutations[i];
oldValue = mutation.oldValue;
newValue = _this.getValue();
if (oldValue !== newValue) {
return;
}
_this.handleMutation(oldValue, newValue);
}
};
})(this));
params = {
attributes: true,
attributeOldValue: true,
attributeFilter: [this.name]
};
return this.observer.observe(this.webViewImpl.webviewNode, params);
};
SrcAttribute.prototype.parse = function() {
var guestContents, httpreferrer, opts, useragent;
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) {
return;
}
if (this.webViewImpl.guestInstanceId == null) {
if (this.webViewImpl.beforeFirstNavigation) {
this.webViewImpl.beforeFirstNavigation = false;
this.webViewImpl.createGuest();
}
return;
} }
SrcAttribute.prototype.getValue = function() { // Navigate to |this.src|.
if (this.webViewImpl.webviewNode.hasAttribute(this.name)) { opts = {};
return resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name)); httpreferrer = this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER].getValue();
} else { if (httpreferrer) {
return this.value; opts.httpReferrer = httpreferrer;
} }
}; useragent = this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_USERAGENT].getValue();
if (useragent) {
SrcAttribute.prototype.setValueIgnoreMutation = function(value) { opts.userAgent = useragent;
WebViewAttribute.prototype.setValueIgnoreMutation.call(this, value); }
guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId);
// takeRecords() is needed to clear queued up src mutations. Without it, it return guestContents.loadURL(this.getValue(), opts);
// 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
// want to handle this mutation.
return this.observer.takeRecords();
};
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
// placeholder state.
if (!newValue && oldValue) {
// src attribute changes normally initiate a navigation. We suppress
// the next src attribute handler call to avoid reloading the page
// on every guest-initiated navigation.
this.setValueIgnoreMutation(oldValue);
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() {
var params;
this.observer = new MutationObserver((function(_this) {
return function(mutations) {
var i, len, mutation, newValue, oldValue;
for (i = 0, len = mutations.length; i < len; i++) {
mutation = mutations[i];
oldValue = mutation.oldValue;
newValue = _this.getValue();
if (oldValue !== newValue) {
return;
}
_this.handleMutation(oldValue, newValue);
}
};
})(this));
params = {
attributes: true,
attributeOldValue: true,
attributeFilter: [this.name]
};
return this.observer.observe(this.webViewImpl.webviewNode, params);
};
SrcAttribute.prototype.parse = function() {
var guestContents, httpreferrer, opts, useragent;
if (!this.webViewImpl.elementAttached || !this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_PARTITION].validPartitionId || !this.getValue()) {
return;
}
if (this.webViewImpl.guestInstanceId == null) {
if (this.webViewImpl.beforeFirstNavigation) {
this.webViewImpl.beforeFirstNavigation = false;
this.webViewImpl.createGuest();
}
return;
}
// Navigate to |this.src|.
opts = {};
httpreferrer = this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_HTTPREFERRER].getValue();
if (httpreferrer) {
opts.httpReferrer = httpreferrer;
}
useragent = this.webViewImpl.attributes[webViewConstants.ATTRIBUTE_USERAGENT].getValue();
if (useragent) {
opts.userAgent = useragent;
}
guestContents = remote.getGuestWebContents(this.webViewImpl.guestInstanceId);
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() {
var preload, protocol;
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) {
return this.value;
} }
preload = resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name));
PreloadAttribute.prototype.getValue = function() { protocol = preload.substr(0, 5);
var preload, protocol; if (protocol !== 'file:') {
if (!this.webViewImpl.webviewNode.hasAttribute(this.name)) { console.error(webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE);
return this.value; preload = '';
} }
preload = resolveURL(this.webViewImpl.webviewNode.getAttribute(this.name)); return preload;
protocol = preload.substr(0, 5); };
if (protocol !== 'file:') {
console.error(webViewConstants.ERROR_MSG_INVALID_PRELOAD_ATTRIBUTE);
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() {