File: SafeBrowsing.js

package info (click to toggle)
wine-gecko-2.21 2.21%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 646,272 kB
  • ctags: 630,086
  • sloc: cpp: 2,895,786; ansic: 1,502,970; python: 156,675; asm: 115,373; java: 111,421; sh: 63,309; xml: 62,872; makefile: 58,685; perl: 19,182; objc: 3,461; yacc: 2,051; lex: 979; pascal: 929; exp: 449; php: 244; lisp: 228; awk: 211; sed: 26; csh: 21; ada: 16; ruby: 3
file content (168 lines) | stat: -rw-r--r-- 5,792 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

const kPhishWardenEnabledPref = "browser.safebrowsing.enabled";
const kMalwareWardenEnabledPref = "browser.safebrowsing.malware.enabled";

// This XPCOM object doesn't have a public interface. It just works quietly in the background
function SafeBrowsing() {
  this.listManager = null;

  // Once we register tables, their respective names will be listed here.
  this.phishing = {
    pref: kPhishWardenEnabledPref,
    blackTables: [],
    whiteTables: []
  };
  this.malware = {
    pref: kMalwareWardenEnabledPref,
    blackTables: [],
    whiteTables: []
  };

  // Get notifications when the phishing or malware warden enabled pref changes
  Services.prefs.addObserver(kPhishWardenEnabledPref, this, true);
  Services.prefs.addObserver(kMalwareWardenEnabledPref, this, true);
}

SafeBrowsing.prototype = {
  classID: Components.ID("{aadaed90-6c03-42d0-924a-fc61198ff283}"),

  QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore,
                                         Ci.nsIDOMEventListener,
                                         Ci.nsIObserver,
                                         Ci.nsISupportsWeakReference]),

  observe: function sb_observe(aSubject, aTopic, aData) {
    switch (aTopic) {
      case "app-startup":
        Services.obs.addObserver(this, "final-ui-startup", true);
        Services.obs.addObserver(this, "xpcom-shutdown", true);
        break;
      case "final-ui-startup":
        Services.obs.removeObserver(this, "final-ui-startup");
        this._startup();
        break;
      case "xpcom-shutdown":
        Services.obs.removeObserver(this, "xpcom-shutdown");
        this._shutdown();
        break;
      case "nsPref:changed":
        if (aData == kPhishWardenEnabledPref)
          this.maybeToggleUpdateChecking(this.phishing);
        else if (aData == kMalwareWardenEnabledPref)
          this.maybeToggleUpdateChecking(this.malware);
        break;
    }
  },

  _startup: function sb_startup() {
    this.listManager = Cc["@mozilla.org/url-classifier/listmanager;1"].getService(Ci.nsIUrlListManager);

    // Add a test chunk to the database
    let testData = "mozilla.org/firefox/its-an-attack.html";
    let testUpdate =
      "n:1000\ni:test-malware-simple\nad:1\n" +
      "a:1:32:" + testData.length + "\n" +
      testData;

    testData = "mozilla.org/firefox/its-a-trap.html";
    testUpdate +=
      "n:1000\ni:test-phish-simple\nad:1\n" +
      "a:1:32:" + testData.length + "\n" +
      testData;

    let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService);

    let listener = {
      QueryInterface: function(aIID) {
        if (aIID.equals(Ci.nsISupports) || aIID.equals(Ci.nsIUrlClassifierUpdateObserver))
          return this;
        throw Cr.NS_ERROR_NO_INTERFACE;
      },

      updateUrlRequested: function(aURL) { },
      streamFinished: function(aStatus) { },
      updateError: function(aErrorCode) { },
      updateSuccess: function(aRequestedTimeout) { }
    };

    try {
      dbService.beginUpdate(listener, "test-malware-simple,test-phish-simple", "");
      dbService.beginStream("", "");
      dbService.updateStream(testUpdate);
      dbService.finishStream();
      dbService.finishUpdate();
    } catch(ex) {}

    this.registerBlackTable("goog-malware-shavar", this.malware);
    this.maybeToggleUpdateChecking(this.malware);

    this.registerBlackTable("goog-phish-shavar", this.phishing);
    this.maybeToggleUpdateChecking(this.phishing);
  },

  _shutdown: function sb_shutdown() {
    Services.prefs.removeObserver(kPhishWardenEnabledPref, this);
    Services.prefs.removeObserver(kMalwareWardenEnabledPref, this);

    this.listManager = null;
  },

  enableBlacklistTableUpdates: function sb_enableBlacklistTableUpdates(aWarden) {
    for (let i = 0; i < aWarden.blackTables.length; ++i) {
      this.listManager.enableUpdate(aWarden.blackTables[i]);
    }
  },

  disableBlacklistTableUpdates: function sb_disableBlacklistTableUpdates(aWarden) {
    for (let i = 0; i < aWarden.blackTables.length; ++i) {
      this.listManager.disableUpdate(aWarden.blackTables[i]);
    }
  },

  enableWhitelistTableUpdates: function sb_enableWhitelistTableUpdates(aWarden) {
    for (let i = 0; i < this.whiteTables.length; ++i) {
      this.listManager.enableUpdate(this.whiteTables[i]);
    }
  },

  disableWhitelistTableUpdates: function sb_disableWhitelistTableUpdates(aWarden) {
    for (let i = 0; i < aWarden.whiteTables.length; ++i) {
      this.listManager.disableUpdate(aWarden.whiteTables[i]);
    }
  },

  registerBlackTable: function sb_registerBlackTable(aTableName, aWarden) {
    let result = this.listManager.registerTable(aTableName, false);
    if (result)
      aWarden.blackTables.push(aTableName);
    return result;
  },

  registerWhiteTable: function sb_registerWhiteTable(aTableName, aWarden) {
    let result = this.listManager.registerTable(aTableName, false);
    if (result)
      aWarden.whiteTables.push(aTableName);
    return result;
  },

  maybeToggleUpdateChecking: function sb_maybeToggleUpdateChecking(aWarden) {
    let enabled = Services.prefs.getBoolPref(aWarden.pref);
    if (enabled)
      this.enableBlacklistTableUpdates(aWarden);
    else
      this.disableBlacklistTableUpdates(aWarden);
  }
}

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SafeBrowsing]);