1// Copyright (c) 2011 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.
4
5cr.define('options', function() {
6  const OptionsPage = options.OptionsPage;
7  const ArrayDataModel = cr.ui.ArrayDataModel;
8
9  /////////////////////////////////////////////////////////////////////////////
10  // PasswordManager class:
11
12  /**
13   * Encapsulated handling of password and exceptions page.
14   * @constructor
15   */
16  function PasswordManager() {
17    this.activeNavTab = null;
18    OptionsPage.call(this,
19                     'passwords',
20                     templateData.passwordsPageTabTitle,
21                     'password-manager');
22  }
23
24  cr.addSingletonGetter(PasswordManager);
25
26  PasswordManager.prototype = {
27    __proto__: OptionsPage.prototype,
28
29    /**
30     * The saved passwords list.
31     * @type {DeletableItemList}
32     * @private
33     */
34    savedPasswordsList_: null,
35
36    /**
37     * The password exceptions list.
38     * @type {DeletableItemList}
39     * @private
40     */
41    passwordExceptionsList_: null,
42
43    /** @inheritDoc */
44    initializePage: function() {
45      OptionsPage.prototype.initializePage.call(this);
46
47      this.createSavedPasswordsList_();
48      this.createPasswordExceptionsList_();
49    },
50
51    /** @inheritDoc */
52    canShowPage: function() {
53      return !PersonalOptions.disablePasswordManagement();
54    },
55
56    /** @inheritDoc */
57    didShowPage: function() {
58      // Updating the password lists may cause a blocking platform dialog pop up
59      // (Mac, Linux), so we delay this operation until the page is shown.
60      chrome.send('updatePasswordLists');
61    },
62
63    /**
64     * Creates, decorates and initializes the saved passwords list.
65     * @private
66     */
67    createSavedPasswordsList_: function() {
68      this.savedPasswordsList_ = $('saved-passwords-list');
69      options.passwordManager.PasswordsList.decorate(this.savedPasswordsList_);
70      this.savedPasswordsList_.autoExpands = true;
71    },
72
73    /**
74     * Creates, decorates and initializes the password exceptions list.
75     * @private
76     */
77    createPasswordExceptionsList_: function() {
78      this.passwordExceptionsList_ = $('password-exceptions-list');
79      options.passwordManager.PasswordExceptionsList.decorate(
80          this.passwordExceptionsList_);
81      this.passwordExceptionsList_.autoExpands = true;
82    },
83
84    /**
85     * Updates the visibility of the list and empty list placeholder.
86     * @param {!List} list The list to toggle visilibility for.
87     */
88    updateListVisibility_: function(list) {
89      var empty = list.dataModel.length == 0;
90      var listPlaceHolderID = list.id + '-empty-placeholder';
91      list.hidden = empty;
92      $(listPlaceHolderID).hidden = !empty;
93    },
94
95    /**
96     * Updates the data model for the saved passwords list with the values from
97     * |entries|.
98     * @param {Array} entries The list of saved password data.
99     */
100    setSavedPasswordsList_: function(entries) {
101      this.savedPasswordsList_.dataModel = new ArrayDataModel(entries);
102      this.updateListVisibility_(this.savedPasswordsList_);
103    },
104
105    /**
106     * Updates the data model for the password exceptions list with the values
107     * from |entries|.
108     * @param {Array} entries The list of password exception data.
109     */
110    setPasswordExceptionsList_: function(entries) {
111      this.passwordExceptionsList_.dataModel = new ArrayDataModel(entries);
112      this.updateListVisibility_(this.passwordExceptionsList_);
113    },
114  };
115
116  /**
117   * Call to remove a saved password.
118   * @param rowIndex indicating the row to remove.
119   */
120  PasswordManager.removeSavedPassword = function(rowIndex) {
121      chrome.send('removeSavedPassword', [String(rowIndex)]);
122  };
123
124  /**
125   * Call to remove a password exception.
126   * @param rowIndex indicating the row to remove.
127   */
128  PasswordManager.removePasswordException = function(rowIndex) {
129      chrome.send('removePasswordException', [String(rowIndex)]);
130  };
131
132  /**
133   * Call to remove all saved passwords.
134   * @param tab contentType of the tab currently on.
135   */
136  PasswordManager.removeAllPasswords = function() {
137    chrome.send('removeAllSavedPasswords');
138  };
139
140  /**
141   * Call to remove all saved passwords.
142   * @param tab contentType of the tab currently on.
143   */
144  PasswordManager.removeAllPasswordExceptions = function() {
145    chrome.send('removeAllPasswordExceptions');
146  };
147
148  PasswordManager.setSavedPasswordsList = function(entries) {
149    PasswordManager.getInstance().setSavedPasswordsList_(entries);
150  };
151
152  PasswordManager.setPasswordExceptionsList = function(entries) {
153    PasswordManager.getInstance().setPasswordExceptionsList_(entries);
154  };
155
156  // Export
157  return {
158    PasswordManager: PasswordManager
159  };
160
161});
162