OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chromeos/cros_settings.h" | 14 #include "chrome/browser/chromeos/cros_settings.h" |
15 #include "chrome/browser/chromeos/cros_settings_names.h" | 15 #include "chrome/browser/chromeos/cros_settings_names.h" |
16 #include "chrome/browser/chromeos/login/authenticator.h" | 16 #include "chrome/browser/chromeos/login/authenticator.h" |
17 #include "chrome/browser/chromeos/login/user_manager.h" | 17 #include "chrome/browser/chromeos/login/user_manager.h" |
18 #include "chrome/browser/chromeos/user_cros_settings_provider.h" | 18 #include "chrome/browser/chromeos/user_cros_settings_provider.h" |
19 #include "chrome/browser/prefs/pref_service.h" | 19 #include "chrome/browser/prefs/pref_service.h" |
20 #include "chrome/browser/policy/browser_policy_connector.h" | 20 #include "chrome/browser/policy/browser_policy_connector.h" |
21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 | 23 |
24 namespace chromeos { | 24 namespace chromeos { |
25 | 25 |
26 namespace { | |
27 | |
28 // Adds specified user to the whitelist. Returns false if that user is already | |
29 // in the whitelist. | |
30 bool WhitelistUser(const std::string& username) { | |
31 CrosSettings* cros_settings = CrosSettings::Get(); | |
32 if (cros_settings->FindEmailInList(kAccountsPrefUsers, username)) | |
33 return false; | |
34 base::StringValue username_value(username); | |
35 cros_settings->AppendToList(kAccountsPrefUsers, &username_value); | |
36 return true; | |
37 } | |
38 | |
39 } // namespace | |
40 | |
26 AccountsOptionsHandler::AccountsOptionsHandler() { | 41 AccountsOptionsHandler::AccountsOptionsHandler() { |
27 } | 42 } |
28 | 43 |
29 AccountsOptionsHandler::~AccountsOptionsHandler() { | 44 AccountsOptionsHandler::~AccountsOptionsHandler() { |
30 } | 45 } |
31 | 46 |
32 void AccountsOptionsHandler::RegisterMessages() { | 47 void AccountsOptionsHandler::RegisterMessages() { |
33 DCHECK(web_ui_); | 48 DCHECK(web_ui_); |
34 web_ui_->RegisterMessageCallback("whitelistUser", | 49 web_ui_->RegisterMessageCallback("whitelistUser", |
35 base::Bind(&AccountsOptionsHandler::WhitelistUser, | 50 base::Bind(&AccountsOptionsHandler::HandleWhitelistUser, |
36 base::Unretained(this))); | 51 base::Unretained(this))); |
37 web_ui_->RegisterMessageCallback("unwhitelistUser", | 52 web_ui_->RegisterMessageCallback("unwhitelistUser", |
38 base::Bind(&AccountsOptionsHandler::UnwhitelistUser, | 53 base::Bind(&AccountsOptionsHandler::HandleUnwhitelistUser, |
39 base::Unretained(this))); | 54 base::Unretained(this))); |
40 web_ui_->RegisterMessageCallback("whitelistExistingUsers", | 55 web_ui_->RegisterMessageCallback("whitelistExistingUsers", |
41 base::Bind(&AccountsOptionsHandler::WhitelistExistingUsers, | 56 base::Bind(&AccountsOptionsHandler::HandleWhitelistExistingUsers, |
42 base::Unretained(this))); | 57 base::Unretained(this))); |
43 } | 58 } |
44 | 59 |
45 void AccountsOptionsHandler::GetLocalizedValues( | 60 void AccountsOptionsHandler::GetLocalizedValues( |
46 base::DictionaryValue* localized_strings) { | 61 base::DictionaryValue* localized_strings) { |
47 DCHECK(localized_strings); | 62 DCHECK(localized_strings); |
48 | 63 |
49 RegisterTitle(localized_strings, "accountsPage", | 64 RegisterTitle(localized_strings, "accountsPage", |
50 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); | 65 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); |
51 | 66 |
52 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( | 67 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( |
53 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); | 68 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); |
54 localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16( | 69 localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16( |
55 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); | 70 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); |
56 localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16( | 71 localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16( |
57 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); | 72 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); |
58 localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16( | 73 localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16( |
59 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); | 74 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); |
60 localized_strings->SetString("username_format",l10n_util::GetStringUTF16( | 75 localized_strings->SetString("username_format",l10n_util::GetStringUTF16( |
61 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); | 76 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); |
62 localized_strings->SetString("add_users",l10n_util::GetStringUTF16( | 77 localized_strings->SetString("add_users",l10n_util::GetStringUTF16( |
63 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); | 78 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); |
64 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( | 79 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( |
65 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); | 80 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); |
66 | 81 |
67 std::string owner; | 82 std::string owner_email; |
68 CrosSettings::Get()->GetString(kDeviceOwner, &owner); | 83 CrosSettings::Get()->GetString(kDeviceOwner, &owner_email); |
69 localized_strings->SetString("owner_user_id", UTF8ToUTF16(owner)); | 84 // Translate owner's email to the display email. |
85 std::string display_email = | |
86 UserManager::Get()->GetUserDisplayEmail(owner_email); | |
87 localized_strings->SetString("owner_user_id", UTF8ToUTF16(display_email)); | |
70 | 88 |
71 localized_strings->SetString("current_user_is_owner", | 89 localized_strings->SetString("current_user_is_owner", |
72 UserManager::Get()->current_user_is_owner() ? | 90 UserManager::Get()->current_user_is_owner() ? |
73 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 91 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
74 localized_strings->SetString("logged_in_as_guest", | 92 localized_strings->SetString("logged_in_as_guest", |
75 UserManager::Get()->IsLoggedInAsGuest() ? | 93 UserManager::Get()->IsLoggedInAsGuest() ? |
76 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 94 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
77 localized_strings->SetString("whitelist_is_managed", | 95 localized_strings->SetString("whitelist_is_managed", |
78 g_browser_process->browser_policy_connector()->IsEnterpriseManaged() ? | 96 g_browser_process->browser_policy_connector()->IsEnterpriseManaged() ? |
79 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 97 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
80 } | 98 } |
81 | 99 |
82 void AccountsOptionsHandler::WhitelistUser(const base::ListValue* args) { | 100 base::DictionaryValue* AccountsOptionsHandler::CreateUserInfo( |
101 const std::string& username, | |
102 const std::string& display_email, | |
103 const std::string& display_name) { | |
104 base::DictionaryValue* user_dict = new DictionaryValue; | |
105 user_dict->SetString("username", username); | |
106 user_dict->SetString("name", display_email); | |
107 user_dict->SetString("email", display_name); | |
108 user_dict->SetBoolean("owner", IsLoggedInOwner(username)); | |
109 return user_dict; | |
110 } | |
111 | |
112 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue* args) { | |
113 std::string email; | |
114 std::string name; | |
115 if (!args->GetString(0, &email) || | |
116 !args->GetString(1, &name)) { | |
117 return; | |
118 } | |
119 | |
120 std::string canonical_email = Authenticator::Canonicalize(email); | |
121 // Record a display email for the whitelisted user if it doesn't exist yet. | |
122 UserManager::Get()->SaveUserDisplayEmail(canonical_email, email, false); | |
123 | |
124 if (WhitelistUser(canonical_email)) { | |
125 base::ListValue single_user; | |
126 single_user.Append(CreateUserInfo(canonical_email, email, name)); | |
127 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", single_user); | |
128 } | |
129 } | |
130 | |
131 void AccountsOptionsHandler::HandleUnwhitelistUser( | |
132 const base::ListValue* args) { | |
83 std::string email; | 133 std::string email; |
84 if (!args->GetString(0, &email)) { | 134 if (!args->GetString(0, &email)) { |
85 return; | 135 return; |
86 } | 136 } |
87 | 137 |
88 scoped_ptr<base::StringValue> canonical_email( | 138 base::StringValue canonical_email(Authenticator::Canonicalize(email)); |
89 base::Value::CreateStringValue(Authenticator::Canonicalize(email))); | 139 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &canonical_email); |
90 CrosSettings::Get()->AppendToList(kAccountsPrefUsers, canonical_email.get()); | 140 // TODO(ivankr): this will not remove user's diplayed email if there is |
91 } | 141 // no such user. |
92 | |
93 void AccountsOptionsHandler::UnwhitelistUser(const base::ListValue* args) { | |
94 std::string email; | |
95 if (!args->GetString(0, &email)) { | |
96 return; | |
97 } | |
98 | |
99 scoped_ptr<base::StringValue> canonical_email( | |
100 base::Value::CreateStringValue(Authenticator::Canonicalize(email))); | |
101 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, | |
102 canonical_email.get()); | |
103 UserManager::Get()->RemoveUser(email, NULL); | 142 UserManager::Get()->RemoveUser(email, NULL); |
104 } | 143 } |
105 | 144 |
106 void AccountsOptionsHandler::WhitelistExistingUsers( | 145 void AccountsOptionsHandler::HandleWhitelistExistingUsers( |
107 const base::ListValue* args) { | 146 const base::ListValue* args) { |
108 base::ListValue whitelist_users; | 147 base::ListValue whitelist_users; |
109 const UserList& users = UserManager::Get()->GetUsers(); | 148 const UserList& users = UserManager::Get()->GetUsers(); |
110 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) { | 149 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) { |
111 const std::string& email = (*it)->email(); | 150 const std::string& email = (*it)->email(); |
112 if (!CrosSettings::Get()->FindEmailInList(kAccountsPrefUsers, email)) { | 151 if (WhitelistUser(email)) { |
113 base::DictionaryValue* user_dict = new DictionaryValue; | 152 whitelist_users.Append(CreateUserInfo( |
114 user_dict->SetString("name", (*it)->GetDisplayName()); | 153 email, (*it)->display_email(), (*it)->GetDisplayName())); |
115 user_dict->SetString("email", email); | |
116 user_dict->SetBoolean("owner", false); | |
117 | |
118 whitelist_users.Append(user_dict); | |
119 } | 154 } |
120 } | 155 } |
121 | 156 |
122 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", whitelist_users); | 157 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", whitelist_users); |
123 } | 158 } |
124 | 159 |
160 bool AccountsOptionsHandler::IsLoggedInOwner(const std::string& username) { | |
James Hawkins
2011/12/02 19:52:30
This should be able to be moved to unnamed namespa
Ivan Korotkov
2011/12/05 14:12:48
Done.
| |
161 UserManager* user_manager = UserManager::Get(); | |
162 return user_manager->current_user_is_owner() && | |
163 user_manager->logged_in_user().email() == username; | |
164 } | |
165 | |
125 } // namespace chromeos | 166 } // namespace chromeos |
OLD | NEW |