replaced global _self with a binded this. This closes #79

This commit is contained in:
R. Aidan Campbell 2016-08-26 13:57:16 -04:00
parent 5c925faf34
commit 0255fa15bf

View File

@ -32,6 +32,7 @@ String.prototype.toHex = function() {
} }
function NvHTTP(address, clientUid, userEnteredAddress = '') { function NvHTTP(address, clientUid, userEnteredAddress = '') {
console.log(this);
this.address = address; this.address = address;
this.paired = false; this.paired = false;
this.currentGame = 0; this.currentGame = 0;
@ -75,81 +76,81 @@ function _base64ToArrayBuffer(base64) {
NvHTTP.prototype = { NvHTTP.prototype = {
refreshServerInfo: function () { refreshServerInfo: function () {
// try HTTPS first // try HTTPS first
return sendMessage('openUrl', [ _self._baseUrlHttps + '/serverinfo?' + _self._buildUidStr(), false]).then(function(ret) { return sendMessage('openUrl', [ this._baseUrlHttps + '/serverinfo?' + this._buildUidStr(), false]).then(function(ret) {
if (!_self._parseServerInfo(ret)) { // if that fails if (!this._parseServerInfo(ret)) { // if that fails
// try HTTP as a failover. Useful to clients who aren't paired yet // try HTTP as a failover. Useful to clients who aren't paired yet
return sendMessage('openUrl', [ _self._baseUrlHttp + '/serverinfo?' + _self._buildUidStr(), false]).then(function(retHttp) { return sendMessage('openUrl', [ this._baseUrlHttp + '/serverinfo?' + this._buildUidStr(), false]).then(function(retHttp) {
_self._parseServerInfo(retHttp); this._parseServerInfo(retHttp);
}); }.bind(this));
} }
}); }.bind(this));
}, },
// refreshes the server info using a given address. This is useful for testing whether we can successfully ping a host at a given address // refreshes the server info using a given address. This is useful for testing whether we can successfully ping a host at a given address
refreshServerInfoAtAddress: function(givenAddress) { refreshServerInfoAtAddress: function(givenAddress) {
// try HTTPS first // try HTTPS first
return sendMessage('openUrl', [ 'https://' + givenAddress + ':47984' + '/serverinfo?' + _self._buildUidStr(), false]).then(function(ret) { return sendMessage('openUrl', [ 'https://' + givenAddress + ':47984' + '/serverinfo?' + this._buildUidStr(), false]).then(function(ret) {
if (!_self._parseServerInfo(ret)) { // if that fails if (!this._parseServerInfo(ret)) { // if that fails
console.log('Failed to parse serverinfo from HTTPS, falling back to HTTP'); console.log('Failed to parse serverinfo from HTTPS, falling back to HTTP');
// try HTTP as a failover. Useful to clients who aren't paired yet // try HTTP as a failover. Useful to clients who aren't paired yet
return sendMessage('openUrl', [ 'http://' + givenAddress + ':47989' + '/serverinfo?' + _self._buildUidStr(), false]).then(function(retHttp) { return sendMessage('openUrl', [ 'http://' + givenAddress + ':47989' + '/serverinfo?' + this._buildUidStr(), false]).then(function(retHttp) {
return _self._parseServerInfo(retHttp); return this._parseServerInfo(retHttp);
}); }.bind(this));
} }
}); }.bind(this));
}, },
// initially pings the server to try and figure out if it's routable by any means. // initially pings the server to try and figure out if it's routable by any means.
initialPing: function(onSuccess, onFailure) { initialPing: function(onSuccess, onFailure) {
_self.refreshServerInfoAtAddress(_self.hostname + '.local').then(function(successLocal) { this.refreshServerInfoAtAddress(this.hostname + '.local').then(function(successLocal) {
_self.address = _self.hostname + '.local'; this.address = this.hostname + '.local';
onSuccess(); onSuccess();
}, function(failureLocal) { }.bind(this), function(failureLocal) {
_self.refreshServerInfoAtAddress(_self.externalIP).then(function(successExternal) { this.refreshServerInfoAtAddress(this.externalIP).then(function(successExternal) {
_self.address = _self.externalIP; this.address = this.externalIP;
onSuccess(); onSuccess();
}, function(failureExternal) { }.bind(this), function(failureExternal) {
_self.refreshServerInfoAtAddress(_self.userEnteredAddress).then(function(successUserEntered) { this.refreshServerInfoAtAddress(this.userEnteredAddress).then(function(successUserEntered) {
_self.address = _self.userEnteredAddress; this.address = this.userEnteredAddress;
onSuccess(); onSuccess();
}, function(failureUserEntered) { }.bind(this), function(failureUserEntered) {
console.log('WARN! Failed to contact host: ' + _self.hostname + '\r\n' + _self.toString()); console.log('WARN! Failed to contact host: ' + this.hostname + '\r\n' + this.toString());
onFailure(); onFailure();
}); }.bind(this));
}); }.bind(this));
}); }.bind(this));
}, },
toString: function() { toString: function() {
var string = ''; var string = '';
string += 'server address: ' + _self.address + '\r\n'; string += 'server address: ' + this.address + '\r\n';
string += 'server UID: ' + _self.serverUid + '\r\n'; string += 'server UID: ' + this.serverUid + '\r\n';
string += 'is paired: ' + _self.paired + '\r\n'; string += 'is paired: ' + this.paired + '\r\n';
string += 'current game: ' + _self.currentGame + '\r\n'; string += 'current game: ' + this.currentGame + '\r\n';
string += 'server major version: ' + _self.serverMajorVersion + '\r\n'; string += 'server major version: ' + this.serverMajorVersion + '\r\n';
string += 'GFE version: ' + _self.GfeVersion + '\r\n'; string += 'GFE version: ' + this.GfeVersion + '\r\n';
string += 'gpu type: ' + _self.gputype + '\r\n'; string += 'gpu type: ' + this.gputype + '\r\n';
string += 'number of apps: ' + _self.numofapps + '\r\n'; string += 'number of apps: ' + this.numofapps + '\r\n';
string += 'supported display modes: ' + '\r\n'; string += 'supported display modes: ' + '\r\n';
for(displayMode in _self.supportedDisplayModes) { for(displayMode in this.supportedDisplayModes) {
string += '\t' + displayMode + ': ' + _self.supportedDisplayModes[displayMode] + '\r\n'; string += '\t' + displayMode + ': ' + this.supportedDisplayModes[displayMode] + '\r\n';
} }
return string; return string;
}, },
_prepareForStorage: function() { _prepareForStorage: function() {
_self._memCachedBoxArtArray = {}; this._memCachedBoxArtArray = {};
}, },
_parseServerInfo: function(xmlStr) { _parseServerInfo: function(xmlStr) {
$xml = _self._parseXML(xmlStr); $xml = this._parseXML(xmlStr);
$root = $xml.find('root'); $root = $xml.find('root');
if($root.attr("status_code") != 200) { if($root.attr("status_code") != 200) {
return false; return false;
} }
if(_self.serverUid != $root.find('uniqueid').text().trim() && _self.serverUid != null) { if(this.serverUid != $root.find('uniqueid').text().trim() && this.serverUid != null) {
// if we received a UID that isn't the one we expected, fail. // if we received a UID that isn't the one we expected, fail.
return false; return false;
} }
@ -157,26 +158,26 @@ NvHTTP.prototype = {
console.log('parsing server info: '); console.log('parsing server info: ');
console.log($root); console.log($root);
_self.paired = $root.find("PairStatus").text().trim() == 1; this.paired = $root.find("PairStatus").text().trim() == 1;
_self.currentGame = parseInt($root.find("currentgame").text().trim(), 10); this.currentGame = parseInt($root.find("currentgame").text().trim(), 10);
_self.serverMajorVersion = parseInt($root.find("appversion").text().trim().substring(0, 1), 10); this.serverMajorVersion = parseInt($root.find("appversion").text().trim().substring(0, 1), 10);
_self.serverUid = $root.find('uniqueid').text().trim(); this.serverUid = $root.find('uniqueid').text().trim();
_self.hostname = $root.find('hostname').text().trim(); this.hostname = $root.find('hostname').text().trim();
_self.externalIP = $root.find('ExternalIP').text().trim(); this.externalIP = $root.find('ExternalIP').text().trim();
try { // these aren't critical for functionality, and don't necessarily exist in older GFE versions. try { // these aren't critical for functionality, and don't necessarily exist in older GFE versions.
_self.GfeVersion = $root.find('GfeVersion').text().trim(); this.GfeVersion = $root.find('GfeVersion').text().trim();
_self.gputype = $root.find('gputype').text().trim(); this.gputype = $root.find('gputype').text().trim();
_self.numofapps = $root.find('numofapps').text().trim(); this.numofapps = $root.find('numofapps').text().trim();
// now for the hard part: parsing the supported streaming // now for the hard part: parsing the supported streaming
$root.find('DisplayMode').each(function(index, value) { // for each resolution:FPS object $root.find('DisplayMode').each(function(index, value) { // for each resolution:FPS object
var yres = parseInt($(value).find('Height').text()); var yres = parseInt($(value).find('Height').text());
var xres = parseInt($(value).find('Width').text()); var xres = parseInt($(value).find('Width').text());
var fps = parseInt($(value).find('RefreshRate').text()); var fps = parseInt($(value).find('RefreshRate').text());
if(!_self.supportedDisplayModes[yres + ':' + xres]) { if(!this.supportedDisplayModes[yres + ':' + xres]) {
_self.supportedDisplayModes[yres + ':' + xres] = []; this.supportedDisplayModes[yres + ':' + xres] = [];
} }
if(!_self.supportedDisplayModes[yres + ':' + xres].includes(fps)) { if(!this.supportedDisplayModes[yres + ':' + xres].includes(fps)) {
_self.supportedDisplayModes[yres + ':' + xres].push(fps); this.supportedDisplayModes[yres + ':' + xres].push(fps);
} }
}); });
} catch (err) { } catch (err) {
@ -188,14 +189,14 @@ NvHTTP.prototype = {
// has the semantics that its name would indicate. To contain the effects of this change as much // has the semantics that its name would indicate. To contain the effects of this change as much
// as possible, we'll force the current game to zero if the server isn't in a streaming session. // as possible, we'll force the current game to zero if the server isn't in a streaming session.
if ($root.find("state").text().trim().endsWith("_SERVER_AVAILABLE")) { if ($root.find("state").text().trim().endsWith("_SERVER_AVAILABLE")) {
_self.currentGame = 0; this.currentGame = 0;
} }
return true; return true;
}, },
getAppById: function (appId) { getAppById: function (appId) {
return _self.getAppList().then(function (list) { return this.getAppList().then(function (list) {
var retApp = null; var retApp = null;
list.some(function (app) { list.some(function (app) {
@ -212,7 +213,7 @@ NvHTTP.prototype = {
}, },
getAppByName: function (appName) { getAppByName: function (appName) {
return _self.getAppList().then(function (list) { return this.getAppList().then(function (list) {
var retApp = null; var retApp = null;
list.some(function (app) { list.some(function (app) {
@ -229,8 +230,8 @@ NvHTTP.prototype = {
}, },
getAppList: function () { getAppList: function () {
return sendMessage('openUrl', [_self._baseUrlHttps + '/applist?' + _self._buildUidStr(), false]).then(function (ret) { return sendMessage('openUrl', [this._baseUrlHttps + '/applist?' + this._buildUidStr(), false]).then(function (ret) {
$xml = _self._parseXML(ret); $xml = this._parseXML(ret);
var rootElement = $xml.find("root")[0]; var rootElement = $xml.find("root")[0];
var appElements = rootElement.getElementsByTagName("App"); var appElements = rootElement.getElementsByTagName("App");
@ -245,7 +246,7 @@ NvHTTP.prototype = {
} }
return appList; return appList;
}); }.bind(this));
}, },
// returns the box art of the given appID. // returns the box art of the given appID.
@ -254,12 +255,12 @@ NvHTTP.prototype = {
// TODO: unfortunately we do N lookups from storage cache, each of them filling up the memory cache. // TODO: unfortunately we do N lookups from storage cache, each of them filling up the memory cache.
// once the first round of calls are all made, each subsequent request hits this and returns from memory cache // once the first round of calls are all made, each subsequent request hits this and returns from memory cache
if (_self._memCachedBoxArtArray[appId] !== undefined) { if (this._memCachedBoxArtArray[appId] !== undefined) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
console.log('returning memory cached box art'); console.log('returning memory cached box art');
resolve(_self._memCachedBoxArtArray[appId]); resolve(this._memCachedBoxArtArray[appId]);
return; return;
}); }.bind(this));
} }
if (chrome.storage) { if (chrome.storage) {
@ -273,7 +274,7 @@ NvHTTP.prototype = {
storedBoxArtArray = JSONCachedBoxArtArray.boxArtCache; storedBoxArtArray = JSONCachedBoxArtArray.boxArtCache;
storedBoxArtArray[appId] = _base64ToArrayBuffer(storedBoxArtArray[appId]); storedBoxArtArray[appId] = _base64ToArrayBuffer(storedBoxArtArray[appId]);
_self._memCachedBoxArtArray[appId] = storedBoxArtArray[appId]; this._memCachedBoxArtArray[appId] = storedBoxArtArray[appId];
} else { } else {
storedBoxArtArray = {}; storedBoxArtArray = {};
@ -288,19 +289,19 @@ NvHTTP.prototype = {
// otherwise, put it in our cache, then return it // otherwise, put it in our cache, then return it
sendMessage('openUrl', [ sendMessage('openUrl', [
_self._baseUrlHttps + this._baseUrlHttps +
'/appasset?'+_self._buildUidStr() + '/appasset?'+this._buildUidStr() +
'&appid=' + appId + '&appid=' + appId +
'&AssetType=2&AssetIdx=0', '&AssetType=2&AssetIdx=0',
true true
]).then(function(streamedBoxArt) { ]).then(function(streamedBoxArt) {
// the memcached data is global to all the async calls we're doing. This way there's only one array that holds everything properly. // the memcached data is global to all the async calls we're doing. This way there's only one array that holds everything properly.
_self._memCachedBoxArtArray[appId] = streamedBoxArt; this._memCachedBoxArtArray[appId] = streamedBoxArt;
var obj = {}; var obj = {};
var arrayToStore = {} var arrayToStore = {}
for (key in _self._memCachedBoxArtArray) { // convert the arraybuffer into a string for (key in this._memCachedBoxArtArray) { // convert the arraybuffer into a string
arrayToStore[key] = _arrayBufferToBase64(_self._memCachedBoxArtArray[key]); arrayToStore[key] = _arrayBufferToBase64(this._memCachedBoxArtArray[key]);
} }
obj['boxArtCache'] = arrayToStore; // storage is in JSON format. JSON does not support binary data. obj['boxArtCache'] = arrayToStore; // storage is in JSON format. JSON does not support binary data.
@ -308,17 +309,17 @@ NvHTTP.prototype = {
console.log('returning streamed box art'); console.log('returning streamed box art');
resolve(streamedBoxArt); resolve(streamedBoxArt);
return; return;
}); }.bind(this));
}); }.bind(this));
}); }.bind(this));
} else { // shouldn't run because we always have chrome.storage, but I'm not going to antagonize other browsers } else { // shouldn't run because we always have chrome.storage, but I'm not going to antagonize other browsers
console.log('WARN: Chrome.storage not detected! Box art will not be saved!'); console.log('WARN: Chrome.storage not detected! Box art will not be saved!');
return sendMessage('openUrl', [ return sendMessage('openUrl', [
_self._baseUrlHttps + this._baseUrlHttps +
'/appasset?'+_self._buildUidStr() + '/appasset?'+this._buildUidStr() +
'&appid=' + appId + '&appid=' + appId +
'&AssetType=2&AssetIdx=0', '&AssetType=2&AssetIdx=0',
true true
@ -328,8 +329,8 @@ NvHTTP.prototype = {
launchApp: function (appId, mode, sops, rikey, rikeyid, localAudio, surroundAudioInfo) { launchApp: function (appId, mode, sops, rikey, rikeyid, localAudio, surroundAudioInfo) {
return sendMessage('openUrl', [ return sendMessage('openUrl', [
_self._baseUrlHttps + this._baseUrlHttps +
'/launch?' + _self._buildUidStr() + '/launch?' + this._buildUidStr() +
'&appid=' + appId + '&appid=' + appId +
'&mode=' + mode + '&mode=' + mode +
'&additionalStates=1&sops=' + sops + '&additionalStates=1&sops=' + sops +
@ -345,8 +346,8 @@ NvHTTP.prototype = {
resumeApp: function (rikey, rikeyid) { resumeApp: function (rikey, rikeyid) {
return sendMessage('openUrl', [ return sendMessage('openUrl', [
_self._baseUrlHttps + this._baseUrlHttps +
'/resume?' + _self._buildUidStr() + '/resume?' + this._buildUidStr() +
'&rikey=' + rikey + '&rikey=' + rikey +
'&rikeyid=' + rikeyid, '&rikeyid=' + rikeyid,
false false
@ -356,35 +357,35 @@ NvHTTP.prototype = {
}, },
quitApp: function () { quitApp: function () {
return sendMessage('openUrl', [_self._baseUrlHttps + '/cancel?' + _self._buildUidStr(), false]).then(function () { return sendMessage('openUrl', [this._baseUrlHttps + '/cancel?' + this._buildUidStr(), false]).then(function () {
_self.currentGame = 0; this.currentGame = 0;
}); });
}, },
pair: function(randomNumber) { pair: function(randomNumber) {
return _self.refreshServerInfo().then(function () { return this.refreshServerInfo().then(function () {
if (_self.paired) if (this.paired)
return true; return true;
if (_self.currentGame != 0) if (this.currentGame != 0)
return false; return false;
return sendMessage('pair', [_self.serverMajorVersion, _self.address, randomNumber]).then(function (pairStatus) { return sendMessage('pair', [this.serverMajorVersion, this.address, randomNumber]).then(function (pairStatus) {
return sendMessage('openUrl', [_self._baseUrlHttps + '/pair?uniqueid=' + _self.clientUid + '&devicename=roth&updateState=1&phrase=pairchallenge', false]).then(function (ret) { return sendMessage('openUrl', [this._baseUrlHttps + '/pair?uniqueid=' + this.clientUid + '&devicename=roth&updateState=1&phrase=pairchallenge', false]).then(function (ret) {
$xml = _self._parseXML(ret); $xml = this._parseXML(ret);
_self.paired = $xml.find('paired').html() == "1"; this.paired = $xml.find('paired').html() == "1";
return _self.paired; return this.paired;
}); }.bind(this));
}); }.bind(this));
}); }.bind(this));
}, },
unpair: function () { unpair: function () {
return sendMessage('openUrl', [_self._baseUrlHttps + '/unpair?' + _self._buildUidStr(), false]); return sendMessage('openUrl', [this._baseUrlHttps + '/unpair?' + this._buildUidStr(), false]);
}, },
_buildUidStr: function () { _buildUidStr: function () {
return 'uniqueid=' + _self.clientUid + '&uuid=' + guuid(); return 'uniqueid=' + this.clientUid + '&uuid=' + guuid();
}, },
_parseXML: function (xmlData) { _parseXML: function (xmlData) {