Index: chrome/browser/chromeos/login/user_manager.cc |
diff --git a/chrome/browser/chromeos/login/user_manager.cc b/chrome/browser/chromeos/login/user_manager.cc |
index 3b56995820865b357fa56d6669ff451387731f35..af449e14d9b08cd572336574216357510cae670e 100644 |
--- a/chrome/browser/chromeos/login/user_manager.cc |
+++ b/chrome/browser/chromeos/login/user_manager.cc |
@@ -36,6 +36,7 @@ |
#include "chrome/browser/chromeos/login/ownership_service.h" |
#include "chrome/browser/chromeos/login/remove_user_delegate.h" |
#include "chrome/browser/chromeos/system/runtime_environment.h" |
+#include "chrome/browser/policy/browser_policy_connector.h" |
#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/browser/prefs/scoped_user_pref_update.h" |
#include "chrome/browser/profiles/profile_downloader.h" |
@@ -322,15 +323,24 @@ void UserManager::UserLoggedIn(const std::string& email) { |
user_is_logged_in_ = true; |
if (email == kGuestUser) { |
+ current_user_is_ephemeral_ = true; |
GuestUserLoggedIn(); |
return; |
} |
if (email == kDemoUser) { |
+ current_user_is_ephemeral_ = true; |
DemoUserLoggedIn(); |
return; |
} |
+ if (IsEphemeralUser(email)) { |
+ current_user_is_ephemeral_ = true; |
+ logged_in_user_ = CreateUser(email); |
+ NotifyOnLogin(); |
+ return; |
+ } |
+ |
EnsureUsersLoaded(); |
// Clear the prefs view of the users. |
@@ -427,52 +437,7 @@ void UserManager::RemoveUser(const std::string& email, |
void UserManager::RemoveUserFromList(const std::string& email) { |
EnsureUsersLoaded(); |
- |
- // Clear the prefs view of the users. |
- PrefService* prefs = g_browser_process->local_state(); |
- ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); |
- prefs_users_update->Clear(); |
- |
- UserList::iterator user_to_remove = users_.end(); |
- for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) { |
- std::string user_email = (*it)->email(); |
- // Skip user that we would like to delete. |
- if (email != user_email) |
- prefs_users_update->Append(Value::CreateStringValue(user_email)); |
- else |
- user_to_remove = it; |
- } |
- |
- DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); |
- std::string image_path_string; |
- prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); |
- prefs_images_update->RemoveWithoutPathExpansion(email, NULL); |
- |
- DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
- int oauth_status; |
- prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); |
- prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); |
- |
- DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); |
- prefs_display_email_update->RemoveWithoutPathExpansion(email, NULL); |
- |
- if (user_to_remove != users_.end()) { |
- --display_name_count_[(*user_to_remove)->GetDisplayName()]; |
- delete *user_to_remove; |
- users_.erase(user_to_remove); |
- } |
- |
- int default_image_id = User::kInvalidImageIndex; |
- if (!image_path_string.empty() && |
- !IsDefaultImagePath(image_path_string, &default_image_id)) { |
- FilePath image_path(image_path_string); |
- BrowserThread::PostTask( |
- BrowserThread::FILE, |
- FROM_HERE, |
- base::Bind(&UserManager::DeleteUserImage, |
- base::Unretained(this), |
- image_path)); |
- } |
+ RemoveUserFromListInternal(email); |
} |
bool UserManager::IsKnownUser(const std::string& email) const { |
@@ -480,15 +445,9 @@ bool UserManager::IsKnownUser(const std::string& email) const { |
} |
const User* UserManager::FindUser(const std::string& email) const { |
- // Speed up search by checking the logged-in user first. |
if (logged_in_user_ && logged_in_user_->email() == email) |
return logged_in_user_; |
- const UserList& users = GetUsers(); |
- for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) { |
- if ((*it)->email() == email) |
- return *it; |
- } |
- return NULL; |
+ return FindUserInList(email); |
} |
bool UserManager::IsDisplayNameUnique(const std::string& display_name) const { |
@@ -499,14 +458,21 @@ void UserManager::SaveUserOAuthStatus( |
const std::string& username, |
User::OAuthTokenStatus oauth_token_status) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- PrefService* local_state = g_browser_process->local_state(); |
- DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); |
- oauth_status_update->SetWithoutPathExpansion(username, |
- new base::FundamentalValue(static_cast<int>(oauth_token_status))); |
+ |
DVLOG(1) << "Saving user OAuth token status in Local State"; |
User* user = const_cast<User*>(FindUser(username)); |
if (user) |
user->set_oauth_token_status(oauth_token_status); |
+ |
+ // Do not update local store if the user is ephemeral. |
+ if (IsEphemeralUser(username)) |
+ return; |
+ |
+ PrefService* local_state = g_browser_process->local_state(); |
+ |
+ DictionaryPrefUpdate oauth_status_update(local_state, kUserOAuthTokenStatus); |
+ oauth_status_update->SetWithoutPathExpansion(username, |
+ new base::FundamentalValue(static_cast<int>(oauth_token_status))); |
} |
User::OAuthTokenStatus UserManager::LoadUserOAuthStatus( |
@@ -543,6 +509,10 @@ void UserManager::SaveUserDisplayEmail(const std::string& username, |
user->set_display_email(display_email); |
+ // Do not update local store if the user is ephemeral. |
+ if (IsEphemeralUser(username)) |
+ return; |
+ |
PrefService* local_state = g_browser_process->local_state(); |
DictionaryPrefUpdate display_email_update(local_state, kUserDisplayEmail); |
@@ -614,6 +584,9 @@ void UserManager::Observe(int type, |
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
base::Bind(&UserManager::CheckOwnership, |
base::Unretained(this))); |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(&UserManager::RetrieveTrustedDevicePolicies, |
+ base::Unretained(this))); |
break; |
case chrome::NOTIFICATION_PROFILE_ADDED: |
if (user_is_logged_in() && !IsLoggedInAsGuest()) { |
@@ -688,7 +661,9 @@ UserManager::UserManager() |
logged_in_user_(NULL), |
current_user_is_owner_(false), |
current_user_is_new_(false), |
+ current_user_is_ephemeral_(false), |
user_is_logged_in_(false), |
+ ephemeral_users_enabled_(false), |
observed_sync_service_(NULL), |
last_image_set_async_(false), |
downloaded_profile_image_data_url_(chrome::kAboutBlankURL) { |
@@ -699,6 +674,7 @@ UserManager::UserManager() |
content::NotificationService::AllSources()); |
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_ADDED, |
content::NotificationService::AllSources()); |
+ RetrieveTrustedDevicePolicies(); |
} |
UserManager::~UserManager() { |
@@ -794,6 +770,83 @@ void UserManager::EnsureUsersLoaded() { |
} |
} |
+void UserManager::RetrieveTrustedDevicePolicies() { |
+ ephemeral_users_enabled_ = false; |
+ owner_email_ = ""; |
+ |
+ CrosSettings* cros_settings = CrosSettings::Get(); |
+ // Schedule a callback if device policy has not yet been verified. |
+ if (!cros_settings->GetTrusted( |
+ kAccountsPrefEphemeralUsersEnabled, |
+ base::Bind(&UserManager::RetrieveTrustedDevicePolicies, |
+ base::Unretained(this)))) { |
+ return; |
+ } |
+ |
+ cros_settings->GetBoolean(kAccountsPrefEphemeralUsersEnabled, |
+ &ephemeral_users_enabled_); |
+ if (g_browser_process->browser_policy_connector()->IsEnterpriseManaged()) |
+ owner_email_ = ""; |
Nikita (slow)
2012/03/07 10:03:04
You could always call
cros_settings->GetString(kD
use bartfab instead
2012/03/07 11:10:08
Done.
|
+ else |
+ cros_settings->GetString(kDeviceOwner, &owner_email_); |
+ |
+ // If ephemeral users are enabled, remove all users except the owner. |
+ if (ephemeral_users_enabled_) { |
+ scoped_ptr<base::ListValue> users( |
+ g_browser_process->local_state()->GetList(kLoggedInUsers)->DeepCopy()); |
+ |
+ bool changed = false; |
+ for (base::ListValue::const_iterator user = users->begin(); |
+ user != users->end(); ++user) { |
+ std::string user_email; |
+ (*user)->GetAsString(&user_email); |
+ if (user_email != owner_email_) { |
+ RemoveUserFromListInternal(user_email); |
+ changed = true; |
+ } |
+ } |
+ |
+ if (changed) { |
+ // Trigger a redraw of the login window. |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, |
+ content::Source<UserManager>(this), |
+ content::NotificationService::NoDetails()); |
+ } |
+ } |
+} |
+ |
+bool UserManager::AreEphemeralUsersEnabled() const { |
+ return ephemeral_users_enabled_ && |
+ (g_browser_process->browser_policy_connector()->IsEnterpriseManaged() || |
+ !owner_email_.empty()); |
+} |
+ |
+bool UserManager::IsEphemeralUser(const std::string& email) const { |
+ // The guest user always is ephemeral. |
+ if (email == guest_user_.email()) |
+ return true; |
+ |
+ // The currently logged-in user is ephemeral iff logged in as ephemeral. |
+ if (logged_in_user_ && (email == logged_in_user_->email())) |
+ return current_user_is_ephemeral_; |
+ |
+ // Any other user is ephemeral iff ephemeral users are enabled, the user is |
+ // not the owner and is not in the persistent list. |
+ return AreEphemeralUsersEnabled() && |
+ (email != owner_email_) && |
+ !FindUserInList(email); |
+} |
+ |
+const User* UserManager::FindUserInList(const std::string& email) const { |
+ const UserList& users = GetUsers(); |
+ for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) { |
+ if ((*it)->email() == email) |
+ return *it; |
+ } |
+ return NULL; |
+} |
+ |
void UserManager::StubUserLoggedIn() { |
logged_in_user_ = &stub_user_; |
stub_user_.SetImage(GetDefaultImage(kStubDefaultImageIndex), |
@@ -833,6 +886,16 @@ void UserManager::NotifyOnLogin() { |
base::Unretained(this))); |
} |
+void UserManager::LogoutForTest() { |
+ if (logged_in_user_ && current_user_is_ephemeral_) |
+ delete logged_in_user_; |
+ logged_in_user_ = NULL; |
+ current_user_is_owner_ = false; |
+ current_user_is_new_ = false; |
+ current_user_is_ephemeral_ = false; |
+ user_is_logged_in_ = false; |
+} |
+ |
void UserManager::SetInitialUserImage(const std::string& username) { |
// Choose a random default image. |
int image_id = base::RandInt(0, kDefaultImagesCount - 1); |
@@ -876,6 +939,10 @@ void UserManager::SaveUserImageInternal(const std::string& username, |
SetUserImage(username, image_index, image); |
+ // Ignore for ephemeral users. |
+ if (IsEphemeralUser(username)) |
+ return; |
+ |
FilePath image_path = GetImagePathForUser(username); |
DVLOG(1) << "Saving user image to " << image_path.value(); |
@@ -922,6 +989,10 @@ void UserManager::SaveImageToLocalState(const std::string& username, |
bool is_async) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ // Ignore for ephemeral users. |
+ if (IsEphemeralUser(username)) |
+ return; |
+ |
// TODO(ivankr): use unique filenames for user images each time |
// a new image is set so that only the last image update is saved |
// to Local State and notified. |
@@ -1073,4 +1144,52 @@ User* UserManager::CreateUser(const std::string& email) const { |
return user; |
} |
+void UserManager::RemoveUserFromListInternal(const std::string& email) { |
+ // Clear the prefs view of the users. |
+ PrefService* prefs = g_browser_process->local_state(); |
+ ListPrefUpdate prefs_users_update(prefs, kLoggedInUsers); |
+ prefs_users_update->Clear(); |
+ |
+ UserList::iterator user_to_remove = users_.end(); |
+ for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) { |
+ std::string user_email = (*it)->email(); |
+ // Skip user that we would like to delete. |
+ if (email != user_email) |
+ prefs_users_update->Append(Value::CreateStringValue(user_email)); |
+ else |
+ user_to_remove = it; |
+ } |
+ |
+ DictionaryPrefUpdate prefs_images_update(prefs, kUserImages); |
+ std::string image_path_string; |
+ prefs_images_update->GetStringWithoutPathExpansion(email, &image_path_string); |
+ prefs_images_update->RemoveWithoutPathExpansion(email, NULL); |
+ |
+ DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
+ int oauth_status; |
+ prefs_oauth_update->GetIntegerWithoutPathExpansion(email, &oauth_status); |
+ prefs_oauth_update->RemoveWithoutPathExpansion(email, NULL); |
+ |
+ DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); |
+ prefs_display_email_update->RemoveWithoutPathExpansion(email, NULL); |
+ |
+ if (user_to_remove != users_.end()) { |
+ --display_name_count_[(*user_to_remove)->GetDisplayName()]; |
+ delete *user_to_remove; |
+ users_.erase(user_to_remove); |
+ } |
+ |
+ int default_image_id = User::kInvalidImageIndex; |
+ if (!image_path_string.empty() && |
+ !IsDefaultImagePath(image_path_string, &default_image_id)) { |
+ FilePath image_path(image_path_string); |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind(&UserManager::DeleteUserImage, |
+ base::Unretained(this), |
+ image_path)); |
+ } |
+} |
+ |
} // namespace chromeos |