Commit dbd091f2 authored by David Roger's avatar David Roger Committed by Commit Bot

[signin][Mac] Show the profile picker when reopening Chrome from the dock

The profile picker may be shown on new browsing sessions. This includes
Chrome being reopened from the dock.

In order to share code, two functions are moved out of
startup_browser_creator:
* IsGuestModeRequested
* ProfilePicker::ShouldShowAtLaunch
This is a simple code move and should not change the behavior. The only
behavior change is in app_controller_mac.mm

Tests for IsGuestModeRequested were added in this CL.

Fixed: 1122536
Change-Id: Ic078c79f300cde7ad5e26141eaebd3b249a66c4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485168Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarLeonard Grey <lgrey@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Commit-Queue: David Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825216}
parent e86672a2
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stddef.h> #include <stddef.h>
#include <memory> #include <memory>
#include <vector>
#include "base/allocator/allocator_shim.h" #include "base/allocator/allocator_shim.h"
#include "base/allocator/buildflags.h" #include "base/allocator/buildflags.h"
...@@ -78,6 +79,7 @@ ...@@ -78,6 +79,7 @@
#import "chrome/browser/ui/cocoa/share_menu_controller.h" #import "chrome/browser/ui/cocoa/share_menu_controller.h"
#import "chrome/browser/ui/cocoa/tab_menu_bridge.h" #import "chrome/browser/ui/cocoa/tab_menu_bridge.h"
#include "chrome/browser/ui/extensions/application_launch.h" #include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/profile_picker.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h" #include "chrome/browser/ui/startup/startup_browser_creator.h"
#include "chrome/browser/ui/startup/startup_browser_creator_impl.h" #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
...@@ -110,6 +112,7 @@ ...@@ -110,6 +112,7 @@
#include "ui/base/cocoa/focus_window_set.h" #include "ui/base/cocoa/focus_window_set.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#include "url/gurl.h"
using apps::AppShimManager; using apps::AppShimManager;
using base::UserMetricsAction; using base::UserMetricsAction;
...@@ -1257,8 +1260,19 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session; ...@@ -1257,8 +1260,19 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
} }
if (lastProfile->IsGuestSession() || IsProfileSignedOut(lastProfile) || if (lastProfile->IsGuestSession() || IsProfileSignedOut(lastProfile) ||
lastProfile->IsSystemProfile()) { lastProfile->IsSystemProfile()) {
// Note: If the profile picker feature is enabled, this opens the profile
// picker, unless forced signin is enabled (in which case the old user
// manager is still shown).
UserManager::Show(base::FilePath(), UserManager::Show(base::FilePath(),
profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
} else if (ProfilePicker::ShouldShowAtLaunch(
// Pass an empty command line, because this is not application
// startup. The original arguments (e.g. --incognito) are no
// longer relevant.
base::CommandLine(base::CommandLine::NO_PROGRAM),
/*urls_to_launch=*/std::vector<GURL>())) {
ProfilePicker::Show(
ProfilePicker::EntryPoint::kNewSessionOnExistingProcess);
} else { } else {
CreateBrowser(lastProfile); CreateBrowser(lastProfile);
} }
......
...@@ -16,8 +16,10 @@ ...@@ -16,8 +16,10 @@
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "chrome/app/chrome_command_ids.h" #include "chrome/app/chrome_command_ids.h"
#import "chrome/browser/app_controller_mac.h" #import "chrome/browser/app_controller_mac.h"
...@@ -29,6 +31,7 @@ ...@@ -29,6 +31,7 @@
#include "chrome/browser/profiles/profile_attributes_entry.h" #include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
...@@ -38,8 +41,10 @@ ...@@ -38,8 +41,10 @@
#include "chrome/browser/ui/cocoa/history_menu_bridge.h" #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
#include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
#include "chrome/browser/ui/cocoa/test/run_loop_testing.h" #include "chrome/browser/ui/cocoa/test/run_loop_testing.h"
#include "chrome/browser/ui/profile_picker.h"
#include "chrome/browser/ui/search/local_ntp_test_utils.h" #include "chrome/browser/ui/search/local_ntp_test_utils.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/user_manager.h" #include "chrome/browser/ui/user_manager.h"
#include "chrome/browser/ui/webui/welcome/helpers.h" #include "chrome/browser/ui/webui/welcome/helpers.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
...@@ -48,6 +53,7 @@ ...@@ -48,6 +53,7 @@
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "components/account_id/account_id.h"
#include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/test/bookmark_test_helpers.h" #include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -458,6 +464,126 @@ IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest, ...@@ -458,6 +464,126 @@ IN_PROC_BROWSER_TEST_F(AppControllerNewProfileManagementBrowserTest,
UserManager::Hide(); UserManager::Hide();
} }
class AppControllerProfilePickerBrowserTest
: public AppControllerNewProfileManagementBrowserTest {
public:
AppControllerProfilePickerBrowserTest() {
feature_list_.InitAndEnableFeature(features::kNewProfilePicker);
}
~AppControllerProfilePickerBrowserTest() override {
signin_util::ResetForceSigninForTesting();
}
private:
base::test::ScopedFeatureList feature_list_;
};
// Test that for a regular last profile, a reopen event opens a browser.
IN_PROC_BROWSER_TEST_F(AppControllerProfilePickerBrowserTest,
RegularProfileReopenWithNoWindows) {
AppController* ac = base::mac::ObjCCastStrict<AppController>(
[[NSApplication sharedApplication] delegate]);
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(UserManager::IsShowing());
EXPECT_FALSE(ProfilePicker::IsOpen());
}
// Test that for a locked last profile, a reopen event opens the User Manager,
// because the profile picker does not support locked profiles yet.
IN_PROC_BROWSER_TEST_F(AppControllerProfilePickerBrowserTest,
LockedProfileReopenWithNoWindows) {
signin_util::SetForceSigninForTesting(true);
// The User Manager uses the system 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.
CreateAndWaitForSystemProfile();
AppController* ac = base::mac::ObjCCastStrict<AppController>(
[[NSApplication sharedApplication] delegate]);
// Lock the active profile.
base::ScopedAllowBlockingForTesting allow_blocking;
Profile* profile = [ac lastProfile];
ProfileAttributesEntry* entry;
ASSERT_TRUE(g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetProfileAttributesWithPath(profile->GetPath(), &entry));
entry->SetIsSigninRequired(true);
EXPECT_TRUE(entry->IsSigninRequired());
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(UserManager::IsShowing());
UserManager::Hide();
}
// Test that for a guest last profile, a reopen event opens the ProfilePicker.
IN_PROC_BROWSER_TEST_F(AppControllerProfilePickerBrowserTest,
GuestProfileReopenWithNoWindows) {
// Create the system profile. Set the guest as the last used profile so the
// app controller can use it on init.
CreateAndWaitForSystemProfile();
PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kProfileLastUsed, chrome::kGuestProfileDir);
AppController* ac = base::mac::ObjCCastStrict<AppController>(
[[NSApplication sharedApplication] delegate]);
base::ScopedAllowBlockingForTesting allow_blocking;
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(ProfilePicker::IsOpen());
ProfilePicker::Hide();
}
// Test that the ProfilePicker is shown when there are multiple profiles.
IN_PROC_BROWSER_TEST_F(AppControllerProfilePickerBrowserTest,
MultiProfilePickerShown) {
CreateAndWaitForSystemProfile();
AppController* ac = base::mac::ObjCCastStrict<AppController>(
[[NSApplication sharedApplication] delegate]);
// Add a profile in the cache (simulate another profile on disk).
base::ScopedAllowBlockingForTesting allow_blocking;
ProfileManager* profile_manager = g_browser_process->profile_manager();
ProfileAttributesStorage* profile_storage =
&profile_manager->GetProfileAttributesStorage();
const base::FilePath profile_path =
profile_manager->GenerateNextProfileDirectoryPath();
profile_storage->AddProfile(
profile_path, base::ASCIIToUTF16("name_1"), "12345", base::string16(),
/*is_consented_primary_account=*/false, /*icon_index=*/0,
/*supervised_user_id*/ std::string(), EmptyAccountId());
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(ProfilePicker::IsOpen());
EXPECT_FALSE(UserManager::IsShowing());
ProfilePicker::Hide();
}
class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest { class AppControllerOpenShortcutBrowserTest : public InProcessBrowserTest {
protected: protected:
AppControllerOpenShortcutBrowserTest() { AppControllerOpenShortcutBrowserTest() {
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#include "chrome/browser/profiles/profiles_state.h" #include "chrome/browser/profiles/profiles_state.h"
#include "base/check.h" #include "base/check.h"
#include "base/command_line.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "build/chromeos_buildflags.h" #include "build/chromeos_buildflags.h"
...@@ -18,6 +20,7 @@ ...@@ -18,6 +20,7 @@
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/ui_features.h" #include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/browsing_data/content/browsing_data_helper.h" #include "components/browsing_data/content/browsing_data_helper.h"
...@@ -169,6 +172,28 @@ bool IsRegularOrGuestSession(Browser* browser) { ...@@ -169,6 +172,28 @@ bool IsRegularOrGuestSession(Browser* browser) {
profile->IsEphemeralGuestProfile(); profile->IsEphemeralGuestProfile();
} }
bool IsGuestModeRequested(const base::CommandLine& command_line,
PrefService* local_state,
bool show_warning) {
#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_WIN) || \
defined(OS_MAC)
DCHECK(local_state);
// Check if guest mode enforcement commandline switch or policy are provided.
if (command_line.HasSwitch(switches::kGuest) ||
local_state->GetBoolean(prefs::kBrowserGuestModeEnforced)) {
// Check if guest mode is allowed by policy.
if (local_state->GetBoolean(prefs::kBrowserGuestModeEnabled))
return true;
if (show_warning) {
LOG(WARNING) << "Guest mode disabled by policy, launching a normal "
<< "browser session.";
}
}
#endif
return false;
}
bool IsProfileLocked(const base::FilePath& profile_path) { bool IsProfileLocked(const base::FilePath& profile_path) {
ProfileAttributesEntry* entry; ProfileAttributesEntry* entry;
if (!g_browser_process->profile_manager()->GetProfileAttributesStorage(). if (!g_browser_process->profile_manager()->GetProfileAttributesStorage().
......
...@@ -19,9 +19,13 @@ ...@@ -19,9 +19,13 @@
struct AccountInfo; struct AccountInfo;
class Browser; class Browser;
class PrefRegistrySimple; class PrefRegistrySimple;
class PrefService;
class Profile; class Profile;
namespace base { class FilePath; } namespace base {
class CommandLine;
class FilePath;
} // namespace base
namespace profiles { namespace profiles {
...@@ -72,6 +76,13 @@ void UpdateProfileName(Profile* profile, ...@@ -72,6 +76,13 @@ void UpdateProfileName(Profile* profile,
// implemented as off-the-record profiles. // implemented as off-the-record profiles.
bool IsRegularOrGuestSession(Browser* browser); bool IsRegularOrGuestSession(Browser* browser);
// Returns true if starting in guest mode is requested at startup (e.g. through
// command line argument). If |show_warning| is true, send a warning if guest
// mode is requested but not allowed by policy.
bool IsGuestModeRequested(const base::CommandLine& command_line,
PrefService* local_state,
bool show_warning);
// Returns true if sign in is required to browse as this profile. Call with // Returns true if sign in is required to browse as this profile. Call with
// profile->GetPath() if you have a profile pointer. // profile->GetPath() if you have a profile pointer.
// TODO(mlerman): Refactor appropriate calls to // TODO(mlerman): Refactor appropriate calls to
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profiles_state.h"
#include "base/command_line.h"
#include "build/build_config.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
#if !defined(OS_ANDROID)
// Params for the parameterized test IsGuestModeRequestedTest.
struct IsGuestModeRequestedTestParams {
bool has_switch;
bool pref_enforced;
bool pref_enabled;
bool expected_guest_mode_requested;
};
// clang-format off
const IsGuestModeRequestedTestParams kIsGuestModeRequestedParams[] {
// has_switch | pref_enforced | pref_enabled | expected_guest_mode_requested
{ true, true, true, true},
{ true, true, false, false},
{ true, false, true, true},
{ true, false, false, false},
{ false, true, true, true},
{ false, true, false, false},
{ false, false, true, false},
{ false, false, false, false},
};
// clang-format on
#endif // !defined(OS_ANDROID)
} // namespace
#if !defined(OS_ANDROID)
class IsGuestModeRequestedTest
: public testing::TestWithParam<IsGuestModeRequestedTestParams> {};
TEST_P(IsGuestModeRequestedTest, Requested) {
TestingPrefServiceSimple local_state;
local_state.registry()->RegisterBooleanPref(prefs::kBrowserGuestModeEnforced,
false);
local_state.registry()->RegisterBooleanPref(prefs::kBrowserGuestModeEnabled,
false);
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
// Set parameters.
IsGuestModeRequestedTestParams params = GetParam();
local_state.SetBoolean(prefs::kBrowserGuestModeEnforced,
params.pref_enforced);
local_state.SetBoolean(prefs::kBrowserGuestModeEnabled, params.pref_enabled);
if (params.has_switch)
command_line.AppendSwitch("guest");
// Check expectation.
EXPECT_EQ(params.expected_guest_mode_requested,
profiles::IsGuestModeRequested(command_line, &local_state,
/*show_warning=*/false));
}
INSTANTIATE_TEST_SUITE_P(ProfilesState,
IsGuestModeRequestedTest,
testing::ValuesIn(kIsGuestModeRequestedParams));
#endif // !defined(OS_ANDROID)
...@@ -2810,6 +2810,7 @@ static_library("ui") { ...@@ -2810,6 +2810,7 @@ static_library("ui") {
sources += [ sources += [
"bookmarks/bookmark_bubble_sign_in_delegate.cc", "bookmarks/bookmark_bubble_sign_in_delegate.cc",
"bookmarks/bookmark_bubble_sign_in_delegate.h", "bookmarks/bookmark_bubble_sign_in_delegate.h",
"profile_picker.cc",
"profile_picker.h", "profile_picker.h",
"signin_reauth_view_controller.cc", "signin_reauth_view_controller.cc",
"signin_reauth_view_controller.h", "signin_reauth_view_controller.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/profile_picker.h"
#include <string>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#if defined(OS_WIN)
#include "chrome/browser/notifications/win/notification_launch_id.h"
#endif
// static
bool ProfilePicker::ShouldShowAtLaunch(
const base::CommandLine& command_line,
const std::vector<GURL>& urls_to_launch) {
// Don't show the picker if a certain profile (or an incognito window in the
// default profile) is explicitly requested.
if (profiles::IsGuestModeRequested(command_line,
g_browser_process->local_state(),
/*show_warning=*/false) ||
command_line.HasSwitch(switches::kIncognito) ||
command_line.HasSwitch(switches::kProfileDirectory)) {
return false;
}
// Don't show the picker if an app is explicitly requested to open. This URL
// param should be ideally paired with switches::kProfileDirectory but it's
// better to err on the side of opening the last profile than to err on the
// side of not opening the app directly.
if (command_line.HasSwitch(switches::kApp) ||
command_line.HasSwitch(switches::kAppId)) {
return false;
}
// If the browser is launched due to activation on Windows native notification,
// the profile id encoded in the notification launch id should be chosen over
// the profile picker.
#if defined(OS_WIN)
std::string profile_id =
NotificationLaunchId::GetNotificationLaunchProfileId(command_line);
if (!profile_id.empty()) {
return false;
}
#endif // defined(OS_WIN)
// Don't show the picker if a any URL is requested to launch via the
// command-line.
if (!urls_to_launch.empty()) {
return false;
}
size_t number_of_profiles = g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetNumberOfProfiles();
if (signin_util::IsForceSigninEnabled() || number_of_profiles == 1)
return false;
bool pref_enabled = g_browser_process->local_state()->GetBoolean(
prefs::kBrowserShowProfilePickerOnStartup);
base::UmaHistogramBoolean("ProfilePicker.AskOnStartup", pref_enabled);
return pref_enabled &&
base::FeatureList::IsEnabled(features::kNewProfilePicker);
}
...@@ -5,8 +5,16 @@ ...@@ -5,8 +5,16 @@
#ifndef CHROME_BROWSER_UI_PROFILE_PICKER_H_ #ifndef CHROME_BROWSER_UI_PROFILE_PICKER_H_
#define CHROME_BROWSER_UI_PROFILE_PICKER_H_ #define CHROME_BROWSER_UI_PROFILE_PICKER_H_
#include <vector>
#include "base/callback_forward.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "url/gurl.h"
namespace base {
class CommandLine;
}
namespace views { namespace views {
class View; class View;
...@@ -60,6 +68,15 @@ class ProfilePicker { ...@@ -60,6 +68,15 @@ class ProfilePicker {
// Overrides the timeout delay for waiting for extended account info. // Overrides the timeout delay for waiting for extended account info.
static void SetExtendedAccountInfoTimeoutForTesting(base::TimeDelta timeout); static void SetExtendedAccountInfoTimeoutForTesting(base::TimeDelta timeout);
// Returns whether the profile picker at launch. This can be called on
// startup or when Chrome is re-opened, e.g. when clicking on the dock icon on
// MacOS when there are no windows, or from Windows tray icon.
// This returns true if this is a new session. Returns false if a specific
// profile is passed in the command line, or if some parameters (such as the
// URLs to launch) cannot be handled by the picker.
static bool ShouldShowAtLaunch(const base::CommandLine& command_line,
const std::vector<GURL>& urls_to_launch);
private: private:
DISALLOW_COPY_AND_ASSIGN(ProfilePicker); DISALLOW_COPY_AND_ASSIGN(ProfilePicker);
}; };
......
...@@ -14,13 +14,13 @@ ...@@ -14,13 +14,13 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/metrics/histogram_base.h" #include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/metrics/statistics_recorder.h" #include "base/metrics/statistics_recorder.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#include "chrome/browser/profiles/profile_observer.h" #include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/profiles/profiles_state.h" #include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
...@@ -273,82 +272,6 @@ bool CanOpenProfileOnStartup(Profile* profile) { ...@@ -273,82 +272,6 @@ bool CanOpenProfileOnStartup(Profile* profile) {
#endif #endif
} }
// Returns true if starting in guest mode is enforced. If |show_warning| is
// true, send a warning if guest mode is requested but not allowed by policy.
bool IsGuestModeEnforced(const base::CommandLine& command_line,
bool show_warning) {
#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_WIN) || \
defined(OS_MAC)
PrefService* service = g_browser_process->local_state();
DCHECK(service);
// Check if guest mode enforcement commandline switch or policy are provided.
if (command_line.HasSwitch(switches::kGuest) ||
service->GetBoolean(prefs::kBrowserGuestModeEnforced)) {
// Check if guest mode is allowed by policy.
if (service->GetBoolean(prefs::kBrowserGuestModeEnabled))
return true;
if (show_warning) {
LOG(WARNING) << "Guest mode disabled by policy, launching a normal "
<< "browser session.";
}
}
#endif
return false;
}
#if !defined(OS_CHROMEOS)
bool ShouldShowProfilePicker(const base::CommandLine& command_line,
const std::vector<GURL>& urls_to_launch) {
// Don't show the picker if a certain profile (or an incognito window in the
// default profile) is explicitly requested.
if (IsGuestModeEnforced(command_line, /*show_warning=*/false) ||
command_line.HasSwitch(switches::kIncognito) ||
command_line.HasSwitch(switches::kProfileDirectory)) {
return false;
}
// Don't show the picker if an app is explicitly requested to open. This URL
// param should be ideally paired with switches::kProfileDirectory but it's
// better to err on the side of opening the last profile than to err on the
// side of not opening the app directly.
if (command_line.HasSwitch(switches::kApp) ||
command_line.HasSwitch(switches::kAppId)) {
return false;
}
// If the browser is launched due to activation on Windows native notification,
// the profile id encoded in the notification launch id should be chosen over
// the profile picker.
#if defined(OS_WIN)
std::string profile_id =
NotificationLaunchId::GetNotificationLaunchProfileId(command_line);
if (!profile_id.empty()) {
return false;
}
#endif // defined(OS_WIN)
// Don't show the picker if a any URL is requested to launch via the
// command-line.
if (!urls_to_launch.empty()) {
return false;
}
size_t number_of_profiles = g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetNumberOfProfiles();
if (signin_util::IsForceSigninEnabled() || number_of_profiles == 1) {
return false;
}
bool pref_enabled = g_browser_process->local_state()->GetBoolean(
prefs::kBrowserShowProfilePickerOnStartup);
base::UmaHistogramBoolean("ProfilePicker.AskOnStartup", pref_enabled);
return pref_enabled &&
base::FeatureList::IsEnabled(features::kNewProfilePicker);
}
#endif // !defined(OS_CHROMEOS)
void ShowUserManagerOnStartup() { void ShowUserManagerOnStartup() {
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
UserManager::Show(base::FilePath(), UserManager::Show(base::FilePath(),
...@@ -420,8 +343,8 @@ bool StartupBrowserCreator::LaunchBrowser( ...@@ -420,8 +343,8 @@ bool StartupBrowserCreator::LaunchBrowser(
in_synchronous_profile_launch_ = in_synchronous_profile_launch_ =
process_startup == chrome::startup::IS_PROCESS_STARTUP; process_startup == chrome::startup::IS_PROCESS_STARTUP;
// Continue with the incognito profile from here on if Incognito mode // Continue with the incognito profile from here on if Incognito mode is
// is forced. // forced.
if (IncognitoModePrefs::ShouldLaunchIncognito(command_line, if (IncognitoModePrefs::ShouldLaunchIncognito(command_line,
profile->GetPrefs())) { profile->GetPrefs())) {
profile = profile->GetPrimaryOTRProfile(); profile = profile->GetPrimaryOTRProfile();
...@@ -430,7 +353,9 @@ bool StartupBrowserCreator::LaunchBrowser( ...@@ -430,7 +353,9 @@ bool StartupBrowserCreator::LaunchBrowser(
<< "browser session."; << "browser session.";
} }
if (IsGuestModeEnforced(command_line, /* show_warning= */ true)) { if (profiles::IsGuestModeRequested(command_line,
g_browser_process->local_state(),
/* show_warning= */ true)) {
profile = g_browser_process->profile_manager()->GetProfile( profile = g_browser_process->profile_manager()->GetProfile(
ProfileManager::GetGuestProfilePath()); ProfileManager::GetGuestProfilePath());
if (!profile->IsEphemeralGuestProfile()) if (!profile->IsEphemeralGuestProfile())
...@@ -897,7 +822,7 @@ bool StartupBrowserCreator::LaunchBrowserForLastProfiles( ...@@ -897,7 +822,7 @@ bool StartupBrowserCreator::LaunchBrowserForLastProfiles(
const std::vector<GURL> urls_to_launch = const std::vector<GURL> urls_to_launch =
StartupBrowserCreator::GetURLsFromCommandLine(command_line, cur_dir, StartupBrowserCreator::GetURLsFromCommandLine(command_line, cur_dir,
last_used_profile); last_used_profile);
if (ShouldShowProfilePicker(command_line, urls_to_launch)) { if (ProfilePicker::ShouldShowAtLaunch(command_line, urls_to_launch)) {
ProfilePicker::Show( ProfilePicker::Show(
process_startup process_startup
? ProfilePicker::EntryPoint::kOnStartup ? ProfilePicker::EntryPoint::kOnStartup
...@@ -1138,10 +1063,13 @@ base::FilePath GetStartupProfilePath(const base::FilePath& user_data_dir, ...@@ -1138,10 +1063,13 @@ base::FilePath GetStartupProfilePath(const base::FilePath& user_data_dir,
} }
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
// If opening in Guest mode is requested, load the default profile so // If opening in Guest mode is requested, load the default profile so that
// that last opened profile would not trigger a user management dialog. // last opened profile would not trigger a user management dialog.
if (IsGuestModeEnforced(command_line, /* show_warning= */ false)) if (profiles::IsGuestModeRequested(command_line,
g_browser_process->local_state(),
/* show_warning= */ false)) {
return profiles::GetDefaultProfileDir(user_data_dir); return profiles::GetDefaultProfileDir(user_data_dir);
}
if (command_line.HasSwitch(switches::kProfileDirectory)) { if (command_line.HasSwitch(switches::kProfileDirectory)) {
return user_data_dir.Append( return user_data_dir.Append(
......
...@@ -3645,6 +3645,7 @@ test("unit_tests") { ...@@ -3645,6 +3645,7 @@ test("unit_tests") {
"../browser/profiles/profile_info_cache_unittest.cc", "../browser/profiles/profile_info_cache_unittest.cc",
"../browser/profiles/profile_info_cache_unittest.h", "../browser/profiles/profile_info_cache_unittest.h",
"../browser/profiles/profile_manager_unittest.cc", "../browser/profiles/profile_manager_unittest.cc",
"../browser/profiles/profiles_state_unittest.cc",
"../browser/profiling_host/background_profiling_triggers_unittest.cc", "../browser/profiling_host/background_profiling_triggers_unittest.cc",
"../browser/profiling_host/chrome_client_connection_manager_unittest.cc", "../browser/profiling_host/chrome_client_connection_manager_unittest.cc",
"../browser/push_messaging/budget_database_unittest.cc", "../browser/push_messaging/budget_database_unittest.cc",
......
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