~ubuntu-branches/debian/stretch/foxyproxy/stretch

« back to all changes in this revision

Viewing changes to src/components/autoconf.js

  • Committer: Package Import Robot
  • Author(s): Georg Koppen
  • Date: 2011-11-18 11:12:37 UTC
  • mfrom: (1.2.3)
  • Revision ID: package-import@ubuntu.com-20111118111237-r5fg91nr5dwfl01n
Tags: 3.3-2
cleaned up source package (removed .svn directories and .xpi files) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
2
  FoxyProxy
3
 
  Copyright (C) 2006-#%#% Eric H. Jung and LeahScape, Inc.
 
3
  Copyright (C) 2006-#%#% Eric H. Jung and FoxyProxy, Inc.
4
4
  http://getfoxyproxy.org/
5
5
  eric.jung@yahoo.com
6
6
 
10
10
**/
11
11
if (!CI) {
12
12
  // we're not being included by foxyproxy.js
13
 
  var CI = Components.interfaces, CC = Components.classes, CR = Components.results, fp;
 
13
  var CI = Components.interfaces, CC = Components.classes,
 
14
    CR = Components.results, fp;
14
15
 
15
16
  // Get attribute from node if it exists, otherwise return |def|.
16
17
  // No exceptions, no errors, no null returns.
21
22
  // Boolean version of GetSafe
22
23
  var gGetSafeAttrB = function(n, name, def) {
23
24
    n.QueryInterface(CI.nsIDOMElement);
24
 
    return n ? (n.hasAttribute(name) ? n.getAttribute(name)=="true" : def) : def;
 
25
    return n ? (n.hasAttribute(name) ? n.getAttribute(name) == "true" : def) :
 
26
      def;
25
27
  };
26
28
}
27
29
 
30
32
///////////////////////////// AutoConf class ///////////////////////
31
33
function AutoConf(owner, fpp) {
32
34
  this.wrappedJSObject = this;
33
 
  fp = fpp || CC["@leahscape.org/foxyproxy/service;1"].getService().wrappedJSObject;    
 
35
  fp = fpp || CC["@leahscape.org/foxyproxy/service;1"].
 
36
    getService().wrappedJSObject;    
34
37
  this.timer = CC["@mozilla.org/timer;1"].createInstance(CI.nsITimer);
35
38
  this.owner = owner;
36
39
  this._resolver = new fpProxyAutoConfig(this);
101
104
    req.channel.loadFlags |= CI.nsIRequest.LOAD_BYPASS_CACHE;
102
105
    req.send(null);
103
106
    if (req.status == 200 ||
104
 
        (req.status == 0 && (url.indexOf("file://") == 0 || url.indexOf("ftp://") == 0 || url.indexOf("relative://") == 0))) {
 
107
        (req.status == 0 && (url.indexOf("file://") == 0 ||
 
108
        url.indexOf("ftp://") == 0 || url.indexOf("relative://") == 0))) {
105
109
      new fpProxyAutoConfig(this).init(url, req.responseText);
106
110
    }
107
111
    else throw new Error(fp.getMessage("http.error", [req.status]));
108
112
  },
109
113
 
