Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Unified Diff: remoting/webapp/me2mom/log_to_server.js

Issue 8775066: The webapp uses localStorage to decide whether logging to the server (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/webapp/me2mom/log_to_server.js
diff --git a/remoting/webapp/me2mom/log_to_server.js b/remoting/webapp/me2mom/log_to_server.js
index d42bb7085d06583502aa2a2a8a4aae98ec833be2..017bc1afe21c0ce4816f1a8d4fcb05803e871f53 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));
Jamie 2011/12/02 20:44:20 Why not just: idArray.push(this.ID_ALPHABET_[in
simonmorris 2011/12/02 21:05:58 I did it this way only because join() joins string
+ }
+ return idArray.join('');
+};

Powered by Google App Engine
This is Rietveld 408576698