summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/me2mom/log_to_server.js
diff options
context:
space:
mode:
Diffstat (limited to 'remoting/webapp/me2mom/log_to_server.js')
-rw-r--r--remoting/webapp/me2mom/log_to_server.js74
1 files changed, 72 insertions, 2 deletions
diff --git a/remoting/webapp/me2mom/log_to_server.js b/remoting/webapp/me2mom/log_to_server.js
index d42bb70..017bc1a 100644
--- a/remoting/webapp/me2mom/log_to_server.js
+++ b/remoting/webapp/me2mom/log_to_server.js
@@ -17,9 +17,30 @@ var remoting = remoting || {};
*/
remoting.LogToServer = function() {
/** @type Array.<string> */ this.pendingEntries = [];
- /** @type boolean */ this.enabled = false;
};
+// Local storage keys.
+/** @private */
+remoting.LogToServer.prototype.KEY_ENABLED_ = 'remoting.LogToServer.enabled';
+/** @private */
+remoting.LogToServer.prototype.KEY_ID_ = 'remoting.LogToServer.id';
+
+// Constants used for generating an ID.
+/** @private */
+remoting.LogToServer.prototype.ID_ALPHABET_ =
+ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
+/** @private */
+remoting.LogToServer.prototype.ID_LEN_ = 20;
+
+/**
+ * Enables or disables logging.
+ *
+ * @param {boolean} enabled whether logging is enabled
+ */
+remoting.LogToServer.prototype.setEnabled = function(enabled) {
+ window.localStorage.setItem(this.KEY_ENABLED_, enabled ? 'true' : 'false');
+}
+
/**
* Logs a client session state change.
*
@@ -33,6 +54,7 @@ remoting.LogToServer.prototype.logClientSessionStateChange =
entry.addHostFields();
entry.addChromeVersionField();
entry.addWebappVersionField();
+ entry.addIdField(this.getId());
this.log(entry);
};
@@ -43,7 +65,7 @@ remoting.LogToServer.prototype.logClientSessionStateChange =
* @param {remoting.ServerLogEntry} entry
*/
remoting.LogToServer.prototype.log = function(entry) {
- if (!this.enabled) {
+ if (!this.isEnabled()) {
return;
}
// Store a stanza for the entry
@@ -62,3 +84,51 @@ remoting.LogToServer.prototype.log = function(entry) {
remoting.debug.logIq(true, stanza);
remoting.wcs.sendIq(stanza);
};
+
+/**
+ * Whether logging is enabled.
+ *
+ * @private
+ * @return {boolean} whether logging is enabled
+ */
+remoting.LogToServer.prototype.isEnabled = function() {
+ var value = window.localStorage.getItem(this.KEY_ENABLED_);
+ return (value == 'true');
+};
+
+/**
+ * Gets an ID from local storage.
+ *
+ * This function returns the empty string if logging is disabled.
+ * If logging is enabled, and there is no ID in local storage, then this
+ * function will create and store an ID.
+ *
+ * @private
+ * @return {string} an ID, or the empty string
+ */
+remoting.LogToServer.prototype.getId = function() {
+ if (!this.isEnabled()) {
+ return '';
+ }
+ var id = window.localStorage.getItem(this.KEY_ID_);
+ if ((!id) || (typeof id != 'string')) {
+ id = this.generateId();
+ window.localStorage.setItem(this.KEY_ID_, id);
+ }
+ return id.toString();
+};
+
+/**
+ * Generates an ID.
+ *
+ * @private
+ * @return {string} an ID
+ */
+remoting.LogToServer.prototype.generateId = function() {
+ var idArray = [];
+ for (var i = 0; i < this.ID_LEN_; i++) {
+ var index = Math.random() * this.ID_ALPHABET_.length;
+ idArray.push(this.ID_ALPHABET_.slice(index, index + 1));
+ }
+ return idArray.join('');
+};