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.
9 * Any changes to the values here need to be coordinated with the host and
10 * server/log proto code.
11 * See remoting/signaling/server_log_entry.{cc|h}
16 /** @suppress {duplicate} */
17 var remoting = remoting || {};
23 remoting.ServerLogEntry = function() {
24 /** @type Object<string> */ this.dict = {};
28 remoting.ServerLogEntry.KEY_EVENT_NAME_ = 'event-name';
30 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_ = 'session-state';
32 remoting.ServerLogEntry.VALUE_EVENT_NAME_SIGNAL_STRATEGY_PROGRESS_ =
33 'signal-strategy-progress';
35 remoting.ServerLogEntry.KEY_SESSION_ID_ = 'session-id';
37 remoting.ServerLogEntry.KEY_ROLE_ = 'role';
39 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_ = 'client';
41 remoting.ServerLogEntry.KEY_SESSION_STATE_ = 'session-state';
43 remoting.ServerLogEntry.KEY_CONNECTION_TYPE_ = 'connection-type';
45 remoting.ServerLogEntry.KEY_SIGNAL_STRATEGY_TYPE_ = 'signal-strategy-type';
47 remoting.ServerLogEntry.KEY_SIGNAL_STRATEGY_PROGRESS_ =
48 'signal-strategy-progress';
50 remoting.ServerLogEntry.KEY_SESSION_DURATION_SECONDS_ = 'session-duration';
55 * @param {remoting.ClientSession.State} state
58 remoting.ServerLogEntry.getValueForSessionState_ = function(state) {
60 case remoting.ClientSession.State.UNKNOWN:
62 case remoting.ClientSession.State.INITIALIZING:
63 return 'initializing';
64 case remoting.ClientSession.State.CONNECTING:
66 case remoting.ClientSession.State.AUTHENTICATED:
67 return 'authenticated';
68 case remoting.ClientSession.State.CONNECTED:
70 case remoting.ClientSession.State.CLOSED:
72 case remoting.ClientSession.State.FAILED:
73 return 'connection-failed';
74 case remoting.ClientSession.State.CONNECTION_DROPPED:
75 return 'connection-dropped';
76 case remoting.ClientSession.State.CONNECTION_CANCELED:
77 return 'connection-canceled';
79 return 'undefined-' + state;
84 remoting.ServerLogEntry.KEY_CONNECTION_ERROR_ = 'connection-error';
88 * @param {!remoting.Error} connectionError
91 remoting.ServerLogEntry.getValueForError_ = function(connectionError) {
92 // Directory service should be updated if a new string is added here as
93 // otherwise the error code will be ignored (i.e. recorded as 0 instead).
94 switch (connectionError.getTag()) {
95 case remoting.Error.Tag.NONE:
97 case remoting.Error.Tag.INVALID_ACCESS_CODE:
98 return 'invalid-access-code';
99 case remoting.Error.Tag.MISSING_PLUGIN:
100 return 'missing_plugin';
101 case remoting.Error.Tag.AUTHENTICATION_FAILED:
102 return 'authentication-failed';
103 case remoting.Error.Tag.HOST_IS_OFFLINE:
104 return 'host-is-offline';
105 case remoting.Error.Tag.INCOMPATIBLE_PROTOCOL:
106 return 'incompatible-protocol';
107 case remoting.Error.Tag.BAD_PLUGIN_VERSION:
108 return 'bad-plugin-version';
109 case remoting.Error.Tag.NETWORK_FAILURE:
110 return 'network-failure';
111 case remoting.Error.Tag.HOST_OVERLOAD:
112 return 'host-overload';
113 case remoting.Error.Tag.P2P_FAILURE:
114 return 'p2p-failure';
115 case remoting.Error.Tag.CLIENT_SUSPENDED:
116 return 'client-suspended';
117 case remoting.Error.Tag.UNEXPECTED:
120 return 'unknown-' + connectionError.getTag();
125 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_ =
126 "connection-statistics";
128 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_ = "video-bandwidth";
130 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_ = "capture-latency";
132 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_ = "encode-latency";
134 remoting.ServerLogEntry.KEY_DECODE_LATENCY_ = "decode-latency";
136 remoting.ServerLogEntry.KEY_RENDER_LATENCY_ = "render-latency";
138 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_ = "roundtrip-latency";
141 remoting.ServerLogEntry.KEY_OS_NAME_ = 'os-name';
144 remoting.ServerLogEntry.KEY_OS_VERSION_ = 'os-version';
147 remoting.ServerLogEntry.KEY_CPU_ = 'cpu';
150 remoting.ServerLogEntry.KEY_BROWSER_VERSION_ = 'browser-version';
153 remoting.ServerLogEntry.KEY_WEBAPP_VERSION_ = 'webapp-version';
156 remoting.ServerLogEntry.KEY_HOST_VERSION_ = 'host-version';
159 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_ = 'session-id-old';
162 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_ = 'session-id-new';
165 remoting.ServerLogEntry.KEY_MODE_ = 'mode';
166 // These values are passed in by the Activity to identify the current mode.
167 remoting.ServerLogEntry.VALUE_MODE_IT2ME = 'it2me';
168 remoting.ServerLogEntry.VALUE_MODE_ME2ME = 'me2me';
169 remoting.ServerLogEntry.VALUE_MODE_APP_REMOTING = 'lgapp';
170 remoting.ServerLogEntry.VALUE_MODE_UNKNOWN = 'unknown';
173 remoting.ServerLogEntry.KEY_APP_ID_ = 'application-id';
176 * Sets one field in this log entry.
179 * @param {string} key
180 * @param {string} value
182 remoting.ServerLogEntry.prototype.set_ = function(key, value) {
183 this.dict[key] = value;
187 * Converts this object into an XML stanza.
191 remoting.ServerLogEntry.prototype.toStanza = function() {
192 var stanza = '<gr:entry ';
193 for (var key in this.dict) {
194 stanza += escape(key) + '="' + escape(this.dict[key]) + '" ';
201 * Prints this object on the debug log.
203 * @param {number} indentLevel the indentation level
205 remoting.ServerLogEntry.prototype.toDebugLog = function(indentLevel) {
206 /** @type Array<string> */ var fields = [];
207 for (var key in this.dict) {
208 fields.push(key + ': ' + this.dict[key]);
210 console.log(Array(indentLevel+1).join(" ") + fields.join(', '));
214 * Makes a log entry for a change of client session state.
216 * @param {remoting.ClientSession.State} state
217 * @param {!remoting.Error} connectionError
218 * @param {string} mode The current app mode (It2Me, Me2Me, AppRemoting).
219 * @param {string} role 'client' if the app is acting as a Chromoting client
220 * or 'host' if it is acting as a host (IT2Me)
221 * @return {remoting.ServerLogEntry}
223 remoting.ServerLogEntry.makeClientSessionStateChange = function(state,
224 connectionError, mode, role) {
225 var entry = new remoting.ServerLogEntry();
226 entry.set_(remoting.ServerLogEntry.KEY_ROLE_, role);
227 entry.set_(remoting.ServerLogEntry.KEY_EVENT_NAME_,
228 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_);
229 entry.set_(remoting.ServerLogEntry.KEY_SESSION_STATE_,
230 remoting.ServerLogEntry.getValueForSessionState_(state));
231 if (!connectionError.isNone()) {
232 entry.set_(remoting.ServerLogEntry.KEY_CONNECTION_ERROR_,
233 remoting.ServerLogEntry.getValueForError_(connectionError));
235 entry.addModeField(mode);
240 * Makes a log entry for a set of connection statistics.
241 * Returns null if all the statistics were zero.
243 * @param {remoting.StatsAccumulator} statsAccumulator
244 * @param {string} connectionType
245 * @param {string} mode The current app mode (It2Me, Me2Me, AppRemoting).
246 * @return {?remoting.ServerLogEntry}
248 remoting.ServerLogEntry.makeStats = function(statsAccumulator,
249 connectionType, mode) {
250 var entry = new remoting.ServerLogEntry();
251 entry.set_(remoting.ServerLogEntry.KEY_ROLE_,
252 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
253 entry.set_(remoting.ServerLogEntry.KEY_EVENT_NAME_,
254 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_);
255 if (connectionType) {
256 entry.set_(remoting.ServerLogEntry.KEY_CONNECTION_TYPE_,
259 entry.addModeField(mode);
261 nonZero |= entry.addStatsField_(
262 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_,
263 remoting.ClientSession.STATS_KEY_VIDEO_BANDWIDTH, statsAccumulator);
264 nonZero |= entry.addStatsField_(
265 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_,
266 remoting.ClientSession.STATS_KEY_CAPTURE_LATENCY, statsAccumulator);
267 nonZero |= entry.addStatsField_(
268 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_,
269 remoting.ClientSession.STATS_KEY_ENCODE_LATENCY, statsAccumulator);
270 nonZero |= entry.addStatsField_(
271 remoting.ServerLogEntry.KEY_DECODE_LATENCY_,
272 remoting.ClientSession.STATS_KEY_DECODE_LATENCY, statsAccumulator);
273 nonZero |= entry.addStatsField_(
274 remoting.ServerLogEntry.KEY_RENDER_LATENCY_,
275 remoting.ClientSession.STATS_KEY_RENDER_LATENCY, statsAccumulator);
276 nonZero |= entry.addStatsField_(
277 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_,
278 remoting.ClientSession.STATS_KEY_ROUNDTRIP_LATENCY, statsAccumulator);
286 * Adds one connection statistic to a log entry.
289 * @param {string} entryKey
290 * @param {string} statsKey
291 * @param {remoting.StatsAccumulator} statsAccumulator
292 * @return {boolean} whether the statistic is non-zero
294 remoting.ServerLogEntry.prototype.addStatsField_ = function(
295 entryKey, statsKey, statsAccumulator) {
296 var val = statsAccumulator.calcMean(statsKey);
297 this.set_(entryKey, val.toFixed(2));
302 * Makes a log entry for a "this session ID is old" event.
304 * @param {string} sessionId
305 * @param {string} mode The current app mode (It2Me, Me2Me, AppRemoting).
306 * @return {remoting.ServerLogEntry}
308 remoting.ServerLogEntry.makeSessionIdOld = function(sessionId, mode) {
309 var entry = new remoting.ServerLogEntry();
310 entry.set_(remoting.ServerLogEntry.KEY_ROLE_,
311 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
312 entry.set_(remoting.ServerLogEntry.KEY_EVENT_NAME_,
313 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_);
314 entry.addSessionIdField(sessionId);
315 entry.addModeField(mode);
320 * Makes a log entry for a "this session ID is new" event.
322 * @param {string} sessionId
323 * @param {string} mode The current app mode (It2Me, Me2Me, AppRemoting).
324 * @return {remoting.ServerLogEntry}
326 remoting.ServerLogEntry.makeSessionIdNew = function(sessionId, mode) {
327 var entry = new remoting.ServerLogEntry();
328 entry.set_(remoting.ServerLogEntry.KEY_ROLE_,
329 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
330 entry.set_(remoting.ServerLogEntry.KEY_EVENT_NAME_,
331 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_);
332 entry.addSessionIdField(sessionId);
333 entry.addModeField(mode);
338 * Makes a log entry for a "signal strategy fallback" event.
340 * @param {string} sessionId
341 * @param {remoting.SignalStrategy.Type} strategyType
342 * @param {remoting.FallbackSignalStrategy.Progress} progress
343 * @return {remoting.ServerLogEntry}
345 remoting.ServerLogEntry.makeSignalStrategyProgress =
346 function(sessionId, strategyType, progress) {
347 var entry = new remoting.ServerLogEntry();
348 entry.set_(remoting.ServerLogEntry.KEY_ROLE_,
349 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_);
351 remoting.ServerLogEntry.KEY_EVENT_NAME_,
352 remoting.ServerLogEntry.VALUE_EVENT_NAME_SIGNAL_STRATEGY_PROGRESS_);
353 entry.addSessionIdField(sessionId);
354 entry.set_(remoting.ServerLogEntry.KEY_SIGNAL_STRATEGY_TYPE_, strategyType);
355 entry.set_(remoting.ServerLogEntry.KEY_SIGNAL_STRATEGY_PROGRESS_, progress);
361 * Adds a session ID field to this log entry.
363 * @param {string} sessionId
365 remoting.ServerLogEntry.prototype.addSessionIdField = function(sessionId) {
366 this.set_(remoting.ServerLogEntry.KEY_SESSION_ID_, sessionId);
370 * Adds fields describing the host to this log entry.
372 remoting.ServerLogEntry.prototype.addClientOSFields = function() {
373 var systemInfo = remoting.getSystemInfo();
375 if (systemInfo.osName.length > 0) {
376 this.set_(remoting.ServerLogEntry.KEY_OS_NAME_, systemInfo.osName);
378 if (systemInfo.osVersion.length > 0) {
379 this.set_(remoting.ServerLogEntry.KEY_OS_VERSION_, systemInfo.osVersion);
381 if (systemInfo.cpu.length > 0) {
382 this.set_(remoting.ServerLogEntry.KEY_CPU_, systemInfo.cpu);
388 * Adds a field to this log entry specifying the time in seconds since the start
389 * of the session to the current event.
390 * @param {number} sessionDurationInSeconds
392 remoting.ServerLogEntry.prototype.addSessionDuration =
393 function(sessionDurationInSeconds) {
394 this.set_(remoting.ServerLogEntry.KEY_SESSION_DURATION_SECONDS_,
395 String(sessionDurationInSeconds));
400 * Adds a field specifying the browser version to this log entry.
402 remoting.ServerLogEntry.prototype.addChromeVersionField = function() {
403 var version = remoting.getChromeVersion();
404 if (version != null) {
405 this.set_(remoting.ServerLogEntry.KEY_BROWSER_VERSION_, version);
410 * Adds a field specifying the webapp version to this log entry.
412 remoting.ServerLogEntry.prototype.addWebappVersionField = function() {
413 var manifest = chrome.runtime.getManifest();
414 if (manifest && manifest.version) {
415 this.set_(remoting.ServerLogEntry.KEY_WEBAPP_VERSION_, manifest.version);
420 * Adds a field specifying the host version to this log entry.
421 * @param {string} hostVersion Version of the host for current session.
422 * @return {void} Nothing.
424 remoting.ServerLogEntry.prototype.addHostVersion = function(hostVersion) {
425 this.set_(remoting.ServerLogEntry.KEY_HOST_VERSION_, hostVersion);
429 * Adds a field specifying the mode to this log entry.
430 * @param {string} mode The current app mode (It2Me, Me2Me, AppRemoting).
432 remoting.ServerLogEntry.prototype.addModeField = function(mode) {
433 this.set_(remoting.ServerLogEntry.KEY_MODE_, mode);
437 * Adds a field specifying the application ID to this log entry.
438 * @return {void} Nothing.
440 remoting.ServerLogEntry.prototype.addApplicationId = function() {
441 this.set_(remoting.ServerLogEntry.KEY_APP_ID_, chrome.runtime.id);