user_manager_view.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright 2014 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/ui/views/profiles/user_manager_view.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/lifetime/application_lifetime.h"
9#include "chrome/browser/profiles/profile_manager.h"
10#include "chrome/browser/profiles/profile_metrics.h"
11#include "chrome/browser/profiles/profile_window.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_dialogs.h"
14#include "chrome/browser/ui/browser_window.h"
15#include "chrome/browser/ui/views/auto_keep_alive.h"
16#include "content/public/browser/web_contents.h"
17#include "grit/generated_resources.h"
18#include "ui/base/l10n/l10n_util.h"
19#include "ui/views/controls/webview/webview.h"
20#include "ui/views/layout/fill_layout.h"
21#include "ui/views/view.h"
22#include "ui/views/widget/widget.h"
23
24#if defined(OS_WIN)
25#include "chrome/browser/shell_integration.h"
26#include "ui/base/win/shell.h"
27#include "ui/views/win/hwnd_util.h"
28#endif
29
30namespace {
31
32// Default window size.
33const int kWindowWidth = 900;
34const int kWindowHeight = 700;
35
36}
37
38namespace chrome {
39
40// Declared in browser_dialogs.h so others don't have to depend on this header.
41void ShowUserManager(const base::FilePath& profile_path_to_focus) {
42  UserManagerView::Show(
43      profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
44}
45
46void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
47  UserManagerView::Show(base::FilePath(), tutorial);
48}
49
50void HideUserManager() {
51  UserManagerView::Hide();
52}
53
54}  // namespace chrome
55
56// static
57UserManagerView* UserManagerView::instance_ = NULL;
58
59UserManagerView::UserManagerView()
60    : web_view_(NULL),
61      keep_alive_(new AutoKeepAlive(NULL)) {
62}
63
64UserManagerView::~UserManagerView() {
65}
66
67// static
68void UserManagerView::Show(const base::FilePath& profile_path_to_focus,
69                           profiles::UserManagerTutorialMode tutorial_mode) {
70  ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::OPEN_USER_MANAGER);
71  if (instance_) {
72    // If there's a user manager window open already, just activate it.
73    instance_->GetWidget()->Activate();
74    return;
75  }
76
77  // Create the guest profile, if necessary, and open the user manager
78  // from the guest profile.
79  profiles::CreateGuestProfileForUserManager(
80      profile_path_to_focus,
81      tutorial_mode,
82      base::Bind(&UserManagerView::OnGuestProfileCreated,
83                 base::Passed(make_scoped_ptr(new UserManagerView))));
84}
85
86// static
87void UserManagerView::Hide() {
88  if (instance_)
89    instance_->GetWidget()->Close();
90}
91
92// static
93bool UserManagerView::IsShowing() {
94  return instance_ ? instance_->GetWidget()->IsActive() : false;
95}
96
97// static
98void UserManagerView::OnGuestProfileCreated(
99    scoped_ptr<UserManagerView> instance,
100    Profile* guest_profile,
101    const std::string& url) {
102  instance_ = instance.release();  // |instance_| takes over ownership.
103  instance_->Init(guest_profile, GURL(url));
104}
105
106void UserManagerView::Init(Profile* guest_profile, const GURL& url) {
107  web_view_ = new views::WebView(guest_profile);
108  SetLayoutManager(new views::FillLayout);
109  AddChildView(web_view_);
110
111  DialogDelegate::CreateDialogWidget(this, NULL, NULL);
112
113#if defined(OS_WIN)
114  // Set the app id for the task manager to the app id of its parent
115  ui::win::SetAppIdForWindow(
116      ShellIntegration::GetChromiumModelIdForProfile(
117          guest_profile->GetPath()),
118      views::HWNDForWidget(GetWidget()));
119#endif
120  GetWidget()->Show();
121
122  web_view_->LoadInitialURL(url);
123  web_view_->RequestFocus();
124}
125
126gfx::Size UserManagerView::GetPreferredSize() const {
127  return gfx::Size(kWindowWidth, kWindowHeight);
128}
129
130bool UserManagerView::CanResize() const {
131  return true;
132}
133
134bool UserManagerView::CanMaximize() const {
135  return true;
136}
137
138base::string16 UserManagerView::GetWindowTitle() const {
139  return l10n_util::GetStringUTF16(IDS_USER_MANAGER_SCREEN_TITLE);
140}
141
142int UserManagerView::GetDialogButtons() const {
143  return ui::DIALOG_BUTTON_NONE;
144}
145
146void UserManagerView::WindowClosing() {
147  // Now that the window is closed, we can allow a new one to be opened.
148  // (WindowClosing comes in asynchronously from the call to Close() and we
149  // may have already opened a new instance).
150  if (instance_ == this)
151    instance_ = NULL;
152}
153
154bool UserManagerView::UseNewStyleForThisDialog() const {
155  return false;
156}
157