summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options/easy_unlock_turn_off_overlay.js
blob: eb065df016ad3252f8fa4712b9c3f8083ee6a9ff (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
// Copyright 2014 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.

cr.define('options', function() {
  var Page = cr.ui.pageManager.Page;
  var PageManager = cr.ui.pageManager.PageManager;

  // UI state of the turn off overlay.
  // @enum {string}
  var UIState = {
    UNKNOWN: 'unknown',
    OFFLINE: 'offline',
    IDLE: 'idle',
    PENDING: 'pending',
    SERVER_ERROR: 'server-error',
  };

  /**
   * EasyUnlockTurnOffOverlay class
   * Encapsulated handling of the Factory Reset confirmation overlay page.
   * @class
   */
  function EasyUnlockTurnOffOverlay() {
    Page.call(this, 'easyUnlockTurnOffOverlay',
              loadTimeData.getString('easyUnlockTurnOffTitle'),
              'easy-unlock-turn-off-overlay');
  }

  cr.addSingletonGetter(EasyUnlockTurnOffOverlay);

  EasyUnlockTurnOffOverlay.prototype = {
    // Inherit EasyUnlockTurnOffOverlay from Page.
    __proto__: Page.prototype,

    /** Current UI state */
    uiState_: UIState.UNKNOWN,
    get uiState() {
      return this.uiState_;
    },
    set uiState(newUiState) {
      if (newUiState == this.uiState_)
        return;

      this.uiState_ = newUiState;
      switch (this.uiState_) {
        case UIState.OFFLINE:
          this.setUpOfflineUI_();
          break;
        case UIState.IDLE:
          this.setUpTurnOffUI_(false);
          break;
        case UIState.PENDING:
          this.setUpTurnOffUI_(true);
          break;
        case UIState.SERVER_ERROR:
          this.setUpServerErrorUI_();
          break;
        default:
          console.error('Unknow Easy unlock turn off UI state: ' +
                        this.uiState_);
          this.setUpTurnOffUI_(false);
          break;
      }
    },

    /** @override */
    initializePage: function() {
      Page.prototype.initializePage.call(this);

      $('easy-unlock-turn-off-dismiss').onclick = function(event) {
        EasyUnlockTurnOffOverlay.dismiss();
      };
      $('easy-unlock-turn-off-confirm').onclick = function(event) {
        this.uiState = UIState.PENDING;
        chrome.send('easyUnlockRequestTurnOff');
      }.bind(this);
    },

    /** @override */
    didShowPage: function() {
      if (navigator.onLine) {
        this.uiState = UIState.IDLE;
        chrome.send('easyUnlockGetTurnOffFlowStatus');
      } else {
        this.uiState = UIState.OFFLINE;
      }
    },

    /** @override */
    didClosePage: function() {
      chrome.send('easyUnlockTurnOffOverlayDismissed');
    },

    /**
     * Returns the button strip element.
     * @return {HTMLDivElement} The container div of action buttons.
     */
    get buttonStrip() {
      return this.pageDiv.querySelector('.button-strip');
    },

    /**
     * Set visibility of action buttons in button strip.
     * @private
     */
    setActionButtonsVisible_: function(visible) {
      var buttons = this.buttonStrip.querySelectorAll('button');
      for (var i = 0; i < buttons.length; ++i) {
        buttons[i].hidden = !visible;
      }
    },

    /**
     * Set visibility of spinner.
     * @private
     */
    setSpinnerVisible_: function(visible) {
      $('easy-unlock-turn-off-spinner').hidden = !visible;
    },

    /**
     * Set up UI for showing offline message.
     * @private
     */
    setUpOfflineUI_: function() {
      $('easy-unlock-turn-off-title').textContent =
          loadTimeData.getString('easyUnlockTurnOffOfflineTitle');
      $('easy-unlock-turn-off-messagee').textContent =
          loadTimeData.getString('easyUnlockTurnOffOfflineMessage');

      this.setActionButtonsVisible_(false);
      this.setSpinnerVisible_(false);
    },

    /**
     * Set up UI for turning off Easy Unlock.
     * @param {boolean} pending Whether there is a pending turn-off call.
     * @private
     */
    setUpTurnOffUI_: function(pending) {
      $('easy-unlock-turn-off-title').textContent =
          loadTimeData.getString('easyUnlockTurnOffTitle');
      $('easy-unlock-turn-off-messagee').textContent =
          loadTimeData.getString('easyUnlockTurnOffDescription');
      $('easy-unlock-turn-off-confirm').textContent =
          loadTimeData.getString('easyUnlockTurnOffButton');

      this.setActionButtonsVisible_(true);
      this.setSpinnerVisible_(pending);
      $('easy-unlock-turn-off-confirm').disabled = pending;
      $('easy-unlock-turn-off-dismiss').hidden = false;
    },

    /**
     * Set up UI for showing server error.
     * @private
     */
    setUpServerErrorUI_: function() {
      $('easy-unlock-turn-off-title').textContent =
          loadTimeData.getString('easyUnlockTurnOffErrorTitle');
      $('easy-unlock-turn-off-messagee').textContent =
          loadTimeData.getString('easyUnlockTurnOffErrorMessage');
      $('easy-unlock-turn-off-confirm').textContent =
          loadTimeData.getString('easyUnlockTurnOffRetryButton');

      this.setActionButtonsVisible_(true);
      this.setSpinnerVisible_(false);
      $('easy-unlock-turn-off-confirm').disabled = false;
      $('easy-unlock-turn-off-dismiss').hidden = true;
    },
  };

  /**
   * Closes the Easy unlock turn off overlay.
   */
  EasyUnlockTurnOffOverlay.dismiss = function() {
    PageManager.closeOverlay();
  };

  /**
   * Update UI to reflect the turn off operation status.
   * @param {string} newState The UIState string representing the new state.
   */
  EasyUnlockTurnOffOverlay.updateUIState = function(newState) {
    EasyUnlockTurnOffOverlay.getInstance().uiState = newState;
  };

  // Export
  return {
    EasyUnlockTurnOffOverlay: EasyUnlockTurnOffOverlay
  };
});