Home | History | Annotate | Download | only in chromeos
      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.
      4 
      5 cr.define('options', function() {
      6   var Page = cr.ui.pageManager.Page;
      7   var PageManager = cr.ui.pageManager.PageManager;
      8 
      9   /////////////////////////////////////////////////////////////////////////////
     10   // AccountsOptions class:
     11 
     12   /**
     13    * Encapsulated handling of ChromeOS accounts options page.
     14    * @constructor
     15    * @extends {cr.ui.pageManager.Page}
     16    */
     17   function AccountsOptions(model) {
     18     Page.call(this, 'accounts', loadTimeData.getString('accountsPageTabTitle'),
     19               'accountsPage');
     20     // Whether to show the whitelist.
     21     this.showWhitelist_ = false;
     22   }
     23 
     24   cr.addSingletonGetter(AccountsOptions);
     25 
     26   AccountsOptions.prototype = {
     27     // Inherit AccountsOptions from Page.
     28     __proto__: Page.prototype,
     29 
     30     /** @override */
     31     initializePage: function() {
     32       Page.prototype.initializePage.call(this);
     33 
     34       // Set up accounts page.
     35       var userList = $('userList');
     36       userList.addEventListener('remove', this.handleRemoveUser_);
     37 
     38       var userNameEdit = $('userNameEdit');
     39       options.accounts.UserNameEdit.decorate(userNameEdit);
     40       userNameEdit.addEventListener('add', this.handleAddUser_);
     41 
     42       // If the current user is not the owner, do not show the user list.
     43       // If the current user is not the owner, or the device is enterprise
     44       // managed, show a warning that settings cannot be modified.
     45       this.showWhitelist_ = UIAccountTweaks.currentUserIsOwner();
     46       if (this.showWhitelist_) {
     47         options.accounts.UserList.decorate(userList);
     48       } else {
     49         $('ownerOnlyWarning').hidden = false;
     50         this.managed = AccountsOptions.whitelistIsManaged();
     51       }
     52 
     53       this.addEventListener('visibleChange', this.handleVisibleChange_);
     54 
     55       $('useWhitelistCheck').addEventListener('change',
     56           this.handleUseWhitelistCheckChange_.bind(this));
     57 
     58       Preferences.getInstance().addEventListener(
     59           $('useWhitelistCheck').pref,
     60           this.handleUseWhitelistPrefChange_.bind(this));
     61 
     62       $('accounts-options-overlay-confirm').onclick =
     63           PageManager.closeOverlay.bind(PageManager);
     64     },
     65 
     66     /**
     67      * Update user list control state.
     68      * @private
     69      */
     70     updateControls_: function() {
     71       $('userList').disabled =
     72       $('userNameEdit').disabled = !this.showWhitelist_ ||
     73                                    AccountsOptions.whitelistIsManaged() ||
     74                                    !$('useWhitelistCheck').checked;
     75     },
     76 
     77     /**
     78      * Handler for Page's visible property change event.
     79      * @private
     80      * @param {Event} e Property change event.
     81      */
     82     handleVisibleChange_: function(e) {
     83       if (this.visible) {
     84         this.updateControls_();
     85         if (this.showWhitelist_)
     86           $('userList').redraw();
     87       }
     88     },
     89 
     90     /**
     91      * Handler for allow guest check change.
     92      * @private
     93      */
     94     handleUseWhitelistCheckChange_: function(e) {
     95       // Whitelist existing users when guest login is being disabled.
     96       if ($('useWhitelistCheck').checked) {
     97         chrome.send('whitelistExistingUsers');
     98       }
     99 
    100       this.updateControls_();
    101     },
    102 
    103     /**
    104      * handler for allow guest pref change.
    105      * @private
    106      */
    107     handleUseWhitelistPrefChange_: function(e) {
    108       this.updateControls_();
    109     },
    110 
    111     /**
    112      * Handler for "add" event fired from userNameEdit.
    113      * @private
    114      * @param {Event} e Add event fired from userNameEdit.
    115      */
    116     handleAddUser_: function(e) {
    117       chrome.send('whitelistUser', [e.user.email, e.user.name]);
    118       chrome.send('coreOptionsUserMetricsAction',
    119                   ['Options_WhitelistedUser_Add']);
    120     },
    121 
    122     /**
    123      * Handler for "remove" event fired from userList.
    124      * @private
    125      * @param {Event} e Remove event fired from userList.
    126      */
    127     handleRemoveUser_: function(e) {
    128       chrome.send('unwhitelistUser', [e.user.username]);
    129       chrome.send('coreOptionsUserMetricsAction',
    130                   ['Options_WhitelistedUser_Remove']);
    131     }
    132   };
    133 
    134 
    135   /**
    136    * Returns whether the whitelist is managed by policy or not.
    137    */
    138   AccountsOptions.whitelistIsManaged = function() {
    139     return loadTimeData.getBoolean('whitelist_is_managed');
    140   };
    141 
    142   /**
    143    * Update account picture.
    144    * @param {string} username User for which to update the image.
    145    */
    146   AccountsOptions.updateAccountPicture = function(username) {
    147     if (this.showWhitelist_)
    148       $('userList').updateAccountPicture(username);
    149   };
    150 
    151   // Export
    152   return {
    153     AccountsOptions: AccountsOptions
    154   };
    155 
    156 });
    157