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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
#include "WriteStumbleOnThread.h"
#include "StumblerLogging.h"
#include "UploadStumbleRunnable.h"
#include "nsDumpUtils.h"
#include "nsGZFileWriter.h"
#include "nsIFileStreams.h"
#include "nsIInputStream.h"
#include "nsPrintfCString.h"
#define MAXFILESIZE_KB (15 * 1024)
#define ONEDAY_IN_MSEC (24 * 60 * 60 * 1000)
#define MAX_UPLOAD_ATTEMPTS 20
mozilla::Atomic<bool> WriteStumbleOnThread::sIsFileWaitingForUpload(false);
mozilla::Atomic<bool> WriteStumbleOnThread::sIsAlreadyRunning(false);
WriteStumbleOnThread::UploadFreqGuard WriteStumbleOnThread::sUploadFreqGuard = {0};
#define FILENAME_INPROGRESS NS_LITERAL_CSTRING("stumbles.json.gz")
#define FILENAME_COMPLETED NS_LITERAL_CSTRING("stumbles.done.json.gz")
#define OUTPUT_DIR NS_LITERAL_CSTRING("mozstumbler")
class DeleteRunnable : public Runnable
{
public:
DeleteRunnable() {}
NS_IMETHOD
Run() override
{
nsCOMPtr<nsIFile> tmpFile;
nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_COMPLETED,
getter_AddRefs(tmpFile),
OUTPUT_DIR,
nsDumpUtils::CREATE);
if (NS_SUCCEEDED(rv)) {
tmpFile->Remove(true);
}
// critically, this sets this flag to false so writing can happen again
WriteStumbleOnThread::sIsAlreadyRunning = false;
WriteStumbleOnThread::sIsFileWaitingForUpload = false;
return NS_OK;
}
private:
~DeleteRunnable() {}
};
bool
WriteStumbleOnThread::IsFileWaitingForUpload()
{
return sIsFileWaitingForUpload;
}
void
WriteStumbleOnThread::UploadEnded(bool deleteUploadFile)
{
if (!deleteUploadFile) {
sIsAlreadyRunning = false;
return;
}
nsCOMPtr<nsIEventTarget> target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
MOZ_ASSERT(target);
nsCOMPtr<nsIRunnable> event = new DeleteRunnable();
target->Dispatch(event, NS_DISPATCH_NORMAL);
}
void
WriteStumbleOnThread::WriteJSON(Partition aPart)
{
MOZ_ASSERT(!NS_IsMainThread());
nsCOMPtr<nsIFile> tmpFile;
nsresult rv;
rv = nsDumpUtils::OpenTempFile(FILENAME_INPROGRESS, getter_AddRefs(tmpFile),
OUTPUT_DIR, nsDumpUtils::CREATE);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("Open a file for stumble failed");
return;
}
RefPtr<nsGZFileWriter> gzWriter = new nsGZFileWriter(nsGZFileWriter::Append);
rv = gzWriter->Init(tmpFile);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("gzWriter init failed");
return;
}
/*
The json format is like below.
{items:[
{item},
{item},
{item}
]}
*/
// Need to add "]}" after the last item
if (aPart == Partition::End) {
rv = gzWriter->Write("]}");
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("gzWriter Write failed");
}
rv = gzWriter->Finish();
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("ostream finish failed");
}
nsCOMPtr<nsIFile> targetFile;
nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_COMPLETED, getter_AddRefs(targetFile),
OUTPUT_DIR, nsDumpUtils::CREATE);
nsAutoString targetFilename;
rv = targetFile->GetLeafName(targetFilename);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("Get Filename failed");
return;
}
rv = targetFile->Remove(true);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("Remove File failed");
return;
}
// Rename tmpfile
rv = tmpFile->MoveTo(/* directory */ nullptr, targetFilename);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("Rename File failed");
return;
}
return;
}
// Need to add "{items:[" before the first item
if (aPart == Partition::Begining) {
rv = gzWriter->Write("{\"items\":[{");
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("ostream write begining failed");
}
} else if (aPart == Partition::Middle) {
rv = gzWriter->Write(",{");
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("ostream write middle failed");
}
}
rv = gzWriter->Write(mDesc.get());
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("ostream write mDesc failed");
}
// one item is ended with '}' (e.g. {item})
rv = gzWriter->Write("}");
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("ostream write end failed");
}
rv = gzWriter->Finish();
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("ostream finish failed");
}
// check if it is the end of this file
int64_t fileSize = 0;
rv = tmpFile->GetFileSize(&fileSize);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("GetFileSize failed");
return;
}
if (fileSize >= MAXFILESIZE_KB) {
WriteJSON(Partition::End);
return;
}
}
WriteStumbleOnThread::Partition
WriteStumbleOnThread::GetWritePosition()
{
MOZ_ASSERT(!NS_IsMainThread());
nsCOMPtr<nsIFile> tmpFile;
nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_INPROGRESS, getter_AddRefs(tmpFile),
OUTPUT_DIR, nsDumpUtils::CREATE);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("Open a file for stumble failed");
return Partition::Unknown;
}
int64_t fileSize = 0;
rv = tmpFile->GetFileSize(&fileSize);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("GetFileSize failed");
return Partition::Unknown;
}
if (fileSize == 0) {
return Partition::Begining;
} else if (fileSize >= MAXFILESIZE_KB) {
return Partition::End;
} else {
return Partition::Middle;
}
}
NS_IMETHODIMP
WriteStumbleOnThread::Run()
{
MOZ_ASSERT(!NS_IsMainThread());
bool b = sIsAlreadyRunning.exchange(true);
if (b) {
return NS_OK;
}
UploadFileStatus status = GetUploadFileStatus();
if (UploadFileStatus::NoFile != status) {
if (UploadFileStatus::ExistsAndReadyToUpload == status) {
sIsFileWaitingForUpload = true;
Upload();
return NS_OK;
}
} else {
Partition partition = GetWritePosition();
if (partition == Partition::Unknown) {
STUMBLER_ERR("GetWritePosition failed, skip once");
} else {
WriteJSON(partition);
}
}
sIsFileWaitingForUpload = false;
sIsAlreadyRunning = false;
return NS_OK;
}
/*
If the upload file exists, then check if it is one day old.
• if it is a day old -> ExistsAndReadyToUpload
• if it is less than the current day old -> Exists
• otherwise -> NoFile
The Exists case means that the upload and the stumbling is rate limited
per-day to the size of the one file.
*/
WriteStumbleOnThread::UploadFileStatus
WriteStumbleOnThread::GetUploadFileStatus()
{
nsCOMPtr<nsIFile> tmpFile;
nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_COMPLETED, getter_AddRefs(tmpFile),
OUTPUT_DIR, nsDumpUtils::CREATE);
int64_t fileSize;
rv = tmpFile->GetFileSize(&fileSize);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("GetFileSize failed");
return UploadFileStatus::NoFile;
}
if (fileSize <= 0) {
tmpFile->Remove(true);
return UploadFileStatus::NoFile;
}
PRTime lastModifiedTime;
tmpFile->GetLastModifiedTime(&lastModifiedTime);
if ((PR_Now() / PR_USEC_PER_MSEC) - lastModifiedTime >= ONEDAY_IN_MSEC) {
return UploadFileStatus::ExistsAndReadyToUpload;
}
return UploadFileStatus::Exists;
}
void
WriteStumbleOnThread::Upload()
{
MOZ_ASSERT(!NS_IsMainThread());
time_t seconds = time(0);
int day = seconds / (60 * 60 * 24);
if (sUploadFreqGuard.daySinceEpoch < day) {
sUploadFreqGuard.daySinceEpoch = day;
sUploadFreqGuard.attempts = 0;
}
sUploadFreqGuard.attempts++;
if (sUploadFreqGuard.attempts > MAX_UPLOAD_ATTEMPTS) {
STUMBLER_ERR("Too many upload attempts today");
sIsAlreadyRunning = false;
return;
}
nsCOMPtr<nsIFile> tmpFile;
nsresult rv = nsDumpUtils::OpenTempFile(FILENAME_COMPLETED, getter_AddRefs(tmpFile),
OUTPUT_DIR, nsDumpUtils::CREATE);
int64_t fileSize;
rv = tmpFile->GetFileSize(&fileSize);
if (NS_WARN_IF(NS_FAILED(rv))) {
STUMBLER_ERR("GetFileSize failed");
sIsAlreadyRunning = false;
return;
}
if (fileSize <= 0) {
sIsAlreadyRunning = false;
return;
}
// prepare json into nsIInputStream
nsCOMPtr<nsIInputStream> inStream;
rv = NS_NewLocalFileInputStream(getter_AddRefs(inStream), tmpFile);
if (NS_FAILED(rv)) {
sIsAlreadyRunning = false;
return;
}
RefPtr<nsIRunnable> uploader = new UploadStumbleRunnable(inStream);
NS_DispatchToMainThread(uploader);
}
|