[Pkg-mozext-commits] [wot] 189/226: Categories, search rules, keeper and shared hosts are stored in the local storage

David Prévot taffit at moszumanska.debian.org
Fri May 1 00:35:50 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository wot.

commit ce4e95490870795f0613dc70b8d0e7246f722749
Author: Sergey Andryukhin <sorgoz at yandex.com>
Date:   Mon Dec 9 20:13:56 2013 +0200

    Categories, search rules, keeper and shared hosts are stored in the local storage
---
 content/api.js        | 117 +++++++++++++++++++++++----------
 content/cache.js      |   4 +-
 content/categories.js |  77 ++++++++++------------
 content/core.js       |  66 ++++++++++---------
 content/overlay.xul   |   3 +-
 content/prefs.js      |   6 +-
 content/search.js     |  79 +++++++++++++---------
 content/shared.js     |  27 ++++----
 content/storage.js    | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++
 content/util.js       |   5 +-
 10 files changed, 399 insertions(+), 163 deletions(-)

diff --git a/content/api.js b/content/api.js
index 160a1c7..d4fdbb6 100644
--- a/content/api.js
+++ b/content/api.js
@@ -81,11 +81,11 @@ var wot_api_link =
 							request.responseXML.getElementsByTagName(
 								WOT_SERVICE_XML_QUERY_TARGET),
 							true);
-			
+
 						var cache = {};
 						var retry = {};
 						var hasretries = false;
-					
+
 						for (var i = 0; i < batch.length; ++i) {
 							var s = wot_cache.get(batch[i], "status");
 
@@ -284,7 +284,7 @@ var wot_api_query =
 			wot_cache.set(hostname, "time", Date.now());
 			wot_cache.set(hostname, "inprogress", false);
 			wot_core.update();
-			
+
 			if (typeof(callback) == "function") {
 				callback();
 			}
@@ -724,7 +724,7 @@ var wot_api_submit =
 			if (!found) return;
 
 			qs += wot_url.getapiparams();
-			   
+
 			var request = new XMLHttpRequest();
 
 			if (!request) return;
@@ -855,7 +855,7 @@ var wot_api_update =
 			}
 
 			wot_prefs.flush();
-			
+
 			/* Build a request */
 			var request = new XMLHttpRequest();
 
@@ -1129,6 +1129,8 @@ var wot_keeper = {
         SUBMITTING: 2   // indicates the saving is temporary until submition is reported succesful
     },
 
