Commit dd5b6a61 authored by noms's avatar noms Committed by Commit bot

After locking a profile and showing the User Manager, make Guest the active profile

This is particularly important on Mac, where unless we do this the Bookmarks/History
menu bar items will reflect the last active profile (which has now been locked).

On Windows this is a nice to have and to be consistent. Overall this adds an extra
layer of security that you won't accidentally open a locked profile's window.

BUG=412952

Review URL: https://codereview.chromium.org/585653002

Cr-Commit-Position: refs/heads/master@{#296187}
parent 5ce3577e
......@@ -142,6 +142,12 @@ class WorkAreaWatcherObserver;
// apps.
- (void)initAppShimMenuController;
// Called when the user has changed browser windows, meaning the backing profile
// may have changed. This can cause a rebuild of the user-data menus. This is a
// no-op if the new profile is the same as the current one. This will always be
// the original profile and never incognito.
- (void)windowChangedToProfile:(Profile*)profile;
@end
#endif // __OBJC__
......
......@@ -217,7 +217,6 @@ bool IsProfileSignedOut(Profile* profile) {
withReply:(NSAppleEventDescriptor*)reply;
- (void)windowLayeringDidChange:(NSNotification*)inNotification;
- (void)activeSpaceDidChange:(NSNotification*)inNotification;
- (void)windowChangedToProfile:(Profile*)profile;
- (void)checkForAnyKeyWindows;
- (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
- (BOOL)shouldQuitWithInProgressDownloads;
......@@ -639,44 +638,6 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver {
[self fixCloseMenuItemKeyEquivalents];
}
// Called when the user has changed browser windows, meaning the backing profile
// may have changed. This can cause a rebuild of the user-data menus. This is a
// no-op if the new profile is the same as the current one. This will always be
// the original profile and never incognito.
- (void)windowChangedToProfile:(Profile*)profile {
if (lastProfile_ == profile)
return;
// Before tearing down the menu controller bridges, return the Cocoa menus to
// their initial state.
if (bookmarkMenuBridge_.get())
bookmarkMenuBridge_->ResetMenu();
if (historyMenuBridge_.get())
historyMenuBridge_->ResetMenu();
// Rebuild the menus with the new profile.
lastProfile_ = profile;
bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_,
[[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu]));
// No need to |BuildMenu| here. It is done lazily upon menu access.
historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
historyMenuBridge_->BuildMenu();
chrome::BrowserCommandController::
UpdateSharedCommandsForIncognitoAvailability(
menuState_.get(), lastProfile_);
profilePrefRegistrar_.reset(new PrefChangeRegistrar());
profilePrefRegistrar_->Init(lastProfile_->GetPrefs());
profilePrefRegistrar_->Add(
prefs::kIncognitoModeAvailability,
base::Bind(&chrome::BrowserCommandController::
UpdateSharedCommandsForIncognitoAvailability,
menuState_.get(),
lastProfile_));
}
- (void)checkForAnyKeyWindows {
if ([NSApp keyWindow])
return;
......@@ -1533,6 +1494,40 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver {
appShimMenuController_.reset([[AppShimMenuController alloc] init]);
}
- (void)windowChangedToProfile:(Profile*)profile {
if (lastProfile_ == profile)
return;
// Before tearing down the menu controller bridges, return the Cocoa menus to
// their initial state.
if (bookmarkMenuBridge_.get())
bookmarkMenuBridge_->ResetMenu();
if (historyMenuBridge_.get())
historyMenuBridge_->ResetMenu();
// Rebuild the menus with the new profile.
lastProfile_ = profile;
bookmarkMenuBridge_.reset(new BookmarkMenuBridge(lastProfile_,
[[[NSApp mainMenu] itemWithTag:IDC_BOOKMARKS_MENU] submenu]));
// No need to |BuildMenu| here. It is done lazily upon menu access.
historyMenuBridge_.reset(new HistoryMenuBridge(lastProfile_));
historyMenuBridge_->BuildMenu();
chrome::BrowserCommandController::
UpdateSharedCommandsForIncognitoAvailability(
menuState_.get(), lastProfile_);
profilePrefRegistrar_.reset(new PrefChangeRegistrar());
profilePrefRegistrar_->Init(lastProfile_->GetPrefs());
profilePrefRegistrar_->Add(
prefs::kIncognitoModeAvailability,
base::Bind(&chrome::BrowserCommandController::
UpdateSharedCommandsForIncognitoAvailability,
menuState_.get(),
lastProfile_));
}
- (void)applicationDidChangeScreenParameters:(NSNotification*)notification {
// During this callback the working area is not always already updated. Defer.
[self performSelector:@selector(delayedScreenParametersUpdate)
......
......@@ -735,7 +735,7 @@ void ProfileInfoCache::SetProfileIsUsingDefaultAvatarAtIndex(
SetInfoForProfileAtIndex(index, info.release());
}
bool ProfileInfoCache::IsDefaultProfileName(const base::string16& name) {
bool ProfileInfoCache::IsDefaultProfileName(const base::string16& name) const {
// Check if it's a "First user" old-style name.
if (name == l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME) ||
name == l10n_util::GetStringUTF16(IDS_LEGACY_DEFAULT_PROFILE_NAME))
......
......@@ -122,7 +122,7 @@ class ProfileInfoCache : public ProfileInfoInterface,
void SetProfileIsUsingDefaultAvatarAtIndex(size_t index, bool value);
// Determines whether |name| is one of the default assigned names.
bool IsDefaultProfileName(const base::string16& name);
bool IsDefaultProfileName(const base::string16& name) const;
// Returns unique name that can be assigned to a newly created profile.
base::string16 ChooseNameForNewProfile(size_t icon_index) const;
......
......@@ -57,7 +57,7 @@ base::string16 GetAvatarNameForProfile(const base::FilePath& profile_path) {
if (profile_path == ProfileManager::GetGuestProfilePath()) {
display_name = l10n_util::GetStringUTF16(IDS_GUEST_PROFILE_NAME);
} else {
ProfileInfoCache& cache =
const ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t index = cache.GetIndexOfProfileWithPath(profile_path);
......@@ -98,7 +98,7 @@ base::string16 GetAvatarButtonTextForProfile(Profile* profile) {
void UpdateProfileName(Profile* profile,
const base::string16& new_profile_name) {
ProfileInfoCache& cache =
const ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
if (profile_index == std::string::npos)
......@@ -156,4 +156,29 @@ SigninErrorController* GetSigninErrorController(Profile* profile) {
return token_service ? token_service->signin_error_controller() : NULL;
}
Profile* SetActiveProfileToGuestIfLocked() {
Profile* active_profile = ProfileManager::GetLastUsedProfile();
DCHECK(active_profile);
if (active_profile->IsGuestSession())
return active_profile;
ProfileManager* profile_manager = g_browser_process->profile_manager();
const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
size_t index = cache.GetIndexOfProfileWithPath(active_profile->GetPath());
if (!cache.ProfileIsSigninRequiredAtIndex(index))
return NULL;
// The guest profile must have been loaded already.
Profile* guest_profile = profile_manager->GetProfile(
ProfileManager::GetGuestProfilePath());
DCHECK(guest_profile);
PrefService* local_state = g_browser_process->local_state();
DCHECK(local_state);
local_state->SetString(prefs::kProfileLastUsed,
guest_profile->GetPath().BaseName().MaybeAsASCII());
return guest_profile;
}
} // namespace profiles
......@@ -68,6 +68,11 @@ void UpdateGaiaProfilePhotoIfNeeded(Profile* profile);
// NULL.
SigninErrorController* GetSigninErrorController(Profile* profile);
// If the current active profile (given by prefs::kProfileLastUsed) is locked,
// changes the active profile to the Guest profile and returns it, otherwise
// returns NULL. This assumes that the Guest profile has been loaded.
Profile* SetActiveProfileToGuestIfLocked();
} // namespace profiles
#endif // CHROME_BROWSER_PROFILES_PROFILES_STATE_H_
......@@ -4,9 +4,12 @@
#include "chrome/browser/ui/cocoa/profiles/user_manager_mac.h"
#include "base/mac/foundation_util.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/ui/browser_dialogs.h"
#import "chrome/browser/ui/cocoa/browser_window_utils.h"
#include "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
......@@ -146,6 +149,16 @@ class UserManagerWebContentsDelegate : public content::WebContentsDelegate {
}
- (void)show {
// Because the User Manager isn't a BrowserWindowController, activating it
// will not trigger a -windowChangedToProfile and update the menu bar.
// This is only important if the active profile is Guest, which may have
// happened after locking a profile.
Profile* guestProfile = profiles::SetActiveProfileToGuestIfLocked();
if (guestProfile && guestProfile->IsGuestSession()) {
AppController* controller =
base::mac::ObjCCast<AppController>([NSApp delegate]);
[controller windowChangedToProfile:guestProfile];
}
[[self window] makeKeyAndOrderFront:self];
}
......
......@@ -9,6 +9,7 @@
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
......@@ -72,6 +73,10 @@ void UserManagerView::Show(const base::FilePath& profile_path_to_focus,
profiles::UserManagerTutorialMode tutorial_mode) {
ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::OPEN_USER_MANAGER);
if (instance_) {
// If we are showing the User Manager after locking a profile, change the
// active profile to Guest.
profiles::SetActiveProfileToGuestIfLocked();
// If there's a user manager window open already, just activate it.
instance_->GetWidget()->Activate();
return;
......@@ -104,6 +109,10 @@ void UserManagerView::OnGuestProfileCreated(
const base::FilePath& profile_path_to_focus,
Profile* guest_profile,
const std::string& url) {
// If we are showing the User Manager after locking a profile, change the
// active profile to Guest.
profiles::SetActiveProfileToGuestIfLocked();
instance_ = instance.release(); // |instance_| takes over ownership.
instance_->Init(profile_path_to_focus, guest_profile, GURL(url));
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment