1 // Copyright (c) 2012 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.
7 * A class of server log entries.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
19 remoting.ServerLogEntry = function() {
20 /** @type Object.<string, string> */ this.dict = {};
24 remoting.ServerLogEntry.KEY_EVENT_NAME_ = 'event-name';
26 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_ = 'session-state';
28 remoting.ServerLogEntry.KEY_SESSION_ID_ = 'session-id';
30 remoting.ServerLogEntry.KEY_ROLE_ = 'role';
32 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_ = 'client';
34 remoting.ServerLogEntry.KEY_SESSION_STATE_ = 'session-state';
36 remoting.ServerLogEntry.KEY_CONNECTION_TYPE_ = 'connection-type';
40 * @param {remoting.ClientSession.State} state
43 remoting.ServerLogEntry.getValueForSessionState = function(state) {
45 case remoting.ClientSession.State.UNKNOWN:
47 case remoting.ClientSession.State.CREATED:
49 case remoting.ClientSession.State.CONNECTING:
51 case remoting.ClientSession.State.INITIALIZING:
52 return 'initializing';
53 case remoting.ClientSession.State.CONNECTED:
55 case remoting.ClientSession.State.CLOSED:
57 case remoting.ClientSession.State.FAILED:
58 return 'connection-failed';
59 case remoting.ClientSession.State.CONNECTION_DROPPED:
60 return 'connection-dropped';
61 case remoting.ClientSession.State.CONNECTION_CANCELED:
62 return 'connection-canceled';
64 return 'undefined-' + state;
69 remoting.ServerLogEntry.KEY_CONNECTION_ERROR_ = 'connection-error';
73 * @param {remoting.Error} connectionError
76 remoting.ServerLogEntry.getValueForError =
77 function(connectionError) {
78 switch(connectionError) {
79 case remoting.Error.NONE:
81 case remoting.Error.INVALID_ACCESS_CODE:
82 return 'invalid-access-code';
83 case remoting.Error.MISSING_PLUGIN:
84 return 'missing_plugin';
85 case remoting.Error.AUTHENTICATION_FAILED:
86 return 'authentication-failed';
87 case remoting.Error.HOST_IS_OFFLINE:
88 return 'host-is-offline';
89 case remoting.Error.INCOMPATIBLE_PROTOCOL:
90 return 'incompatible-protocol';
91 case remoting.Error.BAD_PLUGIN_VERSION:
92 return 'bad-plugin-version';
93 case remoting.Error.NETWORK_FAILURE:
94 return 'network-failure';
95 case remoting.Error.HOST_OVERLOAD:
96 return 'host-overload';
97 case remoting.Error.P2P_FAILURE:
99 case remoting.Error.UNEXPECTED:
102 return 'unknown-' + connectionError;
107 remoting.ServerLogEntry.KEY_SESSION_DURATION_ = 'session-duration';
110 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_ =
111 "connection-statistics";
113 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_ = "video-bandwidth";
115 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_ = "capture-latency";
117 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_ = "encode-latency";
119 remoting.ServerLogEntry.KEY_DECODE_LATENCY_ = "decode-latency";
121 remoting.ServerLogEntry.KEY_RENDER_LATENCY_ = "render-latency";
123 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_ = "roundtrip-latency";
126 remoting.ServerLogEntry.KEY_OS_NAME_ = 'os-name';
128 remoting.ServerLogEntry.VALUE_OS_NAME_WINDOWS_ = 'Windows';
130 remoting.ServerLogEntry.VALUE_OS_NAME_LINUX_ = 'Linux';
132 remoting.ServerLogEntry.VALUE_OS_NAME_MAC_ = 'Mac';
134 remoting.ServerLogEntry.VALUE_OS_NAME_CHROMEOS_ = 'ChromeOS';
137 remoting.ServerLogEntry.KEY_OS_VERSION_ = 'os-version';
140 remoting.ServerLogEntry.KEY_CPU_ = 'cpu';
143 remoting.ServerLogEntry.KEY_BROWSER_VERSION_ = 'browser-version';
146 remoting.ServerLogEntry.KEY_WEBAPP_VERSION_ = 'webapp-version';
149 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_ = 'session-id-old';
152 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_ = 'session-id-new';
155 remoting.ServerLogEntry.KEY_MODE_ = 'mode';
157 remoting.ServerLogEntry.VALUE_MODE_IT2ME_ = 'it2me';
159 remoting.ServerLogEntry.VALUE_MODE_ME2ME_ = 'me2me';
161 remoting.ServerLogEntry.VALUE_MODE_UNKNOWN_ = 'unknown';
164 * Sets one field in this log entry.
167 * @param {string} key
168 * @param {string} value
170 remoting.ServerLogEntry.prototype.set = function(key, value) {
171 this.dict[key] = value;
175 * Converts this object into an XML stanza.
179 remoting.ServerLogEntry.prototype.toStanza = function() {
180 var stanza = '<gr:entry ';
181 for (var key in this.dict) {
182 stanza += escape(key) + '="' + escape(this.dict[key]) + '" ';
189 * Prints this object on the debug log.
191 * @param {number} indentLevel the indentation level
193 remoting.ServerLogEntry.prototype.toDebugLog = function(indentLevel) {
194 /** @type Array.<string> */ var fields = [];
195 for (var key in this.dict) {
196 fields.push(key + ': ' + this.dict[key]);
198 console.log(Array(indentLevel+1).join(" ") + fields.join(', '));
202 * Makes a log entry for a change of client session state.
204 * @param {remoting.ClientSession.State} state
205 * @param {remoting.Error} connectionError
206 * @param {remoting.ClientSession.Mode} mode
207 * @return {remoting.ServerLogEntry}
209 remoting.ServerLogEntry.makeClientSessionStateChange = function(state,
210 connectionError, mode) {
211 var entry = new remoting.ServerLogEntry();
212 entry.set(remoting.ServerLogEntry.KEY_ROLE_,
213 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
214 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_,
215 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_);
216 entry.set(remoting.ServerLogEntry.KEY_SESSION_STATE_,
217 remoting.ServerLogEntry.getValueForSessionState(state));
218 if (connectionError != remoting.Error.NONE) {
219 entry.set(remoting.ServerLogEntry.KEY_CONNECTION_ERROR_,
220 remoting.ServerLogEntry.getValueForError(connectionError));
222 entry.addModeField(mode);
227 * Adds a session duration to a log entry.
229 * @param {number} sessionDuration
231 remoting.ServerLogEntry.prototype.addSessionDurationField = function(
233 this.set(remoting.ServerLogEntry.KEY_SESSION_DURATION_,
234 sessionDuration.toString());
238 * Makes a log entry for a set of connection statistics.
239 * Returns null if all the statistics were zero.
241 * @param {remoting.StatsAccumulator} statsAccumulator
242 * @param {string} connectionType
243 * @param {remoting.ClientSession.Mode} mode
244 * @return {?remoting.ServerLogEntry}
246 remoting.ServerLogEntry.makeStats = function(statsAccumulator,
249 var entry = new remoting.ServerLogEntry();
250 entry.set(remoting.ServerLogEntry.KEY_ROLE_,
251 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
252 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_,
253 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_);
254 if (connectionType) {
255 entry.set(remoting.ServerLogEntry.KEY_CONNECTION_TYPE_,
258 entry.addModeField(mode);
260 nonZero |= entry.addStatsField(
261 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_,
262 remoting.ClientSession.STATS_KEY_VIDEO_BANDWIDTH, statsAccumulator);
263 nonZero |= entry.addStatsField(
264 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_,
265 remoting.ClientSession.STATS_KEY_CAPTURE_LATENCY, statsAccumulator);
266 nonZero |= entry.addStatsField(
267 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_,
268 remoting.ClientSession.STATS_KEY_ENCODE_LATENCY, statsAccumulator);
269 nonZero |= entry.addStatsField(
270 remoting.ServerLogEntry.KEY_DECODE_LATENCY_,
271 remoting.ClientSession.STATS_KEY_DECODE_LATENCY, statsAccumulator);
272 nonZero |= entry.addStatsField(
273 remoting.ServerLogEntry.KEY_RENDER_LATENCY_,
274 remoting.ClientSession.STATS_KEY_RENDER_LATENCY, statsAccumulator);
275 nonZero |= entry.addStatsField(
276 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_,
277 remoting.ClientSession.STATS_KEY_ROUNDTRIP_LATENCY, statsAccumulator);
285 * Adds one connection statistic to a log entry.
288 * @param {string} entryKey
289 * @param {string} statsKey
290 * @param {remoting.StatsAccumulator} statsAccumulator
291 * @return {boolean} whether the statistic is non-zero
293 remoting.ServerLogEntry.prototype.addStatsField = function(
294 entryKey, statsKey, statsAccumulator) {
295 var val = statsAccumulator.calcMean(statsKey);
296 this.set(entryKey, val.toFixed(2));
301 * Makes a log entry for a "this session ID is old" event.
303 * @param {string} sessionId
304 * @param {remoting.ClientSession.Mode} mode
305 * @return {remoting.ServerLogEntry}
307 remoting.ServerLogEntry.makeSessionIdOld = function(sessionId, mode) {
308 var entry = new remoting.ServerLogEntry();
309 entry.set(remoting.ServerLogEntry.KEY_ROLE_,
310 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
311 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_,
312 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_);
313 entry.addSessionIdField(sessionId);
314 entry.addModeField(mode);
319 * Makes a log entry for a "this session ID is new" event.
321 * @param {string} sessionId
322 * @param {remoting.ClientSession.Mode} mode
323 * @return {remoting.ServerLogEntry}
325 remoting.ServerLogEntry.makeSessionIdNew = function(sessionId, mode) {
326 var entry = new remoting.ServerLogEntry();
327 entry.set(remoting.ServerLogEntry.KEY_ROLE_,
328 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
329 entry.set(remoting.ServerLogEntry.KEY_EVENT_NAME_,
330 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_);
331 entry.addSessionIdField(sessionId);
332 entry.addModeField(mode);
337 * Adds a session ID field to this log entry.
339 * @param {string} sessionId
341 remoting.ServerLogEntry.prototype.addSessionIdField = function(sessionId) {
342 this.set(remoting.ServerLogEntry.KEY_SESSION_ID_, sessionId);
346 * Adds fields describing the host to this log entry.
348 remoting.ServerLogEntry.prototype.addHostFields = function() {
349 var host = remoting.ServerLogEntry.getHostData();
351 if (host.os_name.length > 0) {
352 this.set(remoting.ServerLogEntry.KEY_OS_NAME_, host.os_name);
354 if (host.os_version.length > 0) {
355 this.set(remoting.ServerLogEntry.KEY_OS_VERSION_, host.os_version);
357 if (host.cpu.length > 0) {
358 this.set(remoting.ServerLogEntry.KEY_CPU_, host.cpu);
364 * Extracts host data from the userAgent string.
367 * @return {{os_name:string, os_version:string, cpu:string} | null}
369 remoting.ServerLogEntry.getHostData = function() {
370 return remoting.ServerLogEntry.extractHostDataFrom(navigator.userAgent);
374 * Extracts host data from the given userAgent string.
378 * @return {{os_name:string, os_version:string, cpu:string} | null}
380 remoting.ServerLogEntry.extractHostDataFrom = function(s) {
381 // Sample userAgent strings:
382 // 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 ' +
383 // '(KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2'
384 // 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.8 ' +
385 // '(KHTML, like Gecko) Chrome/17.0.933.0 Safari/535.8'
386 // 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 ' +
387 // '(KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1'
388 // 'Mozilla/5.0 (X11; CrOS i686 14.811.154) AppleWebKit/535.1 ' +
389 // '(KHTML, like Gecko) Chrome/14.0.835.204 Safari/535.1'
390 var match = new RegExp('Windows NT ([0-9\\.]*)').exec(s);
391 if (match && (match.length >= 2)) {
393 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_WINDOWS_,
394 'os_version': match[1],
398 match = new RegExp('Linux ([a-zA-Z0-9_]*)').exec(s);
399 if (match && (match.length >= 2)) {
401 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_LINUX_,
406 match = new RegExp('([a-zA-Z]*) Mac OS X ([0-9_]*)').exec(s);
407 if (match && (match.length >= 3)) {
409 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_MAC_,
410 'os_version': match[2].replace(/_/g, '.'),
414 match = new RegExp('CrOS ([a-zA-Z0-9]*) ([0-9.]*)').exec(s);
415 if (match && (match.length >= 3)) {
417 'os_name': remoting.ServerLogEntry.VALUE_OS_NAME_CHROMEOS_,
418 'os_version': match[2],
426 * Adds a field specifying the browser version to this log entry.
428 remoting.ServerLogEntry.prototype.addChromeVersionField = function() {
429 var version = remoting.getChromeVersion();
430 if (version != null) {
431 this.set(remoting.ServerLogEntry.KEY_BROWSER_VERSION_, version);
436 * Adds a field specifying the webapp version to this log entry.
438 remoting.ServerLogEntry.prototype.addWebappVersionField = function() {
439 var manifest = chrome.runtime.getManifest();
440 if (manifest && manifest.version) {
441 this.set(remoting.ServerLogEntry.KEY_WEBAPP_VERSION_, manifest.version);
446 * Adds a field specifying the mode to this log entry.
448 * @param {remoting.ClientSession.Mode} mode
450 remoting.ServerLogEntry.prototype.addModeField = function(mode) {
451 this.set(remoting.ServerLogEntry.KEY_MODE_,
452 remoting.ServerLogEntry.getModeField(mode));
456 * Gets the value of the mode field to be put in a log entry.
459 * @param {remoting.ClientSession.Mode} mode
462 remoting.ServerLogEntry.getModeField = function(mode) {
464 case remoting.ClientSession.Mode.IT2ME:
465 return remoting.ServerLogEntry.VALUE_MODE_IT2ME_;
466 case remoting.ClientSession.Mode.ME2ME:
467 return remoting.ServerLogEntry.VALUE_MODE_ME2ME_;
469 return remoting.ServerLogEntry.VALUE_MODE_UNKNOWN_;