summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/FormHistoryParent.jsm
blob: a66d36a323bb55c180ff7d3215f669336b772bf0 (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
/* 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/. */

"use strict";

var EXPORTED_SYMBOLS = ["FormHistoryParent"];

ChromeUtils.defineModuleGetter(
  this,
  "FormHistory",
  "resource://gre/modules/FormHistory.jsm"
);

class FormHistoryParent extends JSWindowActorParent {
  receiveMessage(message) {
    switch (message.name) {
      case "FormHistory:FormSubmitEntries": {
        let entries = message.data;
        let changes = entries.map(entry => ({
          op: "bump",
          fieldname: entry.name,
          value: entry.value,
        }));

        FormHistory.update(changes);
        break;
      }

      case "FormHistory:AutoCompleteSearchAsync":
        return this.autoCompleteSearch(message);

      case "FormHistory:RemoveEntry":
        this.removeEntry(message);
        break;
    }

    return undefined;
  }

  autoCompleteSearch(message) {
    let { searchString, params } = message.data;

    return new Promise((resolve, reject) => {
      let results = [];
      let processResults = {
        handleResult: aResult => {
          results.push(aResult);
        },
        handleCompletion: aReason => {
          if (!aReason) {
            resolve(results);
          } else {
            reject();
          }
        },
      };

      FormHistory.getAutoCompleteResults(searchString, params, processResults);
    });
  }

  removeEntry(message) {
    let { inputName, value, guid } = message.data;
    FormHistory.update({
      op: "remove",
      fieldname: inputName,
      value,
      guid,
    });
  }
}