Chromium Code Reviews| Index: remoting/webapp/base/js/platform.js |
| diff --git a/remoting/webapp/base/js/platform.js b/remoting/webapp/base/js/platform.js |
| index 627edccc62620fa134e03b390d4d59332b191f2b..853678eb8f1430ed56eb9c57838e2f08885e757b 100644 |
| --- a/remoting/webapp/base/js/platform.js |
| +++ b/remoting/webapp/base/js/platform.js |
| @@ -7,28 +7,33 @@ |
| /** @suppress {duplicate} */ |
| var remoting = remoting || {}; |
| +/** @enum {string} */ |
| +remoting.Os = { |
| + WINDOWS: 'Windows', |
| + LINUX: 'Linux', |
| + MAC: 'Mac', |
| + CHROMEOS: 'ChromeOS', |
| + UNKNOWN: 'Unknown' |
| +}; |
| + |
| /** |
| - * Returns full Chrome version. |
| - * @return {string?} |
| + * @typedef {{ |
| + * os_name: remoting.Os, |
| + * os_version: string, |
| + * cpu: string, |
| + * chrome_version: string |
|
Jamie
2015/06/06 01:05:41
I realize this is mostly copy/paste, but you might
kelvinp
2015/06/06 02:09:54
Done.
|
| + * }} |
| */ |
| -remoting.getChromeVersion = function() { |
| - var match = new RegExp('Chrome/([0-9.]*)').exec(navigator.userAgent); |
| - if (match && (match.length >= 2)) { |
| - return match[1]; |
| - } |
| - return null; |
| -}; |
| +remoting.SystemInfo; |
| + |
| +(function() { |
| /** |
| - * Returns Chrome major version. |
| - * @return {number} |
| + * Returns full Chrome version. |
| + * @return {string?} |
| */ |
| -remoting.getChromeMajorVersion = function() { |
| - var match = new RegExp('Chrome/([0-9]+)\.').exec(navigator.userAgent); |
| - if (match && (match.length >= 2)) { |
| - return parseInt(match[1], 10); |
| - } |
| - return 0; |
| +remoting.getChromeVersion = function() { |
| + return remoting.getSystemInfo().chrome_version; |
| }; |
| /** |
| @@ -37,8 +42,8 @@ remoting.getChromeMajorVersion = function() { |
| * @return {boolean} True if the platform is Mac. |
| */ |
| remoting.platformIsMac = function() { |
| - return navigator.platform.indexOf('Mac') != -1; |
| -} |
| + return remoting.getSystemInfo().os_name === remoting.Os.MAC; |
| +}; |
| /** |
| * Tests whether we are running on Windows. |
| @@ -46,9 +51,8 @@ remoting.platformIsMac = function() { |
| * @return {boolean} True if the platform is Windows. |
| */ |
| remoting.platformIsWindows = function() { |
| - return (navigator.platform.indexOf('Win32') != -1) || |
| - (navigator.platform.indexOf('Win64') != -1); |
| -} |
| + return remoting.getSystemInfo().os_name === remoting.Os.WINDOWS; |
| +}; |
| /** |
| * Tests whether we are running on Linux. |
| @@ -56,9 +60,8 @@ remoting.platformIsWindows = function() { |
| * @return {boolean} True if the platform is Linux. |
| */ |
| remoting.platformIsLinux = function() { |
| - return (navigator.platform.indexOf('Linux') != -1) && |
| - !remoting.platformIsChromeOS(); |
| -} |
| + return remoting.getSystemInfo().os_name === remoting.Os.LINUX; |
| +}; |
| /** |
| * Tests whether we are running on ChromeOS. |
| @@ -66,5 +69,70 @@ remoting.platformIsLinux = function() { |
| * @return {boolean} True if the platform is ChromeOS. |
| */ |
| remoting.platformIsChromeOS = function() { |
| - return navigator.userAgent.match(/\bCrOS\b/) != null; |
| -} |
| + return remoting.getSystemInfo().os_name === remoting.Os.CHROMEOS; |
| +}; |
| + |
| +/** |
| + * @return {?remoting.SystemInfo} |
| + */ |
| +remoting.getSystemInfo = function() { |
|
kelvinp
2015/06/06 00:48:47
mainly copied from remoting.ServerLogEntry.extract
|
| + var userAgent = remoting.getUserAgent(); |
| + |
| + /** @type {remoting.SystemInfo} */ |
| + var result = { |
| + chrome_version: '', |
| + os_name: remoting.Os.UNKNOWN, |
| + os_version: '', |
| + cpu: '' |
| + }; |
| + |
| + // See platform_unittest.js for sample user agent strings. |
| + var chromeVersion = new RegExp('Chrome/([0-9.]*)').exec(userAgent); |
| + if (chromeVersion && chromeVersion.length >= 2) { |
| + result.chrome_version = chromeVersion[1]; |
| + } |
| + |
| + var match = new RegExp('Windows NT ([0-9\\.]*)').exec(userAgent); |
| + if (match && (match.length >= 2)) { |
| + result.os_name = remoting.Os.WINDOWS; |
| + result.os_version = match[1]; |
| + return result; |
| + } |
| + |
| + match = new RegExp('Linux ([a-zA-Z0-9_]*)').exec(userAgent); |
| + if (match && (match.length >= 2)) { |
| + result.os_name = remoting.Os.LINUX; |
| + result.os_version = ''; |
| + result.cpu = match[1]; |
| + return result; |
| + } |
| + |
| + match = new RegExp('([a-zA-Z]*) Mac OS X ([0-9_]*)').exec(userAgent); |
| + if (match && (match.length >= 3)) { |
| + result.os_name = remoting.Os.MAC; |
| + result.os_version = match[2].replace(/_/g, '.'); |
| + result.cpu = match[1]; |
| + return result; |
| + } |
| + |
| + match = new RegExp('CrOS ([a-zA-Z0-9_]*) ([0-9.]*)').exec(userAgent); |
| + if (match && (match.length >= 3)) { |
| + result.os_name = remoting.Os.CHROMEOS; |
| + result.os_version = match[2]; |
| + result.cpu = match[1]; |
| + return result; |
| + } |
| + return null; |
| +}; |
| + |
| +/** |
| + * To be overwritten by unit test. |
| + * |
| + * @return {!string} |
| + */ |
| +remoting.getUserAgent = function() { |
| + return navigator.userAgent; |
| +}; |
| + |
| +})(); |
| + |