Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / webapp / base / js / server_log_entry.js
blobc38f9aeda75a5db60605f4d1ac6ffdf501850098
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.
5 /**
6  * @fileoverview
7  * A class of server log entries.
8  *
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}
12  */
14 'use strict';
16 /** @suppress {duplicate} */
17 var remoting = remoting || {};
19 /**
20  * @private
21  * @constructor
22  */
23 remoting.ServerLogEntry = function() {
24   /** @type Object<string> */ this.dict = {};
27 /** @private */
28 remoting.ServerLogEntry.KEY_EVENT_NAME_ = 'event-name';
29 /** @private */
30 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_STATE_ = 'session-state';
31 /** @private */
32 remoting.ServerLogEntry.VALUE_EVENT_NAME_SIGNAL_STRATEGY_PROGRESS_ =
33     'signal-strategy-progress';
34 /** @private */
35 remoting.ServerLogEntry.KEY_SESSION_ID_ = 'session-id';
36 /** @private */
37 remoting.ServerLogEntry.KEY_ROLE_ = 'role';
38 /** @private */
39 remoting.ServerLogEntry.VALUE_ROLE_CLIENT_ = 'client';
40 /** @private */
41 remoting.ServerLogEntry.KEY_SESSION_STATE_ = 'session-state';
42 /** @private */
43 remoting.ServerLogEntry.KEY_CONNECTION_TYPE_ = 'connection-type';
44 /** @private */
45 remoting.ServerLogEntry.KEY_SIGNAL_STRATEGY_TYPE_ = 'signal-strategy-type';
46 /** @private */
47 remoting.ServerLogEntry.KEY_SIGNAL_STRATEGY_PROGRESS_ =
48     'signal-strategy-progress';
49 /** @private */
50 remoting.ServerLogEntry.KEY_SESSION_DURATION_SECONDS_ = 'session-duration';
53 /**
54  * @private
55  * @param {remoting.ClientSession.State} state
56  * @return {string}
57  */
58 remoting.ServerLogEntry.getValueForSessionState_ = function(state) {
59   switch(state) {
60     case remoting.ClientSession.State.UNKNOWN:
61       return 'unknown';
62     case remoting.ClientSession.State.INITIALIZING:
63       return 'initializing';
64     case remoting.ClientSession.State.CONNECTING:
65       return 'connecting';
66     case remoting.ClientSession.State.AUTHENTICATED:
67       return 'authenticated';
68     case remoting.ClientSession.State.CONNECTED:
69       return 'connected';
70     case remoting.ClientSession.State.CLOSED:
71       return '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';
78     default:
79       return 'undefined-' + state;
80   }
83 /** @private */
84 remoting.ServerLogEntry.KEY_CONNECTION_ERROR_ = 'connection-error';
86 /**
87  * @private
88  * @param {!remoting.Error} connectionError
89  * @return {string}
90  */
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:
96       return '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:
118       return 'unexpected';
119     default:
120       return 'unknown-' + connectionError.getTag();
121   }
124 /** @private */
125 remoting.ServerLogEntry.VALUE_EVENT_NAME_CONNECTION_STATISTICS_ =
126     "connection-statistics";
127 /** @private */
128 remoting.ServerLogEntry.KEY_VIDEO_BANDWIDTH_ = "video-bandwidth";
129 /** @private */
130 remoting.ServerLogEntry.KEY_CAPTURE_LATENCY_ = "capture-latency";
131 /** @private */
132 remoting.ServerLogEntry.KEY_ENCODE_LATENCY_ = "encode-latency";
133 /** @private */
134 remoting.ServerLogEntry.KEY_DECODE_LATENCY_ = "decode-latency";
135 /** @private */
136 remoting.ServerLogEntry.KEY_RENDER_LATENCY_ = "render-latency";
137 /** @private */
138 remoting.ServerLogEntry.KEY_ROUNDTRIP_LATENCY_ = "roundtrip-latency";
140 /** @private */
141 remoting.ServerLogEntry.KEY_OS_NAME_ = 'os-name';
143 /** @private */
144 remoting.ServerLogEntry.KEY_OS_VERSION_ = 'os-version';
146 /** @private */
147 remoting.ServerLogEntry.KEY_CPU_ = 'cpu';
149 /** @private */
150 remoting.ServerLogEntry.KEY_BROWSER_VERSION_ = 'browser-version';
152 /** @private */
153 remoting.ServerLogEntry.KEY_WEBAPP_VERSION_ = 'webapp-version';
155 /** @private */
156 remoting.ServerLogEntry.KEY_HOST_VERSION_ = 'host-version';
158 /** @private */
159 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_OLD_ = 'session-id-old';
161 /** @private */
162 remoting.ServerLogEntry.VALUE_EVENT_NAME_SESSION_ID_NEW_ = 'session-id-new';
164 /** @private */
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';
172 /** @private */
173 remoting.ServerLogEntry.KEY_APP_ID_ = 'application-id';
176  * Sets one field in this log entry.
178  * @private
179  * @param {string} key
180  * @param {string} value
181  */
182 remoting.ServerLogEntry.prototype.set_ = function(key, value) {
183   this.dict[key] = value;
187  * Converts this object into an XML stanza.
189  * @return {string}
190  */
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]) + '" ';
195   }
196   stanza += '/>';
197   return stanza;
201  * Prints this object on the debug log.
203  * @param {number} indentLevel the indentation level
204  */
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]);
209   }
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}
222  */
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));
234   }
235   entry.addModeField(mode);
236   return entry;
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}
247  */
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_,
257                connectionType);
258   }
259   entry.addModeField(mode);
260   var nonZero = false;
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);
279   if (nonZero) {
280     return entry;
281   }
282   return null;
286  * Adds one connection statistic to a log entry.
288  * @private
289  * @param {string} entryKey
290  * @param {string} statsKey
291  * @param {remoting.StatsAccumulator} statsAccumulator
292  * @return {boolean} whether the statistic is non-zero
293  */
294 remoting.ServerLogEntry.prototype.addStatsField_ = function(
295     entryKey, statsKey, statsAccumulator) {
296   var val = statsAccumulator.calcMean(statsKey);
297   this.set_(entryKey, val.toFixed(2));
298   return (val != 0);
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}
307  */
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);
316   return entry;
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}
325  */
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);
334   return entry;
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}
344  */
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_);
350   entry.set_(
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);
357   return entry;
361  * Adds a session ID field to this log entry.
363  * @param {string} sessionId
364  */
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.
371  */
372 remoting.ServerLogEntry.prototype.addClientOSFields = function() {
373   var systemInfo = remoting.getSystemInfo();
374   if (systemInfo) {
375     if (systemInfo.osName.length > 0) {
376       this.set_(remoting.ServerLogEntry.KEY_OS_NAME_, systemInfo.osName);
377     }
378     if (systemInfo.osVersion.length > 0) {
379       this.set_(remoting.ServerLogEntry.KEY_OS_VERSION_, systemInfo.osVersion);
380     }
381     if (systemInfo.cpu.length > 0) {
382       this.set_(remoting.ServerLogEntry.KEY_CPU_, systemInfo.cpu);
383     }
384   }
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
391  */
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.
401  */
402 remoting.ServerLogEntry.prototype.addChromeVersionField = function() {
403   var version = remoting.getChromeVersion();
404   if (version != null) {
405     this.set_(remoting.ServerLogEntry.KEY_BROWSER_VERSION_, version);
406   }
410  * Adds a field specifying the webapp version to this log entry.
411  */
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);
416   }
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.
423  */
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).
431  */
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.
439  */
440 remoting.ServerLogEntry.prototype.addApplicationId = function() {
441   this.set_(remoting.ServerLogEntry.KEY_APP_ID_, chrome.runtime.id);