OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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. | |
4 | |
5 /** | |
6 * @fileoverview | |
7 * A class of server log entries. | |
8 */ | |
9 | |
10 'use strict'; | |
11 | |
12 /** @suppress {duplicate} */ | |
13 var remoting = remoting || {}; | |
14 | |
15 /** | |
16 * @private | |
17 * @constructor | |
18 */ | |
19 remoting.ServerLogEntry = function() { | |
20 /** @type Object.<string, string> */ this.dict = {}; | |
21 }; | |
22 | |
23 /** @private */ | |
24 remoting.ServerLogEntry.prototype.KEY_EVENT_NAME_ = 'event-name'; | |
25 /** @private */ | |
26 remoting.ServerLogEntry.prototype.VALUE_EVENT_NAME_SESSION_STATE_ = | |
27 'session-state'; | |
28 | |
29 /** @private */ | |
30 remoting.ServerLogEntry.prototype.KEY_ROLE_ = 'role'; | |
31 /** @private */ | |
32 remoting.ServerLogEntry.prototype.VALUE_ROLE_CLIENT_ = 'client'; | |
33 | |
34 /** @private */ | |
35 remoting.ServerLogEntry.prototype.KEY_SESSION_STATE_ = 'session-state'; | |
36 | |
37 /** | |
38 * @private | |
39 * @param {remoting.ClientSession.State} state | |
40 * @return {string} | |
41 */ | |
42 remoting.ServerLogEntry.prototype.getValueForSessionState = function(state) { | |
Jamie
2011/11/11 19:08:40
This also doesn't need to be a member function.
| |
43 switch(state) { | |
44 case remoting.ClientSession.State.UNKNOWN: | |
45 return 'unknown'; | |
46 case remoting.ClientSession.State.CREATED: | |
47 return 'created'; | |
48 case remoting.ClientSession.State.BAD_PLUGIN_VERSION: | |
49 return 'bad-plugin-version'; | |
50 case remoting.ClientSession.State.UNKNOWN_PLUGIN_ERROR: | |
51 return 'unknown-plugin-error'; | |
52 case remoting.ClientSession.State.CONNECTING: | |
53 return 'connecting'; | |
54 case remoting.ClientSession.State.INITIALIZING: | |
55 return 'initializing'; | |
56 case remoting.ClientSession.State.CONNECTED: | |
57 return 'connected'; | |
58 case remoting.ClientSession.State.CLOSED: | |
59 return 'closed'; | |
60 case remoting.ClientSession.State.CONNECTION_FAILED: | |
61 return 'connection-failed'; | |
62 default: | |
63 return 'undefined-' + state; | |
64 } | |
65 }; | |
66 | |
67 /** @private */ | |
68 remoting.ServerLogEntry.prototype.KEY_CONNECTION_ERROR_ = 'connection-error'; | |
69 | |
70 /** | |
71 * @private | |
72 * @param {remoting.ClientSession.ConnectionError} connectionError | |
73 * @return {string} | |
74 */ | |
75 remoting.ServerLogEntry.prototype.getValueForConnectionError = | |
76 function(connectionError) { | |
77 switch(connectionError) { | |
78 case remoting.ClientSession.ConnectionError.NONE: | |
79 return 'none'; | |
80 case remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE: | |
81 return 'host-is-offline'; | |
82 case remoting.ClientSession.ConnectionError.SESSION_REJECTED: | |
83 return 'session-rejected'; | |
84 case remoting.ClientSession.ConnectionError.INCOMPATIBLE_PROTOCOL: | |
85 return 'incompatible-protocol'; | |
86 case remoting.ClientSession.ConnectionError.NETWORK_FAILURE: | |
87 return 'network-failure'; | |
88 case remoting.ClientSession.ConnectionError.OTHER: | |
89 return 'other'; | |
90 default: | |
91 return 'unknown-' + connectionError; | |
92 } | |
93 } | |
94 | |
95 /** @private */ | |
96 remoting.ServerLogEntry.prototype.KEY_OS_NAME_ = 'os-name'; | |
97 /** @private */ | |
98 remoting.ServerLogEntry.prototype.VALUE_OS_NAME_WINDOWS_ = 'Windows'; | |
99 /** @private */ | |
100 remoting.ServerLogEntry.prototype.VALUE_OS_NAME_LINUX_ = 'Linux'; | |
101 /** @private */ | |
102 remoting.ServerLogEntry.prototype.VALUE_OS_NAME_MAC_ = 'Mac'; | |
103 /** @private */ | |
104 remoting.ServerLogEntry.prototype.VALUE_OS_NAME_CHROMEOS_ = 'ChromeOS'; | |
105 | |
106 /** @private */ | |
107 remoting.ServerLogEntry.prototype.KEY_OS_VERSION_ = 'os-version'; | |
108 | |
109 /** @private */ | |
110 remoting.ServerLogEntry.prototype.KEY_CPU_ = 'cpu'; | |
111 | |
112 /** @private */ | |
113 remoting.ServerLogEntry.prototype.KEY_BROWSER_VERSION_ = 'browser-version'; | |
114 | |
115 /** @private */ | |
116 remoting.ServerLogEntry.prototype.KEY_WEBAPP_VERSION_ = 'webapp-version'; | |
117 | |
118 /** | |
119 * Sets one field in this log entry. | |
120 * | |
121 * @private | |
122 * @param {string} key | |
123 * @param {string} value | |
124 */ | |
125 remoting.ServerLogEntry.prototype.set = function(key, value) { | |
126 this.dict[key] = value; | |
127 }; | |
128 | |
129 /** | |
130 * Converts this object into an XML stanza. | |
131 * | |
132 * @return {string} | |
133 */ | |
134 remoting.ServerLogEntry.prototype.toStanza = function() { | |
135 var stanza = '<gr:entry '; | |
136 for (var key in this.dict) { | |
137 stanza += escape(key) + '="' + escape(this.dict[key]) + '" '; | |
138 } | |
139 stanza += '/>'; | |
140 return stanza; | |
141 }; | |
142 | |
143 /** | |
144 * Makes a log entry for a change of client session state. | |
145 * | |
146 * @param {remoting.ClientSession.State} state | |
147 * @param {remoting.ClientSession.ConnectionError} connectionError | |
148 * @return {remoting.ServerLogEntry} | |
149 */ | |
150 remoting.ServerLogEntry.prototype.makeClientSessionStateChange = | |
151 function(state, connectionError) { | |
152 var entry = new remoting.ServerLogEntry(); | |
153 entry.set(this.KEY_ROLE_, this.VALUE_ROLE_CLIENT_); | |
154 entry.set(this.KEY_EVENT_NAME_, this.VALUE_EVENT_NAME_SESSION_STATE_); | |
155 entry.set(this.KEY_SESSION_STATE_, this.getValueForSessionState(state)); | |
156 if (connectionError != remoting.ClientSession.ConnectionError.NONE) { | |
157 entry.set(this.KEY_CONNECTION_ERROR_, | |
158 this.getValueForConnectionError(connectionError)); | |
159 } | |
160 return entry; | |
161 }; | |
162 | |
163 /** | |
164 * Adds fields describing the host to this log entry. | |
165 */ | |
166 remoting.ServerLogEntry.prototype.addHostFields = function() { | |
167 var host = this.getHostData(); | |
168 if (host) { | |
169 if (host.os_name.length > 0) { | |
170 this.set(this.KEY_OS_NAME_, host.os_name); | |
171 } | |
172 if (host.os_version.length > 0) { | |
173 this.set(this.KEY_OS_VERSION_, host.os_version); | |
174 } | |
175 if (host.cpu.length > 0) { | |
176 this.set(this.KEY_CPU_, host.cpu); | |
177 } | |
178 } | |
179 }; | |
180 | |
181 /** | |
182 * Extracts host data from the userAgent string. | |
183 * | |
184 * @private | |
185 * @return {{os_name:string, os_version:string, cpu:string} | null} | |
186 */ | |
187 remoting.ServerLogEntry.prototype.getHostData = function() { | |
188 return this.extractHostDataFrom(navigator.userAgent); | |
189 }; | |
190 | |
191 /** | |
192 * Extracts host data from the given userAgent string. | |
193 * | |
194 * @private | |
195 * @param {string} s | |
196 * @return {{os_name:string, os_version:string, cpu:string} | null} | |
197 */ | |
198 remoting.ServerLogEntry.prototype.extractHostDataFrom = function(s) { | |
199 // Sample userAgent strings: | |
200 // 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 ' + | |
201 // '(KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2' | |
202 // 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.8 ' + | |
203 // '(KHTML, like Gecko) Chrome/17.0.933.0 Safari/535.8' | |
204 // 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 ' + | |
205 // '(KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1' | |
206 // 'Mozilla/5.0 (X11; CrOS i686 14.811.154) AppleWebKit/535.1 ' + | |
207 // '(KHTML, like Gecko) Chrome/14.0.835.204 Safari/535.1' | |
208 var match = new RegExp('Windows NT ([0-9\\.]*)').exec(s); | |
209 if (match && (match.length >= 2)) { | |
210 return { | |
211 'os_name': this.VALUE_OS_NAME_WINDOWS_, | |
212 'os_version': match[1], | |
213 'cpu': '' | |
214 }; | |
215 } | |
216 match = new RegExp('Linux ([a-zA-Z0-9_]*)').exec(s); | |
217 if (match && (match.length >= 2)) { | |
218 return { | |
219 'os_name': this.VALUE_OS_NAME_LINUX_, | |
220 'os_version' : '', | |
221 'cpu': match[1] | |
222 }; | |
223 } | |
224 match = new RegExp('([a-zA-Z]*) Mac OS X ([0-9_]*)').exec(s); | |
225 if (match && (match.length >= 3)) { | |
226 return { | |
227 'os_name': this.VALUE_OS_NAME_MAC_, | |
228 'os_version': match[2].replace(/_/g, '.'), | |
229 'cpu': match[1] | |
230 }; | |
231 } | |
232 match = new RegExp('CrOS ([a-zA-Z0-9]*) ([0-9.]*)').exec(s); | |
233 if (match && (match.length >= 3)) { | |
234 return { | |
235 'os_name': this.VALUE_OS_NAME_CHROMEOS_, | |
236 'os_version': match[2], | |
237 'cpu': match[1] | |
238 }; | |
239 } | |
240 return null; | |
241 }; | |
242 | |
243 /** | |
244 * Adds a field specifying the browser version to this log entry. | |
245 */ | |
246 remoting.ServerLogEntry.prototype.addChromeVersionField = function() { | |
247 var version = this.getChromeVersion(); | |
248 if (version != null) { | |
249 this.set(this.KEY_BROWSER_VERSION_, version); | |
250 } | |
251 }; | |
252 | |
253 /** | |
254 * Extracts the Chrome version from the userAgent string. | |
255 * | |
256 * @private | |
257 * @return {string | null} | |
258 */ | |
259 remoting.ServerLogEntry.prototype.getChromeVersion = function() { | |
260 return this.extractChromeVersionFrom(navigator.userAgent); | |
261 }; | |
262 | |
263 /** | |
264 * Extracts the Chrome version from the given userAgent string. | |
265 * | |
266 * @private | |
267 * @param {string} s | |
268 * @return {string | null} | |
269 */ | |
270 remoting.ServerLogEntry.prototype.extractChromeVersionFrom = function(s) { | |
271 var match = new RegExp('Chrome/([0-9.]*)').exec(s); | |
272 if (match && (match.length >= 2)) { | |
273 return match[1]; | |
274 } | |
275 return null; | |
276 }; | |
277 | |
278 /** | |
279 * Adds a field specifying the webapp version to this log entry. | |
280 */ | |
281 remoting.ServerLogEntry.prototype.addWebappVersionField = function() { | |
282 this.set(this.KEY_WEBAPP_VERSION_, chrome.app.getDetails().version); | |
283 }; | |
OLD | NEW |