Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = []; |
michael@0 | 8 | |
michael@0 | 9 | #ifdef XP_WIN |
michael@0 | 10 | #ifdef MOZ_METRO |
michael@0 | 11 | |
michael@0 | 12 | const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu, manager: Cm} = |
michael@0 | 13 | Components; |
michael@0 | 14 | const PREF_BASE_KEY = "Software\\Mozilla\\Firefox\\Metro\\Prefs\\"; |
michael@0 | 15 | |
michael@0 | 16 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 17 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 18 | |
michael@0 | 19 | this.EXPORTED_SYMBOLS = [ "WindowsPrefSync" ]; |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * Manages preferences that need to be pulled and pushed between Metro |
michael@0 | 23 | * and desktop. |
michael@0 | 24 | */ |
michael@0 | 25 | this.WindowsPrefSync = { |
michael@0 | 26 | init: function() { |
michael@0 | 27 | this.pullSharedPrefs(); |
michael@0 | 28 | this.prefListToPush.forEach(function(prefName) { |
michael@0 | 29 | this.pushSharedPref(prefName); |
michael@0 | 30 | Services.prefs.addObserver(prefName, this, false); |
michael@0 | 31 | }, this); |
michael@0 | 32 | }, |
michael@0 | 33 | |
michael@0 | 34 | uninit: function() { |
michael@0 | 35 | this.prefListToPush.forEach(function(prefName) { |
michael@0 | 36 | Services.prefs.removeObserver(prefName, this); |
michael@0 | 37 | }, this); |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * Returns the list of prefs that should be pushed for the current |
michael@0 | 42 | * environment. |
michael@0 | 43 | */ |
michael@0 | 44 | get prefListToPush() { |
michael@0 | 45 | return !Services.metro.immersive ? this.desktopControlledPrefs : |
michael@0 | 46 | this.metroControlledPrefs; |
michael@0 | 47 | }, |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * Returns the list of prefs that should be pulled for the current |
michael@0 | 51 | * environment. |
michael@0 | 52 | */ |
michael@0 | 53 | get prefListToPull() { |
michael@0 | 54 | return Services.metro.immersive ? this.desktopControlledPrefs : |
michael@0 | 55 | this.metroControlledPrefs; |
michael@0 | 56 | }, |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * The following preferences will be pushed to registry from Desktop |
michael@0 | 60 | * Firefox and pulled in from Metro Firefox. |
michael@0 | 61 | * |
michael@0 | 62 | * app.update.* prefs are because Metro shares an installation directory with |
michael@0 | 63 | * Firefox, and the options for these are only present in the Desktop options. |
michael@0 | 64 | * |
michael@0 | 65 | * browser.sessionstore.resume_session_once is mainly for the switch to Metro |
michael@0 | 66 | * and switch to Desktop feature. |
michael@0 | 67 | * |
michael@0 | 68 | * browser.startup.page - if a desktop Firefox user wants her/his sessions |
michael@0 | 69 | * to always restore, we need to honor that in metro Firefox as well. |
michael@0 | 70 | */ |
michael@0 | 71 | desktopControlledPrefs: ["app.update.auto", |
michael@0 | 72 | "app.update.enabled", |
michael@0 | 73 | "app.update.service.enabled", |
michael@0 | 74 | "app.update.metro.enabled", |
michael@0 | 75 | "browser.sessionstore.resume_session_once", |
michael@0 | 76 | "browser.startup.page"], |
michael@0 | 77 | |
michael@0 | 78 | /** |
michael@0 | 79 | * Returns the base path where registry sync prefs are stored. |
michael@0 | 80 | */ |
michael@0 | 81 | get prefRegistryPath() { |
michael@0 | 82 | let profileService = Cc["@mozilla.org/toolkit/profile-service;1"]. |
michael@0 | 83 | createInstance(Ci.nsIToolkitProfileService); |
michael@0 | 84 | return PREF_BASE_KEY + profileService.selectedProfile.name + "\\"; |
michael@0 | 85 | }, |
michael@0 | 86 | |
michael@0 | 87 | /** |
michael@0 | 88 | * The following preferences will be pushed to registry from Metro |
michael@0 | 89 | * Firefox and pulled in from Desktop Firefox. |
michael@0 | 90 | * |
michael@0 | 91 | * browser.sessionstore.resume_session_once is mainly for the switch to Metro |
michael@0 | 92 | * and switch to Desktop feature. |
michael@0 | 93 | */ |
michael@0 | 94 | metroControlledPrefs: ["browser.sessionstore.resume_session_once"], |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Observes preference changes and writes them to the registry, only |
michael@0 | 98 | * the list of preferences initialized will be observed |
michael@0 | 99 | */ |
michael@0 | 100 | observe: function (aSubject, aTopic, aPrefName) { |
michael@0 | 101 | if (aTopic != "nsPref:changed") |
michael@0 | 102 | return; |
michael@0 | 103 | |
michael@0 | 104 | this.pushSharedPref(aPrefName); |
michael@0 | 105 | }, |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Writes the pref to HKCU in the registry and adds a pref-observer to keep |
michael@0 | 109 | * the registry in sync with changes to the value. |
michael@0 | 110 | */ |
michael@0 | 111 | pushSharedPref : function(aPrefName) { |
michael@0 | 112 | let registry = Cc["@mozilla.org/windows-registry-key;1"]. |
michael@0 | 113 | createInstance(Ci.nsIWindowsRegKey); |
michael@0 | 114 | try { |
michael@0 | 115 | var prefType = Services.prefs.getPrefType(aPrefName); |
michael@0 | 116 | let prefFunc; |
michael@0 | 117 | if (prefType == Ci.nsIPrefBranch.PREF_INT) |
michael@0 | 118 | prefFunc = "getIntPref"; |
michael@0 | 119 | else if (prefType == Ci.nsIPrefBranch.PREF_BOOL) |
michael@0 | 120 | prefFunc = "getBoolPref"; |
michael@0 | 121 | else if (prefType == Ci.nsIPrefBranch.PREF_STRING) |
michael@0 | 122 | prefFunc = "getCharPref"; |
michael@0 | 123 | else |
michael@0 | 124 | throw "Unsupported pref type"; |
michael@0 | 125 | |
michael@0 | 126 | let prefValue = Services.prefs[prefFunc](aPrefName); |
michael@0 | 127 | registry.create(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, |
michael@0 | 128 | this.prefRegistryPath + prefType, Ci.nsIWindowsRegKey.ACCESS_WRITE); |
michael@0 | 129 | // Always write as string, but the registry subfolder will determine |
michael@0 | 130 | // how Metro interprets that string value. |
michael@0 | 131 | registry.writeStringValue(aPrefName, prefValue); |
michael@0 | 132 | } catch (ex) { |
michael@0 | 133 | Cu.reportError("Couldn't push pref " + aPrefName + ": " + ex); |
michael@0 | 134 | } finally { |
michael@0 | 135 | registry.close(); |
michael@0 | 136 | } |
michael@0 | 137 | }, |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Pulls in all shared prefs from the registry |
michael@0 | 141 | */ |
michael@0 | 142 | pullSharedPrefs: function() { |
michael@0 | 143 | function pullSharedPrefType(prefType, prefFunc) { |
michael@0 | 144 | try { |
michael@0 | 145 | registry.create(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, |
michael@0 | 146 | self.prefRegistryPath + prefType, |
michael@0 | 147 | Ci.nsIWindowsRegKey.ACCESS_ALL); |
michael@0 | 148 | for (let i = 0; i < registry.valueCount; i++) { |
michael@0 | 149 | let prefName = registry.getValueName(i); |
michael@0 | 150 | let prefValue = registry.readStringValue(prefName); |
michael@0 | 151 | if (prefType == Ci.nsIPrefBranch.PREF_BOOL) { |
michael@0 | 152 | prefValue = prefValue == "true"; |
michael@0 | 153 | } |
michael@0 | 154 | if (self.prefListToPull.indexOf(prefName) != -1) { |
michael@0 | 155 | Services.prefs[prefFunc](prefName, prefValue); |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | } catch (ex) { |
michael@0 | 159 | dump("Could not pull for prefType " + prefType + ": " + ex + "\n"); |
michael@0 | 160 | } finally { |
michael@0 | 161 | registry.close(); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | let self = this; |
michael@0 | 165 | let registry = Cc["@mozilla.org/windows-registry-key;1"]. |
michael@0 | 166 | createInstance(Ci.nsIWindowsRegKey); |
michael@0 | 167 | pullSharedPrefType(Ci.nsIPrefBranch.PREF_INT, "setIntPref"); |
michael@0 | 168 | pullSharedPrefType(Ci.nsIPrefBranch.PREF_BOOL, "setBoolPref"); |
michael@0 | 169 | pullSharedPrefType(Ci.nsIPrefBranch.PREF_STRING, "setCharPref"); |
michael@0 | 170 | } |
michael@0 | 171 | }; |
michael@0 | 172 | #endif |
michael@0 | 173 | #endif |