353c098822bfd8dffe0cf37a53843b3f2385c7e8
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / chromeos / accounts_options_handler.cc
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 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/login/user_manager.h"
18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/ui/webui/chromeos/ui_account_tweaks.h"
21 #include "chromeos/settings/cros_settings_names.h"
22 #include "content/public/browser/web_ui.h"
23 #include "google_apis/gaia/gaia_auth_util.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 namespace chromeos {
28
29 namespace {
30
31 // Adds specified user to the whitelist. Returns false if that user is already
32 // in the whitelist.
33 bool WhitelistUser(const std::string& username) {
34   CrosSettings* cros_settings = CrosSettings::Get();
35   if (cros_settings->FindEmailInList(kAccountsPrefUsers, username))
36     return false;
37   base::StringValue username_value(username);
38   cros_settings->AppendToList(kAccountsPrefUsers, &username_value);
39   return true;
40 }
41
42 }  // namespace
43
44 namespace options {
45
46 AccountsOptionsHandler::AccountsOptionsHandler() {
47 }
48
49 AccountsOptionsHandler::~AccountsOptionsHandler() {
50 }
51
52 void AccountsOptionsHandler::RegisterMessages() {
53   web_ui()->RegisterMessageCallback("whitelistUser",
54       base::Bind(&AccountsOptionsHandler::HandleWhitelistUser,
55                  base::Unretained(this)));
56   web_ui()->RegisterMessageCallback("unwhitelistUser",
57       base::Bind(&AccountsOptionsHandler::HandleUnwhitelistUser,
58                  base::Unretained(this)));
59   web_ui()->RegisterMessageCallback("whitelistExistingUsers",
60       base::Bind(&AccountsOptionsHandler::HandleWhitelistExistingUsers,
61                  base::Unretained(this)));
62 }
63
64 void AccountsOptionsHandler::GetLocalizedValues(
65     base::DictionaryValue* localized_strings) {
66   DCHECK(localized_strings);
67
68   RegisterTitle(localized_strings, "accountsPage",
69                 IDS_OPTIONS_ACCOUNTS_TAB_LABEL);
70
71   localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16(
72       IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION));
73   localized_strings->SetString("use_whitelist", l10n_util::GetStringUTF16(
74       IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION));
75   localized_strings->SetString("show_user_on_signin", l10n_util::GetStringUTF16(
76       IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION));
77   localized_strings->SetString("username_edit_hint", l10n_util::GetStringUTF16(
78       IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT));
79   localized_strings->SetString("username_format", l10n_util::GetStringUTF16(
80       IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT));
81   localized_strings->SetString("add_users", l10n_util::GetStringUTF16(
82       IDS_OPTIONS_ACCOUNTS_ADD_USERS));
83   localized_strings->SetString("owner_only", l10n_util::GetStringUTF16(
84       IDS_OPTIONS_ACCOUNTS_OWNER_ONLY));
85
86   policy::BrowserPolicyConnectorChromeOS* connector =
87       g_browser_process->platform_part()->browser_policy_connector_chromeos();
88   localized_strings->SetBoolean("whitelist_is_managed",
89                                 connector->IsEnterpriseManaged());
90
91   AddAccountUITweaksLocalizedValues(localized_strings);
92 }
93
94 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue* args) {
95   std::string typed_email;
96   std::string name;
97   if (!args->GetString(0, &typed_email) ||
98       !args->GetString(1, &name)) {
99     return;
100   }
101
102   WhitelistUser(gaia::CanonicalizeEmail(typed_email));
103 }
104
105 void AccountsOptionsHandler::HandleUnwhitelistUser(
106     const base::ListValue* args) {
107   std::string email;
108   if (!args->GetString(0, &email)) {
109     return;
110   }
111
112   base::StringValue canonical_email(gaia::CanonicalizeEmail(email));
113   CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &canonical_email);
114   UserManager::Get()->RemoveUser(email, NULL);
115 }
116
117 void AccountsOptionsHandler::HandleWhitelistExistingUsers(
118     const base::ListValue* args) {
119   DCHECK(args && args->empty());
120
121   // Creates one list to set. This is needed because user white list update is
122   // asynchronous and sequential. Before previous write comes back, cached list
123   // is stale and should not be used for appending. See http://crbug.com/127215
124   scoped_ptr<base::ListValue> new_list;
125
126   CrosSettings* cros_settings = CrosSettings::Get();
127   const base::ListValue* existing = NULL;
128   if (cros_settings->GetList(kAccountsPrefUsers, &existing) && existing)
129     new_list.reset(existing->DeepCopy());
130   else
131     new_list.reset(new base::ListValue);
132
133   const UserList& users = UserManager::Get()->GetUsers();
134   for (UserList::const_iterator it = users.begin(); it < users.end(); ++it)
135     new_list->AppendIfNotPresent(new base::StringValue((*it)->email()));
136
137   cros_settings->Set(kAccountsPrefUsers, *new_list.get());
138 }
139
140 }  // namespace options
141 }  // namespace chromeos