globalstore.js
Go to the documentation of this file.
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 Google Safe Browsing.
15 #
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
19 #
20 # Contributor(s):
21 # Fritz Schneider <fritz@google.com> (original author)
22 # J. Paul Reed <preed@mozilla.com>
23 #
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
35 #
36 # ***** END LICENSE BLOCK *****
37 
38 
39 // A class that encapsulates data provider specific values. The
40 // root of the provider pref tree is browser.safebrowsing.provider.
41 // followed by a number, followed by specific properties. The properties
42 // that a data provider can supply are:
43 //
44 // name: The name of the provider
45 // lookupURL: The URL to send requests to in enhanced mode
46 // keyURL: Before we send URLs in enhanced mode, we need to encrypt them
47 // reportURL: When shown a warning bubble, we send back the user decision
48 // (get me out of here/ignore warning) to this URL (strip cookies
49 // first). This is optional.
50 // gethashURL: Url for requesting complete hashes from the provider.
51 // reportGenericURL: HTML page for general user feedback
52 // reportPhishURL: HTML page for notifying the provider of a new phishing page
53 // reportErrorURL: HTML page for notifying the provider of a false positive
54 
55 const kDataProviderIdPref = 'browser.safebrowsing.dataProvider';
56 const kProviderBasePref = 'browser.safebrowsing.provider.';
57 
58 #ifdef OFFICIAL_BUILD
59 const MOZ_OFFICIAL_BUILD = true;
60 #else
61 const MOZ_OFFICIAL_BUILD = false;
62 #endif
63 
64 const MOZ_PARAM_LOCALE = /\{moz:locale\}/g;
65 const MOZ_PARAM_CLIENT = /\{moz:client\}/g;
66 const MOZ_PARAM_BUILDID = /\{moz:buildid\}/g;
67 const MOZ_PARAM_VERSION = /\{moz:version\}/g;
68 
72 function PROT_DataProvider() {
73  this.prefs_ = new G_Preferences();
74 
75  this.loadDataProviderPrefs_();
76 
77  // Watch for changes in the data provider and update accordingly.
78  this.prefs_.addObserver(kDataProviderIdPref,
79  BindToObject(this.loadDataProviderPrefs_, this));
80 
81  // Watch for when anti-phishing is toggled on or off.
82  this.prefs_.addObserver(kPhishWardenEnabledPref,
83  BindToObject(this.loadDataProviderPrefs_, this));
84 }
85 
90 PROT_DataProvider.prototype.loadDataProviderPrefs_ = function() {
91  // Currently, there's no UI for changing local list provider so we
92  // hard code the value for provider 0.
93  this.updateURL_ = this.getUrlPref_(
94  'browser.safebrowsing.provider.0.updateURL');
95 
96  var id = this.prefs_.getPref(kDataProviderIdPref, null);
97 
98  // default to 0
99  if (null == id)
100  id = 0;
101 
102  var basePref = kProviderBasePref + id + '.';
103 
104  this.name_ = this.prefs_.getPref(basePref + "name", "");
105 
106  // Urls used to get data from a provider
107  this.lookupURL_ = this.getUrlPref_(basePref + "lookupURL");
108  this.keyURL_ = this.getUrlPref_(basePref + "keyURL");
109  this.reportURL_ = this.getUrlPref_(basePref + "reportURL");
110  this.gethashURL_ = this.getUrlPref_(basePref + "gethashURL");
111 
112  // Urls to HTML report pages
113  this.reportGenericURL_ = this.getUrlPref_(basePref + "reportGenericURL");
114  this.reportErrorURL_ = this.getUrlPref_(basePref + "reportErrorURL");
115  this.reportPhishURL_ = this.getUrlPref_(basePref + "reportPhishURL");
116  this.reportMalwareURL_ = this.getUrlPref_(basePref + "reportMalwareURL")
117  this.reportMalwareErrorURL_ = this.getUrlPref_(basePref + "reportMalwareErrorURL")
118 
119  // Propagate the changes to the list-manager.
120  this.updateListManager_();
121 }
122 
127 PROT_DataProvider.prototype.updateListManager_ = function() {
128  var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
129  .getService(Ci.nsIUrlListManager);
130 
131  // If we add support for changing local data providers, we need to add a
132  // pref observer that sets the update url accordingly.
133  listManager.setUpdateUrl(this.getUpdateURL());
134 
135  // setKeyUrl has the side effect of fetching a key from the server.
136  // This shouldn't happen if anti-phishing/anti-malware is disabled.
137  var isEnabled = this.prefs_.getPref(kPhishWardenEnabledPref, false) ||
138  this.prefs_.getPref(kMalwareWardenEnabledPref, false);
139  if (isEnabled) {
140  listManager.setKeyUrl(this.keyURL_);
141  }
142 
143  listManager.setGethashUrl(this.getGethashURL());
144 }
145 
149 PROT_DataProvider.prototype.getUrlPref_ = function(prefName) {
150  var url = this.prefs_.getPref(prefName);
151 
152  var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
153  .getService(Components.interfaces.nsIXULAppInfo);
154 
155  var mozClientStr = this.prefs_.getPref("browser.safebrowsing.clientid",
156  MOZ_OFFICIAL_BUILD ? 'navclient-auto-ffox' : appInfo.name);
157 
158  var versionStr = this.prefs_.getPref("browser.safebrowsing.clientver",
159  appInfo.version);
160 
161  // Parameter substitution
162  url = url.replace(MOZ_PARAM_LOCALE, this.getLocale_());
163  url = url.replace(MOZ_PARAM_CLIENT, mozClientStr);
164  url = url.replace(MOZ_PARAM_BUILDID, appInfo.appBuildID);
165  url = url.replace(MOZ_PARAM_VERSION, versionStr);
166  return url;
167 }
168 
172 PROT_DataProvider.prototype.getLocale_ = function() {
173  const localePref = "general.useragent.locale";
174  var locale = this.getLocalizedPref_(localePref);
175  if (locale)
176  return locale;
177 
178  // Not localized
179  var prefs = new G_Preferences();
180  return prefs.getPref(localePref, "");
181 }
182 
186 PROT_DataProvider.prototype.getLocalizedPref_ = function(aPrefName) {
187  // G_Preferences doesn't know about complex values, so we use the
188  // xpcom object directly.
189  var prefs = Cc["@mozilla.org/preferences-service;1"]
190  .getService(Ci.nsIPrefBranch);
191  try {
192  return prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data;
193  } catch (ex) {
194  }
195  return "";
196 }
197 
199 // Getters for the remote provider pref values mentioned above.
200 PROT_DataProvider.prototype.getName = function() {
201  return this.name_;
202 }
203 
204 PROT_DataProvider.prototype.getUpdateURL = function() {
205  return this.updateURL_;
206 }
207 
208 PROT_DataProvider.prototype.getLookupURL = function() {
209  return this.lookupURL_;
210 }
211 
212 PROT_DataProvider.prototype.getGethashURL = function() {
213  return this.gethashURL_;
214 }
215 
216 PROT_DataProvider.prototype.getReportGenericURL = function() {
217  return this.reportGenericURL_;
218 }
219 PROT_DataProvider.prototype.getReportErrorURL = function() {
220  return this.reportErrorURL_;
221 }
222 PROT_DataProvider.prototype.getReportPhishURL = function() {
223  return this.reportPhishURL_;
224 }
225 PROT_DataProvider.prototype.getReportMalwareURL = function() {
226  return this.reportMalwareURL_;
227 }
228 PROT_DataProvider.prototype.getReportMalwareErrorURL = function() {
229  return this.reportMalwareErrorURL_;
230 }
const Cc
version(170)
const kProviderBasePref
Definition: globalstore.js:56
const MOZ_PARAM_BUILDID
Definition: globalstore.js:66
const MOZ_PARAM_LOCALE
Definition: globalstore.js:64
const MOZ_OFFICIAL_BUILD
Definition: globalstore.js:61
function PROT_DataProvider()
Definition: globalstore.js:72
this _document this
Definition: FeedWriter.js:1085
const kMalwareWardenEnabledPref
const kDataProviderIdPref
Definition: globalstore.js:55
return null
Definition: FeedWriter.js:1143
AddonMetadata prototype
function url(spec)
var prefs
Definition: FeedWriter.js:1169
const Ci
const kPhishWardenEnabledPref
const MOZ_PARAM_CLIENT
Definition: globalstore.js:65
const MOZ_PARAM_VERSION
Definition: globalstore.js:67