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 "content/public/browser/browser_thread.h"
14 #include "content/public/browser/user_metrics.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/browser_list_observer.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/startup/startup_browser_creator.h"
22 #endif // !defined (OS_IOS)
24 using content::BrowserThread;
25 using content::UserMetricsAction;
29 // Handles running a callback when a new Browser has been completely created.
30 class BrowserAddedObserver : public chrome::BrowserListObserver {
32 explicit BrowserAddedObserver(
33 profiles::ProfileSwitchingDoneCallback callback) : callback_(callback) {
34 BrowserList::AddObserver(this);
36 virtual ~BrowserAddedObserver() {
37 BrowserList::RemoveObserver(this);
41 // Overridden from BrowserListObserver:
42 virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
43 DCHECK(!callback_.is_null());
47 profiles::ProfileSwitchingDoneCallback callback_;
49 DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver);
52 void OpenBrowserWindowForProfile(
53 profiles::ProfileSwitchingDoneCallback callback,
56 chrome::HostDesktopType desktop_type,
58 Profile::CreateStatus status) {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
61 if (status != Profile::CREATE_STATUS_INITIALIZED)
64 chrome::startup::IsProcessStartup is_process_startup =
65 chrome::startup::IS_NOT_PROCESS_STARTUP;
66 chrome::startup::IsFirstRun is_first_run = chrome::startup::IS_NOT_FIRST_RUN;
68 // If this is a brand new profile, then start a first run window.
70 is_process_startup = chrome::startup::IS_PROCESS_STARTUP;
71 is_first_run = chrome::startup::IS_FIRST_RUN;
74 // If we are not showing any browsers windows (we could be showing the
75 // desktop User Manager, for example), then this is a process startup browser.
76 if (BrowserList::GetInstance(desktop_type)->size() == 0)
77 is_process_startup = chrome::startup::IS_PROCESS_STARTUP;
79 // If there is a callback, create an observer to make sure it is only
80 // run when the browser has been completely created.
81 scoped_ptr<BrowserAddedObserver> browser_added_observer;
82 if (!callback.is_null())
83 browser_added_observer.reset(new BrowserAddedObserver(callback));
85 profiles::FindOrCreateNewWindowForProfile(
97 void FindOrCreateNewWindowForProfile(
99 chrome::startup::IsProcessStartup process_startup,
100 chrome::startup::IsFirstRun is_first_run,
101 chrome::HostDesktopType desktop_type,
102 bool always_create) {
108 if (!always_create) {
109 Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
111 browser->window()->Activate();
116 content::RecordAction(UserMetricsAction("NewWindow"));
117 CommandLine command_line(CommandLine::NO_PROGRAM);
119 StartupBrowserCreator browser_creator;
120 browser_creator.LaunchBrowser(command_line, profile, base::FilePath(),
121 process_startup, is_first_run, &return_code);
122 #endif // defined(OS_IOS)
125 void SwitchToProfile(
126 const base::FilePath& path,
127 chrome::HostDesktopType desktop_type,
129 ProfileSwitchingDoneCallback callback) {
130 g_browser_process->profile_manager()->CreateProfileAsync(
132 base::Bind(&OpenBrowserWindowForProfile,
142 void SwitchToGuestProfile(chrome::HostDesktopType desktop_type,
143 ProfileSwitchingDoneCallback callback) {
144 g_browser_process->profile_manager()->CreateProfileAsync(
145 ProfileManager::GetGuestProfilePath(),
146 base::Bind(&OpenBrowserWindowForProfile,
156 void CreateAndSwitchToNewProfile(chrome::HostDesktopType desktop_type,
157 ProfileSwitchingDoneCallback callback) {
158 ProfileManager::CreateMultiProfileAsync(
161 base::Bind(&OpenBrowserWindowForProfile,
169 void CloseGuestProfileWindows() {
170 ProfileManager* profile_manager = g_browser_process->profile_manager();
171 Profile* profile = profile_manager->GetProfileByPath(
172 ProfileManager::GetGuestProfilePath());
175 BrowserList::CloseAllBrowsersWithProfile(profile);
179 } // namespace profiles