-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathupdater.js
227 lines (194 loc) · 5.9 KB
/
updater.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var URL = "http://skrul.com/projects/greasefire/indexes/";
var TIME_CHECK_INTERVAL = 60 * 1000;
var DAYS_TO_MILLISECONDS = 24 * 60 * 60 * 1000;
function Updater(store) {
function setIf(key, value) {
if (localStorage[key] === undefined) {
localStorage[key] = value;
}
}
setIf("enable_scheduled_updates", true);
setIf("update_interval_ms", DAYS_TO_MILLISECONDS);
// Schedule our first update for 30 seconds from now.
setIf("next_update_date", Date.now() + (1000 * 30));
this.store_ = store;
this.update_interval_ms_ = parseInt(localStorage["update_interval_ms"]);
this.next_update_date_ =
new Date(parseInt(localStorage["next_update_date"]));
this.isUpdating_ = false;
this.enable_scheduled_updates_ =
localStorage["enable_scheduled_updates"] == "true";
this.timer_id = null;
}
Updater.prototype = {
reset: function() {
delete localStorage["enable_scheduled_updates"];
delete localStorage["update_interval_ms"];
delete localStorage["next_update_date"];
},
startScheduledUpdates: function() {
this.startTimer_();
},
enable_scheduled_updates: function() {
return this.enable_scheduled_updates_;
},
set_enable_scheduled_updates: function(val) {
this.enable_scheduled_updates_ = val;
localStorage["enable_scheduled_updates"] = val;
},
next_update_date: function() {
return this.next_update_date_;
},
notifyStart_: function() {
chrome.extension.sendRequest({action: "updater-start"});
},
notifyDone_: function() {
chrome.extension.sendRequest({action: "updater-done"});
},
notifyStatus_: function(message) {
chrome.extension.sendRequest(
{action: "updater-status", message: message});
},
update: wrap(function(force, callback) {
if (this.isUpdating_) {
callback(false, "Already updating");
return;
}
var that = this;
this.notifyStart_();
var finish = function(success, error) {
that.scheduleNextUpdate_();
that.notifyDone_();
callback(success, error);
};
this.getVersion_(URL + "latest", function(success, latest_version) {
if (!success) {
finish(false, "can't read version");
return;
}
var current_version = that.store_.current_version();
d("latest version " + latest_version);
d("current version " + current_version);
if (!force && current_version && latest_version <= current_version) {
that.notifyStatus_("Index is up to date.");
finish(true);
return;
}
var base_url = URL + formatISO8601(latest_version);
that.updateFromBaseUrl_(base_url, latest_version, function(success, error) {
finish(success, error);
});
});
}),
updateWithLocalData: wrap(function(callback) {
if (this.isUpdating_) {
callback(false, "Already updating");
return;
}
var that = this;
this.getVersion_("indexes/latest", function(success, local_version) {
if (!success) {
callback(false, "can't read local version");
return;
}
d("local version " + local_version);
that.updateFromBaseUrl_("indexes", local_version, callback);
});
}),
updateFromBaseUrl_: wrap(function(base_url, version, callback) {
d("Updater.updateFromBaseUrl_ " + base_url + " " + version);
var that = this;
this.isUpdating_ = true;
var finish = function(success, error) {
that.isUpdating_ = false;
callback(success, error);
};
var includes;
var excludes;
var scripts;
var on_progress = function(loaded, total) {
chrome.extension.sendRequest({
action: "updater-status",
message: "Downloading indexes (" + loaded + ")"});
};
var done_downloading = function(data, error) {
if (data) {
scripts = data;
that.store_.installNewData(
version, includes, excludes, scripts, function() {
finish(true);
});
} else {
finish(false, error);
}
};
var includes_dl = new Downloader(on_progress);
var excludes_dl = new Downloader(on_progress);
var scripts_dl = new Downloader(on_progress);
includes_dl.get(base_url + "/include.dat", function(data, status) {
if (!data) {
finish(false, status);
return;
}
includes = fixEncoding(data);
excludes_dl.get(base_url + "/exclude.dat", function(data, status) {
if (!data) {
finish(false, status);
return;
}
excludes = fixEncoding(data);
scripts_dl.get(base_url + "/scripts.txt", function(data, status) {
if (!data) {
finish(false, status);
return;
}
scripts = data;
that.store_.installNewData(
version, includes, excludes, scripts, function() {
finish(true);
});
});
});
});
}),
getVersion_: wrap(function(url, callback) {
var downloader = new Downloader();
downloader.get(url + "?" + Math.random(), function(data, error) {
if (data) {
var d = parseISO8601(data);
if (d) {
callback(true, d);
return;
}
}
callback(false, error);
});
}),
startTimer_: function() {
if (this.timer_id)
return;
var that = this;
this.timer_id = window.setTimeout(function() {
that.tick_();
}, TIME_CHECK_INTERVAL);
},
tick_: function() {
d("tick " + (new Date()));
if (this.enable_scheduled_updates_ &&
Date.now() >= this.next_update_date_) {
var that = this;
this.update(false, function(success, error) {
if (!success) {
d("Error doing scheduled update: " + error);
}
that.startTimer_();
});
} else {
this.startTimer_();
}
},
scheduleNextUpdate_: function() {
this.next_update_date_ = new Date(Date.now() + this.update_interval_ms_);
localStorage["next_update_date"] = this.next_update_date_.getTime();
}
}