Commit 7108d91e authored by noms@chromium.org's avatar noms@chromium.org

[Mac] Check if a profile is locked before allowing a new window to be opened.

BUG=331406
TEST=With the --new-profile-management flag, start Chrome. Sign in a profile, and
choose "View all people" from the avatar menu once that is complete. Close the
profile browser, then close the user manager window (you should have no windows open)
1. Clicking on the docked Chrome icon should open the user manager and 2. Command-N
(or Open New Window) form the docked Chrome icon should do the same.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247891 0039d316-1c4b-4281-b951-d872f2087c98
parent 6e018548
......@@ -48,6 +48,7 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/browser_mac.h"
......@@ -76,6 +77,7 @@
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/mac/app_mode_common.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/profile_management_switches.h"
#include "chrome/common/service_messages.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
......@@ -189,6 +191,19 @@ void RecordLastRunAppBundlePath() {
base::TimeDelta::FromMilliseconds(1500));
}
bool IsProfileSignedOut(Profile* profile) {
// The signed out status only makes sense at the moment in the context of the
// --new-profile-management flag.
if (!switches::IsNewProfileManagement())
return false;
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
if (profile_index == std::string::npos)
return false;
return cache.ProfileIsSigninRequiredAtIndex(profile_index);
}
} // anonymous namespace
@interface AppController (Private)
......@@ -974,6 +989,15 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver {
return;
NSInteger tag = [sender tag];
// If there are no browser windows, and we are trying to open a browser
// for a locked profile, we have to show the User Manager instead as the
// locked profile needs authentication.
if (IsProfileSignedOut(lastProfile)) {
chrome::ShowUserManager(lastProfile->GetPath());
return;
}
switch (tag) {
case IDC_NEW_TAB:
// Create a new tab in an existing browser window (which we activate) if
......@@ -1186,7 +1210,15 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver {
}
// Otherwise open a new window.
CreateBrowser([self lastProfile]);
// If the last profile was locked, we have to open the User Manager, as the
// profile requires authentication. Similarly, because guest mode is
// implemented as forced incognito, we can't open a new guest browser either,
// so we have to show the User Manager as well.
Profile* lastProfile = [self lastProfile];
if (lastProfile->IsGuestSession() || IsProfileSignedOut(lastProfile))
chrome::ShowUserManager(lastProfile->GetPath());
else
CreateBrowser(lastProfile);
// We've handled the reopen event, so return NO to tell AppKit not
// to do anything.
......
......@@ -7,16 +7,22 @@
#include "apps/shell_window_registry.h"
#include "base/command_line.h"
#include "base/mac/scoped_nsobject.h"
#include "base/prefs/pref_service.h"
#include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/apps/app_browsertest_util.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/user_manager_mac.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#import "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
......@@ -113,4 +119,110 @@ IN_PROC_BROWSER_TEST_F(AppControllerWebAppBrowserTest,
EXPECT_EQ(GetAppURL(), current_url.spec());
}
// Called when the ProfileManager has created a profile.
void CreateProfileCallback(const base::Closure& quit_closure,
Profile* profile,
Profile::CreateStatus status) {
EXPECT_TRUE(profile);
EXPECT_NE(Profile::CREATE_STATUS_LOCAL_FAIL, status);
EXPECT_NE(Profile::CREATE_STATUS_REMOTE_FAIL, status);
// This will be called multiple times. Wait until the profile is initialized
// fully to quit the loop.
if (status == Profile::CREATE_STATUS_INITIALIZED)
quit_closure.Run();
}
void CreateAndWaitForGuestProfile() {
ProfileManager::CreateCallback create_callback =
base::Bind(&CreateProfileCallback,
base::MessageLoop::current()->QuitClosure());
g_browser_process->profile_manager()->CreateProfileAsync(
ProfileManager::GetGuestProfilePath(),
create_callback,
base::string16(),
base::string16(),
std::string());
base::RunLoop().Run();
}
class AppControllerNewProfileManagementBrowserTest
: public InProcessBrowserTest {
protected:
AppControllerNewProfileManagementBrowserTest()
: active_browser_list_(BrowserList::GetInstance(
chrome::GetActiveDesktop())) {
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kNewProfileManagement);
}
const BrowserList* active_browser_list_;
};
// Test that for a regular last profile, a reopen event opens a browser.
IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
RegularProfileReopenWithNoWindows) {
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
EXPECT_EQ(2u, active_browser_list_->size());
EXPECT_FALSE(UserManagerMac::IsShowing());
}
// Test that for a locked last profile, a reopen event opens the User Manager.
IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
LockedProfileReopenWithNoWindows) {
// The User Manager uses the guest profile as its underlying profile. To
// minimize flakiness due to the scheduling/descheduling of tasks on the
// different threads, pre-initialize the guest profile before it is needed.
CreateAndWaitForGuestProfile();
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
// Lock the active profile.
Profile* profile = [ac lastProfile];
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
cache.SetProfileSigninRequiredAtIndex(profile_index, true);
EXPECT_TRUE(cache.ProfileIsSigninRequiredAtIndex(profile_index));
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, active_browser_list_->size());
EXPECT_TRUE(UserManagerMac::IsShowing());
UserManagerMac::Hide();
}
// Test that for a guest last profile, a reopen event opens the User Manager.
IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
GuestProfileReopenWithNoWindows) {
// Create the guest profile, and set it as the last used profile so the
// app controller can use it on init.
CreateAndWaitForGuestProfile();
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
base::scoped_nsobject<AppController> ac([[AppController alloc] init]);
Profile* profile = [ac lastProfile];
EXPECT_EQ(ProfileManager::GetGuestProfilePath(), profile->GetPath());
EXPECT_TRUE(profile->IsGuestSession());
EXPECT_EQ(1u, active_browser_list_->size());
BOOL result = [ac applicationShouldHandleReopen:NSApp hasVisibleWindows:NO];
EXPECT_FALSE(result);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, active_browser_list_->size());
EXPECT_TRUE(UserManagerMac::IsShowing());
UserManagerMac::Hide();
}
} // namespace
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