Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / remoting / webapp / crd / js / log_to_server.js
blobe76f67a6662b33bb9e1202d3e25b0ffbb7f1c524
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 * Module for sending log entries to the server.
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16 * @param {remoting.SignalStrategy} signalStrategy Signal strategy.
17 * @constructor
19 remoting.LogToServer = function(signalStrategy) {
20 /** @private */
21 this.statsAccumulator_ = new remoting.StatsAccumulator();
22 /** @private */
23 this.sessionId_ = '';
24 /** @private */
25 this.sessionIdGenerationTime_ = 0;
26 /** @private */
27 this.sessionStartTime_ = new Date().getTime();
28 /** @private */
29 this.signalStrategy_ = signalStrategy;
30 /** @private {string} */
31 this.connectionType_ = '';
32 /** @private */
33 this.authTotalTime_ = 0;
35 this.setSessionId_();
36 signalStrategy.sendConnectionSetupResults(this);
39 // Constants used for generating a session ID.
40 /** @private */
41 remoting.LogToServer.SESSION_ID_ALPHABET_ =
42 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
43 /** @private */
44 remoting.LogToServer.SESSION_ID_LEN_ = 20;
46 // The maximum age of a session ID, in milliseconds.
47 remoting.LogToServer.MAX_SESSION_ID_AGE = 24 * 60 * 60 * 1000;
49 // The time over which to accumulate connection statistics before logging them
50 // to the server, in milliseconds.
51 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME = 60 * 1000;
53 /**
54 * Logs a client session state change.
56 * @param {remoting.ClientSession.State} state
57 * @param {!remoting.Error} connectionError
59 remoting.LogToServer.prototype.logClientSessionStateChange =
60 function(state, connectionError) {
61 this.maybeExpireSessionId_();
62 // Log the session state change.
63 var entry = remoting.ServerLogEntry.makeClientSessionStateChange(
64 state, connectionError);
65 entry.addHostFields();
66 entry.addChromeVersionField();
67 entry.addWebappVersionField();
68 entry.addSessionIdField(this.sessionId_);
69 this.log_(entry);
70 // Don't accumulate connection statistics across state changes.
71 this.logAccumulatedStatistics_();
72 this.statsAccumulator_.empty();
73 // Maybe clear the session ID.
74 if (remoting.LogToServer.isEndOfSession_(state)) {
75 this.clearSessionId_();
79 /**
80 * Set the connection type (direct, stun relay).
82 * @param {string} connectionType
84 remoting.LogToServer.prototype.setConnectionType = function(connectionType) {
85 this.connectionType_ = connectionType;
88 /**
89 * @param {remoting.SignalStrategy.Type} strategyType
90 * @param {remoting.FallbackSignalStrategy.Progress} progress
92 remoting.LogToServer.prototype.logSignalStrategyProgress =
93 function(strategyType, progress) {
94 this.maybeExpireSessionId_();
95 var entry = remoting.ServerLogEntry.makeSignalStrategyProgress(
96 this.sessionId_, strategyType, progress);
97 this.log_(entry);
101 * Whether a session state is one of the states that occurs at the start of
102 * a session.
104 * @private
105 * @param {remoting.ClientSession.State} state
106 * @return {boolean}
108 remoting.LogToServer.isStartOfSession_ = function(state) {
109 return ((state == remoting.ClientSession.State.CONNECTING) ||
110 (state == remoting.ClientSession.State.INITIALIZING) ||
111 (state == remoting.ClientSession.State.AUTHENTICATED) ||
112 (state == remoting.ClientSession.State.CONNECTED));
116 * Whether a session state is one of the states that occurs at the end of
117 * a session.
119 * @private
120 * @param {remoting.ClientSession.State} state
121 * @return {boolean}
123 remoting.LogToServer.isEndOfSession_ = function(state) {
124 return ((state == remoting.ClientSession.State.CLOSED) ||
125 (state == remoting.ClientSession.State.FAILED) ||
126 (state == remoting.ClientSession.State.CONNECTION_DROPPED) ||
127 (state == remoting.ClientSession.State.CONNECTION_CANCELED));
132 * Logs connection statistics.
133 * @param {Object<string, number>} stats The connection statistics
135 remoting.LogToServer.prototype.logStatistics = function(stats) {
136 this.maybeExpireSessionId_();
137 // Store the statistics.
138 this.statsAccumulator_.add(stats);
139 // Send statistics to the server if they've been accumulating for at least
140 // 60 seconds.
141 if (this.statsAccumulator_.getTimeSinceFirstValue() >=
142 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME) {
143 this.logAccumulatedStatistics_();
148 * Moves connection statistics from the accumulator to the log server.
150 * If all the statistics are zero, then the accumulator is still emptied,
151 * but the statistics are not sent to the log server.
153 * @private
155 remoting.LogToServer.prototype.logAccumulatedStatistics_ = function() {
156 var entry = remoting.ServerLogEntry.makeStats(this.statsAccumulator_,
157 this.connectionType_);
158 if (entry) {
159 entry.addHostFields();
160 entry.addChromeVersionField();
161 entry.addWebappVersionField();
162 entry.addSessionIdField(this.sessionId_);
163 this.log_(entry);
165 this.statsAccumulator_.empty();
169 * Sends a log entry to the server.
171 * @private
172 * @param {remoting.ServerLogEntry} entry
174 remoting.LogToServer.prototype.log_ = function(entry) {
175 // Log the time taken to get to this point from the time this session started.
176 // Exclude time taken for authorization.
177 var sessionDurationInSeconds =
178 (new Date().getTime() - this.sessionStartTime_ -
179 this.authTotalTime_) / 1000.0;
180 entry.addSessionDuration(sessionDurationInSeconds);
182 // Send the stanza to the debug log.
183 console.log('Enqueueing log entry:');
184 entry.toDebugLog(1);
186 var stanza = '<cli:iq to="' + remoting.settings.DIRECTORY_BOT_JID + '" ' +
187 'type="set" xmlns:cli="jabber:client">' +
188 '<gr:log xmlns:gr="google:remoting">' +
189 entry.toStanza() +
190 '</gr:log>' +
191 '</cli:iq>';
192 this.signalStrategy_.sendMessage(stanza);
196 * Sets the session ID to a random string.
198 * @private
200 remoting.LogToServer.prototype.setSessionId_ = function() {
201 this.sessionId_ = remoting.LogToServer.generateSessionId_();
202 this.sessionIdGenerationTime_ = new Date().getTime();
206 * Clears the session ID.
208 * @private
210 remoting.LogToServer.prototype.clearSessionId_ = function() {
211 this.sessionId_ = '';
212 this.sessionIdGenerationTime_ = 0;
216 * Sets a new session ID, if the current session ID has reached its maximum age.
218 * This method also logs the old and new session IDs to the server, in separate
219 * log entries.
221 * @private
223 remoting.LogToServer.prototype.maybeExpireSessionId_ = function() {
224 if ((this.sessionId_ != '') &&
225 (new Date().getTime() - this.sessionIdGenerationTime_ >=
226 remoting.LogToServer.MAX_SESSION_ID_AGE)) {
227 // Log the old session ID.
228 var entry = remoting.ServerLogEntry.makeSessionIdOld(this.sessionId_);
229 this.log_(entry);
230 // Generate a new session ID.
231 this.setSessionId_();
232 // Log the new session ID.
233 entry = remoting.ServerLogEntry.makeSessionIdNew(this.sessionId_);
234 this.log_(entry);
239 * Generates a string that can be used as a session ID.
241 * @private
242 * @return {string} a session ID
244 remoting.LogToServer.generateSessionId_ = function() {
245 var idArray = [];
246 for (var i = 0; i < remoting.LogToServer.SESSION_ID_LEN_; i++) {
247 var index =
248 Math.random() * remoting.LogToServer.SESSION_ID_ALPHABET_.length;
249 idArray.push(
250 remoting.LogToServer.SESSION_ID_ALPHABET_.slice(index, index + 1));
252 return idArray.join('');
256 * @param {number} totalTime The value of time taken to complete authorization.
257 * @return {void} Nothing.
259 remoting.LogToServer.prototype.setAuthTotalTime = function(totalTime) {
260 this.authTotalTime_ = totalTime;