From 91cb1731767264eb2c4a7bee5f1449fd681f8971 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 24 Sep 2014 18:47:03 -0400 Subject: [PATCH] rework apiclient startup --- dashboard-ui/scripts/site.js | 2 +- .../thirdparty/mediabrowser.apiclient.js | 130 +++++++++--------- 2 files changed, 65 insertions(+), 67 deletions(-) diff --git a/dashboard-ui/scripts/site.js b/dashboard-ui/scripts/site.js index d060267a99..c1d2dbdd4b 100644 --- a/dashboard-ui/scripts/site.js +++ b/dashboard-ui/scripts/site.js @@ -1162,7 +1162,7 @@ var Dashboard = { alert(Globalize.translate('MessageBrowserDoesNotSupportWebSockets')); } - window.ApiClient = MediaBrowser.ApiClient.create("Dashboard", window.dashboardVersion); + window.ApiClient = MediaBrowser.ApiClient.create("Dashboard", window.dashboardVersion, MediaBrowser.ApiClient.generateDeviceName(), MediaBrowser.ApiClient.generateDeviceId()); $(ApiClient).on("websocketopen", Dashboard.onWebSocketOpened) .on("websocketmessage", Dashboard.onWebSocketMessageReceived); diff --git a/dashboard-ui/thirdparty/mediabrowser.apiclient.js b/dashboard-ui/thirdparty/mediabrowser.apiclient.js index fa69dfa290..586b759d89 100644 --- a/dashboard-ui/thirdparty/mediabrowser.apiclient.js +++ b/dashboard-ui/thirdparty/mediabrowser.apiclient.js @@ -53,30 +53,7 @@ if (!window.MediaBrowser) { window.MediaBrowser = {}; } -MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, window, FileReader, store) { - - function generateDeviceId() { - - var keys = []; - - keys.push(navigator.userAgent); - keys.push((navigator.cpuClass || "")); - - var randomId = ''; - - // Since the above is not guaranteed to be unique per device, add a little more - randomId = store.getItem('randomId'); - - if (!randomId) { - - randomId = new Date().getTime(); - - store.setItem('randomId', randomId.toString()); - } - - keys.push(randomId); - return MediaBrowser.SHA1(keys.join('|')); - } +MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, window, FileReader) { /** * Creates a new api client instance @@ -84,15 +61,13 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi * @param {String} clientName * @param {String} applicationVersion */ - return function (serverAddress, clientName, applicationVersion) { + return function (serverAddress, clientName, applicationVersion, deviceName, deviceId) { if (!serverAddress) { throw new Error("Must supply a serverAddress"); } var self = this; - var deviceName = "Web Browser"; - var deviceId = generateDeviceId(); var currentUserId; var accessToken; var webSocket; @@ -128,42 +103,6 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi accessToken = token; }; - deviceName = (function () { - - var name = ""; - - if ($.browser.chrome) { - name = "Chrome"; - } else if ($.browser.safari) { - name = "Safari"; - } else if ($.browser.webkit) { - name = "WebKit"; - } else if ($.browser.msie) { - name = "Internet Explorer"; - } else if ($.browser.opera) { - name = "Opera"; - } else if ($.browser.firefox || $.browser.mozilla) { - name = "Firefox"; - } - - if (name) { - if ($.browser.version) { - name += " " + $.browser.version; - } - } else { - name = "Web Browser"; - } - - if ($.browser.ipad) { - name += " Ipad"; - } else if ($.browser.iphone) { - name += " Iphone"; - } else if ($.browser.android) { - name += " Android"; - } - return name; - }()); - self.deviceName = function () { return deviceName; }; @@ -3274,12 +3213,71 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi }; -}(jQuery, navigator, window.JSON, window.WebSocket, setTimeout, window, window.FileReader, window.store); +}(jQuery, navigator, window.JSON, window.WebSocket, setTimeout, window, window.FileReader); + +(function (store) { + + MediaBrowser.ApiClient.generateDeviceId = function() { + + var keys = []; + + keys.push(navigator.userAgent); + keys.push((navigator.cpuClass || "")); + + var randomId = ''; + + // Since the above is not guaranteed to be unique per device, add a little more + randomId = store.getItem('randomId'); + + if (!randomId) { + + randomId = new Date().getTime(); + + store.setItem('randomId', randomId.toString()); + } + + keys.push(randomId); + return MediaBrowser.SHA1(keys.join('|')); + }; + + MediaBrowser.ApiClient.generateDeviceName = function () { + + var name = "Web Browser"; + + if ($.browser.chrome) { + name = "Chrome"; + } else if ($.browser.safari) { + name = "Safari"; + } else if ($.browser.webkit) { + name = "WebKit"; + } else if ($.browser.msie) { + name = "Internet Explorer"; + } else if ($.browser.opera) { + name = "Opera"; + } else if ($.browser.firefox || $.browser.mozilla) { + name = "Firefox"; + } + + if ($.browser.version) { + name += " " + $.browser.version; + } + + if ($.browser.ipad) { + name += " Ipad"; + } else if ($.browser.iphone) { + name += " Iphone"; + } else if ($.browser.android) { + name += " Android"; + } + return name; + }; + +})(window.store); /** * Provides a friendly way to create an api client instance using information from the browser's current url */ -MediaBrowser.ApiClient.create = function (clientName, applicationVersion) { +MediaBrowser.ApiClient.create = function (clientName, applicationVersion, deviceName, deviceId) { var loc = window.location; @@ -3289,7 +3287,7 @@ MediaBrowser.ApiClient.create = function (clientName, applicationVersion) { address += ':' + loc.port; } - return new MediaBrowser.ApiClient(address, clientName, applicationVersion); + return new MediaBrowser.ApiClient(address, clientName, applicationVersion, deviceName, deviceId); }; /**