- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_window.cc
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.
4
5 #include "chrome/browser/profiles/profile_window.h"
6
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"
15
16 #if !defined(OS_IOS)
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)
23
24 using content::BrowserThread;
25 using content::UserMetricsAction;
26
27 namespace {
28
29 // Handles running a callback when a new Browser has been completely created.
30 class BrowserAddedObserver : public chrome::BrowserListObserver {
31  public:
32   explicit BrowserAddedObserver(
33       profiles::ProfileSwitchingDoneCallback callback) : callback_(callback) {
34     BrowserList::AddObserver(this);
35   }
36   virtual ~BrowserAddedObserver() {
37     BrowserList::RemoveObserver(this);
38   }
39
40  private:
41   // Overridden from BrowserListObserver:
42   virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
43     DCHECK(!callback_.is_null());
44     callback_.Run();
45   }
46
47   profiles::ProfileSwitchingDoneCallback callback_;
48
49   DISALLOW_COPY_AND_ASSIGN(BrowserAddedObserver);
50 };
51
52 void OpenBrowserWindowForProfile(
53     profiles::ProfileSwitchingDoneCallback callback,
54     bool always_create,
55     bool is_new_profile,
56     chrome::HostDesktopType desktop_type,
57     Profile* profile,
58     Profile::CreateStatus status) {
59   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
60
61   if (status != Profile::CREATE_STATUS_INITIALIZED)
62     return;
63
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;
67
68   // If this is a brand new profile, then start a first run window.
69   if (is_new_profile) {
70     is_process_startup = chrome::startup::IS_PROCESS_STARTUP;
71     is_first_run = chrome::startup::IS_FIRST_RUN;
72   }
73
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;
78
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));
84
85   profiles::FindOrCreateNewWindowForProfile(
86       profile,
87       is_process_startup,
88       is_first_run,
89       desktop_type,
90       always_create);
91 }
92
93 }  // namespace
94
95 namespace profiles {
96
97 void FindOrCreateNewWindowForProfile(
98     Profile* profile,
99     chrome::startup::IsProcessStartup process_startup,
100     chrome::startup::IsFirstRun is_first_run,
101     chrome::HostDesktopType desktop_type,
102     bool always_create) {
103 #if defined(OS_IOS)
104   NOTREACHED();
105 #else
106   DCHECK(profile);
107
108   if (!always_create) {
109     Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
110     if (browser) {
111       browser->window()->Activate();
112       return;
113     }
114   }
115
116   content::RecordAction(UserMetricsAction("NewWindow"));
117   CommandLine command_line(CommandLine::NO_PROGRAM);
118   int return_code;
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)
123 }
124
125 void SwitchToProfile(
126     const base::FilePath& path,
127     chrome::HostDesktopType desktop_type,
128     bool always_create,
129     ProfileSwitchingDoneCallback callback) {
130   g_browser_process->profile_manager()->CreateProfileAsync(
131       path,
132       base::Bind(&OpenBrowserWindowForProfile,
133                  callback,
134                  always_create,
135                  false,
136                  desktop_type),
137       string16(),
138       string16(),
139       std::string());
140 }
141
142 void SwitchToGuestProfile(chrome::HostDesktopType desktop_type,
143                           ProfileSwitchingDoneCallback callback) {
144   g_browser_process->profile_manager()->CreateProfileAsync(
145       ProfileManager::GetGuestProfilePath(),
146       base::Bind(&OpenBrowserWindowForProfile,
147                  callback,
148                  false,
149                  false,
150                  desktop_type),
151       string16(),
152       string16(),
153       std::string());
154 }
155
156 void CreateAndSwitchToNewProfile(chrome::HostDesktopType desktop_type,
157                                  ProfileSwitchingDoneCallback callback) {
158   ProfileManager::CreateMultiProfileAsync(
159       string16(),
160       string16(),
161       base::Bind(&OpenBrowserWindowForProfile,
162                  callback,
163                  true,
164                  true,
165                  desktop_type),
166       std::string());
167 }
168
169 void CloseGuestProfileWindows() {
170   ProfileManager* profile_manager = g_browser_process->profile_manager();
171   Profile* profile = profile_manager->GetProfileByPath(
172       ProfileManager::GetGuestProfilePath());
173
174   if (profile) {
175     BrowserList::CloseAllBrowsersWithProfile(profile);
176   }
177 }
178
179 }  // namespace profiles