+	STORAGE_NAME: "keeper",
+
     /* Comment-specific methods to work with Keeper */
 
     get_comment: function (target) {
@@ -1163,10 +1165,9 @@ var wot_keeper = {
 
     get_by_name: function (target, name) {
         // generic method to get data from local by target and name
-//        console.log("keeper.get_by_name()", target, name);
-
         try {
-            return wot_prefs.getJSON(wot_keeper._fullname(target, name)) || null;
+	        var keeper_data = wot_storage.get(this.STORAGE_NAME, {});
+            return keeper_data[wot_keeper._fullname(target, name)] || null;
         } catch (e) {
             wdump("wot_keeper.get_by_name() Failed with " + e);
         }
@@ -1175,20 +1176,56 @@ var wot_keeper = {
 
     store_by_name: function (target, name, obj) {
 //        console.log("keeper.store_by_name()", target, name, data);
-        wot_prefs.setJSON(wot_keeper._fullname(target, name), obj);
+	    var keeper_data = wot_storage.get(this.STORAGE_NAME, {});
+	    keeper_data[wot_keeper._fullname(target, name)] = obj;
+	    wot_storage.set(this.STORAGE_NAME, keeper_data, true);
     },
 
     remove_by_name: function (target, name) {
-//        wdump("keeper.remove_by_name()" + target + " " + name);
-        wot_prefs.clear(wot_keeper._fullname(target, name));
+	    var keeper_data = wot_storage.get(this.STORAGE_NAME, {});
+	    keeper_data[wot_keeper._fullname(target, name)] = undefined;
+	    wot_storage.set(this.STORAGE_NAME, keeper_data, true);
     },
 
     _fullname: function (target, name) {
-        return "keeper." + name + "." + target;
-    }
+        return name + "." + target;
+    },
+
+	move_from_prefs_to_storage: function () {
+		var branches = {
+			"keeper.": true
+		};
+
+		var keeper_data = wot_storage.get(this.STORAGE_NAME, {});
+
+		for (var b in branches) {
+
+			try {
+				var branch = wot_prefs.ps.getBranch(WOT_PREF + b);
+				var items = branch.getChildList("", {});
+				for (var i = 0; i < items.length; i++) {
+					var subname = items[i];
+					keeper_data[subname] = wot_prefs.getJSON("keeper." + items[i]) || null;
+				}
+
+				wot_storage.set(this.STORAGE_NAME, keeper_data);
+				wot_prefs.deleteBranch(b);
+
+			} catch (e) {
+				wdump("move_from_prefs_to_storage() / ["+b+"] failed with " + e);
+			}
+		}
+		wot_storage.flush();
+	},
+
+	load_delayed: function () {
+		this.move_from_prefs_to_storage();
+	}
 
 };
 
+wot_modules.push({ name: "wot_keeper", obj: wot_keeper });
+
 var wot_api_comments = {
     server: "www.mywot.com",
     version: "1",   // Comments API version
@@ -1349,7 +1386,7 @@ var wot_api_comments = {
             pref_pending_name = _this.PENDING_COMMENT_SID + target;
 
         // try to restore pending submission first
-        var state = wot_prefs.getJSON(pref_pending_name, {
+        var state = wot_storage.get(pref_pending_name, {
 	        target: target,
 	        comment_data: {},
 	        tries: 0
@@ -1365,11 +1402,11 @@ var wot_api_comments = {
 
         if (++state.tries > _this.MAX_TRIES) {
             wdump("FAIL: api.comments.submit: failed " + target + " (max tries)");
-            wot_prefs.clear(pref_pending_name);
+	        wot_storage.clear(pref_pending_name);
             return;
         }
 
-        wot_prefs.setJSON(pref_pending_name, state);    // remember the submission
+	    wot_storage.set(pref_pending_name, state);    // remember the submission
 
         state.comment_data['target'] = target;
 
@@ -1386,7 +1423,7 @@ var wot_api_comments = {
                     wot_api_comments.retry("submit", [ target ]);
                 } else {
                     wdump("api.comment.submit: failed " + target + " (403)");
-                    wot_prefs.clear(wot_api_comments.PENDING_COMMENT_SID + target);
+	                wot_storage.clear(wot_api_comments.PENDING_COMMENT_SID + target);
                 }
             },
             wot_api_comments.on_submit_comment_response
@@ -1407,18 +1444,18 @@ var wot_api_comments = {
             pref_pending_name = _this.PENDING_REMOVAL_SID + target;
 
         // try to restore pending submission first
-        var state = wot_prefs.getJSON(pref_pending_name, {
+        var state = wot_storage.get(pref_pending_name, {
             target: target,
             tries: 0
         });
 
         if (++state.tries > _this.MAX_TRIES) {
             wdump("api.comments.submit: failed " + target + " (max tries)");
-            wot_prefs.clear(pref_pending_name);
+	        wot_storage.clear(pref_pending_name);
             return;
         }
 
-        wot_prefs.setJSON(pref_pending_name, state);    // remember the submission
+	    wot_storage.set(pref_pending_name, state);    // remember the submission
 
         _this.call("remove",
             {
@@ -1434,7 +1471,7 @@ var wot_api_comments = {
                     wot_api_comments.retry("remove", [ target ]);
                 } else {
                     wdump("api.comment.remove: failed " + target + " (403)");
-                    wot_prefs.clear(wot_api_comments.PENDING_REMOVAL_SID + target);
+	                wot_storage.clear(wot_api_comments.PENDING_REMOVAL_SID + target);
                 }
             },
             wot_api_comments.on_remove_comment_response
@@ -1458,17 +1495,25 @@ var wot_api_comments = {
         branches[this.PENDING_COMMENT_SID] = wot_api_comments.submit;
         branches[this.PENDING_REMOVAL_SID] = wot_api_comments.remove;
 
-        for (var b in branches) {
-            try {
-                var comments_pending_branch = wot_prefs.ps.getBranch(WOT_PREF + b);
-                var comments_pending = comments_pending_branch.getChildList("", {});
-                for (var i = 0; i < comments_pending.length; i++) {
-                    branches[b].apply(wot_api_comments, [comments_pending[i]]); // call the proper function and provide it a target hostname
-                }
-            } catch (e) {
-                wdump("wot_api_comments.processpending() / ["+b+"] failed with " + e);
-            }
-        }
+	    var bag = wot_hashtable.get_enumerator();
+
+	    // go through stored in memory values and keep ones that belong to Storage
+	    while (bag.hasMoreElements()) {
+		    var name = wot_storage.get_name_from_element(bag.getNext());
+		    if (name && name != "is_loaded" && name != "flushed") {
+			    for (var b in branches) {
+				    try {
+					    if (name.indexOf(b) == 0) {
+						    var target = name.slice(b.length);
+						    branches[b].apply(wot_api_comments, [target]);
+					    }
+
+				    } catch (e) {
+					    wdump("wot_api_comments.processpending() / ["+b+"] failed with " + e);
+				    }
+			    }
+		    }
+	    }
     },
 
     pull_nonce: function (nonce) {
@@ -1542,7 +1587,7 @@ var wot_api_comments = {
             case WOT_COMMENTS.error_codes.SUCCESS:
                 wot_keeper.remove_comment(target);  // delete the locally saved comment only on successful submit
                 wot_cache.update_comment(target, { status: WOT_QUERY_OK, error_code: error_code });
-                wot_prefs.clear(_this.PENDING_COMMENT_SID + target); // don't try to send again
+                wot_storage.clear(_this.PENDING_COMMENT_SID + target); // don't try to send again
                 break;
 
             // for these errors we should try again, because there is non-zero possibility of quantum glitches around
@@ -1555,7 +1600,7 @@ var wot_api_comments = {
 
             default:
                 wot_cache.update_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
-                wot_prefs.clear(_this.PENDING_COMMENT_SID + target);
+	            wot_storage.clear(_this.PENDING_COMMENT_SID + target);
         }
 
         wot_cache.set_captcha(!!data.captcha);
@@ -1575,7 +1620,7 @@ var wot_api_comments = {
             case WOT_COMMENTS.error_codes.SUCCESS:
                 wot_cache.remove_comment(target);
                 wot_keeper.remove_comment(target);
-                wot_prefs.clear(_this.PENDING_REMOVAL_SID + target);
+	            wot_storage.clear(_this.PENDING_REMOVAL_SID + target);
                 break;
 
             // some errors require retry due to singularity of the Universe
@@ -1588,7 +1633,7 @@ var wot_api_comments = {
 
             default:
                 wot_cache.update_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
-                wot_prefs.clear(_this.PENDING_REMOVAL_SID + target);
+	            wot_storage.clear(_this.PENDING_REMOVAL_SID + target);
         }
 
         wot_rw.update_ratingwindow_comment(); // to update status "the website is commented by the user"
diff --git a/content/cache.js b/content/cache.js
index 8ea1d4c..ae5bb42 100644
--- a/content/cache.js
+++ b/content/cache.js
@@ -20,7 +20,7 @@
 
 var wot_hashtable =
 {
-	load_delayed: function()
+	load: function()
 	{
 		try {
 			if (this.bag) {
@@ -268,7 +268,7 @@ var wot_cache =
 				if (!pending) {
 					this.set(name, "testimony_" + a, -1);
 				}
-				
+
 				this.set(name, "inherited_" + a, 0);
 				this.set(name, "lowered_" + a, 0);
 			}
diff --git a/content/categories.js b/content/categories.js
index c8f7098..fddb947 100644
--- a/content/categories.js
+++ b/content/categories.js
@@ -36,32 +36,32 @@ var wot_categories = {
         if (this.inited) return;
 
         this.init_categories();
-        this.pbi = wot_prefs.pref.QueryInterface(Components.interfaces.nsIPrefBranch2);
-        this.pbi.addObserver(WOT_PREF + this.PREF_CATEGORY, this, false);
+//        this.pbi = wot_prefs.pref.QueryInterface(Components.interfaces.nsIPrefBranch2);
+//        this.pbi.addObserver(WOT_PREF + this.PREF_CATEGORY, this, false);
         this.inited = true;
     },
 
     unload: function () {
         try {
-            if (this.pbi) {
-                this.pbi.removeObserver(WOT_PREF + this.PREF_CATEGORY, this);
-                this.pbi = null;
-            }
+//            if (this.pbi) {
+//                this.pbi.removeObserver(WOT_PREF + this.PREF_CATEGORY, this);
+//                this.pbi = null;
+//            }
         } catch (e) {
             wdump("wot_categories.unload: failed with " + e);
         }
     },
 
-    observe: function (subject, topic, state) {
-        // see load_delayed(). This function is used as a listener to
-        try {
-            if (!this.loading && topic == "nsPref:changed") {
-                this.init_categories();
-            }
-        } catch (e) {
-            wdump("wot_search.observe: failed with " + e);
-        }
-    },
+//    observe: function (subject, topic, state) {
+//        // see load_delayed(). This function is used as a listener to
+//        try {
+//            if (!this.loading && topic == "nsPref:changed") {
+//                this.init_categories();
+//            }
+//        } catch (e) {
+//            wdump("wot_search.observe: failed with " + e);
+//        }
+//    },
 
     parse: function (categories_node) {
 //        wdump("INFO: parse() categories");
@@ -70,7 +70,7 @@ var wot_categories = {
         var i, j, gs_obj, cat_obj, res_grouping = [];
 
         var groupings = categories_node.getElementsByTagName("grouping");
-        wot_prefs.deleteBranch(this.PREF_GROUPINGS + ".");
+        wot_prefs.deleteBranch(this.PREF_GROUPINGS + ".");          // remove old pref
         if (groupings) {
             for (j = 0; j < groupings.length; j++) {
                 gs_obj = wot_util.copy_attrs(groupings[j]);
@@ -93,14 +93,17 @@ var wot_categories = {
 
                 res_grouping.push(gs_obj);
             }
-            wot_prefs.setChar(this.PREF_GROUPINGS + ".all", JSON.stringify(res_grouping), true);
+//            wot_prefs.setChar(this.PREF_GROUPINGS + ".all", JSON.stringify(res_grouping), true);
+            wot_storage.set(this.PREF_GROUPINGS + ".all", res_grouping, false);
         }
 
         // remove all categories from prefs
-        wot_prefs.deleteBranch(this.PREF_CATEGORY + ".");
+        wot_prefs.deleteBranch(this.PREF_CATEGORY + ".");          // remove old pref
 
         // Iterate through <category> tags
-        var categories = categories_node.getElementsByTagName("category");
+        var categories = categories_node.getElementsByTagName("category"),
+	        cats = {};
+
         for (i = 0; i < categories.length; i++) {
             cat_obj = wot_util.copy_attrs(categories[i]);
             if (isNaN(cat_obj.name) || cat_obj.text == null || cat_obj.text.length == 0) {
@@ -111,10 +114,11 @@ var wot_categories = {
             cat_obj.id = parseInt(cat_obj.name);
             cat_obj.cs = (cat_obj.application == "4");               // set ChildSafety flag
             cat_obj.type = this.cgroups[cat_obj.group].type; // set type of the category based on parent group
-
-            wot_prefs.setChar(this.PREF_CATEGORY + "." + cat_obj.name, JSON.stringify(cat_obj), true); // using utf8
+	        cats[cat_obj.id] = cat_obj;
         }
 
+	    wot_storage.set(this.PREF_CATEGORY, cats, false);
+
 	    // Iterage through <conflict> tag that describes how categories may conflict with each other.
 	    var conflicts_node = categories_node.getElementsByTagName("conflict"),
 		    conflicts = [];
@@ -133,11 +137,14 @@ var wot_categories = {
 		    conflicts.push(conflict_obj);
 	    }
 
-	    wot_prefs.setJSON(this.PREF_CONFLICTS, conflicts);      // save parsed results to preferences storage
+	    wot_prefs.deleteBranch(this.PREF_CONFLICTS);          // remove old pref
+	    wot_storage.set(this.PREF_CONFLICTS, conflicts, false);      // save parsed results to preferences storage
 
         this.init_categories();
 
         this.loading = false;
+
+	    wot_storage.flush(true);    // save storage's data
     },
 
     init_categories: function () {
@@ -145,33 +152,15 @@ var wot_categories = {
         /* Reads categories info from local preferences */
 
         try {
-            var branch = wot_prefs.ps.getBranch(WOT_PREF + this.PREF_CATEGORY + ".");
-            var children = branch.getChildList("", {});
-
-            this.categories = {};   // clear categories in memory
-
-            for (var i = 0; i < children.length; i++) {
-                try {
-                    var cat_id = children[i];
-                    var cat_json = wot_prefs.getChar(this.PREF_CATEGORY + "." + cat_id, "{}", true); // using utf8
-                    var cat = JSON.parse(cat_json);
-                    if (!wot_util.isEmpty(cat)) this.categories[cat_id] = cat;
-                } catch (e) {
-                    wdump("ERROR: exception in wot_categories.init_categories()" + e);
-                    wdump("ERROR: cat id:" + cat_id);
-                    wdump("ERROR: problematic string: " + cat_json);
-                    continue;
-                }
-            }
 
-            var groupings_json = wot_prefs.getChar(this.PREF_GROUPINGS + ".all", "{}", true); //using utf8
-            this.grouping = JSON.parse(groupings_json);
+            this.categories = wot_storage.get(this.PREF_CATEGORY, {});   // clear categories in memory
+            this.grouping = wot_storage.get(this.PREF_GROUPINGS + ".all", {});
+	        var conflicts = wot_storage.get(this.PREF_CONFLICTS, []);
 
 	        // Init proper structure for categories conflicts rules
 	        this.cat_combinations = {};
 	        this.cat_combinations_prio = ["6a"];    // first default value
 
-	        var conflicts = wot_prefs.getJSON(this.PREF_CONFLICTS, []);
 	        for (i = 0; i < conflicts.length; i++) {
 		        var conflict = conflicts[i],
 			        rule = String(conflict.rule).toLowerCase();
diff --git a/content/core.js b/content/core.js
index 08e98c4..ec64b34 100644
--- a/content/core.js
+++ b/content/core.js
@@ -151,38 +151,42 @@ var wot_core =
 				}
 			}
 
-			window.setTimeout(function() {
-				for (var i in wot_modules) {
-					if (typeof(wot_modules[i].obj.load_delayed) == "function") {
-						wot_modules[i].obj.load_delayed();
+			wot_storage.init(function() {   // continue initialization only after the storage is loaded
+				// schedule delayed load of modules ()
+				window.setTimeout(function() {
+					for (var i in wot_modules) {
+						if (typeof(wot_modules[i].obj.load_delayed) == "function") {
+							wot_modules[i].obj.load_delayed();
+						}
 					}
-				}
 
-				wot_prefs.setupdateui();
-				wot_api_register.send();
-				wot_my_session.update(true);
+					wot_prefs.setupdateui();
+					wot_api_register.send();
+					wot_my_session.update(true);
 
-				wot_core.loaded = true;
-				wot_core.update();
-			}, 250);
+					wot_core.loaded = true;
+					wot_core.update();
+				}, 250);
 
-			try {
-				Components.utils.import("resource://gre/modules/AddonManager.jsm");
-				AddonManager.addAddonListener(wot_core.install_listener);
-			} catch (e) {
-				dump("wot_core.load() setting up uninstall listener failed with " + e + "\n");
-			}
+				try {
+					Components.utils.import("resource://gre/modules/AddonManager.jsm");
+					AddonManager.addAddonListener(wot_core.install_listener);
+				} catch (e) {
+					dump("wot_core.load() setting up uninstall listener failed with " + e + "\n");
+				}
 
-			var browser = getBrowser();
-			this.listener = new wot_listener(browser);
-			browser.addProgressListener(this.listener);
+				var browser = getBrowser();
+				this.listener = new wot_listener(browser);
+				browser.addProgressListener(this.listener);
+
+				if (browser.tabContainer) {
+					browser.tabContainer.addEventListener("TabOpen",
+						wot_core.tabopen, false);
+					browser.tabContainer.addEventListener("TabSelect",
+						wot_core.tabselect, false);
+				}
+			});
 
-			if (browser.tabContainer) {
-				browser.tabContainer.addEventListener("TabOpen",
-					wot_core.tabopen, false);
-				browser.tabContainer.addEventListener("TabSelect",
-					wot_core.tabselect, false);
-			}
 		} catch (e) {
 			dump("wot_core.load: failed with " + e + "\n");
 		}
@@ -336,7 +340,7 @@ var wot_core =
 						}
 					}
 				}
-				
+
 				pl.abort(request);
 			}
 
@@ -410,7 +414,7 @@ var wot_core =
 			if (!wot_util.isenabled() || !pl || !pl.browser || !url) {
 				return;
 			}
-			
+
 			if (!wot_warning.isblocking()) {
 				return;
 			}
@@ -468,7 +472,7 @@ var wot_core =
 				if (!hostname || !wot_cache.iscached(hostname)) {
 					continue;
 				}
-				
+
 				var age = Date.now() - wot_cache.get(hostname, "time");
 
 				if (wot_cache.get(hostname, "status") == WOT_QUERY_ERROR &&
@@ -549,7 +553,7 @@ var wot_core =
 				if (wot_cache.get(name, "inprogress")) {
 					continue;
 				}
-				
+
 				var age = now - wot_cache.get(name, "time");
 
 				if (age > interval) {
@@ -666,7 +670,7 @@ var wot_core =
 				wot_api_query.send(wot_core.hostname);
 				return;
 			}
-			
+
 			var age = Date.now() - wot_cache.get(wot_core.hostname, "time");
 
 			if (wot_cache.get(wot_core.hostname, "inprogress")) {
diff --git a/content/overlay.xul b/content/overlay.xul
index ff93f5d..fef11bf 100644
--- a/content/overlay.xul
+++ b/content/overlay.xul
@@ -26,14 +26,15 @@
          xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 
     <script type="application/x-javascript" src="chrome://wot/content/config.js"/>
-    <script type="application/x-javascript" src="chrome://wot/content/categories.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/cache.js"/>
+    <script type="application/x-javascript" src="chrome://wot/content/categories.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/commands.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/shared.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/util.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/ratingwindow.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/ui.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/prefs.js"/>
+    <script type="application/x-javascript" src="chrome://wot/content/storage.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/my.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/warning.js"/>
     <script type="application/x-javascript" src="chrome://wot/content/surveys.js"/>
diff --git a/content/prefs.js b/content/prefs.js
index 52cdfad..0368347 100644
--- a/content/prefs.js
+++ b/content/prefs.js
@@ -260,9 +260,9 @@ var wot_prefs =
     setSmart: function (name, value) {
         // Looks up through preferences names and call the proper function to set the value of the named preference
         var prefs_sets = [
-            [wot_prefs_char, wot_prefs.setChar],
-            [wot_prefs_int, wot_prefs.setInt ],
-            [wot_prefs_bool, wot_prefs.setBool]
+            [ wot_prefs_char, wot_prefs.setChar ],
+            [ wot_prefs_int, wot_prefs.setInt ],
+            [ wot_prefs_bool, wot_prefs.setBool ]
         ];
 
         for (var s = 0; s < prefs_sets.length; s++) {
diff --git a/content/search.js b/content/search.js
index 5404b95..380439a 100644
--- a/content/search.js
+++ b/content/search.js
@@ -75,27 +75,20 @@ var wot_search =
 
 	setint: function(entry, value)
 	{
-		try {
-			if (value != null) {
-				return wot_prefs.setInt(entry, Number(value));
-			} else {
-				wot_prefs.clear(entry);
-			}
-		} catch (e) {
-			dump("wot_search.setint: failed with " + e + "\n");
-		}
-
-		return false;
+		return this.setchar(entry, value, true);
 	},
 
-	setchar: function(entry, value)
+	setchar: function(entry, value, is_number)
 	{
 		try {
-			if (value != null) {
-				return wot_prefs.setChar(entry, value);
+			var sr = wot_storage.get("search", {});
+			if (value !== null && value !== undefined) {
+				sr[entry] = value;
 			} else {
-				wot_prefs.clear(entry);
+				delete sr[entry];
 			}
+			wot_storage.set("search", sr);
+			return true;
 		} catch (e) {
 			dump("wot_search.setchar: failed with " + e + "\n");
 		}
@@ -103,6 +96,13 @@ var wot_search =
 		return false;
 	},
 
+	getrule: function (entry, def_value) {
+		// extend entry name to fully qualified (prepended with "search.")
+		var fullentry = entry.indexOf("search") == 0 ? entry : "search." + entry,
+			val = this._searchrules[fullentry];
+		return val === undefined ? def_value : val;
+	},
+
 	parsecontentrules: function(base, child)
 	{
 		try {
@@ -125,12 +125,15 @@ var wot_search =
 							node.getAttribute(WOT_SEARCH_CONTENT_RE))) {
 						++attr;
 					} else {
-						wot_prefs.deleteBranch(key);
+//						wot_prefs.deleteBranch(key);
+						this.setchar(key + WOT_SEARCH_CONTENT_FLAGS, null);
+						this.setchar(key + WOT_SEARCH_CONTENT_NAME, null);
+						this.setchar(key + WOT_SEARCH_CONTENT_RE, null);
 					}
 				} else if (node.nodeName == WOT_SEARCH_CONTENT_VALUE) {
 					/* Value rule */
 					var key = base + WOT_SEARCH_CONTENT_VALUE + value + ".";
-					
+
 					/* Flags (optional) */
 					this.setchar(key + WOT_SEARCH_CONTENT_FLAGS,
 						node.getAttribute(WOT_SEARCH_CONTENT_FLAGS));
@@ -140,7 +143,9 @@ var wot_search =
 							node.getAttribute(WOT_SEARCH_CONTENT_RE))) {
 						++value;
 					} else {
-						wot_prefs.deleteBranch(key);
+//						wot_prefs.deleteBranch(key);
+						this.setchar(key + WOT_SEARCH_CONTENT_FLAGS, null);
+						this.setchar(key + WOT_SEARCH_CONTENT_RE, null);
 					}
 				}
 
@@ -204,7 +209,9 @@ var wot_search =
 					child.getAttribute(WOT_SEARCH_PRE_MATCH))) {
 				return true;
 			} else {
-				wot_prefs.deleteBranch(entry);
+//				wot_prefs.deleteBranch(entry);
+				this.setchar(entry + WOT_SEARCH_PRE_RE, null);
+				this.setchar(entry + WOT_SEARCH_PRE_MATCH, null);
 			}
 		} catch (e) {
 			dump("wot_search.parseprerule: failed with " + e + "\n");
@@ -287,11 +294,14 @@ var wot_search =
 		try {
 			this.loading = true;
 
+			wot_storage.set("search", {});  // clear stored search rules and start from scratch
+
 			for (var i = 0; i < search.length; ++i) {
 				this.parserule(search[i]);
 			}
-
+			wot_storage.flush(true);
 			this.sync();
+
 		} catch (e) {
 			dump("wot_search.parse: failed with " + e + "\n");
 		}
@@ -325,7 +335,7 @@ var wot_search =
 				node[name][index] =
 					this.loadruletree(node[name][index] || {}, pref, m[4]);
 			} else {
-				node[next] = wot_prefs.getChar(pref, "");
+				node[next] = this.getrule(pref, "");
 			}
 
 			return node;
@@ -344,7 +354,7 @@ var wot_search =
 				condition: "and"
 			};
 
-			this.rules[name][attr].match[index] = 
+			this.rules[name][attr].match[index] =
 				this.loadruletree(this.rules[name][attr].match[index] || {},
 					pref, next);
 		} catch (e) {
@@ -359,9 +369,9 @@ var wot_search =
 			this.rules[name].pre[index] = this.rules[name].pre[index] || {};
 
 			if (attr == WOT_SEARCH_PRE_MATCH) {
-				this.rules[name].pre[index][attr] = wot_prefs.getInt(pref, 0);
+				this.rules[name].pre[index][attr] = this.getrule(pref, 0);
 			} else if (attr == WOT_SEARCH_PRE_RE) {
-				this.rules[name].pre[index][attr] = wot_prefs.getChar(pref, "");
+				this.rules[name].pre[index][attr] = this.getrule(pref, "");
 			}
 		} catch (e) {
 			dump("wot_search.loadprerule: failed with " + e + "\n");
@@ -405,11 +415,11 @@ var wot_search =
 					this.loadruletree(this.rules[name].target || {},
 						pref, next);
 			} else if (this.attrint.indexOf(attr) >= 0) {
-				this.rules[name][attr] = wot_prefs.getInt(pref, 0);
+				this.rules[name][attr] = this.getrule(pref, 0);
 			} else if (this.attrstr.indexOf(attr) >= 0) {
-				this.rules[name][attr] = wot_prefs.getChar(pref, "");
+				this.rules[name][attr] = this.getrule(pref, "");
 			} else {
-				this.rules[name][attr] = wot_prefs.getBool(pref, true);
+				this.rules[name][attr] = this.getrule(pref, true);
 			}
 		} catch (e) {
 			wdump("wot_search.loadrule: failed with " + e);
@@ -420,12 +430,17 @@ var wot_search =
 	{
 		try {
 			this.rules = {};
+			this._searchrules = wot_storage.get("search", {});
 
-			var branch = wot_prefs.ps.getBranch(WOT_PREF + WOT_SEARCH + ".");
-			var children = branch.getChildList("", {});
+			var keyword = "search.",
+				kw_len = keyword.length;
 
-			for (var i = 0; i < children.length; ++i) {
-				this.loadrule(children[i]);
+			for (var i in this._searchrules) {
+				if (!this._searchrules.hasOwnProperty(i)) continue;
+				if (i.indexOf(keyword) == 0) {  // remove prepending "search." keyword
+					i = i.slice(kw_len);
+				}
+				this.loadrule(i);
 			}
 		} catch (e) {
 			wdump("wot_search.sync: failed with " + e);
@@ -685,7 +700,7 @@ var wot_search =
 	{
 		try {
 			var set = [];
-				
+
 			if (match.element == "$frame") {
 				set.push(content.defaultView.frameElement);
 			} else {
diff --git a/content/shared.js b/content/shared.js
index 48c2ce1..746188b 100644
--- a/content/shared.js
+++ b/content/shared.js
@@ -41,8 +41,7 @@ var wot_shared =
 			var data = {};
 
 			for (i = 0; i < shared.length; ++i) {
-				var attr = shared[i].attributes.getNamedItem(
-								WOT_SERVICE_XML_UPDATE_SHARED_LEVEL);
+				var attr = shared[i].attributes.getNamedItem(WOT_SERVICE_XML_UPDATE_SHARED_LEVEL);
 
 				if (!attr || !attr.value) {
 					return;
@@ -54,8 +53,7 @@ var wot_shared =
 					return;
 				}
 
-				var attr = shared[i].attributes.getNamedItem(
-								WOT_SERVICE_XML_UPDATE_SHARED_DOMAINS);
+				attr = shared[i].attributes.getNamedItem(WOT_SERVICE_XML_UPDATE_SHARED_DOMAINS);
 
 				if (!attr || !attr.value) {
 					return;
@@ -68,9 +66,12 @@ var wot_shared =
 				data[level] = data[level].concat(attr.value.split(","));
 			}
 
+			var stor_shared = {};
 			for (i in data) {
-				wot_prefs.setChar("shared." + i, data[i].join(","));
+				stor_shared["shared." + i] = data[i].join(",");
 			}
+			wot_storage.set("shared", stor_shared, true);
+			wot_prefs.deleteBranch("shared.");   // we don't need this in preferences any more
 
 			this.sync();
 		} catch (e) {
@@ -81,17 +82,19 @@ var wot_shared =
 	sync: function()
 	{
 		try {
-			var branch = wot_prefs.ps.getBranch(WOT_PREF + "shared.");
-			var children = branch.getChildList("", {});
+			var stor_shared = wot_storage.get("shared", {});
 
-			for (var i = 0; i < children.length; ++i) {
-				var level = Number(children[i]);
+			for (var i in stor_shared) {
+				if (!stor_shared.hasOwnProperty(i)) continue;
+
+				var parts = i.split(".", 2);
+				var level = Number(parts[1]);
 
 				if (level < 1) {
 					continue;
 				}
 
-				var data = wot_prefs.getChar("shared." + children[i]);
+				var data = stor_shared[i];
 
 				if (!data || !data.length) {
 					continue;
@@ -151,7 +154,7 @@ var wot_shared =
 			path = path.replace(/^\s+/, "")
 					.replace(/\s+$/, "")
 					.replace(/[\?#].*$/, "");
- 
+
 			if (path.length < 2 || path[0] != "/") {
 				return host;
 			}
@@ -261,7 +264,7 @@ var wot_shared =
 		} catch (e) {
 			dump("wot_shared.base32encode: failed with " + e + "\n");
 		}
-		
+
 		return null;
 	},
 
diff --git a/content/storage.js b/content/storage.js
new file mode 100644
index 0000000..f705bfd
--- /dev/null
+++ b/content/storage.js
@@ -0,0 +1,178 @@
+/*
+	storage.js
+	Copyright © 2013  WOT Services Oy <info at mywot.com>
+
+	This file is part of WOT.
+
+	WOT is free software: you can redistribute it and/or modify it
+	under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 3 of the License, or
+	(at your option) any later version.
+
+	WOT is distributed in the hope that it will be useful, but WITHOUT
+	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+	License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with WOT. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// Stores the data from server in the local JSON file in the profile directory. Kind of persistent cache.
+
+var wot_storage =
+{
+	_FILENAME: "storage.json",
+	_HASHPREFIX: "wot_storage",
+	flushed: null,
+	flush_timer: null,
+
+	init: function(callback)
+	{
+		// we can't use name "load" for this function to avoid it been loaded in the core.load without callback
+		try {
+
+			var is_loaded = wot_storage.get_from_memory("is_loaded");   // no need to read file when new window is opened. Storage data is already in shared memory
+
+			if (!is_loaded) {
+				// read the file and place data to this object's data field
+				wot_file.read_json(this._FILENAME, function (data) {
+
+					if (data && !wot_util.isEmpty(data)) {
+						for (var k in data) {
+							if (data.hasOwnProperty(k)) {
+								wot_storage.put_to_memory(k, data[k]);
+							}
+						}
+						wot_storage.put_to_memory("is_loaded", true);
+					} else {
+						// TODO: remove storage data from memory
+					}
+
+				});
+			}
+
+		} catch (e) {
+			wdump("wot_storage.load: failed with " + e);
+		}
+
+		if(typeof(callback) == "function") callback(); // call the callback anyway, even if storage is failed to load in order to make the add-on work
+	},
+
+	put_to_memory: function (key, val) {
+		wot_hashtable.set(this._HASHPREFIX + ":" + key, wot_util.unicode_to_utf8(JSON.stringify(val)));
+	},
+
+	get_from_memory: function (key) {
+		var v = wot_hashtable.get(this._HASHPREFIX + ":" + key);
+		if (v) {
+			try {
+				return JSON.parse(wot_util.utf8_to_unicode(v));
+			} catch (e) {
+				return undefined;
+			}
+		} else {
+			return undefined;
+		}
+	},
+
+	get_name_from_element: function(element)
+	{
+		var RE = RegExp(this._HASHPREFIX + "\:(.+)");
+		try {
+			if (!element || !element.QueryInterface) return null;
+
+			var property = element.QueryInterface(Components.interfaces.nsIProperty);
+
+			if (!property) return null;
+
+			var match = property.name.match(RE);
+
+			if (!match || !match[1]) return null;
+
+			return match[1];
+		} catch (e) {
+			dump("wot_storage.get_name_from_element: failed with " + e + "\n");
+		}
+		return null;
+	},
+
+	get: function (name, default_value) {
+		try {
+            var t = this.get_from_memory(name);
+            return t !== undefined ? t : default_value;
+		} catch (e) {
+			wdump("wot_storage.get(" + name + "): failed with " + e);
+			return default_value;
+		}
+	},
+
+	set: function (name, obj, flush) {
+		try {
+			this.put_to_memory(name, obj);
+			if (flush !== false) {
+				this.flush(flush);
+			}
+			return true;
+		} catch (e) {
+			wdump("wot_storage.set(" + name + "): failed with " + e);
+			return false;
+		}
+	},
+
+	clear: function(key)
+	{
+		try {
+			wot_hashtable.remove(this._HASHPREFIX + ":" + key)
+		} catch (e) {
+		}
+	},
+
+	flush: function(force, callback)
+	{
+		try {
+
+			// don't flush too often (3 secs) unless explicitly asked
+			var flushed = this.get("flushed", null);
+			if (flushed && Date.now() - flushed < 3000 && !force) {
+
+				// flush after short delay
+				if (!this.flush_timer) {
+					this.flush_timer = window.setTimeout(function() {
+						wot_storage.flush(true);
+					}, 5000);
+				}
+
+				if (typeof(callback) == "function") callback(false);
+				return;
+			}
+
+			// cancel scheduled flushing since we already flushing
+			if (this.flush_timer) {
+				window.clearTimeout(this.flush_timer);
+				this.flush_timer = null;
+			}
+
+			var bag = wot_hashtable.get_enumerator(),
+				data = {};
+
+			// go through stored in memory values and keep ones that belong to Storage
+			while (bag.hasMoreElements()) {
+				var name = this.get_name_from_element(bag.getNext());
+				if (name && name != "is_loaded" && name != "flushed") {
+					data[name] = this.get(name, null);
+				}
+			}
+
+			this.set("flushed", Date.now());
+			wot_file.save_json(this._FILENAME, data, callback);
+			wot_storage.put_to_memory("is_loaded", true);   // since we had something to dump to file, lets assume that data is in memory
+
+		} catch (e) {
+			dump("wot_storage.flush: failed with " + e + "\n");
+		}
+	}
+
+};
+
+wot_modules.push({ name: "wot_storage", obj: wot_storage });
diff --git a/content/util.js b/content/util.js
index d2168a3..5684612 100644
--- a/content/util.js
+++ b/content/util.js
@@ -270,7 +270,7 @@ var wot_url =
 			if (!ts) {
 				return true;
 			}
-			
+
 			var domain = name.replace(/^[^\.]*\./, "");
 			var tld = ts.getPublicSuffixFromHost(domain);
 
@@ -1230,9 +1230,10 @@ var wot_file = {
 					var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
 
 					if (data) {
-						var res = JSON.parse(data);
+						var res = JSON.parse(wot_util.utf8_to_unicode(data));
 						if (res instanceof Object) {
 							callback(res);
+							return;
 						}
 					}
 					callback({});   // whether no data is loaded call it anyway to finish the load process

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/wot.git



More information about the Pkg-mozext-commits mailing list