1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_window.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_dialogs.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/user_metrics.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/browser/ui/browser_list_observer.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/startup/startup_browser_creator.h"
23 #endif // !defined (OS_IOS)
25 using base::UserMetricsAction
;
26 using content::BrowserThread
;
30 // Handles running a callback when a new Browser has been completely created.
31 class BrowserAddedObserver
: public chrome::BrowserListObserver
{
33 explicit BrowserAddedObserver(
34 profiles::ProfileSwitchingDoneCallback callback
) : callback_(callback
) {
35 BrowserList::AddObserver(this);
37 virtual ~BrowserAddedObserver() {
38 BrowserList::RemoveObserver(this);
42 // Overridden from BrowserListObserver:
43 virtual void OnBrowserAdded(Browser
* browser
) OVERRIDE
{
44 DCHECK(!callback_
.is_null());
48 profiles::ProfileSwitchingDoneCallback callback_
;
50 DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver
);
53 void OpenBrowserWindowForProfile(
54 profiles::ProfileSwitchingDoneCallback callback
,
57 chrome::HostDesktopType desktop_type
,
59 Profile::CreateStatus status
) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
62 if (status
!= Profile::CREATE_STATUS_INITIALIZED
)
65 chrome::startup::IsProcessStartup is_process_startup
=
66 chrome::startup::IS_NOT_PROCESS_STARTUP
;
67 chrome::startup::IsFirstRun is_first_run
= chrome::startup::IS_NOT_FIRST_RUN
;
69 // If this is a brand new profile, then start a first run window.
71 is_process_startup
= chrome::startup::IS_PROCESS_STARTUP
;
72 is_first_run
= chrome::startup::IS_FIRST_RUN
;
75 // If we are not showing any browsers windows (we could be showing the
76 // desktop User Manager, for example), then this is a process startup browser.
77 if (BrowserList::GetInstance(desktop_type
)->size() == 0)
78 is_process_startup
= chrome::startup::IS_PROCESS_STARTUP
;
80 // If there is a callback, create an observer to make sure it is only
81 // run when the browser has been completely created.
82 scoped_ptr
<BrowserAddedObserver
> browser_added_observer
;
83 if (!callback
.is_null())
84 browser_added_observer
.reset(new BrowserAddedObserver(callback
));
86 profiles::FindOrCreateNewWindowForProfile(
98 void FindOrCreateNewWindowForProfile(
100 chrome::startup::IsProcessStartup process_startup
,
101 chrome::startup::IsFirstRun is_first_run
,
102 chrome::HostDesktopType desktop_type
,
103 bool always_create
) {
109 if (!always_create
) {
110 Browser
* browser
= chrome::FindTabbedBrowser(profile
, false, desktop_type
);
112 browser
->window()->Activate();
117 content::RecordAction(UserMetricsAction("NewWindow"));
118 CommandLine
command_line(CommandLine::NO_PROGRAM
);
120 StartupBrowserCreator browser_creator
;
121 browser_creator
.LaunchBrowser(command_line
, profile
, base::FilePath(),
122 process_startup
, is_first_run
, &return_code
);
123 #endif // defined(OS_IOS)
126 void SwitchToProfile(
127 const base::FilePath
& path
,
128 chrome::HostDesktopType desktop_type
,
130 ProfileSwitchingDoneCallback callback
) {
131 g_browser_process
->profile_manager()->CreateProfileAsync(
133 base::Bind(&OpenBrowserWindowForProfile
,
143 void SwitchToGuestProfile(chrome::HostDesktopType desktop_type
,
144 ProfileSwitchingDoneCallback callback
) {
145 g_browser_process
->profile_manager()->CreateProfileAsync(
146 ProfileManager::GetGuestProfilePath(),
147 base::Bind(&OpenBrowserWindowForProfile
,
157 void CreateAndSwitchToNewProfile(chrome::HostDesktopType desktop_type
,
158 ProfileSwitchingDoneCallback callback
) {
159 ProfileManager::CreateMultiProfileAsync(
162 base::Bind(&OpenBrowserWindowForProfile
,
170 void CloseGuestProfileWindows() {
171 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
172 Profile
* profile
= profile_manager
->GetProfileByPath(
173 ProfileManager::GetGuestProfilePath());
176 BrowserList::CloseAllBrowsersWithProfile(profile
);
180 void LockProfile(Profile
* profile
) {
182 ProfileInfoCache
& cache
=
183 g_browser_process
->profile_manager()->GetProfileInfoCache();
185 size_t index
= cache
.GetIndexOfProfileWithPath(profile
->GetPath());
186 cache
.SetProfileSigninRequiredAtIndex(index
, true);
187 chrome::ShowUserManager(profile
->GetPath());
188 BrowserList::CloseAllBrowsersWithProfile(profile
);
191 } // namespace profiles