110
114
  loadPAC : function() {
 
115
    let autoconfMode = this.owner.autoconfMode;
 
116
    let autoconfMessage = "";
111
117
    try {
112
118
      var req = CC["@mozilla.org/xmlextras/xmlhttprequest;1"]
113
119
        .createInstance(CI.nsIXMLHttpRequest);
117
123
      req.send(null);
118
124
    }
119
125
    catch(e) {
120
 
      this.badPAC("pac.status.loadfailure", e);
 
126
      if (autoconfMode === "pac") {
 
127
        autoconfMessage = "pac.status.loadfailure2"; 
 
128
      } else {
 
129
        autoconfMessage = "wpad.status.loadfailure";
 
130
      }
 
131
      this.badPAC(autoconfMessage, e);
121
132
      return;
122
133
    }
123
 
    if (req.status == 200 ||
124
 
      (req.status == 0 && (this.url.indexOf("file://") == 0 || this.url.indexOf("ftp://") == 0 || this.url.indexOf("relative://") == 0))) {
125
 
        try {
126
 
          this._resolver.init(this.url, req.responseText);
127
 
        }
128
 
        catch(e) {
129
 
          this.badPAC("pac.status.error", e);
130
 
          return;
131
 
        }
132
 
        this.loadNotification && fp.notifier.alert(fp.getMessage("pac.status"), fp.getMessage("pac.status.success", [this.owner.name]));
133
 
        this.owner._enabled = true; // Use _enabled so we don't loop infinitely
134
 
        //if (this.disabledDueToBadPAC) {
135
 
          //this.disabledDueToBadPAC = false; /* reset */
136
 
          //this.owner.fp.writeSettings();
137
 
        //}
 
134
    if (req.status == 200 || (req.status == 0 &&
 
135
         (this.url.indexOf("file://") == 0 || this.url.indexOf("ftp://") == 0 ||
 
136
          this.url.indexOf("relative://") == 0))) {
 
137
      try {
 
138
        this._resolver.init(this.url, req.responseText);
 
139
      }
 
140
      catch(e) {
 
141
        if (autoconfMode === "pac") {
 
142
          autoconfMessage = "pac.status.error2"; 
 
143
        } else {
 
144
          autoconfMessage = "wpad.status.error";
 
145
        }
 
146
        this.badPAC(autoconfMessage, e); 
 
147
        return;
 
148
      }
 
149
      let autoconfMessageHelper = "";
 
150
      if (autoconfMode === "pac") {
 
151
          autoconfMessage = "pac.status"; 
 
152
          autoconfMessageHelper = "pac.status.success2";
 
153
      } else {
 
154
          autoconfMessage = "wpad.status";
 
155
          autoconfMessageHelper = "wpad.status.success";
 
156
      } 
 
157
      this.loadNotification && fp.notifier.alert(fp.getMessage(autoconfMessage),
 
158
        fp.getMessage(autoconfMessageHelper, [this.owner.name]));
 
159
      // Use _enabled so we don't loop infinitely 
 
160
      this.owner._enabled = true;
 
161
      //if (this.disabledDueToBadPAC) {
 
162
        //this.disabledDueToBadPAC = false; /* reset */
 
163
        //this.owner.fp.writeSettings();
 
164
      //}
138
165
    }
139
166
    else {
140
 
      this.badPAC("pac.status.loadfailure", new Error(fp.getMessage("http.error", [req.status])));
 
167
      if (autoconfMode === "pac") {
 
168
        autoconfMessage = "pac.status.loadfailure2"; 
 
169
      } else {
 
170
        autoconfMessage = "wpad.status.loadfailure";
 
171
      } 
 
172
      this.badPAC(autoconfMessage,
 
173
        new Error(fp.getMessage("http.error", [req.status])));
141
174
    }
142
175
  },
143
176
 
144
177
  badPAC : function(r, e) {
145
 
    //if (!this.disabledDueToBadPAC) { /* marker to try loading the PAC next time */
 
178
    //if (!this.disabledDueToBadPAC) {
 
179
      /* marker to try loading the PAC next time */
146
180
      //this.disabledDueToBadPAC = true;
147
181
      //this.owner.fp.writeSettings();
148
182
    //}
 
183
    let autoconfMessage = "";
 
184
    if (this.owner.autoconfMode === "pac") {
 
185
      autoconfMessage = "pac.status"; 
 
186
    } else {
 
187
      autoconfMessage = "wpad.status";
 
188
    } 
149
189
    var msg = fp.getMessage(r, [this.owner.name]) + "\n\n" + e.message;
150
 
    this.errorNotification && fp.notifier.alert(fp.getMessage("pac.status"), msg);
 
190
    this.errorNotification && fp.notifier.alert(fp.getMessage(autoconfMessage),
 
191
      msg);
151
192
    if (this.owner.lastresort)
152
193
      this.owner.mode = "direct"; // don't disable!
153
194
    else if (this.disableOnBadPAC)
193
234
 
194
235
    /** throws a localized Error object on error */
195
236
    init: function(pacURI, pacText) {
 
237
      let autoconfMessage = "";
196
238
      if (pacURI == "" || pacText == "") {
197
239
        dump("FoxyProxy: init(), pacURI or pacText empty\n");    
198
 
        throw new Error(fp.getMessage("pac.empty"));
 
240
        if (this.owner.owner.autoconfMode  === "pac") {
 
241
          autoconfMessage = "pac.empty"; 
 
242
        } else {
 
243
          autoconfMessage = "wpad.empty";
 
244
        } 
 
245
        throw new Error(fp.getMessage(autoconfMessage));
199
246
      }
200
247
      this.sandbox = new Components.utils.Sandbox(pacURI);
201
248
      Components.utils.evalInSandbox(pacUtils, this.sandbox);
218
265
      // everytime in getProxyxForURI().
219
266
      if (!("FindProxyForURL" in this.sandbox)) {
220
267
        dump("FoxyProxy: init(), FindProxyForURL not found\n");
221
 
        throw new Error(fp.getMessage("pac.fcn.notfound"));
 
268
        if (this.owner.owner.autoconfMode === "pac") {
 
269
          autoconfMessage = "pac.fcn.notfound2"; 
 
270
        } else {
 
271
          autoconfMessage = "wpad.fcn.notfound";
 
272
        } 
 
273
        throw new Error(fp.getMessage(autoconfMessage));
222
274
      }
223
275
      return true;
224
276
    },