summaryrefslogtreecommitdiffstats
path: root/components/proximity_auth/webui/resources/cryptauth_interface.js
blob: a7dbdb7d0f4ea61df1321319682c2a1dbfebcd81 (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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Responsible for interfacing with the native component of the WebUI to make
 * CryptAuth API requests and handling the responses.
 */
CryptAuthInterface = {
  /**
   * A list of observers of CryptAuth events.
   */
  observers_: [],

  /**
   * Adds an observer.
   */
  addObserver: function(observer) {
    CryptAuthInterface.observers_.push(observer);
  },

  /**
   * Removes an observer.
   */
  removeObserver: function(observer) {
    var index = CryptAuthInterface.observers_.indexOf(observer);
    if (observer)
      CryptAuthInterface.observers_.splice(index, 1);
  },

  /**
   * Starts the findEligibleUnlockDevices API call.
   * The onGotEligibleDevices() function will be called upon success.
   */
  findEligibleUnlockDevices: function() {
    chrome.send('findEligibleUnlockDevices');
  },

  /**
   * Starts the flow to find reachable devices. Reachable devices are those that
   * respond to a CryptAuth ping.
   * The onGotReachableDevices() function will be called upon success.
   */
  findReachableDevices: function() {
    chrome.send('findReachableDevices');
  },

  /**
   * Makes the device with |publicKey| an unlock key if |makeUnlockKey| is true.
   * Otherwise, the device will be removed as an unlock key.
   */
  toggleUnlockKey: function(publicKey, makeUnlockKey) {
    chrome.send('toggleUnlockKey', [publicKey, makeUnlockKey]);
  },

  /**
   * Called by the browser when the API request fails.
   */
  onError: function(errorMessage) {
    CryptAuthInterface.observers_.forEach(function(observer) {
      if (observer.onCryptAuthError != null)
        observer.onCryptAuthError(errorMessage);
    });
  },

  /**
   * Called by the browser when a findEligibleUnlockDevices completes
   * successfully.
   * @param {Array<DeviceInfo>} eligibleDevices
   * @param {Array<DeviceInfo>} ineligibleDevices
   */
  onGotEligibleDevices: function(eligibleDevices, ineligibleDevices) {
    CryptAuthInterface.observers_.forEach(function(observer) {
      if (observer.onGotEligibleDevices != null)
        observer.onGotEligibleDevices(eligibleDevices, ineligibleDevices);
    });
  },

  /*
   * Called by the browser when the reachable devices flow completes
   * successfully.
   * @param {Array<DeviceInfo>} reachableDevices
   */
  onGotReachableDevices: function(reachableDevices) {
    CryptAuthInterface.observers_.forEach(function(observer) {
      if (observer.onGotReachableDevices != null)
        observer.onGotReachableDevices(reachableDevices);
    });
  },

  /**
   * Called by the browser when an unlock key is toggled.
   */
  onUnlockKeyToggled: function() {
    CryptAuthInterface.observers_.forEach(function(observer) {
      if (observer.onUnlockKeyToggled != null)
        observer.onUnlockKeyToggled();
    });
  },
};

// This message tells the native WebUI handler that the WebContents backing the
// WebUI has been iniitalized. This signal allows the native handler to execute
// JavaScript inside the page.
chrome.send('onWebContentsInitialized');