-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathprefs.js
More file actions
162 lines (144 loc) · 4.09 KB
/
prefs.js
File metadata and controls
162 lines (144 loc) · 4.09 KB
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
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
//
// The values are hardcoded for now.
//
let defaults = {
__proto__: null,
enabled: true,
patternsbackups: 5,
patternsbackupinterval: 24,
savestats: false,
privateBrowsing: false,
subscriptions_fallbackerrors: 5,
subscriptions_fallbackurl: "https://adblockplus.org/getSubscription?version=%VERSION%&url=%SUBSCRIPTION%&downloadURL=%URL%&error=%ERROR%&channelStatus=%CHANNELSTATUS%&responseStatus=%RESPONSESTATUS%",
subscriptions_autoupdate: true,
subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=%LANG%",
update_url_release: "https://update.adblockplus.org/%NAME%/update.json?type=%TYPE%",
update_url_devbuild: "https://adblockplus.org/devbuilds/%NAME%/update.json?type=%TYPE%",
update_last_check: 0,
update_last_error: 0,
update_soft_expiration: 0,
update_hard_expiration: 0,
currentVersion: "0.0",
notificationdata: {},
notificationurl: "https://notification.adblockplus.org/notification.json",
suppress_first_run_page: false,
disable_auto_updates: false,
first_run_subscription_auto_select: true,
notifications_ignoredcategories: [],
allowed_connection_type: ""
};
let preconfigurable = [
"suppress_first_run_page", "disable_auto_updates",
"first_run_subscription_auto_select", "allowed_connection_type"];
let values;
let prefsFileName = "prefs.json";
let listeners = [];
let isDirty = false;
let isSaving = false;
function defineProperty(key)
{
Object.defineProperty(Prefs, key,
{
get: () => values[key],
set(value)
{
if (typeof value != typeof defaults[key])
throw new Error("Attempt to change preference type");
if (value == defaults[key])
delete values[key];
else
values[key] = value;
save();
for (let listener of listeners)
listener(key);
},
enumerable: true
});
}
function load()
{
new Promise((resolve, reject) =>
{
_fileSystem.read(prefsFileName, resolve, reject);
}).then((result) =>
{
try
{
let data = JSON.parse(result.content);
for (let key in data)
if (key in defaults)
values[key] = data[key];
}
catch (e)
{
Cu.reportError(e);
}
}).catch(() =>
{
// prefs.json is expected to be missing, ignore file reading errors
}).then(() =>
{
Prefs.initialized = true;
if (typeof Prefs._initListener == "function")
Prefs._initListener();
});
}
function save()
{
if (isSaving)
{
isDirty = true;
return;
}
isDirty = false;
isSaving = true;
_fileSystem.write(prefsFileName, JSON.stringify(values), () =>
{
isSaving = false;
if (isDirty)
save();
});
}
let Prefs = exports.Prefs = {
initialized: false,
addListener(listener)
{
if (listeners.indexOf(listener) < 0)
listeners.push(listener);
},
removeListener(listener)
{
let index = listeners.indexOf(listener);
if (index >= 0)
listeners.splice(index, 1);
}
};
if (typeof _preconfiguredPrefs !== "undefined")
// Update the default prefs with what was preconfigured
for (let key in _preconfiguredPrefs)
if (preconfigurable.indexOf(key) != -1)
defaults[key] = _preconfiguredPrefs[key];
// Define defaults
for (let key in defaults)
defineProperty(key);
// Set values of prefs based on defaults
values = Object.create(defaults);
load();