Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / platform.js
blob8865c099a3e80b994d16c76cba524513a5eb518e
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
3 // found in the LICENSE file.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 /** @enum {string} */
11 remoting.Os = {
12 WINDOWS: 'Windows',
13 LINUX: 'Linux',
14 MAC: 'Mac',
15 CHROMEOS: 'ChromeOS',
16 UNKNOWN: 'Unknown'
19 /**
20 * @typedef {{
21 * osName: remoting.Os,
22 * osVersion: string,
23 * cpu: string,
24 * chromeVersion: string
25 * }}
27 remoting.SystemInfo;
29 (function() {
31 /**
32 * Returns full Chrome version.
33 * @return {string?}
35 remoting.getChromeVersion = function() {
36 return remoting.getSystemInfo().chromeVersion;
39 /**
40 * Tests whether we are running on Mac.
42 * @return {boolean} True if the platform is Mac.
44 remoting.platformIsMac = function() {
45 return remoting.getSystemInfo().osName === remoting.Os.MAC;
48 /**
49 * Tests whether we are running on Windows.
51 * @return {boolean} True if the platform is Windows.
53 remoting.platformIsWindows = function() {
54 return remoting.getSystemInfo().osName === remoting.Os.WINDOWS;
57 /**
58 * Tests whether we are running on Linux.
60 * @return {boolean} True if the platform is Linux.
62 remoting.platformIsLinux = function() {
63 return remoting.getSystemInfo().osName === remoting.Os.LINUX;
66 /**
67 * Tests whether we are running on ChromeOS.
69 * @return {boolean} True if the platform is ChromeOS.
71 remoting.platformIsChromeOS = function() {
72 return remoting.getSystemInfo().osName === remoting.Os.CHROMEOS;
75 /**
76 * @return {?remoting.SystemInfo}
78 remoting.getSystemInfo = function() {
79 var userAgent = remoting.getUserAgent();
81 /** @type {remoting.SystemInfo} */
82 var result = {
83 chromeVersion: '',
84 osName: remoting.Os.UNKNOWN,
85 osVersion: '',
86 cpu: ''
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];
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;
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;
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;
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;
125 return null;
129 * To be overwritten by unit test.
131 * @return {!string}
133 remoting.getUserAgent = function() {
134 return navigator.userAgent;
137 })();