Backed out 2 changesets (bug 1864896) for causing node failures. CLOSED TREE
[gecko.git] / browser / components / preferences / dialogs / blocklists.js
blobc28ee09f96968bc61c7a16ad050a29f824bcfea5
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const BASE_LIST_ID = "base";
6 const CONTENT_LIST_ID = "content";
7 const TRACK_SUFFIX = "-track-digest256";
8 const TRACKING_TABLE_PREF = "urlclassifier.trackingTable";
9 const LISTS_PREF_BRANCH = "browser.safebrowsing.provider.mozilla.lists.";
11 var gBlocklistManager = {
12   _type: "",
13   _blockLists: [],
14   _tree: null,
16   _view: {
17     _rowCount: 0,
18     get rowCount() {
19       return this._rowCount;
20     },
21     getCellText(row, column) {
22       if (column.id == "listCol") {
23         let list = gBlocklistManager._blockLists[row];
24         return list.name;
25       }
26       return "";
27     },
29     isSeparator(index) {
30       return false;
31     },
32     isSorted() {
33       return false;
34     },
35     isContainer(index) {
36       return false;
37     },
38     setTree(tree) {},
39     getImageSrc(row, column) {},
40     getCellValue(row, column) {
41       if (column.id == "selectionCol") {
42         return gBlocklistManager._blockLists[row].selected;
43       }
44       return undefined;
45     },
46     cycleHeader(column) {},
47     getRowProperties(row) {
48       return "";
49     },
50     getColumnProperties(column) {
51       return "";
52     },
53     getCellProperties(row, column) {
54       if (column.id == "selectionCol") {
55         return "checkmark";
56       }
58       return "";
59     },
60   },
62   onLoad() {
63     this.init();
64     document.addEventListener("dialogaccept", () => this.onApplyChanges());
65   },
67   init() {
68     if (this._type) {
69       // reusing an open dialog, clear the old observer
70       this.uninit();
71     }
73     this._type = "tracking";
75     this._loadBlockLists();
76   },
78   uninit() {},
80   onListSelected() {
81     for (let list of this._blockLists) {
82       list.selected = false;
83     }
84     this._blockLists[this._tree.currentIndex].selected = true;
86     this._updateTree();
87   },
89   onApplyChanges() {
90     let activeList = this._getActiveList();
91     let selected = null;
92     for (let list of this._blockLists) {
93       if (list.selected) {
94         selected = list;
95         break;
96       }
97     }
99     if (activeList !== selected.id) {
100       let trackingTable = Services.prefs.getCharPref(TRACKING_TABLE_PREF);
101       if (selected.id != CONTENT_LIST_ID) {
102         trackingTable = trackingTable.replace(
103           "," + CONTENT_LIST_ID + TRACK_SUFFIX,
104           ""
105         );
106       } else {
107         trackingTable += "," + CONTENT_LIST_ID + TRACK_SUFFIX;
108       }
109       Services.prefs.setCharPref(TRACKING_TABLE_PREF, trackingTable);
111       // Force an update after changing the tracking protection table.
112       let listmanager = Cc[
113         "@mozilla.org/url-classifier/listmanager;1"
114       ].getService(Ci.nsIUrlListManager);
115       if (listmanager) {
116         listmanager.forceUpdates(trackingTable);
117       }
118     }
119   },
121   async _loadBlockLists() {
122     this._blockLists = [];
124     // Load blocklists into a table.
125     let branch = Services.prefs.getBranch(LISTS_PREF_BRANCH);
126     let itemArray = branch.getChildList("");
127     for (let itemName of itemArray) {
128       try {
129         let list = await this._createBlockList(itemName);
130         this._blockLists.push(list);
131       } catch (e) {
132         // Ignore bogus or missing list name.
133         continue;
134       }
135     }
137     this._updateTree();
138   },
140   async _createBlockList(id) {
141     let branch = Services.prefs.getBranch(LISTS_PREF_BRANCH);
142     let l10nKey = branch.getCharPref(id);
144     // eslint-disable-next-line mozilla/prefer-formatValues
145     let [listName, description] = await document.l10n.formatValues([
146       { id: `blocklist-item-${l10nKey}-listName` },
147       { id: `blocklist-item-${l10nKey}-description` },
148     ]);
150     // eslint-disable-next-line mozilla/prefer-formatValues
151     let name = await document.l10n.formatValue("blocklist-item-list-template", {
152       listName,
153       description,
154     });
156     return {
157       id,
158       name,
159       selected: this._getActiveList() === id,
160     };
161   },
163   _updateTree() {
164     this._tree = document.getElementById("blocklistsTree");
165     this._view._rowCount = this._blockLists.length;
166     this._tree.view = this._view;
167   },
169   _getActiveList() {
170     let trackingTable = Services.prefs.getCharPref(TRACKING_TABLE_PREF);
171     return trackingTable.includes(CONTENT_LIST_ID)
172       ? CONTENT_LIST_ID
173       : BASE_LIST_ID;
174   },