root/trunk/chrome/content/dta/manager/prefs.js

Revision 872, 4.9 kB (checked in by MaierMan, 2 years ago)

#604: Reset connection prefs when no longer needed.

Line 
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is DownThemAll!
15  *
16  * The Initial Developers of the Original Code are Stefano Verna and Federico Parodi
17  * Portions created by the Initial Developers are Copyright (C) 2004-2007
18  * the Initial Developers. All Rights Reserved.
19  *
20  * Contributor(s):
21  *    Stefano Verna <stefano.verna@gmail.com>
22  *    Federico Parodi <f.parodi@tiscali.it>
23  *    Nils Maier <MaierMan@web.de>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38  
39  var Prefs = {
40   tempLocation: null,
41  
42   mappings: [
43     ['removeCompleted', true],
44     ['removeAborted', false],
45     ['removeCanceled', false],
46     ['autoClose', 'closedta', false],
47     ['timeout', 300],
48     ['maxInProgress', 'ntask', 4],
49     ['maxChunks', 4],
50     ['setTime', true],
51     ['showOnlyFilenames', true],
52     ['conflictResolution', 3],
53     ['alertingSystem', 'alertbox', (SYSTEMSLASH == '\\') ? 1 : 0],
54     ['finishEvent', ''],
55     ['showTooltip', true],
56     ['maxAutoRetries', 10],
57     ['autoRetryInterval', 0],
58     ['autoClearComplete', false],
59     ['confirmRemove', true],
60     ['permissions', 384],
61   ],
62
63   // nsIObserver
64   observe: function(subject, topic, prefName) {
65     this._refreshPrefs(prefName);
66   },
67
68   init: function() {
69     makeObserver(this);
70
71     try {
72       this._resetConnPrefs();
73       this._refreshPrefs();
74       Preferences.addObserver('extensions.dta.', this);
75       Preferences.addObserver('network.', this);
76     }
77     catch (ex) {
78       Debug.log("failed to add pref-observer", ex);
79     }
80   },
81
82   _refreshPrefs: function(prefName) {
83     Debug.logString("pref reload");
84     this.mappings.forEach(
85       function(e) {
86         let key, pref, def;
87         if (!e) {
88           return;
89         }
90         else if (e.length == 3) {
91           key = e[0];
92           pref = e[1];
93           def = e[2];
94         }
95         else {
96           key = e[0];
97           pref = key.toLowerCase();
98           def = e[1];
99         }
100         this[key] = Preferences.getDTA(pref, def);
101       },
102       this
103     );
104    
105     var perms = Prefs.permissions;
106     if (perms & 0600) {
107       perms |= 0100;
108     }
109     if (perms & 0060) {
110       perms |= 0010;
111     }       
112     if (perms & 0006) {
113       perms |= 0001;
114     }
115     this.dirPermissions = perms;   
116
117     if (Preferences.getDTA("saveTemp", true)) {
118       try {
119         this.tempLocation = Preferences.getMultiByteDTA("tempLocation", '');
120         if (this.tempLocation == '') {
121           // #44: generate a default tmp dir on per-profile basis
122           // hash the profD, as it would be otherwise a minor information leak
123           var dsp = Serv('@mozilla.org/file/directory_service;1', 'nsIProperties');
124           this.tempLocation = dsp.get("TmpD", Ci.nsIFile);
125           var profD = hash(dsp.get("ProfD", Ci.nsIFile).leafName);
126           this.tempLocation.append("dtatmp-" + profD);
127           Debug.log(this.tempLocation.path);
128         }
129         else {
130           this.tempLocation = new FileFactory(this.tempLocation);
131         }
132       } catch (ex) {
133         this.tempLocation = null;
134         // XXX: error handling
135       }
136     }
137     else {
138       this.tempLocation = null;
139     }
140     var conns = (this.maxInProgress * this.maxChunks) + 2;
141     ['network.http.max-connections', 'network.http.max-connections-per-server'].forEach(
142       function(e) {
143         if ((!prefName || prefName == e) && conns != Preferences.get(e, conns)) {
144           Preferences.setDTA(e, Preferences.get(e, conns));
145         }       
146         if (conns > Preferences.get(e, conns)) {
147           Preferences.set(e, conns);
148         }
149         conns = Math.floor(conns / 1.5);
150       }
151     );
152   },
153   shutdown: function() {
154     Preferences.removeObserver('network.', this);
155     this._resetConnPrefs();
156   },
157   _resetConnPrefs: function() {
158     ['network.http.max-connections', 'network.http.max-connections-per-server'].forEach(
159       function(e) {
160         let conn = Preferences.getDTA(e, 0);
161         if (conn) {
162           Preferences.set(e, conn);
163           Preferences.setDTA(e, 0);
164         }
165       }
166     );
167   }
168 }
169 Prefs.init();
Note: See TracBrowser for help on using the browser.