Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/prefs.js

Issue 29545700: Issue 5685 - Pass ESLint (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Address review comments Created Sept. 18, 2017, 12:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/notificationShowRegistration.js ('k') | lib/updater.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict";
19
18 // 20 //
19 // The values are hardcoded for now. 21 // The values are hardcoded for now.
20 // 22 //
21 23
22 let defaults = { 24 let defaults = {
23 __proto__: null, 25 __proto__: null,
24 enabled: true, 26 enabled: true,
25 patternsbackups: 5, 27 patternsbackups: 5,
26 patternsbackupinterval: 24, 28 patternsbackupinterval: 24,
27 savestats: false, 29 savestats: false,
(...skipping 12 matching lines...) Expand all
40 currentVersion: "0.0", 42 currentVersion: "0.0",
41 notificationdata: {}, 43 notificationdata: {},
42 notificationurl: "https://notification.adblockplus.org/notification.json", 44 notificationurl: "https://notification.adblockplus.org/notification.json",
43 suppress_first_run_page: false, 45 suppress_first_run_page: false,
44 disable_auto_updates: false, 46 disable_auto_updates: false,
45 first_run_subscription_auto_select: true, 47 first_run_subscription_auto_select: true,
46 notifications_ignoredcategories: [], 48 notifications_ignoredcategories: [],
47 allowed_connection_type: "" 49 allowed_connection_type: ""
48 }; 50 };
49 51
50 let preconfigurable = ["suppress_first_run_page", "disable_auto_updates", 52 let preconfigurable = [
53 "suppress_first_run_page", "disable_auto_updates",
51 "first_run_subscription_auto_select", "allowed_connection_type"]; 54 "first_run_subscription_auto_select", "allowed_connection_type"];
52 55
53 let values; 56 let values;
54 let prefsFileName = "prefs.json"; 57 let prefsFileName = "prefs.json";
55 let listeners = []; 58 let listeners = [];
56 let isDirty = false; 59 let isDirty = false;
57 let isSaving = false; 60 let isSaving = false;
58 61
59 function defineProperty(key) 62 function defineProperty(key)
60 { 63 {
61 Object.defineProperty(Prefs, key, 64 Object.defineProperty(Prefs, key,
62 {
63 get: () => values[key],
64 set: function(value)
65 { 65 {
66 if (typeof value != typeof defaults[key]) 66 get: () => values[key],
67 throw new Error("Attempt to change preference type"); 67 set(value)
68 {
69 if (typeof value != typeof defaults[key])
70 throw new Error("Attempt to change preference type");
68 71
69 if (value == defaults[key]) 72 if (value == defaults[key])
70 delete values[key]; 73 delete values[key];
71 else 74 else
72 values[key] = value; 75 values[key] = value;
73 save(); 76 save();
74 77
75 for (let listener of listeners) 78 for (let listener of listeners)
76 listener(key); 79 listener(key);
77 }, 80 },
78 enumerable: true 81 enumerable: true
79 }); 82 });
80 } 83 }
81 84
82 function load() 85 function load()
83 { 86 {
84 _fileSystem.read(prefsFileName, function (result) 87 _fileSystem.read(prefsFileName, result =>
85 { 88 {
86 // prefs.json is expected to be missing, ignore errors reading file 89 // prefs.json is expected to be missing, ignore errors reading file
87 if (!result.error) 90 if (!result.error)
88 { 91 {
89 try 92 try
90 { 93 {
91 let data = JSON.parse(result.content); 94 let data = JSON.parse(result.content);
92 for (let key in data) 95 for (let key in data)
93 if (key in defaults) 96 if (key in defaults)
94 values[key] = data[key]; 97 values[key] = data[key];
(...skipping 13 matching lines...) Expand all
108 function save() 111 function save()
109 { 112 {
110 if (isSaving) 113 if (isSaving)
111 { 114 {
112 isDirty = true; 115 isDirty = true;
113 return; 116 return;
114 } 117 }
115 118
116 isDirty = false; 119 isDirty = false;
117 isSaving = true; 120 isSaving = true;
118 _fileSystem.write(prefsFileName, JSON.stringify(values), function () 121 _fileSystem.write(prefsFileName, JSON.stringify(values), () =>
119 { 122 {
120 isSaving = false; 123 isSaving = false;
121 if (isDirty) 124 if (isDirty)
122 save(); 125 save();
123 }); 126 });
124 } 127 }
125 128
126 let Prefs = exports.Prefs = { 129 let Prefs = exports.Prefs = {
127 initialized: false, 130 initialized: false,
128 131
129 addListener: function(listener) 132 addListener(listener)
130 { 133 {
131 if (listeners.indexOf(listener) < 0) 134 if (listeners.indexOf(listener) < 0)
132 listeners.push(listener); 135 listeners.push(listener);
133 }, 136 },
134 137
135 removeListener: function(listener) 138 removeListener(listener)
136 { 139 {
137 let index = listeners.indexOf(listener); 140 let index = listeners.indexOf(listener);
138 if (index >= 0) 141 if (index >= 0)
139 listeners.splice(index, 1); 142 listeners.splice(index, 1);
140 }, 143 }
141 }; 144 };
142 145
143 // Update the default prefs with what was preconfigured 146 // Update the default prefs with what was preconfigured
144 for (let key in _preconfiguredPrefs) 147 for (let key in _preconfiguredPrefs)
145 if (preconfigurable.indexOf(key) != -1) 148 if (preconfigurable.indexOf(key) != -1)
146 defaults[key] = _preconfiguredPrefs[key]; 149 defaults[key] = _preconfiguredPrefs[key];
147 150
148 // Define defaults 151 // Define defaults
149 for (let key in defaults) 152 for (let key in defaults)
150 defineProperty(key); 153 defineProperty(key);
151 154
152 // Set values of prefs based on defaults 155 // Set values of prefs based on defaults
153 values = Object.create(defaults); 156 values = Object.create(defaults);
154 157
155 load(); 158 load();
OLDNEW
« no previous file with comments | « lib/notificationShowRegistration.js ('k') | lib/updater.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld