summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/modules/index_im.sys.mjs
blob: bcea54e1ea1527b55bed59b5c4a8f36eeca9d2d1 (plain)
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/* 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/. */

var CC = Components.Constructor;

const { Gloda } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaPublic.jsm"
);
const { GlodaAccount } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaDataModel.jsm"
);
const { GlodaConstants } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaConstants.jsm"
);
const { GlodaIndexer, IndexingJob } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaIndexer.jsm"
);
import { IMServices } from "resource:///modules/IMServices.sys.mjs";

const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
import { FileUtils } from "resource://gre/modules/FileUtils.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

import { clearTimeout, setTimeout } from "resource://gre/modules/Timer.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  AsyncShutdown: "resource://gre/modules/AsyncShutdown.sys.mjs",
});
ChromeUtils.defineModuleGetter(
  lazy,
  "GlodaDatastore",
  "resource:///modules/gloda/GlodaDatastore.jsm"
);

var kCacheFileName = "indexedFiles.json";

var FileInputStream = CC(
  "@mozilla.org/network/file-input-stream;1",
  "nsIFileInputStream",
  "init"
);
var ScriptableInputStream = CC(
  "@mozilla.org/scriptableinputstream;1",
  "nsIScriptableInputStream",
  "init"
);

// kIndexingDelay is how long we wait from the point of scheduling an indexing
// job to actually carrying it out.
var kIndexingDelay = 5000; // in milliseconds

XPCOMUtils.defineLazyGetter(lazy, "MailFolder", () =>
  Cc["@mozilla.org/mail/folder-factory;1?name=mailbox"].createInstance(
    Ci.nsIMsgFolder
  )
);

var gIMAccounts = {};

function GlodaIMConversation(aTitle, aTime, aPath, aContent) {
  // grokNounItem from Gloda.jsm puts automatically the values of all
  // JS properties in the jsonAttributes magic attribute, except if
  // they start with _, so we put the values in _-prefixed properties,
  // and have getters in the prototype.
  this._title = aTitle;
  this._time = aTime;
  this._path = aPath;
  this._content = aContent;
}
GlodaIMConversation.prototype = {
  get title() {
    return this._title;
  },
  get time() {
    return this._time;
  },
  get path() {
    return this._path;
  },
  get content() {
    return this._content;
  },

  // for glodaFacetBindings.xml compatibility (pretend we are a message object)
  get account() {
    let [protocol, username] = this._path.split("/", 2);

    let cacheName = protocol + "/" + username;
    if (cacheName in gIMAccounts) {
      return gIMAccounts[cacheName];
    }

    // Find the nsIIncomingServer for the current imIAccount.
    for (let account of MailServices.accounts.accounts) {
      let incomingServer = account.incomingServer;
      if (!incomingServer || incomingServer.type != "im") {
        continue;
      }
      let imAccount = incomingServer.wrappedJSObject.imAccount;
      if (
        imAccount.protocol.normalizedName == protocol &&
        imAccount.normalizedName == username
      ) {
        return (gIMAccounts[cacheName] = new GlodaAccount(incomingServer));
      }
    }
    // The IM conversation is probably for an account that no longer exists.
    return null;
  },
  get subject() {
    return this._title;
  },
  get date() {
    return new Date(this._time * 1000);
  },
  get involves() {
    return GlodaConstants.IGNORE_FACET;
  },
  _recipients: null,
  get recipients() {
    if (!this._recipients) {
      this._recipients = [{ contact: { name: this._path.split("/", 2)[1] } }];
    }
    return this._recipients;
  },
  _from: null,
  get from() {
    if (!this._from) {
      let from = "";
      let account = this.account;
      if (account) {
        from = account.incomingServer.wrappedJSObject.imAccount.protocol.name;
      }
      this._from = { value: "", contact: { name: from } };
    }
    return this._from;
  },
  get tags() {
    return [];
  },
  get starred() {
    return false;
  },
  get attachmentNames() {
    return null;
  },
  get indexedBodyText() {
    return this._content;
  },
  get read() {
    return true;
  },
  get folder() {
    return GlodaConstants.IGNORE_FACET;
  },

  // for glodaFacetView.js _removeDupes
  get headerMessageID() {
    return this.id;
  },
};

// FIXME
var WidgetProvider = {
  providerName: "widget",
  *process() {
    // XXX What is this supposed to do?
    yield GlodaConstants.kWorkDone;
  },
};

var IMConversationNoun = {
  name: "im-conversation",
  clazz: GlodaIMConversation,
  allowsArbitraryAttrs: true,
  tableName: "imConversations",
  schema: {
    columns: [
      ["id", "INTEGER PRIMARY KEY"],
      ["title", "STRING"],
      ["time", "NUMBER"],
      ["path", "STRING"],
    ],
    fulltextColumns: [["content", "STRING"]],
  },
};
Gloda.defineNoun(IMConversationNoun);

// Needs to be set after calling defineNoun, otherwise it's replaced
// by GlodaDatabind.jsm' implementation.
IMConversationNoun.objFromRow = function (aRow) {
  // Row columns are:
  // 0 id
  // 1 title
  // 2 time
  // 3 path
  // 4 jsonAttributes
  // 5 content
  // 6 offsets
  let conv = new GlodaIMConversation(
    aRow.getString(1),
    aRow.getInt64(2),
    aRow.getString(3),
    aRow.getString(5)
  );
  conv.id = aRow.getInt64(0); // handleResult will keep only our first result
  // if the id property isn't set.
  return conv;
};

var EXT_NAME = "im";

// --- special (on-row) attributes
Gloda.defineAttribute({
  provider: WidgetProvider,
  extensionName: EXT_NAME,
  attributeType: GlodaConstants.kAttrFundamental,
  attributeName: "time",
  singular: true,
  special: GlodaConstants.kSpecialColumn,
  specialColumnName: "time",
  subjectNouns: [IMConversationNoun.id],
  objectNoun: GlodaConstants.NOUN_NUMBER,
  canQuery: true,
});
Gloda.defineAttribute({
  provider: WidgetProvider,
  extensionName: EXT_NAME,
  attributeType: GlodaConstants.kAttrFundamental,
  attributeName: "title",
  singular: true,
  special: GlodaConstants.kSpecialString,
  specialColumnName: "title",
  subjectNouns: [IMConversationNoun.id],
  objectNoun: GlodaConstants.NOUN_STRING,
  canQuery: true,
});
Gloda.defineAttribute({
  provider: WidgetProvider,
  extensionName: EXT_NAME,
  attributeType: GlodaConstants.kAttrFundamental,
  attributeName: "path",
  singular: true,
  special: GlodaConstants.kSpecialString,
  specialColumnName: "path",
  subjectNouns: [IMConversationNoun.id],
  objectNoun: GlodaConstants.NOUN_STRING,
  canQuery: true,
});

// --- fulltext attributes
Gloda.defineAttribute({
  provider: WidgetProvider,
  extensionName: EXT_NAME,
  attributeType: GlodaConstants.kAttrFundamental,
  attributeName: "content",
  singular: true,
  special: GlodaConstants.kSpecialFulltext,
  specialColumnName: "content",
  subjectNouns: [IMConversationNoun.id],
  objectNoun: GlodaConstants.NOUN_FULLTEXT,
  canQuery: true,
});

// -- fulltext search helper
// fulltextMatches.  Match over message subject, body, and attachments
// @testpoint gloda.noun.message.attr.fulltextMatches
Gloda.defineAttribute({
  provider: WidgetProvider,
  extensionName: EXT_NAME,
  attributeType: GlodaConstants.kAttrDerived,
  attributeName: "fulltextMatches",
  singular: true,
  special: GlodaConstants.kSpecialFulltext,
  specialColumnName: "imConversationsText",
  subjectNouns: [IMConversationNoun.id],
  objectNoun: GlodaConstants.NOUN_FULLTEXT,
});
// For Facet.jsm DateFaceter
Gloda.defineAttribute({
  provider: WidgetProvider,
  extensionName: EXT_NAME,
  attributeType: GlodaConstants.kAttrDerived,
  attributeName: "date",
  singular: true,
  special: GlodaConstants.kSpecialColumn,
  subjectNouns: [IMConversationNoun.id],
  objectNoun: GlodaConstants.NOUN_NUMBER,
  facet: {
    type: "date",
  },
  canQuery: true,
});

