OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** @suppress {duplicate} */ | 7 /** @suppress {duplicate} */ |
8 var remoting = remoting || {}; | 8 var remoting = remoting || {}; |
9 | 9 |
| 10 /** @enum {string} */ |
| 11 remoting.Os = { |
| 12 WINDOWS: 'Windows', |
| 13 LINUX: 'Linux', |
| 14 MAC: 'Mac', |
| 15 CHROMEOS: 'ChromeOS', |
| 16 UNKNOWN: 'Unknown' |
| 17 }; |
| 18 |
| 19 /** |
| 20 * @typedef {{ |
| 21 * osName: remoting.Os, |
| 22 * osVersion: string, |
| 23 * cpu: string, |
| 24 * chromeVersion: string |
| 25 * }} |
| 26 */ |
| 27 remoting.SystemInfo; |
| 28 |
| 29 (function() { |
| 30 |
10 /** | 31 /** |
11 * Returns full Chrome version. | 32 * Returns full Chrome version. |
12 * @return {string?} | 33 * @return {string?} |
13 */ | 34 */ |
14 remoting.getChromeVersion = function() { | 35 remoting.getChromeVersion = function() { |
15 var match = new RegExp('Chrome/([0-9.]*)').exec(navigator.userAgent); | 36 return remoting.getSystemInfo().chromeVersion; |
16 if (match && (match.length >= 2)) { | |
17 return match[1]; | |
18 } | |
19 return null; | |
20 }; | 37 }; |
21 | 38 |
22 /** | 39 /** |
23 * Returns Chrome major version. | |
24 * @return {number} | |
25 */ | |
26 remoting.getChromeMajorVersion = function() { | |
27 var match = new RegExp('Chrome/([0-9]+)\.').exec(navigator.userAgent); | |
28 if (match && (match.length >= 2)) { | |
29 return parseInt(match[1], 10); | |
30 } | |
31 return 0; | |
32 }; | |
33 | |
34 /** | |
35 * Tests whether we are running on Mac. | 40 * Tests whether we are running on Mac. |
36 * | 41 * |
37 * @return {boolean} True if the platform is Mac. | 42 * @return {boolean} True if the platform is Mac. |
38 */ | 43 */ |
39 remoting.platformIsMac = function() { | 44 remoting.platformIsMac = function() { |
40 return navigator.platform.indexOf('Mac') != -1; | 45 return remoting.getSystemInfo().osName === remoting.Os.MAC; |
41 } | 46 }; |
42 | 47 |
43 /** | 48 /** |
44 * Tests whether we are running on Windows. | 49 * Tests whether we are running on Windows. |
45 * | 50 * |
46 * @return {boolean} True if the platform is Windows. | 51 * @return {boolean} True if the platform is Windows. |
47 */ | 52 */ |
48 remoting.platformIsWindows = function() { | 53 remoting.platformIsWindows = function() { |
49 return (navigator.platform.indexOf('Win32') != -1) || | 54 return remoting.getSystemInfo().osName === remoting.Os.WINDOWS; |
50 (navigator.platform.indexOf('Win64') != -1); | 55 }; |
51 } | |
52 | 56 |
53 /** | 57 /** |
54 * Tests whether we are running on Linux. | 58 * Tests whether we are running on Linux. |
55 * | 59 * |
56 * @return {boolean} True if the platform is Linux. | 60 * @return {boolean} True if the platform is Linux. |
57 */ | 61 */ |
58 remoting.platformIsLinux = function() { | 62 remoting.platformIsLinux = function() { |
59 return (navigator.platform.indexOf('Linux') != -1) && | 63 return remoting.getSystemInfo().osName === remoting.Os.LINUX; |
60 !remoting.platformIsChromeOS(); | 64 }; |
61 } | |
62 | 65 |
63 /** | 66 /** |
64 * Tests whether we are running on ChromeOS. | 67 * Tests whether we are running on ChromeOS. |
65 * | 68 * |
66 * @return {boolean} True if the platform is ChromeOS. | 69 * @return {boolean} True if the platform is ChromeOS. |
67 */ | 70 */ |
68 remoting.platformIsChromeOS = function() { | 71 remoting.platformIsChromeOS = function() { |
69 return navigator.userAgent.match(/\bCrOS\b/) != null; | 72 return remoting.getSystemInfo().osName === remoting.Os.CHROMEOS; |
70 } | 73 }; |
| 74 |
| 75 /** |
| 76 * @return {?remoting.SystemInfo} |
| 77 */ |
| 78 remoting.getSystemInfo = function() { |
| 79 var userAgent = remoting.getUserAgent(); |
| 80 |
| 81 /** @type {remoting.SystemInfo} */ |
| 82 var result = { |
| 83 chromeVersion: '', |
| 84 osName: remoting.Os.UNKNOWN, |
| 85 osVersion: '', |
| 86 cpu: '' |
| 87 }; |
| 88 |
| 89 // See platform_unittest.js for sample user agent strings. |
| 90 var chromeVersion = new RegExp('Chrome/([0-9.]*)').exec(userAgent); |
| 91 if (chromeVersion && chromeVersion.length >= 2) { |
| 92 result.chromeVersion = chromeVersion[1]; |
| 93 } |
| 94 |
| 95 var match = new RegExp('Windows NT ([0-9\\.]*)').exec(userAgent); |
| 96 if (match && (match.length >= 2)) { |
| 97 result.osName = remoting.Os.WINDOWS; |
| 98 result.osVersion = match[1]; |
| 99 return result; |
| 100 } |
| 101 |
| 102 match = new RegExp('Linux ([a-zA-Z0-9_]*)').exec(userAgent); |
| 103 if (match && (match.length >= 2)) { |
| 104 result.osName = remoting.Os.LINUX; |
| 105 result.osVersion = ''; |
| 106 result.cpu = match[1]; |
| 107 return result; |
| 108 } |
| 109 |
| 110 match = new RegExp('([a-zA-Z]*) Mac OS X ([0-9_]*)').exec(userAgent); |
| 111 if (match && (match.length >= 3)) { |
| 112 result.osName = remoting.Os.MAC; |
| 113 result.osVersion = match[2].replace(/_/g, '.'); |
| 114 result.cpu = match[1]; |
| 115 return result; |
| 116 } |
| 117 |
| 118 match = new RegExp('CrOS ([a-zA-Z0-9_]*) ([0-9.]*)').exec(userAgent); |
| 119 if (match && (match.length >= 3)) { |
| 120 result.osName = remoting.Os.CHROMEOS; |
| 121 result.osVersion = match[2]; |
| 122 result.cpu = match[1]; |
| 123 return result; |
| 124 } |
| 125 return null; |
| 126 }; |
| 127 |
| 128 /** |
| 129 * To be overwritten by unit test. |
| 130 * |
| 131 * @return {!string} |
| 132 */ |
| 133 remoting.getUserAgent = function() { |
| 134 return navigator.userAgent; |
| 135 }; |
| 136 |
| 137 })(); |
| 138 |
OLD | NEW |