1 // Bug 1506 P1-3: This code is mostly hackish remnants of session store
2 // support. There are a couple of observer events that *might* be worth
3 // listening to. Search for 1506 in the code.
5 /*************************************************************************
6 * Startup observer (JavaScript XPCOM component)
8 * Cases tested (each during Tor and Non-Tor, FF4 and FF3.6)
13 *************************************************************************/
15 const { AppConstants } = ChromeUtils.import(
16 "resource://gre/modules/AppConstants.jsm"
18 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
19 const { XPCOMUtils } = ChromeUtils.import(
20 "resource://gre/modules/XPCOMUtils.jsm"
23 const { TorProtocolService } = ChromeUtils.import(
24 "resource://gre/modules/TorProtocolService.jsm"
27 XPCOMUtils.defineLazyModuleGetters(this, {
28 ComponentUtils: "resource://gre/modules/ComponentUtils.jsm",
31 // Module specific constants
32 const kMODULE_NAME = "Startup";
33 const kMODULE_CONTRACTID = "@torproject.org/startup-observer;1";
34 const kMODULE_CID = Components.ID("06322def-6fde-4c06-aef6-47ae8e799629");
36 function cleanupCookies() {
37 const migratedPref = "extensions.torbutton.cookiejar_migrated";
38 if (!Services.prefs.getBoolPref(migratedPref, false)) {
39 // Cleanup stored cookie-jar-selector json files
40 const profileFolder = Services.dirsvc.get("ProfD", Ci.nsIFile).clone();
41 for (const file of profileFolder.directoryEntries) {
42 if (file.leafName.match(/^(cookies|protected)-.*[.]json$/)) {
48 Services.prefs.setBoolPref(migratedPref, true);
52 function StartupObserver() {
53 this.logger = Cc["@torproject.org/torbutton-logger;1"].getService(
56 this._prefs = Services.prefs;
57 this.logger.log(3, "Startup Observer created");
59 var env = Cc["@mozilla.org/process/environment;1"].getService(
62 var prefName = "browser.startup.homepage";
63 if (env.exists("TOR_DEFAULT_HOMEPAGE")) {
64 // if the user has set this value in a previous installation, don't override it
65 if (!this._prefs.prefHasUserValue(prefName)) {
66 this._prefs.setCharPref(prefName, env.get("TOR_DEFAULT_HOMEPAGE"));
73 // XXX: We're in a race with HTTPS-Everywhere to update our proxy settings
74 // before the initial SSL-Observatory test... If we lose the race, Firefox
75 // caches the old proxy settings for check.tp.o somehwere, and it never loads :(
76 this.setProxySettings();
80 "Early proxy change failed. Will try again at profile load. Error: " + e
87 StartupObserver.prototype = {
88 // Bug 6803: We need to get the env vars early due to
89 // some weird proxy caching code that showed up in FF15.
90 // Otherwise, homepage domain loads fail forever.
96 // Bug 1506: Still want to get these env vars
97 let environ = Cc["@mozilla.org/process/environment;1"].getService(
100 if (environ.exists("TOR_TRANSPROXY")) {
101 this.logger.log(3, "Resetting Tor settings to transproxy");
102 this._prefs.setBoolPref("network.proxy.socks_remote_dns", false);
103 this._prefs.setIntPref("network.proxy.type", 0);
104 this._prefs.setIntPref("network.proxy.socks_port", 0);
105 this._prefs.setCharPref("network.proxy.socks", "");
107 // Try to retrieve SOCKS proxy settings from Tor Launcher.
110 socksPortInfo = TorProtocolService.torGetSOCKSPortInfo();
112 this.logger.log(3, "tor launcher failed " + e);
115 // If Tor Launcher is not available, check environment variables.
116 if (!socksPortInfo) {
117 socksPortInfo = { ipcFile: undefined, host: undefined, port: 0 };
119 let isWindows = Services.appinfo.OS === "WINNT";
120 if (!isWindows && environ.exists("TOR_SOCKS_IPC_PATH")) {
121 socksPortInfo.ipcFile = new FileUtils.File(
122 environ.get("TOR_SOCKS_IPC_PATH")
125 if (environ.exists("TOR_SOCKS_HOST")) {
126 socksPortInfo.host = environ.get("TOR_SOCKS_HOST");
128 if (environ.exists("TOR_SOCKS_PORT")) {
129 socksPortInfo.port = parseInt(environ.get("TOR_SOCKS_PORT"));
134 // Adjust network.proxy prefs.
135 if (socksPortInfo.ipcFile) {
136 let fph = Services.io
137 .getProtocolHandler("file")
138 .QueryInterface(Ci.nsIFileProtocolHandler);
139 let fileURI = fph.newFileURI(socksPortInfo.ipcFile);
140 this.logger.log(3, "Reset socks to " + fileURI.spec);
141 this._prefs.setCharPref("network.proxy.socks", fileURI.spec);
142 this._prefs.setIntPref("network.proxy.socks_port", 0);
144 if (socksPortInfo.host) {
145 this._prefs.setCharPref("network.proxy.socks", socksPortInfo.host);
146 this.logger.log(3, "Reset socks host to " + socksPortInfo.host);
148 if (socksPortInfo.port) {
149 this._prefs.setIntPref(
150 "network.proxy.socks_port",
153 this.logger.log(3, "Reset socks port to " + socksPortInfo.port);
157 if (socksPortInfo.ipcFile || socksPortInfo.host || socksPortInfo.port) {
158 this._prefs.setBoolPref("network.proxy.socks_remote_dns", true);
159 this._prefs.setIntPref("network.proxy.type", 1);
163 // Force prefs to be synced to disk
164 Services.prefs.savePrefFile(null);
166 this.logger.log(3, "Synced network settings to environment.");
169 observe(subject, topic, data) {
170 if (topic == "profile-after-change") {
171 // Bug 1506 P1: We listen to these prefs as signals for startup,
172 // but only for hackish reasons.
173 this._prefs.setBoolPref("extensions.torbutton.startup", true);
175 this.setProxySettings();
178 // In all cases, force prefs to be synced to disk
179 Services.prefs.savePrefFile(null);
182 QueryInterface: ChromeUtils.generateQI([Ci.nsIClassInfo]),
184 // method of nsIClassInfo
185 classDescription: "Torbutton Startup Observer",
186 classID: kMODULE_CID,
187 contractID: kMODULE_CONTRACTID,
189 // Hack to get us registered early to observe recovery
190 _xpcom_categories: [{ category: "profile-after-change" }],
193 // Assign factory to global object.
194 const NSGetFactory = XPCOMUtils.generateNSGetFactory
195 ? XPCOMUtils.generateNSGetFactory([StartupObserver])
196 : ComponentUtils.generateNSGetFactory([StartupObserver]);