| 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.KEY_EVENT_NAME_ = 'event-name'; | |
| 25 /** @private */ | |
| 26 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_ = | |
| 27 'session-state'; | |
| 28 | |
| 29 /** @private */ | |
| 30 remoting.ServerLogEntry.KEY_SESSION_ID_ = 'session-id'; | |
| 31 | |
| 32 /** @private */ | |
| 33 remoting.ServerLogEntry.KEY_ROLE_ = 'role'; | |
| 34 /** @private */ | |
| 35 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_ = 'client'; | |
| 36 | |
| 37 /** @private */ | |
| 38 remoting.ServerLogEntry.KEY_SESSION_STATE_ = 'session-state'; | |
| 39 | |
| 40 /** | |
| 41 * @private | |
| 42 * @param {remoting.ClientSession.State} state | |
| 43 * @return {string} | |
| 44 */ | |
| 45 remoting.ServerLogEntry.getValueForSessionState = function(state) { | |
| 46 switch(state) { | |
| 47 case remoting.ClientSession.State.UNKNOWN: | |
| 48 return 'unknown'; | |
| 49 case remoting.ClientSession.State.CREATED: | |
| 50 return 'created'; | |
| 51 case remoting.ClientSession.State.BAD_PLUGIN_VERSION: | |
| 52 return 'bad-plugin-version'; | |
| 53 case remoting.ClientSession.State.UNKNOWN_PLUGIN_ERROR: | |
| 54 return 'unknown-plugin-error'; | |
| 55 case remoting.ClientSession.State.CONNECTING: | |
| 56 return 'connecting'; | |
| 57 case remoting.ClientSession.State.INITIALIZING: | |
| 58 return 'initializing'; | |
| 59 case remoting.ClientSession.State.CONNECTED: | |
| 60 return 'connected'; | |
| 61 case remoting.ClientSession.State.CLOSED: | |
| 62 return 'closed'; | |
| 63 case remoting.ClientSession.State.CONNECTION_FAILED: | |
| 64 return 'connection-failed'; | |
| 65 default: | |
| 66 return 'undefined-' + state; | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 /** @private */ | |
| 71 remoting.ServerLogEntry.KEY_CONNECTION_ERROR_ = 'connection-error'; | |
| 72 | |
| 73 /** | |
| 74 * @private | |
| 75 * @param {remoting.ClientSession.ConnectionError} connectionError | |
| 76 * @return {string} | |
| 77 */ | |
| 78 remoting.ServerLogEntry.getValueForConnectionError = | |
| 79 function(connectionError) { | |
| 80 switch(connectionError) { | |
| 81 case remoting.ClientSession.ConnectionError.NONE: | |
| 82 return 'none'; | |
| 83 case remoting.ClientSession.ConnectionError.HOST_IS_OFFLINE: | |
| 84 return 'host-is-offline'; | |
| 85 case remoting.ClientSession.ConnectionError.SESSION_REJECTED: | |
| 86 return 'session-rejected'; | |
| 87 case remoting.ClientSession.ConnectionError.INCOMPATIBLE_PROTOCOL: | |
| 88 return 'incompatible-protocol'; | |
| 89 case remoting.ClientSession.ConnectionError.NETWORK_FAILURE: | |
| 90 return 'network-failure'; | |
| 91 default: | |
| 92 return 'unknown-' + connectionError; | |
| 93 } | |
| 94 }; | |
| 95 | |
| 96 /** @private */ | |
| 97 remoting.ServerLogEntry.KEY_SESSION_DURATION_ = 'session-duration'; | |
| 98 | |
| 99 /** @private */ | |
| 100 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_ = | |
| 101 "connection-statistics"; | |
| 102 /** @private */ | |
| 103 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_ = "video-bandwidth"; | |
| 104 /** @private */ | |
| 105 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_ = "capture-latency"; | |
| 106 /** @private */ | |
| 107 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_ = "encode-latency"; | |
| 108 /** @private */ | |
| 109 remoting.ServerLogEntry.KEY_DECODE_LATENCY_ = "decode-latency"; | |
| 110 /** @private */ | |
| 111 remoting.ServerLogEntry.KEY_RENDER_LATENCY_ = "render-latency"; | |
| 112 /** @private */ | |
| 113 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_ = "roundtrip-latency"; | |
| 114 | |
| 115 /** @private */ | |
| 116 remoting.ServerLogEntry.KEY_OS_NAME_ = 'os-name'; | |
| 117 /** @private */ | |
| 118 remoting.ServerLogEntry.VALUE_OS_NAME_WINDOWS_ = 'Windows'; | |
| 119 /** @private */ | |
| 120 remoting.ServerLogEntry.VALUE_OS_NAME_LINUX_ = 'Linux'; | |
| 121 /** @private */ | |
| 122 remoting.ServerLogEntry.VALUE_OS_NAME_MAC_ = 'Mac'; | |
| 123 /** @private */ | |
| 124 remoting.ServerLogEntry.VALUE_OS_NAME_CHROMEOS_ = 'ChromeOS'; | |
| 125 | |
| 126 /** @private */ | |
| 127 remoting.ServerLogEntry.KEY_OS_VERSION_ = 'os-version'; | |
| 128 | |
| 129 /** @private */ | |
| 130 remoting.ServerLogEntry.KEY_CPU_ = 'cpu'; | |
| 131 | |
| 132 /** @private */ | |
| 133 remoting.ServerLogEntry.KEY_BROWSER_VERSION_ = 'browser-version'; | |
| 134 | |
| 135 /** @private */ | |
| 136 remoting.ServerLogEntry.KEY_WEBAPP_VERSION_ = 'webapp-version'; | |
| 137 | |
| 138 /** @private */ | |
| 139 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_ = 'session-id-old'; | |
| 140 | |
| 141 /** @private */ | |
| 142 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_ = 'session-id-new'; | |
| 143 | |
| 144 /** | |
| 145 * Sets one field in this log entry. | |
| 146 * | |
| 147 * @private | |
| 148 * @param {string} key | |
| 149 * @param {string} value | |
| 150 */ | |
| 151 remoting.ServerLogEntry.prototype.set = function(key, value) { | |
| 152 this.dict[key] = value; | |
| 153 }; | |
| 154 | |
| 155 /** | |
| 156 * Converts this object into an XML stanza. | |
| 157 * | |
| 158 * @return {string} | |
| 159 */ | |
| 160 remoting.ServerLogEntry.prototype.toStanza = function() { | |
| 161 var stanza = '<gr:entry '; | |
| 162 for (var key in this.dict) { | |
| 163 stanza += escape(key) + '="' + escape(this.dict[key]) + '" '; | |
| 164 } | |
| 165 stanza += '/>'; | |
| 166 return stanza; | |
| 167 }; | |
| 168 | |
| 169 /** | |
| 170 * Prints this object on the debug log. | |
| 171 * | |
| 172 * @param {number} indentLevel the indentation level | |
| 173 */ | |
| 174 remoting.ServerLogEntry.prototype.toDebugLog = function(indentLevel) { | |
| 175 /** @type Array.<string> */ var fields = []; | |
| 176 for (var key in this.dict) { | |
| 177 fields.push(key + ': ' + this.dict[key]); | |
| 178 } | |
| 179 remoting.debug.logIndent(indentLevel, fields.join(', ')); | |
| 180 }; | |
| 181 | |
| 182 /** | |
| 183 * Makes a log entry for a change of client session state. | |
| 184 * | |
| 185 * @param {remoting.ClientSession.State} state | |
| 186 * @param {remoting.ClientSession.ConnectionError} connectionError | |
| 187 * @return {remoting.ServerLogEntry} | |
| 188 */ | |
| 189 remoting.ServerLogEntry.makeClientSessionStateChange = function(state, | |
| 190 connectionError) { | |
| 191 var entry = new remoting.ServerLogEntry(); | |
| 192 entry.set(remoting.ServerLogEntry.KEY_ROLE_, | |
| 193 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_); | |
| 194 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_, | |
| 195 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_); | |
| 196 entry.set(remoting.ServerLogEntry.KEY_SESSION_STATE_, | |
| 197 remoting.ServerLogEntry.getValueForSessionState(state)); | |
| 198 if (connectionError != remoting.ClientSession.ConnectionError.NONE) { | |
| 199 entry.set(remoting.ServerLogEntry.KEY_CONNECTION_ERROR_, | |
| 200 remoting.ServerLogEntry.getValueForConnectionError( | |
| 201 connectionError)); | |
| 202 } | |
| 203 return entry; | |
| 204 }; | |
| 205 | |
| 206 /** | |
| 207 * Adds a session duration to a log entry. | |
| 208 * | |
| 209 * @param {number} sessionDuration | |
| 210 */ | |
| 211 remoting.ServerLogEntry.prototype.addSessionDurationField = function( | |
| 212 sessionDuration) { | |
| 213 this.set(remoting.ServerLogEntry.KEY_SESSION_DURATION_, | |
| 214 sessionDuration.toString()); | |
| 215 }; | |
| 216 | |
| 217 /** | |
| 218 * Makes a log entry for a set of connection statistics. | |
| 219 * Returns null if all the statistics were zero. | |
| 220 * | |
| 221 * @param {remoting.StatsAccumulator} statsAccumulator | |
| 222 * @return {?remoting.ServerLogEntry} | |
| 223 */ | |
| 224 remoting.ServerLogEntry.makeStats = function(statsAccumulator) { | |
| 225 var entry = new remoting.ServerLogEntry(); | |
| 226 entry.set(remoting.ServerLogEntry.KEY_ROLE_, | |
| 227 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_); | |
| 228 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_, | |
| 229 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_); | |
| 230 var nonZero = false; | |
| 231 nonZero |= entry.addStatsField( | |
| 232 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_, | |
| 233 remoting.ClientSession.STATS_KEY_VIDEO_BANDWIDTH, statsAccumulator); | |
| 234 nonZero |= entry.addStatsField( | |
| 235 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_, | |
| 236 remoting.ClientSession.STATS_KEY_CAPTURE_LATENCY, statsAccumulator); | |
| 237 nonZero |= entry.addStatsField( | |
| 238 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_, | |
| 239 remoting.ClientSession.STATS_KEY_ENCODE_LATENCY, statsAccumulator); | |
| 240 nonZero |= entry.addStatsField( | |
| 241 remoting.ServerLogEntry.KEY_DECODE_LATENCY_, | |
| 242 remoting.ClientSession.STATS_KEY_DECODE_LATENCY, statsAccumulator); | |
| 243 nonZero |= entry.addStatsField( | |
| 244 remoting.ServerLogEntry.KEY_RENDER_LATENCY_, | |
| 245 remoting.ClientSession.STATS_KEY_RENDER_LATENCY, statsAccumulator); | |
| 246 nonZero |= entry.addStatsField( | |
| 247 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_, | |
| 248 remoting.ClientSession.STATS_KEY_ROUNDTRIP_LATENCY, statsAccumulator); | |
| 249 if (nonZero) { | |
| 250 return entry; | |
| 251 } | |
| 252 return null; | |
| 253 }; | |
| 254 | |
| 255 /** | |
| 256 * Adds one connection statistic to a log entry. | |
| 257 * | |
| 258 * @private | |
| 259 * @param {string} entryKey | |
| 260 * @param {string} statsKey | |
| 261 * @param {remoting.StatsAccumulator} statsAccumulator | |
| 262 * @return {boolean} whether the statistic is non-zero | |
| 263 */ | |
| 264 remoting.ServerLogEntry.prototype.addStatsField = function( | |
| 265 entryKey, statsKey, statsAccumulator) { | |
| 266 var val = statsAccumulator.calcMean(statsKey); | |
| 267 this.set(entryKey, val.toString()); | |
| 268 return (val != 0); | |
| 269 }; | |
| 270 | |
| 271 /** | |
| 272 * Makes a log entry for a "this session ID is old" event. | |
| 273 * | |
| 274 * @param {string} sessionId | |
| 275 * @return {remoting.ServerLogEntry} | |
| 276 */ | |
| 277 remoting.ServerLogEntry.makeSessionIdOld = function(sessionId) { | |
| 278 var entry = new remoting.ServerLogEntry(); | |
| 279 entry.set(remoting.ServerLogEntry.KEY_ROLE_, | |
| 280 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_); | |
| 281 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_, | |
| 282 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_); | |
| 283 entry.addSessionIdField(sessionId); | |
| 284 return entry; | |
| 285 }; | |
| 286 | |
| 287 /** | |
| 288 * Makes a log entry for a "this session ID is new" event. | |
| 289 * | |
| 290 * @param {string} sessionId | |
| 291 * @return {remoting.ServerLogEntry} | |
| 292 */ | |
| 293 remoting.ServerLogEntry.makeSessionIdNew = function(sessionId) { | |
| 294 var entry = new remoting.ServerLogEntry(); | |
| 295 entry.set(remoting.ServerLogEntry.KEY_ROLE_, | |
| 296 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_); | |
| 297 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_, | |
| 298 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_); | |
| 299 entry.addSessionIdField(sessionId); | |
| 300 return entry; | |
| 301 }; | |
| 302 | |
| 303 /** | |
| 304 * Adds a session ID field to this log entry. | |
| 305 * | |
| 306 * @param {string} sessionId | |
| 307 */ | |
| 308 remoting.ServerLogEntry.prototype.addSessionIdField = function(sessionId) { | |
| 309 this.set(remoting.ServerLogEntry.KEY_SESSION_ID_, sessionId); | |
| 310 } | |
| 311 | |
| 312 /** | |
| 313 * Adds fields describing the host to this log entry. | |
| 314 */ | |
| 315 remoting.ServerLogEntry.prototype.addHostFields = function() { | |
| 316 var host = remoting.ServerLogEntry.getHostData(); | |
| 317 if (host) { | |
| 318 if (host.os_name.length > 0) { | |
| 319 this.set(remoting.ServerLogEntry.KEY_OS_NAME_, host.os_name); | |
| 320 } | |
| 321 if (host.os_version.length > 0) { | |
| 322 this.set(remoting.ServerLogEntry.KEY_OS_VERSION_, host.os_version); | |
| 323 } | |
| 324 if (host.cpu.length > 0) { | |
| 325 this.set(remoting.ServerLogEntry.KEY_CPU_, host.cpu); | |
| 326 } | |
| 327 } | |
| 328 }; | |
| 329 | |
| 330 /** | |
| 331 * Extracts host data from the userAgent string. | |
| 332 * | |
| 333 * @private | |
| 334 * @return {{os_name:string, os_version:string, cpu:string} | null} | |
| 335 */ | |
| 336 remoting.ServerLogEntry.getHostData = function() { | |
| 337 return remoting.ServerLogEntry.extractHostDataFrom(navigator.userAgent); | |
| 338 }; | |
| 339 | |
| 340 /** | |
| 341 * Extracts host data from the given userAgent string. | |
| 342 * | |
| 343 * @private | |
| 344 * @param {string} s | |
| 345 * @return {{os_name:string, os_version:string, cpu:string} | null} | |
| 346 */ | |
| 347 remoting.ServerLogEntry.extractHostDataFrom = function(s) { | |
| 348 // Sample userAgent strings: | |
| 349 // 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 ' + | |
| 350 // '(KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2' | |
| 351 // 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.8 ' + | |
| 352 // '(KHTML, like Gecko) Chrome/17.0.933.0 Safari/535.8' | |
| 353 // 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 ' + | |
| 354 // '(KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1' | |
| 355 // 'Mozilla/5.0 (X11; CrOS i686 14.811.154) AppleWebKit/535.1 ' + | |
| 356 // '(KHTML, like Gecko) Chrome/14.0.835.204 Safari/535.1' | |
| 357 var match = new RegExp('Windows NT ([0-9\\.]*)').exec(s); | |
| 358 if (match && (match.length >= 2)) { | |
| 359 return { | |
| 360 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_WINDOWS_, | |
| 361 'os_version': match[1], | |
| 362 'cpu': '' | |
| 363 }; | |
| 364 } | |
| 365 match = new RegExp('Linux ([a-zA-Z0-9_]*)').exec(s); | |
| 366 if (match && (match.length >= 2)) { | |
| 367 return { | |
| 368 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_LINUX_, | |
| 369 'os_version' : '', | |
| 370 'cpu': match[1] | |
| 371 }; | |
| 372 } | |
| 373 match = new RegExp('([a-zA-Z]*) Mac OS X ([0-9_]*)').exec(s); | |
| 374 if (match && (match.length >= 3)) { | |
| 375 return { | |
| 376 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_MAC_, | |
| 377 'os_version': match[2].replace(/_/g, '.'), | |
| 378 'cpu': match[1] | |
| 379 }; | |
| 380 } | |
| 381 match = new RegExp('CrOS ([a-zA-Z0-9]*) ([0-9.]*)').exec(s); | |
| 382 if (match && (match.length >= 3)) { | |
| 383 return { | |
| 384 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_CHROMEOS_, | |
| 385 'os_version': match[2], | |
| 386 'cpu': match[1] | |
| 387 }; | |
| 388 } | |
| 389 return null; | |
| 390 }; | |
| 391 | |
| 392 /** | |
| 393 * Adds a field specifying the browser version to this log entry. | |
| 394 */ | |
| 395 remoting.ServerLogEntry.prototype.addChromeVersionField = function() { | |
| 396 var version = remoting.ServerLogEntry.getChromeVersion(); | |
| 397 if (version != null) { | |
| 398 this.set(remoting.ServerLogEntry.KEY_BROWSER_VERSION_, version); | |
| 399 } | |
| 400 }; | |
| 401 | |
| 402 /** | |
| 403 * Extracts the Chrome version from the userAgent string. | |
| 404 * | |
| 405 * @private | |
| 406 * @return {string | null} | |
| 407 */ | |
| 408 remoting.ServerLogEntry.getChromeVersion = function() { | |
| 409 return remoting.ServerLogEntry.extractChromeVersionFrom(navigator.userAgent); | |
| 410 }; | |
| 411 | |
| 412 /** | |
| 413 * Extracts the Chrome version from the given userAgent string. | |
| 414 * | |
| 415 * @private | |
| 416 * @param {string} s | |
| 417 * @return {string | null} | |
| 418 */ | |
| 419 remoting.ServerLogEntry.extractChromeVersionFrom = function(s) { | |
| 420 var match = new RegExp('Chrome/([0-9.]*)').exec(s); | |
| 421 if (match && (match.length >= 2)) { | |
| 422 return match[1]; | |
| 423 } | |
| 424 return null; | |
| 425 }; | |
| 426 | |
| 427 /** | |
| 428 * Adds a field specifying the webapp version to this log entry. | |
| 429 */ | |
| 430 remoting.ServerLogEntry.prototype.addWebappVersionField = function() { | |
| 431 this.set(remoting.ServerLogEntry.KEY_WEBAPP_VERSION_, | |
| 432 chrome.app.getDetails().version); | |
| 433 }; | |
| OLD | NEW |