Home | History | Annotate | Download | only in chromeos
      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 
      5 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h"
      6 
      7 #include "base/json/json_reader.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/utf_string_conversions.h"
     10 #include "base/values.h"
     11 #include "chrome/browser/browser_process.h"
     12 #include "chrome/browser/chromeos/cros_settings_names.h"
     13 #include "chrome/browser/chromeos/login/authenticator.h"
     14 #include "chrome/browser/chromeos/login/user_manager.h"
     15 #include "chrome/browser/chromeos/user_cros_settings_provider.h"
     16 #include "chrome/browser/prefs/pref_service.h"
     17 #include "chrome/browser/ui/webui/options/options_managed_banner_handler.h"
     18 #include "chrome/common/url_constants.h"
     19 #include "grit/generated_resources.h"
     20 #include "ui/base/l10n/l10n_util.h"
     21 
     22 namespace chromeos {
     23 
     24 AccountsOptionsHandler::AccountsOptionsHandler()
     25     : CrosOptionsPageUIHandler(new UserCrosSettingsProvider) {
     26 }
     27 
     28 AccountsOptionsHandler::~AccountsOptionsHandler() {
     29 }
     30 
     31 void AccountsOptionsHandler::RegisterMessages() {
     32   DCHECK(web_ui_);
     33   web_ui_->RegisterMessageCallback("whitelistUser",
     34       NewCallback(this, &AccountsOptionsHandler::WhitelistUser));
     35   web_ui_->RegisterMessageCallback("unwhitelistUser",
     36       NewCallback(this, &AccountsOptionsHandler::UnwhitelistUser));
     37   web_ui_->RegisterMessageCallback("fetchUserPictures",
     38       NewCallback(this, &AccountsOptionsHandler::FetchUserPictures));
     39   web_ui_->RegisterMessageCallback("whitelistExistingUsers",
     40       NewCallback(this, &AccountsOptionsHandler::WhitelistExistingUsers));
     41 }
     42 
     43 void AccountsOptionsHandler::GetLocalizedValues(
     44     DictionaryValue* localized_strings) {
     45   DCHECK(localized_strings);
     46 
     47   RegisterTitle(localized_strings, "accountsPage",
     48                 IDS_OPTIONS_ACCOUNTS_TAB_LABEL);
     49 
     50   localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16(
     51       IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION));
     52   localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16(
     53       IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION));
     54   localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16(
     55       IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION));
     56   localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16(
     57       IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT));
     58   localized_strings->SetString("username_format",l10n_util::GetStringUTF16(
     59       IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT));
     60   localized_strings->SetString("add_users",l10n_util::GetStringUTF16(
     61       IDS_OPTIONS_ACCOUNTS_ADD_USERS));
     62   localized_strings->SetString("owner_only", l10n_util::GetStringUTF16(
     63       IDS_OPTIONS_ACCOUNTS_OWNER_ONLY));
     64   localized_strings->SetString("owner_user_id", UTF8ToUTF16(
     65       UserCrosSettingsProvider::cached_owner()));
     66 
     67   localized_strings->SetString("current_user_is_owner",
     68       UserManager::Get()->current_user_is_owner() ?
     69       ASCIIToUTF16("true") : ASCIIToUTF16("false"));
     70   localized_strings->SetString("whitelist_is_managed",
     71       g_browser_process->local_state()->IsManagedPreference(
     72           kAccountsPrefUsers) ?  ASCIIToUTF16("true") : ASCIIToUTF16("false"));
     73 }
     74 
     75 UserCrosSettingsProvider* AccountsOptionsHandler::users_settings() const {
     76   return static_cast<UserCrosSettingsProvider*>(settings_provider_.get());
     77 }
     78 
     79 void AccountsOptionsHandler::WhitelistUser(const ListValue* args) {
     80   std::string email;
     81   if (!args->GetString(0, &email)) {
     82     return;
     83   }
     84 
     85   users_settings()->WhitelistUser(Authenticator::Canonicalize(email));
     86 }
     87 
     88 void AccountsOptionsHandler::UnwhitelistUser(const ListValue* args) {
     89   std::string email;
     90   if (!args->GetString(0, &email)) {
     91     return;
     92   }
     93 
     94   users_settings()->UnwhitelistUser(Authenticator::Canonicalize(email));
     95   UserManager::Get()->RemoveUser(email, NULL);
     96 }
     97 
     98 void AccountsOptionsHandler::FetchUserPictures(const ListValue* args) {
     99   DictionaryValue user_pictures;
    100 
    101   UserVector users = UserManager::Get()->GetUsers();
    102   for (UserVector::const_iterator it = users.begin();
    103        it != users.end(); ++it) {
    104     StringValue* image_url =
    105         new StringValue(chrome::kChromeUIUserImageURL + it->email());
    106     // SetWithoutPathExpansion because email has "." in it.
    107     user_pictures.SetWithoutPathExpansion(it->email(), image_url);
    108   }
    109 
    110   web_ui_->CallJavascriptFunction("AccountsOptions.setUserPictures",
    111                                   user_pictures);
    112 }
    113 
    114 void AccountsOptionsHandler::WhitelistExistingUsers(const ListValue* args) {
    115   ListValue whitelist_users;
    116 
    117   UserVector users = UserManager::Get()->GetUsers();
    118   for (UserVector::const_iterator it = users.begin();
    119        it < users.end(); ++it) {
    120     const std::string& email = it->email();
    121     if (!UserCrosSettingsProvider::IsEmailInCachedWhitelist(email)) {
    122       DictionaryValue* user_dict = new DictionaryValue;
    123       user_dict->SetString("name", it->GetDisplayName());
    124       user_dict->SetString("email", email);
    125       user_dict->SetBoolean("owner", false);
    126 
    127       whitelist_users.Append(user_dict);
    128     }
    129   }
    130 
    131   web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", whitelist_users);
    132 }
    133 
    134 void AccountsOptionsHandler::Initialize() {
    135   DCHECK(web_ui_);
    136   banner_handler_.reset(
    137       new OptionsManagedBannerHandler(web_ui_,
    138                                       ASCIIToUTF16("AccountsOptions"),
    139                                       OPTIONS_PAGE_ACCOUNTS));
    140 }
    141 
    142 }  // namespace chromeos
    143