[Pkg-mozext-commits] [requestpolicy] 202/257: [fix] old rules: when to import automatically

David Prévot taffit at moszumanska.debian.org
Thu Jan 28 03:20:13 UTC 2016


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

taffit pushed a commit to branch master
in repository requestpolicy.

commit 49894f66459f985daa2a5a8c8484e2fe7fffc2eb
Author: Martin Kimmerle <dev at 256k.de>
Date:   Sun Dec 6 14:35:25 2015 +0100

    [fix] old rules: when to import automatically
    
    Old rules should be imported automatically if and only if …
    
    * it's a RP version change from v0.5 to v1.0
    * and the v1.0 rules file doesn't exist yet.
    
    This commit introduces the first "controller", see #733.
    
    Fixes #731.
---
 src/content/controllers/old-rules.jsm              | 85 ++++++++++++++++++++++
 src/content/lib/environment.process.js             | 20 +++++
 src/content/lib/logger.jsm                         |  1 +
 src/content/lib/policy-manager.jsm                 |  2 +
 src/content/lib/utils/info.jsm                     |  9 +++
 src/content/settings/setup.js                      | 33 +--------
 .../tests/policy/old_rules/test_import.py          |  2 -
 7 files changed, 118 insertions(+), 34 deletions(-)

diff --git a/src/content/controllers/old-rules.jsm b/src/content/controllers/old-rules.jsm
new file mode 100644
index 0000000..4bf2827
--- /dev/null
+++ b/src/content/controllers/old-rules.jsm
@@ -0,0 +1,85 @@
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ *
+ * RequestPolicy - A Firefox extension for control over cross-site requests.
+ * Copyright (c) 2015 Martin Kimmerle
+ *
+ * This program 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.
+ *
+ * This program 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
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * ***** END LICENSE BLOCK *****
+ */
+
+/* global Components */
+const {utils: Cu} = Components;
+
+/* exported OldRulesController */
+this.EXPORTED_SYMBOLS = ["OldRulesController"];
+
+let {ScriptLoader: {importModule}} = Cu.import(
+    "chrome://rpcontinued/content/lib/script-loader.jsm", {});
+let {Logger} = importModule("lib/logger");
+let {Info} = importModule("lib/utils/info");
+let {OldRules} = importModule("lib/old-rules");
+var {PolicyManager} = importModule("lib/policy-manager");
+
+//==============================================================================
+// OldRulesController
+//==============================================================================
+
+var OldRulesController = (function() {
+  "use strict";
+
+  let self = {};
+
+  self.startup = function() {
+    // If the user ...
+    //   * upgrades to 1.0,
+    //   * downgrades back to 0.5
+    //   * and upgrades again
+    // the user ruleset (user.json) already exists after the first step.
+    let isFirstRPUpgrade = true === Info.isRPUpgrade &&
+        false === PolicyManager.userRulesetExistedOnStartup;
+
+    if (isFirstRPUpgrade) {
+      importOldRulesAutomatically();
+    } else {
+      // TODO inform the user about old rules
+    }
+  };
+
+  function importOldRulesAutomatically() {
+    Logger.dump("Performing automatic rule import.");
+    let rv = self.importOldRules();
+    if (false === rv) {
+      Logger.error("Failed to automatically import old rules.");
+    }
+  }
+
+  /**
+   * @return {boolean} If the import was successful.
+   */
+  self.importOldRules = function() {
+    try {
+      let oldRules = new OldRules();
+      let rules = oldRules.getAsNewRules();
+      PolicyManager.addAllowRules(rules);
+      return true;
+    } catch (e) {
+      Cu.reportError(e);
+      return false;
+    }
+  };
+
+  return self;
+}());
diff --git a/src/content/lib/environment.process.js b/src/content/lib/environment.process.js
index 66da3eb..228f04d 100644
--- a/src/content/lib/environment.process.js
+++ b/src/content/lib/environment.process.js
@@ -105,6 +105,14 @@ var ProcessEnvironment = (function() {
       ProcessEnvironmentBase.prototype);
   ParentProcessEnvironment.prototype.constructor = ProcessEnvironmentBase;
 
+  Object.defineProperty(ParentProcessEnvironment.prototype, "controllers", {
+    get() {
+      return [
+        ScriptLoader.importModule("controllers/old-rules").OldRulesController
+      ];
+    }
+  });
+
   /**
    * @override
    */
@@ -143,6 +151,12 @@ var ProcessEnvironment = (function() {
     ], dummyScope);
 
     ProcessEnvironmentBase.prototype.startup.apply(self, arguments);
+
+    self.controllers.forEach(function(controller) {
+      if (typeof controller.startup === "function") {
+        controller.startup.apply(null, arguments);
+      }
+    });
   };
 
   /**
@@ -151,6 +165,12 @@ var ProcessEnvironment = (function() {
   ParentProcessEnvironment.prototype.shutdown = function() {
     let self = this;
 
+    self.controllers.reverse().forEach(function(controller) {
+      if (typeof controller.shutdown === "function") {
+        controller.shutdown.apply(null, arguments);
+      }
+    });
+
     ProcessEnvironmentBase.prototype.shutdown.apply(self, arguments);
 
     ScriptLoader.doShutdownTasks();
diff --git a/src/content/lib/logger.jsm b/src/content/lib/logger.jsm
index be37c20..1b4f5b0 100644
--- a/src/content/lib/logger.jsm
+++ b/src/content/lib/logger.jsm
@@ -167,6 +167,7 @@ var Logger = (function() {
   self.severe = doLog.bind(self, self.LEVEL_SEVERE);
   self.severeError = doLog.bind(self, self.LEVEL_SEVERE, self.TYPE_ERROR);
   self.warning = doLog.bind(self, self.LEVEL_WARNING);
+  self.error = doLog.bind(self, self.LEVEL_WARNING, self.TYPE_ERROR);
   self.info = doLog.bind(self, self.LEVEL_INFO);
   self.debug = doLog.bind(self, self.LEVEL_DEBUG);
   self.dump = doLog.bind(self, self.LEVEL_DEBUG, self.TYPE_INTERNAL);
diff --git a/src/content/lib/policy-manager.jsm b/src/content/lib/policy-manager.jsm
index 131fcaa..cff8143 100644
--- a/src/content/lib/policy-manager.jsm
+++ b/src/content/lib/policy-manager.jsm
@@ -93,11 +93,13 @@ var PolicyManager = (function() {
     try {
       dprint("PolicyManager::loadUserRules loading user rules");
       rawRuleset = RulesetStorage.loadRawRulesetFromFile("user.json");
+      self.userRulesetExistedOnStartup = true;
     } catch (e) {
       // TODO: log a message about missing user.json ruleset file.
       // There's no user ruleset. This is either because RP has just been
       // installed, the file has been deleted, or something is wrong. For now,
       // we'll assume this is a new install.
+      self.userRulesetExistedOnStartup = false;
       rawRuleset = new RawRuleset();
     }
     userRulesets.user = {
diff --git a/src/content/lib/utils/info.jsm b/src/content/lib/utils/info.jsm
index 114ea91..11af9ad 100644
--- a/src/content/lib/utils/info.jsm
+++ b/src/content/lib/utils/info.jsm
@@ -28,6 +28,7 @@ const {utils: Cu} = Components;
 this.EXPORTED_SYMBOLS = ["Info"];
 
 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
 
 let {ScriptLoader: {importModule}} = Cu.import(
     "chrome://rpcontinued/content/lib/script-loader.jsm", {});
@@ -64,6 +65,14 @@ var Info = (function() {
         Services.prefs.savePrefFile(null);
       }
     });
+
+    XPCOMUtils.defineLazyGetter(self, "isRPUpgrade", function() {
+      // Compare with version 1.0.0a8 since that version introduced
+      // the "welcome window".
+      return self.lastRPVersion &&
+          Services.vc.compare(self.lastRPVersion, "0.0") > 0 &&
+          Services.vc.compare(self.lastRPVersion, "1.0.0a8") <= 0;
+    });
   }
 
   // bad smell...
diff --git a/src/content/settings/setup.js b/src/content/settings/setup.js
index 198b528..d5df2b0 100644
--- a/src/content/settings/setup.js
+++ b/src/content/settings/setup.js
@@ -10,12 +10,9 @@
       "chrome://rpcontinued/content/lib/script-loader.jsm", {});
   var {Info} = importModule("lib/utils/info");
   var {rpPrefBranch} = importModule("lib/prefs");
-  var {Logger} = importModule("lib/logger");
-  var {PolicyManager} = importModule("lib/policy-manager");
   var {SUBSCRIPTION_ADDED_TOPIC, SUBSCRIPTION_REMOVED_TOPIC} =
       importModule("lib/subscription");
   var {rpService} = importModule("main/requestpolicy-service");
-  var {OldRules} = importModule("lib/old-rules");
 
   //============================================================================
 
@@ -115,19 +112,10 @@
   }*/
 
   window.onload = function() {
-    // To retrieve the last RP version, `Info` needs to be used,
-    // because the pref "extensions.requestpolicy.lastVersion" has
-    // already been updated.
-    var lastRPVersion = Info.lastRPVersion;
-
     // Populate the form values based on the user's current settings.
     // If the use has just upgrade from an 0.x version, populate based on the old
     // preferences and also do a rule import based on the old strictness settings.
-    // Note: using version 1.0.0a8 instead of 1.0 as that was the last version
-    // before this setup window was added.
-    if (lastRPVersion &&
-        Services.vc.compare(lastRPVersion, "0.0") > 0 &&
-        Services.vc.compare(lastRPVersion, "1.0.0a8") <= 0) {
+    if (Info.isRPUpgrade) {
       var identLevel;
       if (rpPrefBranch.prefHasUserValue("uriIdentificationLevel")) {
         identLevel = rpPrefBranch.getIntPref("uriIdentificationLevel");
@@ -141,25 +129,6 @@
       $id("allowsamedomain").checked = identLevel === 1;
       handleAllowSameDomainChange();
 
-      // If the user doesn't have any new-style rules, automatically do an import
-      // of the old rules. We check for new-style rules just in case the user has
-      // opened the setup window again after initial upgrade.
-      var ruleCount;
-      try {
-        ruleCount = PolicyManager.getUserRuleCount();
-      } catch (e) {
-        Logger.warning(Logger.TYPE_INTERNAL,
-            "Unable to get new rule count: " + e);
-        ruleCount = -1;
-      }
-      Logger.dump("Rule count: " + ruleCount);
-      if (ruleCount <= 0) {
-        Logger.dump("Performing rule import.");
-        var oldRules = new OldRules();
-        var rules = oldRules.getAsNewRules();
-        PolicyManager.addAllowRules(rules);
-      }
-
       // Skip the welcome screen.
       showConfigure();
 
diff --git a/tests/marionette/tests/policy/old_rules/test_import.py b/tests/marionette/tests/policy/old_rules/test_import.py
index 1fcdf9e..0993b48 100644
--- a/tests/marionette/tests/policy/old_rules/test_import.py
+++ b/tests/marionette/tests/policy/old_rules/test_import.py
@@ -40,7 +40,6 @@ class TestAutomaticRulesImportOnUpgrade(RulesImportTestCase):
                                      should_autoimport=True)
 
     def test_autoimport__upgrade_without_welcomewin(self):
-        raise SkipTest("FIXME: issue #731")
         self._test_autoimport_or_not(is_upgrade=True,
                                      with_existing_rules_file=False,
                                      with_welcomewin=False,
@@ -53,7 +52,6 @@ class TestAutomaticRulesImportOnUpgrade(RulesImportTestCase):
                                      should_autoimport=False)
 
     def test_no_autoimport__upgrade_with_existing_rules_file(self):
-        raise SkipTest("FIXME: issue #731")
         self._test_autoimport_or_not(is_upgrade=True,
                                      with_existing_rules_file=True,
                                      with_welcomewin=True,

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



More information about the Pkg-mozext-commits mailing list