OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Module for sending log entries to the server. | 7 * Module for sending log entries to the server. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
14 | 14 |
15 /** | 15 /** |
16 * @constructor | 16 * @constructor |
17 */ | 17 */ |
18 remoting.LogToServer = function() { | 18 remoting.LogToServer = function() { |
19 /** @type Array.<string> */ | 19 /** @type Array.<string> */ |
20 this.pendingEntries = []; | 20 this.pendingEntries = []; |
21 /** @type {remoting.StatsAccumulator} */ | 21 /** @type {remoting.StatsAccumulator} */ |
22 this.statsAccumulator = new remoting.StatsAccumulator(); | 22 this.statsAccumulator = new remoting.StatsAccumulator(); |
23 /** @type string */ | 23 /** @type string */ |
24 this.sessionId = ''; | 24 this.sessionId = ''; |
25 /** @type number */ | 25 /** @type number */ |
26 this.sessionIdGenerationTime = 0; | 26 this.sessionIdGenerationTime = 0; |
27 /** @type number */ | 27 /** @type number */ |
28 this.sessionStartTime = 0; | 28 this.sessionStartTime = 0; |
29 }; | 29 }; |
30 | 30 |
31 // Local storage key. | |
32 /** @private */ | |
33 remoting.LogToServer.KEY_ENABLED_ = 'remoting.LogToServer.enabled'; | |
34 | |
35 // Constants used for generating a session ID. | 31 // Constants used for generating a session ID. |
36 /** @private */ | 32 /** @private */ |
37 remoting.LogToServer.SESSION_ID_ALPHABET_ = | 33 remoting.LogToServer.SESSION_ID_ALPHABET_ = |
38 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | 34 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; |
39 /** @private */ | 35 /** @private */ |
40 remoting.LogToServer.SESSION_ID_LEN_ = 20; | 36 remoting.LogToServer.SESSION_ID_LEN_ = 20; |
41 | 37 |
42 // The maximum age of a session ID, in milliseconds. | 38 // The maximum age of a session ID, in milliseconds. |
43 remoting.LogToServer.MAX_SESSION_ID_AGE = 24 * 60 * 60 * 1000; | 39 remoting.LogToServer.MAX_SESSION_ID_AGE = 24 * 60 * 60 * 1000; |
44 | 40 |
45 // The time over which to accumulate connection statistics before logging them | 41 // The time over which to accumulate connection statistics before logging them |
46 // to the server, in milliseconds. | 42 // to the server, in milliseconds. |
47 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME = 60 * 1000; | 43 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME = 60 * 1000; |
48 | 44 |
49 /** | 45 /** |
50 * Enables or disables logging. | |
51 * | |
52 * @param {boolean} enabled whether logging is enabled | |
53 */ | |
54 remoting.LogToServer.prototype.setEnabled = function(enabled) { | |
55 window.localStorage.setItem(remoting.LogToServer.KEY_ENABLED_, | |
56 enabled ? 'true' : 'false'); | |
57 } | |
58 | |
59 /** | |
60 * Logs a client session state change. | 46 * Logs a client session state change. |
61 * | 47 * |
62 * @param {remoting.ClientSession.State} state | 48 * @param {remoting.ClientSession.State} state |
63 * @param {remoting.ClientSession.ConnectionError} connectionError | 49 * @param {remoting.ClientSession.ConnectionError} connectionError |
64 */ | 50 */ |
65 remoting.LogToServer.prototype.logClientSessionStateChange = | 51 remoting.LogToServer.prototype.logClientSessionStateChange = |
66 function(state, connectionError) { | 52 function(state, connectionError) { |
67 this.maybeExpireSessionId(); | 53 this.maybeExpireSessionId(); |
68 // Maybe set the session ID and start time. | 54 // Maybe set the session ID and start time. |
69 if (remoting.LogToServer.isStartOfSession(state)) { | 55 if (remoting.LogToServer.isStartOfSession(state)) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 this.statsAccumulator.empty(); | 147 this.statsAccumulator.empty(); |
162 }; | 148 }; |
163 | 149 |
164 /** | 150 /** |
165 * Sends a log entry to the server. | 151 * Sends a log entry to the server. |
166 * | 152 * |
167 * @private | 153 * @private |
168 * @param {remoting.ServerLogEntry} entry | 154 * @param {remoting.ServerLogEntry} entry |
169 */ | 155 */ |
170 remoting.LogToServer.prototype.log = function(entry) { | 156 remoting.LogToServer.prototype.log = function(entry) { |
171 if (!this.isEnabled()) { | |
172 return; | |
173 } | |
174 // Send the stanza to the debug log. | 157 // Send the stanza to the debug log. |
175 remoting.debug.log('Enqueueing log entry:'); | 158 remoting.debug.log('Enqueueing log entry:'); |
176 entry.toDebugLog(1); | 159 entry.toDebugLog(1); |
177 // Store a stanza for the entry. | 160 // Store a stanza for the entry. |
178 this.pendingEntries.push(entry.toStanza()); | 161 this.pendingEntries.push(entry.toStanza()); |
179 // Stop if there's no connection to the server. | 162 // Stop if there's no connection to the server. |
180 if (!remoting.wcs) { | 163 if (!remoting.wcs) { |
181 return; | 164 return; |
182 } | 165 } |
183 // Send all pending entries to the server. | 166 // Send all pending entries to the server. |
184 remoting.debug.log('Sending ' + this.pendingEntries.length + ' log ' + | 167 remoting.debug.log('Sending ' + this.pendingEntries.length + ' log ' + |
185 ((this.pendingEntries.length == 1) ? 'entry' : 'entries') + | 168 ((this.pendingEntries.length == 1) ? 'entry' : 'entries') + |
186 ' to the server.'); | 169 ' to the server.'); |
187 var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + | 170 var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + |
188 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; | 171 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; |
189 while (this.pendingEntries.length > 0) { | 172 while (this.pendingEntries.length > 0) { |
190 stanza += /** @type string */ this.pendingEntries.shift(); | 173 stanza += /** @type string */ this.pendingEntries.shift(); |
191 } | 174 } |
192 stanza += '</gr:log></cli:iq>'; | 175 stanza += '</gr:log></cli:iq>'; |
193 remoting.wcs.sendIq(stanza); | 176 remoting.wcs.sendIq(stanza); |
194 }; | 177 }; |
195 | 178 |
196 /** | 179 /** |
197 * Whether logging is enabled. | |
198 * | |
199 * @private | |
200 * @return {boolean} whether logging is enabled | |
201 */ | |
202 remoting.LogToServer.prototype.isEnabled = function() { | |
203 var value = window.localStorage.getItem(remoting.LogToServer.KEY_ENABLED_); | |
204 return (value == 'true'); | |
205 }; | |
206 | |
207 /** | |
208 * Sets the session ID to a random string. | 180 * Sets the session ID to a random string. |
209 * | 181 * |
210 * @private | 182 * @private |
211 */ | 183 */ |
212 remoting.LogToServer.prototype.setSessionId = function() { | 184 remoting.LogToServer.prototype.setSessionId = function() { |
213 this.sessionId = remoting.LogToServer.generateSessionId(); | 185 this.sessionId = remoting.LogToServer.generateSessionId(); |
214 this.sessionIdGenerationTime = new Date().getTime(); | 186 this.sessionIdGenerationTime = new Date().getTime(); |
215 }; | 187 }; |
216 | 188 |
217 /** | 189 /** |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 remoting.LogToServer.generateSessionId = function() { | 228 remoting.LogToServer.generateSessionId = function() { |
257 var idArray = []; | 229 var idArray = []; |
258 for (var i = 0; i < remoting.LogToServer.SESSION_ID_LEN_; i++) { | 230 for (var i = 0; i < remoting.LogToServer.SESSION_ID_LEN_; i++) { |
259 var index = | 231 var index = |
260 Math.random() * remoting.LogToServer.SESSION_ID_ALPHABET_.length; | 232 Math.random() * remoting.LogToServer.SESSION_ID_ALPHABET_.length; |
261 idArray.push( | 233 idArray.push( |
262 remoting.LogToServer.SESSION_ID_ALPHABET_.slice(index, index + 1)); | 234 remoting.LogToServer.SESSION_ID_ALPHABET_.slice(index, index + 1)); |
263 } | 235 } |
264 return idArray.join(''); | 236 return idArray.join(''); |
265 }; | 237 }; |
OLD | NEW |