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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
const EXPORTED_SYMBOLS = ["autosyncModule"];
var nsActProcess = Components.Constructor(
"@mozilla.org/activity-process;1",
"nsIActivityProcess",
"init"
);
var nsActEvent = Components.Constructor(
"@mozilla.org/activity-event;1",
"nsIActivityEvent",
"init"
);
/**
* This code aims to mediate between the auto-sync code and the activity mgr.
*
* Not every auto-sync activity is directly mapped to a process or event.
* To prevent a possible event overflow, Auto-Sync monitor generates one
* sync'd event per account when after all its _pending_ folders are sync'd,
* rather than generating one event per folder sync.
*/
var autosyncModule = {
_inQFolderList: [],
_running: false,
_syncInfoPerFolder: new Map(),
_syncInfoPerServer: new Map(),
_lastMessage: new Map(),
get log() {
delete this.log;
return (this.log = console.createInstance({
prefix: "mail.activity",
maxLogLevel: "Warn",
maxLogLevelPref: "mail.activity.loglevel",
}));
},
get activityMgr() {
delete this.activityMgr;
return (this.activityMgr = Cc["@mozilla.org/activity-manager;1"].getService(
Ci.nsIActivityManager
));
},
get autoSyncManager() {
delete this.autoSyncManager;
return (this.autoSyncManager = Cc[
"@mozilla.org/imap/autosyncmgr;1"
].getService(Ci.nsIAutoSyncManager));
},
get bundle() {
delete this.bundle;
return (this.bundle = Services.strings.createBundle(
"chrome://messenger/locale/activity.properties"
));
},
getString(stringName) {
try {
return this.bundle.GetStringFromName(stringName);
} catch (e) {
this.log.error("error trying to get a string called: " + stringName);
throw e;
}
},
createSyncMailProcess(folder) {
try {
// create an activity process for this folder
let msg = this.bundle.formatStringFromName("autosyncProcessDisplayText", [
folder.prettyName,
]);
let process = new nsActProcess(msg, this.autoSyncManager);
// we want to use default auto-sync icon
process.iconClass = "syncMail";
process.addSubject(folder);
// group processes under folder's imap account
process.contextType = "account";
process.contextDisplayText = this.bundle.formatStringFromName(
"autosyncContextDisplayText",
[folder.server.prettyName]
);
process.contextObj = folder.server;
return process;
} catch (e) {
this.log.error("createSyncMailProcess: " + e);
throw e;
}
},
createSyncMailEvent(syncItem) {
try {
// extract the relevant parts
let process = syncItem.activity;
let folder = syncItem.syncFolder;
// create an activity event
let msg = this.bundle.formatStringFromName("autosyncEventDisplayText", [
folder.server.prettyName,
]);
let statusMsg;
let numOfMessages = this._syncInfoPerServer.get(
folder.server
).totalDownloads;
if (numOfMessages) {
statusMsg = this.bundle.formatStringFromName(
"autosyncEventStatusText",
[numOfMessages]
);
} else {
statusMsg = this.getString("autosyncEventStatusTextNoMsgs");
}
let event = new nsActEvent(
msg,
this.autoSyncManager,
statusMsg,
this._syncInfoPerServer.get(folder.server).startTime,
Date.now()
); // completion time
// since auto-sync events do not have undo option by nature,
// setting these values are informational only.
event.contextType = process.contextType;
event.contextDisplayText = this.bundle.formatStringFromName(
"autosyncContextDisplayText",
[folder.server.prettyName]
);
event.contextObj = process.contextObj;
event.iconClass = "syncMail";
// transfer all subjects.
// same as above, not mandatory
let subjects = process.getSubjects();
for (let subject of subjects) {
event.addSubject(subject);
}
return event;
} catch (e) {
this.log.error("createSyncMailEvent: " + e);
throw e;
}
},
onStateChanged(running) {
try {
this._running = running;
this.log.info(
"OnStatusChanged: " + (running ? "running" : "sleeping") + "\n"
);
} catch (e) {
this.log.error("onStateChanged: " + e);
throw e;
}
},
onFolderAddedIntoQ(queue, folder) {
try {
if (
folder instanceof Ci.nsIMsgFolder &&
queue == Ci.nsIAutoSyncMgrListener.PriorityQueue
) {
this._inQFolderList.push(folder);
this.log.info(
"Auto_Sync OnFolderAddedIntoQ [" +
this._inQFolderList.length +
"] " +
folder.prettyName +
" of " +
folder.server.prettyName
);
// create an activity process for this folder
let process = this.createSyncMailProcess(folder);
// create a sync object to keep track of the process of this folder
let imapFolder = folder.QueryInterface(Ci.nsIMsgImapMailFolder);
let syncItem = {
syncFolder: folder,
activity: process,
percentComplete: 0,
totalDownloaded: 0,
pendingMsgCount: imapFolder.autoSyncStateObj.pendingMessageCount,
};
// if this is the first folder of this server in the queue, then set the sync start time
// for activity event
if (!this._syncInfoPerServer.has(folder.server)) {
this._syncInfoPerServer.set(folder.server, {
startTime: Date.now(),
totalDownloads: 0,
});
}
// associate the sync object with the folder in question
// use folder.URI as key
this._syncInfoPerFolder.set(folder.URI, syncItem);
}
} catch (e) {
this.log.error("onFolderAddedIntoQ: " + e);
throw e;
}
},
onFolderRemovedFromQ(queue, folder) {
try {
if (
folder instanceof Ci.nsIMsgFolder &&
queue == Ci.nsIAutoSyncMgrListener.PriorityQueue
) {
let i = this._inQFolderList.indexOf(folder);
if (i > -1) {
this._inQFolderList.splice(i, 1);
}
this.log.info(
"OnFolderRemovedFromQ [" +
this._inQFolderList.length +
"] " +
folder.prettyName +
" of " +
folder.server.prettyName +
"\n"
);
let syncItem = this._syncInfoPerFolder.get(folder.URI);
let process = syncItem.activity;
let canceled = false;
if (process instanceof Ci.nsIActivityProcess) {
canceled = process.state == Ci.nsIActivityProcess.STATE_CANCELED;
process.state = Ci.nsIActivityProcess.STATE_COMPLETED;
try {
this.activityMgr.removeActivity(process.id);
} catch (e) {
// It is OK to end up here; If the folder is queued and the
// message get manually downloaded by the user, we might get
// a folder removed notification even before a download
// started for this folder. This behavior stems from the fact
// that we add activities into the activity manager in
// onDownloadStarted notification rather than onFolderAddedIntoQ.
// This is an expected side effect.
// Log a warning, but do not throw an error.
this.log.warn("onFolderRemovedFromQ: " + e);
}
// remove the folder/syncItem association from the table
this._syncInfoPerFolder.delete(folder.URI);
}
// if this is the last folder of this server in the queue
// create a sync event and clean the sync start time
let found = false;
for (let value of this._syncInfoPerFolder.values()) {
if (value.syncFolder.server == folder.server) {
found = true;
break;
}
}
this.log.info(
"Auto_Sync OnFolderRemovedFromQ Last folder of the server: " + !found
);
if (!found) {
// create an sync event for the completed process if it's not canceled
if (!canceled) {
let key = folder.server.prettyName;
if (
this._lastMessage.has(key) &&
this.activityMgr.containsActivity(this._lastMessage.get(key))
) {
this.activityMgr.removeActivity(this._lastMessage.get(key));
}
this._lastMessage.set(
key,
this.activityMgr.addActivity(this.createSyncMailEvent(syncItem))
);
}
this._syncInfoPerServer.delete(folder.server);
}
}
} catch (e) {
this.log.error("onFolderRemovedFromQ: " + e);
throw e;
}
},
onDownloadStarted(folder, numOfMessages, totalPending) {
try {
if (folder instanceof Ci.nsIMsgFolder) {
this.log.info(
"OnDownloadStarted (" +
numOfMessages +
"/" +
totalPending +
"): " +
folder.prettyName +
" of " +
folder.server.prettyName +
"\n"
);
let syncItem = this._syncInfoPerFolder.get(folder.URI);
let process = syncItem.activity;
// Update the totalPending number. if new messages have been discovered in the folder
// after we added the folder into the q, totalPending might be greater than what we have
// initially set
if (totalPending > syncItem.pendingMsgCount) {
syncItem.pendingMsgCount = totalPending;
}
if (process instanceof Ci.nsIActivityProcess) {
// if the process has not beed added to activity manager already, add now
if (!this.activityMgr.containsActivity(process.id)) {
this.log.info(
"Auto_Sync OnDownloadStarted: No process, adding a new process"
);
this.activityMgr.addActivity(process);
}
syncItem.totalDownloaded += numOfMessages;
process.state = Ci.nsIActivityProcess.STATE_INPROGRESS;
let percent =
(syncItem.totalDownloaded / syncItem.pendingMsgCount) * 100;
if (percent > syncItem.percentComplete) {
syncItem.percentComplete = percent;
}
let msg = this.bundle.formatStringFromName(
"autosyncProcessProgress2",
[
syncItem.totalDownloaded,
syncItem.pendingMsgCount,
folder.prettyName,
folder.server.prettyName,
]
);
process.setProgress(
msg,
syncItem.totalDownloaded,
syncItem.pendingMsgCount
);
let serverInfo = this._syncInfoPerServer.get(
syncItem.syncFolder.server
);
serverInfo.totalDownloads += numOfMessages;
this._syncInfoPerServer.set(syncItem.syncFolder.server, serverInfo);
}
}
} catch (e) {
this.log.error("onDownloadStarted: " + e);
throw e;
}
},
onDownloadCompleted(folder) {
try {
if (folder instanceof Ci.nsIMsgFolder) {
this.log.info(
"OnDownloadCompleted: " +
folder.prettyName +
" of " +
folder.server.prettyName
);
let process = this._syncInfoPerFolder.get(folder.URI).activity;
if (process instanceof Ci.nsIActivityProcess && !this._running) {
this.log.info(
"OnDownloadCompleted: Auto-Sync Manager is paused, pausing the process"
);
process.state = Ci.nsIActivityProcess.STATE_PAUSED;
}
}
} catch (e) {
this.log.error("onDownloadCompleted: " + e);
throw e;
}
},
onDownloadError(folder) {
if (folder instanceof Ci.nsIMsgFolder) {
this.log.error(
"OnDownloadError: " +
folder.prettyName +
" of " +
folder.server.prettyName +
"\n"
);
}
},
onDiscoveryQProcessed(folder, numOfHdrsProcessed, leftToProcess) {
this.log.info(
"onDiscoveryQProcessed: Processed " +
numOfHdrsProcessed +
"/" +
(leftToProcess + numOfHdrsProcessed) +
" of " +
folder.prettyName +
"\n"
);
},
onAutoSyncInitiated(folder) {
this.log.info(
"onAutoSyncInitiated: " +
folder.prettyName +
" of " +
folder.server.prettyName +
" has been updated.\n"
);
},
init() {
// XXX when do we need to remove ourselves?
this.log.info("initing");
Cc["@mozilla.org/imap/autosyncmgr;1"]
.getService(Ci.nsIAutoSyncManager)
.addListener(this);
},
};
|