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 * Module for sending log entries to the server.
12 /** @suppress {duplicate} */
13 var remoting
= remoting
|| {};
16 * @param {remoting.SignalStrategy} signalStrategy Signal strategy.
19 remoting
.LogToServer = function(signalStrategy
) {
21 this.statsAccumulator_
= new remoting
.StatsAccumulator();
25 this.sessionIdGenerationTime_
= 0;
27 this.sessionStartTime_
= new Date().getTime();
29 this.signalStrategy_
= signalStrategy
;
30 /** @private {string} */
31 this.connectionType_
= '';
33 this.authTotalTime_
= 0;
36 signalStrategy
.sendConnectionSetupResults(this);
39 // Constants used for generating a session ID.
41 remoting
.LogToServer
.SESSION_ID_ALPHABET_
=
42 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
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;
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_
);
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_();
80 * Set the connection type (direct, stun relay).
82 * @param {string} connectionType
84 remoting
.LogToServer
.prototype.setConnectionType = function(connectionType
) {
85 this.connectionType_
= connectionType
;
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
);
101 * Whether a session state is one of the states that occurs at the start of
105 * @param {remoting.ClientSession.State} state
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
120 * @param {remoting.ClientSession.State} state
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
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.
155 remoting
.LogToServer
.prototype.logAccumulatedStatistics_ = function() {
156 var entry
= remoting
.ServerLogEntry
.makeStats(this.statsAccumulator_
,
157 this.connectionType_
);
159 entry
.addHostFields();
160 entry
.addChromeVersionField();
161 entry
.addWebappVersionField();
162 entry
.addSessionIdField(this.sessionId_
);
165 this.statsAccumulator_
.empty();
169 * Sends a log entry to the server.
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:');
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">' +
192 this.signalStrategy_
.sendMessage(stanza
);
196 * Sets the session ID to a random string.
200 remoting
.LogToServer
.prototype.setSessionId_ = function() {
201 this.sessionId_
= remoting
.LogToServer
.generateSessionId_();
202 this.sessionIdGenerationTime_
= new Date().getTime();
206 * Clears the session ID.
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
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_
);
230 // Generate a new session ID.
231 this.setSessionId_();
232 // Log the new session ID.
233 entry
= remoting
.ServerLogEntry
.makeSessionIdNew(this.sessionId_
);
239 * Generates a string that can be used as a session ID.
242 * @return {string} a session ID
244 remoting
.LogToServer
.generateSessionId_ = function() {
246 for (var i
= 0; i
< remoting
.LogToServer
.SESSION_ID_LEN_
; i
++) {
248 Math
.random() * remoting
.LogToServer
.SESSION_ID_ALPHABET_
.length
;
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
;