var GlodaIMIndexer = {
  name: "index_im",
  cacheVersion: 1,
  enable() {
    Services.obs.addObserver(this, "conversation-closed");
    Services.obs.addObserver(this, "new-ui-conversation");
    Services.obs.addObserver(this, "conversation-update-type");
    Services.obs.addObserver(this, "ui-conversation-closed");
    Services.obs.addObserver(this, "ui-conversation-replaced");

    // The shutdown blocker ensures pending saves happen even if the app
    // gets shut down before the timer fires.
    if (this._shutdownBlockerAdded) {
      return;
    }
    this._shutdownBlockerAdded = true;
    lazy.AsyncShutdown.profileBeforeChange.addBlocker(
      "GlodaIMIndexer cache save",
      () => {
        if (!this._cacheSaveTimer) {
          return Promise.resolve();
        }
        clearTimeout(this._cacheSaveTimer);
        return this._saveCacheNow();
      }
    );

    this._knownFiles = {};

    let dir = FileUtils.getFile("ProfD", ["logs"]);
    if (!dir.exists() || !dir.isDirectory()) {
      return;
    }
    let cacheFile = dir.clone();
    cacheFile.append(kCacheFileName);
    if (!cacheFile.exists()) {
      return;
    }

    const PR_RDONLY = 0x01;
    let fis = new FileInputStream(
      cacheFile,
      PR_RDONLY,
      parseInt("0444", 8),
      Ci.nsIFileInputStream.CLOSE_ON_EOF
    );
    let sis = new ScriptableInputStream(fis);
    let text = sis.read(sis.available());
    sis.close();

    let data = JSON.parse(text);

    // Check to see if the Gloda datastore ID matches the one that we saved
    // in the cache. If so, we can trust it. If not, that means that the
    // cache is likely invalid now, so we ignore it (and eventually
    // overwrite it).
    if (
      "datastoreID" in data &&
      Gloda.datastoreID &&
      data.datastoreID === Gloda.datastoreID
    ) {
      // Ok, the cache's datastoreID matches the one we expected, so it's
      // still valid.
      this._knownFiles = data.knownFiles;
    }

    this.cacheVersion = data.version;

    // If there was no version set on the cache, there is a chance that the index
    // is affected by bug 1069845. fixEntriesWithAbsolutePaths() sets the version to 1.
    if (!this.cacheVersion) {
      this.fixEntriesWithAbsolutePaths();
    }
  },
  disable() {
    Services.obs.removeObserver(this, "conversation-closed");
    Services.obs.removeObserver(this, "new-ui-conversation");
    Services.obs.removeObserver(this, "conversation-update-type");
    Services.obs.removeObserver(this, "ui-conversation-closed");
    Services.obs.removeObserver(this, "ui-conversation-replaced");
  },

  /* _knownFiles is a tree whose leaves are the last modified times of
   * log files when they were last indexed.
   * Each level of the tree is stored as an object. The root node is an
   * object that maps a protocol name to an object representing the subtree
   * for that protocol. The structure is:
   * _knownFiles    -> protoObj      -> accountObj  -> convObj
   * The corresponding keys of the above objects are:
   * protocol names -> account names -> conv names  -> file names -> last modified time
   * convObj maps ALL previously indexed log files of a chat buddy or MUC to
   * their last modified times. Note that gloda knows nothing about log grouping
   * done by logger.js.
   */
  _knownFiles: {},
  _cacheSaveTimer: null,
  _shutdownBlockerAdded: false,
  _scheduleCacheSave() {
    if (this._cacheSaveTimer) {
      return;
    }
    this._cacheSaveTimer = setTimeout(this._saveCacheNow, 5000);
  },
  _saveCacheNow() {
    GlodaIMIndexer._cacheSaveTimer = null;

    let data = {
      knownFiles: GlodaIMIndexer._knownFiles,
      datastoreID: Gloda.datastoreID,
      version: GlodaIMIndexer.cacheVersion,
    };

    // Asynchronously copy the data to the file.
    let path = PathUtils.join(
      Services.dirsvc.get("ProfD", Ci.nsIFile).path,
      "logs",
      kCacheFileName
    );
    return IOUtils.writeJSON(path, data, {
      tmpPath: path + ".tmp",
    }).catch(aError => console.error("Failed to write cache file: " + aError));
  },

  _knownConversations: {},
  // Promise queue for indexing jobs. The next indexing job is queued using this
  // promise's then() to ensure we only load logs for one conv at a time.
  _indexingJobPromise: null,
  // Maps a conv id to the function that resolves the promise representing the
  // ongoing indexing job on it. This is called from indexIMConversation when it
  // finishes and will trigger the next queued indexing job.
  _indexingJobCallbacks: new Map(),

  _scheduleIndexingJob(aConversation) {
    let convId = aConversation.id;

    // If we've already scheduled this conversation to be indexed, let's
    // not repeat.
    if (!(convId in this._knownConversations)) {
      this._knownConversations[convId] = {
        id: convId,
        scheduledIndex: null,
        logFileCount: null,
        convObj: {},
      };
    }

    if (!this._knownConversations[convId].scheduledIndex) {
      // Ok, let's schedule the job.
      this._knownConversations[convId].scheduledIndex = setTimeout(
        this._beginIndexingJob.bind(this, aConversation),
        kIndexingDelay
      );
    }
  },

  _beginIndexingJob(aConversation) {
    let convId = aConversation.id;

    // In the event that we're triggering this indexing job manually, without
    // bothering to schedule it (for example, when a conversation is closed),
    // we give the conversation an entry in _knownConversations, which would
    // normally have been done in _scheduleIndexingJob.
    if (!(convId in this._knownConversations)) {
      this._knownConversations[convId] = {
        id: convId,
        scheduledIndex: null,
        logFileCount: null,
        convObj: {},
      };
    }

    let conv = this._knownConversations[convId];
    (async () => {
      // We need to get the log files every time, because a new log file might
      // have been started since we last got them.
      let logFiles = await IMServices.logs.getLogPathsForConversation(
        aConversation
      );
      if (!logFiles || !logFiles.length) {
        // No log files exist yet, nothing to do!
        return;
      }

      if (conv.logFileCount == undefined) {
        // We initialize the _knownFiles tree path for the current files below in
        // case it doesn't already exist.
        let folder = PathUtils.parent(logFiles[0]);
        let convName = PathUtils.filename(folder);
        folder = PathUtils.parent(folder);
        let accountName = PathUtils.filename(folder);
        folder = PathUtils.parent(folder);
        let protoName = PathUtils.filename(folder);
        if (
          !Object.prototype.hasOwnProperty.call(this._knownFiles, protoName)
        ) {
          this._knownFiles[protoName] = {};
        }
        let protoObj = this._knownFiles[protoName];
        if (!Object.prototype.hasOwnProperty.call(protoObj, accountName)) {
          protoObj[accountName] = {};
        }
        let accountObj = protoObj[accountName];
        if (!Object.prototype.hasOwnProperty.call(accountObj, convName)) {
          accountObj[convName] = {};
        }

        // convObj is the penultimate level of the tree,
        // maps file name -> last modified time
        conv.convObj = accountObj[convName];
        conv.logFileCount = 0;
      }

      // The last log file in the array is the one currently being written to.
      // When new log files are started, we want to finish indexing the previous
      // one as well as index the new ones. The index of the previous one is
      // conv.logFiles.length - 1, so we slice from there. This gives us all new
      // log files even if there are multiple new ones.
      let currentLogFiles =
        conv.logFileCount > 1
          ? logFiles.slice(conv.logFileCount - 1)
          : logFiles;
      for (let logFile of currentLogFiles) {
        let fileName = PathUtils.filename(logFile);
        let lastModifiedTime = (await IOUtils.stat(logFile)).lastModified;
        if (
          Object.prototype.hasOwnProperty.call(conv.convObj, fileName) &&
          conv.convObj[fileName] == lastModifiedTime
        ) {
          // The file hasn't changed since we last indexed it, so we're done.
          continue;
        }

        if (this._indexingJobPromise) {
          await this._indexingJobPromise;
        }
        this._indexingJobPromise = new Promise(aResolve => {
          this._indexingJobCallbacks.set(convId, aResolve);
        });

        let job = new IndexingJob("indexIMConversation", null);
        job.conversation = conv;
        job.path = logFile;
        job.lastModifiedTime = lastModifiedTime;
        GlodaIndexer.indexJob(job);
      }
      conv.logFileCount = logFiles.length;
    })().catch(console.error);

    // Now clear the job, so we can index in the future.
    this._knownConversations[convId].scheduledIndex = null;
  },

  observe(aSubject, aTopic, aData) {
    if (
      aTopic == "new-ui-conversation" ||
      aTopic == "conversation-update-type"
    ) {
      // Add ourselves to the ui-conversation's list of observers for the
      // unread-message-count-changed notification.
      // For this notification, aSubject is the ui-conversation that is opened.
      aSubject.addObserver(this);
      return;
    }

    if (
      aTopic == "ui-conversation-closed" ||
      aTopic == "ui-conversation-replaced"
    ) {
      aSubject.removeObserver(this);
      return;
    }

    if (aTopic == "unread-message-count-changed") {
      // We get this notification by attaching observers to conversations
      // directly (see the new-ui-conversation handler for when we attach).
      if (aSubject.unreadIncomingMessageCount == 0) {
        // The unread message count changed to 0, meaning that a conversation
        // that had been in the background and receiving messages was suddenly
        // moved to the foreground and displayed to the user. We schedule an
        // indexing job on this conversation now, since we want to index messages
        // that the user has seen.
        this._scheduleIndexingJob(aSubject.target);
      }
      return;
    }

    if (aTopic == "conversation-closed") {
      let convId = aSubject.id;
      // If there's a scheduled indexing job, cancel it, because we're going
      // to index now.
      if (
        convId in this._knownConversations &&
        this._knownConversations[convId].scheduledIndex != null
      ) {
        clearTimeout(this._knownConversations[convId].scheduledIndex);
      }

      this._beginIndexingJob(aSubject);
      delete this._knownConversations[convId];
      return;
    }

    if (aTopic == "new-text" && !aSubject.noLog) {
      // Ok, some new text is about to be put into a conversation. For this
      // notification, aSubject is a prplIMessage.
      let conv = aSubject.conversation;
      let uiConv = IMServices.conversations.getUIConversation(conv);

      // We only want to schedule an indexing job if this message is
      // immediately visible to the user. We figure this out by finding
      // the unread message count on the associated UIConversation for this
      // message. If the unread count is 0, we know that the message has been
      // displayed to the user.
      if (uiConv.unreadIncomingMessageCount == 0) {
        this._scheduleIndexingJob(conv);
      }
    }
  },

  /* If there is an existing gloda conversation for the given path,
   * find its id.
   */
  _getIdFromPath(aPath) {
    let selectStatement = lazy.GlodaDatastore._createAsyncStatement(
      "SELECT id FROM imConversations WHERE path = ?1"
    );
    selectStatement.bindByIndex(0, aPath);
    let id;
    return new Promise((resolve, reject) => {
      selectStatement.executeAsync({
        handleResult: aResultSet => {
          let row = aResultSet.getNextRow();
          if (!row) {
            return;
          }
          if (id || aResultSet.getNextRow()) {
            console.error(
              "Warning: found more than one gloda conv id for " + aPath + "\n"
            );
          }
          id = id || row.getInt64(0); // We use the first found id.
        },
        handleError: aError =>
          console.error("Error finding gloda id from path:\n" + aError),
        handleCompletion: () => {
          resolve(id);
        },
      });
    });
  },

  // Get the path of a log file relative to the logs directory - the last 4
  // components of the path.
  _getRelativePath(aLogPath) {
    return PathUtils.split(aLogPath).slice(-4).join("/");
  },

  /**
   * @param {object} aCache - An object mapping file names to their last
   *   modified times at the time they were last indexed. The value for the file
   *   currently being indexed is updated to the aLastModifiedTime parameter's
   *   value once indexing is complete.
   * @param {GlodaIMConversation} [aGlodaConv] - An optional in-out param that
   *   lets the caller save and reuse the GlodaIMConversation instance created
   *   when the conversation is indexed the first time. After a conversation is
   *   indexed for the first time, the GlodaIMConversation instance has its id
   *   property set to the row id of the conversation in the database. This id
   *   is required to later update the conversation in the database, so the
   *   caller dealing with ongoing conversation has to provide the aGlodaConv
   *   parameter, while the caller dealing with old conversations doesn't care.
   */
  async indexIMConversation(
    aCallbackHandle,
    aLogPath,
    aLastModifiedTime,
    aCache,
    aGlodaConv
  ) {
    let log = await IMServices.logs.getLogFromFile(aLogPath);
    let logConv = await log.getConversation();

    // Ignore corrupted log files.
    if (!logConv) {
      return GlodaConstants.kWorkDone;
    }

    let fileName = PathUtils.filename(aLogPath);
    let messages = logConv
      .getMessages()
      // Some messages returned, e.g. sessionstart messages,
      // may have the noLog flag set. Ignore these.
      .filter(m => !m.noLog);
    let content = [];
    while (messages.length > 0) {
      await new Promise(resolve => {
        ChromeUtils.idleDispatch(timing => {
          while (timing.timeRemaining() > 5 && messages.length > 0) {
            let m = messages.shift();
            let who = m.alias || m.who;
            // Messages like topic change notifications may not have a source.
            let prefix = who ? who + ": " : "";
            content.push(
              prefix +
                lazy.MailFolder.convertMsgSnippetToPlainText(
                  "<!DOCTYPE html>" + m.message
                )
            );
          }
          resolve();
        });
      });
    }
    content = content.join("\n\n");
    let glodaConv;
    if (aGlodaConv && aGlodaConv.value) {
      glodaConv = aGlodaConv.value;
      glodaConv._content = content;
    } else {
      let relativePath = this._getRelativePath(aLogPath);
      glodaConv = new GlodaIMConversation(
        logConv.title,
        log.time,
        relativePath,
        content
      );
      // If we've indexed this file before, we need the id of the existing
      // gloda conversation so that the existing entry gets updated. This can
      // happen if the log sweep detects that the last messages in an open
      // chat were not in fact indexed before that session was shut down.
      let id = await this._getIdFromPath(relativePath);
      if (id) {
        glodaConv.id = id;
      }
      if (aGlodaConv) {
        aGlodaConv.value = glodaConv;
      }
    }

    if (!aCache) {
      throw new Error("indexIMConversation called without aCache parameter.");
    }
    let isNew =
      !Object.prototype.hasOwnProperty.call(aCache, fileName) && !glodaConv.id;
    let rv = aCallbackHandle.pushAndGo(
      Gloda.grokNounItem(glodaConv, {}, true, isNew, aCallbackHandle)
    );

    if (!aLastModifiedTime) {
      console.error(
        "indexIMConversation called without lastModifiedTime parameter."
      );
    }
    aCache[fileName] = aLastModifiedTime || 1;
    this._scheduleCacheSave();

    return rv;
  },

  *_worker_indexIMConversation(aJob, aCallbackHandle) {
    let glodaConv = {};
    let existingGlodaConv = aJob.conversation.glodaConv;
    if (
      existingGlodaConv &&
      existingGlodaConv.path == this._getRelativePath(aJob.path)
    ) {
      glodaConv.value = aJob.conversation.glodaConv;
    }

    // indexIMConversation may initiate an async grokNounItem sub-job.
    this.indexIMConversation(
      aCallbackHandle,
      aJob.path,
      aJob.lastModifiedTime,
      aJob.conversation.convObj,
      glodaConv
    ).then(() => GlodaIndexer.callbackDriver());
    // Tell the Indexer that we're doing async indexing. We'll be left alone
    // until callbackDriver() is called above.
    yield GlodaConstants.kWorkAsync;

    // Resolve the promise for this job.
    this._indexingJobCallbacks.get(aJob.conversation.id)();
    this._indexingJobCallbacks.delete(aJob.conversation.id);
    this._indexingJobPromise = null;
    aJob.conversation.indexPending = false;
    aJob.conversation.glodaConv = glodaConv.value;
    yield GlodaConstants.kWorkDone;
  },

  *_worker_logsFolderSweep(aJob) {
    let dir = FileUtils.getFile("ProfD", ["logs"]);
    if (!dir.exists() || !dir.isDirectory()) {
      // If the folder does not exist, then we are done.
      yield GlodaConstants.kWorkDone;
    }

    // Sweep the logs directory for log files, adding any new entries to the
    // _knownFiles tree as we traverse.
    for (let proto of dir.directoryEntries) {
      if (!proto.isDirectory()) {
        continue;
      }
      let protoName = proto.leafName;
      if (!Object.prototype.hasOwnProperty.call(this._knownFiles, protoName)) {
        this._knownFiles[protoName] = {};
      }
      let protoObj = this._knownFiles[protoName];
      let accounts = proto.directoryEntries;
      for (let account of accounts) {
        if (!account.isDirectory()) {
          continue;
        }
        let accountName = account.leafName;
        if (!Object.prototype.hasOwnProperty.call(protoObj, accountName)) {
          protoObj[accountName] = {};
        }
        let accountObj = protoObj[accountName];
        for (let conv of account.directoryEntries) {
          let convName = conv.leafName;
          if (!conv.isDirectory() || convName == ".system") {
            continue;
          }
          if (!Object.prototype.hasOwnProperty.call(accountObj, convName)) {
            accountObj[convName] = {};
          }
          let job = new IndexingJob("convFolderSweep", null);
          job.folder = conv;
          job.convObj = accountObj[convName];
          GlodaIndexer.indexJob(job);
        }
      }
    }

    yield GlodaConstants.kWorkDone;
  },

  *_worker_convFolderSweep(aJob, aCallbackHandle) {
    let folder = aJob.folder;

    for (let file of folder.directoryEntries) {
      let fileName = file.leafName;
      if (
        !file.isFile() ||
        !file.isReadable() ||
        !fileName.endsWith(".json") ||
        (Object.prototype.hasOwnProperty.call(aJob.convObj, fileName) &&
          aJob.convObj[fileName] == file.lastModifiedTime)
      ) {
        continue;
      }
      // indexIMConversation may initiate an async grokNounItem sub-job.
      this.indexIMConversation(
        aCallbackHandle,
        file.path,
        file.lastModifiedTime,
        aJob.convObj
      ).then(() => GlodaIndexer.callbackDriver());
      // Tell the Indexer that we're doing async indexing. We'll be left alone
      // until callbackDriver() is called above.
      yield GlodaConstants.kWorkAsync;
    }
    yield GlodaConstants.kWorkDone;
  },

  get workers() {
    return [
      ["indexIMConversation", { worker: this._worker_indexIMConversation }],
      ["logsFolderSweep", { worker: this._worker_logsFolderSweep }],
      ["convFolderSweep", { worker: this._worker_convFolderSweep }],
    ];
  },

  initialSweep() {
    let job = new IndexingJob("logsFolderSweep", null);
    GlodaIndexer.indexJob(job);
  },

  // Due to bug 1069845, some logs were indexed against their full paths instead
  // of their path relative to the logs directory. These entries are updated to
  // use relative paths below.
  fixEntriesWithAbsolutePaths() {
    let store = lazy.GlodaDatastore;
    let selectStatement = store._createAsyncStatement(
      "SELECT id, path FROM imConversations"
    );
    let updateStatement = store._createAsyncStatement(
      "UPDATE imConversations SET path = ?1 WHERE id = ?2"
    );

    store._beginTransaction();
    selectStatement.executeAsync({
      handleResult: aResultSet => {
        let row;
        while ((row = aResultSet.getNextRow())) {
          // If the path has more than 4 components, it is not relative to
          // the logs folder. Update it to use only the last 4 components.
          // The absolute paths were stored as OS-specific paths, so we split
          // them with PathUtils.split(). It's a safe assumption that nobody
          // ported their profile folder to a different OS since the regression,
          // so this should work.
          let pathComponents = PathUtils.split(row.getString(1));
          if (pathComponents.length > 4) {
            updateStatement.bindByIndex(1, row.getInt64(0)); // id
            updateStatement.bindByIndex(0, pathComponents.slice(-4).join("/")); // Last 4 path components
            updateStatement.executeAsync({
              handleResult: () => {},
              handleError: aError =>
                console.error("Error updating bad entry:\n" + aError),
              handleCompletion: () => {},
            });
          }
        }
      },

      handleError: aError =>
        console.error("Error looking for bad entries:\n" + aError),

      handleCompletion: () => {
        store.runPostCommit(() => {
          this.cacheVersion = 1;
          this._scheduleCacheSave();
        });
        store._commitTransaction();
      },
    });
  },
};

GlodaIndexer.registerIndexer(GlodaIMIndexer);