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_window.h"
19#include "chrome/browser/ui/startup/startup_browser_creator.h"
20#endif  // !defined (OS_IOS)
21
22using content::BrowserThread;
23using content::UserMetricsAction;
24
25namespace {
26
27void OpenBrowserWindowForProfile(bool always_create,
28                                 chrome::HostDesktopType desktop_type,
29                                 Profile* profile,
30                                 Profile::CreateStatus status) {
31  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32
33  if (status == Profile::CREATE_STATUS_INITIALIZED) {
34    profiles::FindOrCreateNewWindowForProfile(
35        profile,
36        chrome::startup::IS_NOT_PROCESS_STARTUP,
37        chrome::startup::IS_NOT_FIRST_RUN,
38        desktop_type,
39        always_create);
40  }
41}
42
43}  // namespace
44
45namespace profiles {
46
47void FindOrCreateNewWindowForProfile(
48    Profile* profile,
49    chrome::startup::IsProcessStartup process_startup,
50    chrome::startup::IsFirstRun is_first_run,
51    chrome::HostDesktopType desktop_type,
52    bool always_create) {
53#if defined(OS_IOS)
54  NOTREACHED();
55#else
56  DCHECK(profile);
57
58  if (!always_create) {
59    Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
60    if (browser) {
61      browser->window()->Activate();
62      return;
63    }
64  }
65
66  content::RecordAction(UserMetricsAction("NewWindow"));
67  CommandLine command_line(CommandLine::NO_PROGRAM);
68  int return_code;
69  StartupBrowserCreator browser_creator;
70  browser_creator.LaunchBrowser(command_line, profile, base::FilePath(),
71                                process_startup, is_first_run, &return_code);
72#endif  // defined(OS_IOS)
73}
74
75void SwitchToProfile(
76    const base::FilePath& path,
77    chrome::HostDesktopType desktop_type,
78    bool always_create) {
79  g_browser_process->profile_manager()->CreateProfileAsync(
80      path,
81      base::Bind(&OpenBrowserWindowForProfile,
82                 always_create,
83                 desktop_type),
84      string16(),
85      string16(),
86      std::string());
87}
88
89}  // namespace profiles
90