ChromeOS Gaia: SAML password confirmation dialog
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / login / screen_password_changed.js
blob8001a670d2397e6f0e31b445735c0d741ebe314c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6  * @fileoverview Password changed screen implementation.
7  */
9 login.createScreen('PasswordChangedScreen', 'password-changed', function() {
10   return {
11     EXTERNAL_API: [
12       'show'
13     ],
15     /** @override */
16     decorate: function() {
17       $('old-password').addEventListener(
18         'keydown', function(e) {
19           $('password-changed').classList.remove('password-error');
20           if (e.keyIdentifier == 'Enter') {
21             $('password-changed').migrate();
22             e.stopPropagation();
23           }
24         });
25       $('old-password').addEventListener(
26         'keyup', function(e) {
27           if ($('password-changed').disabled)
28             return;
29           $('password-changed-ok-button').disabled = this.value.length == 0;
30         });
31       $('password-changed-cant-remember-link').addEventListener(
32         'click', function(e) {
33           if (this.classList.contains('disabled'))
34             return;
35           var screen = $('password-changed');
36           if (screen.classList.contains('migrate')) {
37             screen.classList.remove('migrate');
38             screen.classList.add('resync');
39             $('password-changed-proceed-button').focus();
40             $('password-changed').classList.remove('password-error');
41             $('old-password').value = '';
42             $('password-changed-ok-button').disabled = true;
43           }
44         });
46       var gaiaPasswordChanged = $('gaia-password-changed');
47       gaiaPasswordChanged.addEventListener('cancel', function(e) {
48           chrome.send('cancelPasswordChangedFlow',
49               [$('gaia-password-changed').email]);
50           gaiaPasswordChanged.reset();
51         });
53       gaiaPasswordChanged.addEventListener('passwordEnter', function(e) {
54           chrome.send('migrateUserData', [e.detail.password]);
55         });
57       gaiaPasswordChanged.addEventListener('proceedAnyway', function() {
58           chrome.send('resyncUserData');
59         });
60     },
62     /**
63      * Screen controls.
64      * @type {array} Array of Buttons.
65      */
66     get buttons() {
67       var buttons = [];
69       var backButton = this.ownerDocument.createElement('button');
70       backButton.id = 'password-changed-back-button';
71       backButton.textContent =
72           loadTimeData.getString('passwordChangedBackButton');
73       backButton.addEventListener('click', function(e) {
74         var screen = $('password-changed');
75         if (screen.classList.contains('migrate')) {
76           screen.cancel();
77         } else {
78           // Resync all data UI step.
79           screen.classList.remove('resync');
80           screen.classList.add('migrate');
81           $('old-password').focus();
82         }
83         e.stopPropagation();
84       });
85       buttons.push(backButton);
87       var okButton = this.ownerDocument.createElement('button');
88       okButton.id = 'password-changed-ok-button';
89       okButton.textContent = loadTimeData.getString('passwordChangedsOkButton');
90       okButton.addEventListener('click', function(e) {
91         $('password-changed').migrate();
92         e.stopPropagation();
93       });
94       buttons.push(okButton);
96       var proceedAnywayButton = this.ownerDocument.createElement('button');
97       proceedAnywayButton.id = 'password-changed-proceed-button';
98       proceedAnywayButton.textContent =
99           loadTimeData.getString('proceedAnywayButton');
100       proceedAnywayButton.addEventListener('click', function(e) {
101         var screen = $('password-changed');
102         if (screen.classList.contains('resync'))
103           $('password-changed').resync();
104         e.stopPropagation();
105       });
106       buttons.push(proceedAnywayButton);
108       return buttons;
109     },
111     /**
112      * Returns a control which should receive an initial focus.
113      */
114     get defaultControl() {
115       return $('old-password');
116     },
118     /**
119      * True if the the screen is disabled (handles no user interaction).
120      * @type {boolean}
121      */
122     disabled_: false,
123     get disabled() {
124       return this.disabled_;
125     },
126     set disabled(value) {
127       this.disabled_ = value;
128       var controls = this.querySelectorAll('button,input');
129       for (var i = 0, control; control = controls[i]; ++i) {
130         control.disabled = value;
131       }
132       $('login-header-bar').disabled = value;
133       $('password-changed-cant-remember-link').classList[
134           value ? 'add' : 'remove']('disabled');
135     },
137     /**
138      * Cancels password migration and drops the user back to the login screen.
139      */
140     cancel: function() {
141       this.disabled = true;
142       chrome.send('cancelPasswordChangedFlow', ['']);
143     },
145     /**
146      * Starts migration process using old password that user provided.
147      */
148     migrate: function() {
149       if (!$('old-password').value) {
150         $('old-password').focus();
151         return;
152       }
153       this.disabled = true;
154       chrome.send('migrateUserData', [$('old-password').value]);
155     },
157     onAfterShow: function(data) {
158       if (Oobe.isNewGaiaFlow())
159         $('gaia-password-changed').focus();
160     },
162     /**
163      * Event handler that is invoked just before the screen is hidden.
164      */
165     onBeforeHide: function() {
166       $('login-header-bar').disabled = false;
167     },
169     /**
170      * Starts migration process by removing old cryptohome and re-syncing data.
171      */
172     resync: function() {
173       this.disabled = true;
174       chrome.send('resyncUserData');
175     },
177     /**
178      * Show password changed screen.
179      * @param {boolean} showError Whether to show the incorrect password error.
180      */
181     show: function(showError, email) {
182       if (Oobe.isNewGaiaFlow()) {
183         $('password-changed-contents').hidden = true;
184         $('password-changed-controls').hidden = true;
185         var gaiaPasswordChanged = $('gaia-password-changed');
186         gaiaPasswordChanged.reset();
187         gaiaPasswordChanged.hidden = false;
188         if (showError)
189           gaiaPasswordChanged.invalidate();
190         if (email)
191           gaiaPasswordChanged.email = email;
192       } else {
193         var screen = $('password-changed');
194         screen.classList.toggle('password-error', showError);
195         screen.classList.add('migrate');
196         screen.classList.remove('resync');
197         $('old-password').value = '';
198         $('password-changed').disabled = false;
199       }
200       // We'll get here after the successful online authentication.
201       // It assumes session is about to start so hides login screen controls.
202       Oobe.getInstance().headerHidden = false;
203       Oobe.showScreen({id: SCREEN_PASSWORD_CHANGED});
204       $('login-header-bar').signinUIState = SIGNIN_UI_STATE.PASSWORD_CHANGED;
205       if (!Oobe.isNewGaiaFlow())
206         $('password-changed-ok-button').disabled = true;
207     }
208   };