user_manager_view.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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 "chrome/grit/chromium_strings.h"
17#include "content/public/browser/web_contents.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#include "ui/views/window/dialog_client_view.h"
24
25#if defined(OS_WIN)
26#include "chrome/browser/shell_integration.h"
27#include "ui/base/win/shell.h"
28#include "ui/views/win/hwnd_util.h"
29#endif
30
31namespace {
32
33// Default window size.
34const int kWindowWidth = 900;
35const int kWindowHeight = 700;
36
37}
38
39namespace chrome {
40
41// Declared in browser_dialogs.h so others don't have to depend on this header.
42void ShowUserManager(const base::FilePath& profile_path_to_focus) {
43  UserManagerView::Show(
44      profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
45}
46
47void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
48  UserManagerView::Show(base::FilePath(), tutorial);
49}
50
51void HideUserManager() {
52  UserManagerView::Hide();
53}
54
55}  // namespace chrome
56
57// static
58UserManagerView* UserManagerView::instance_ = NULL;
59
60UserManagerView::UserManagerView()
61    : web_view_(NULL),
62      keep_alive_(new AutoKeepAlive(NULL)) {
63}
64
65UserManagerView::~UserManagerView() {
66}
67
68// static
69void UserManagerView::Show(const base::FilePath& profile_path_to_focus,
70                           profiles::UserManagerTutorialMode tutorial_mode) {
71  ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::OPEN_USER_MANAGER);
72  if (instance_) {
73    // If there's a user manager window open already, just activate it.
74    instance_->GetWidget()->Activate();
75    return;
76  }
77
78  // Create the guest profile, if necessary, and open the user manager
79  // from the guest profile.
80  profiles::CreateGuestProfileForUserManager(
81      profile_path_to_focus,
82      tutorial_mode,
83      base::Bind(&UserManagerView::OnGuestProfileCreated,
84                 base::Passed(make_scoped_ptr(new UserManagerView))));
85}
86
87// static
88void UserManagerView::Hide() {
89  if (instance_)
90    instance_->GetWidget()->Close();
91}
92
93// static
94bool UserManagerView::IsShowing() {
95  return instance_ ? instance_->GetWidget()->IsActive() : false;
96}
97
98// static
99void UserManagerView::OnGuestProfileCreated(
100    scoped_ptr<UserManagerView> instance,
101    Profile* guest_profile,
102    const std::string& url) {
103  instance_ = instance.release();  // |instance_| takes over ownership.
104  instance_->Init(guest_profile, GURL(url));
105}
106
107void UserManagerView::Init(Profile* guest_profile, const GURL& url) {
108  web_view_ = new views::WebView(guest_profile);
109  web_view_->set_allow_accelerators(true);
110  AddChildView(web_view_);
111  SetLayoutManager(new views::FillLayout);
112  AddAccelerator(ui::Accelerator(ui::VKEY_W, ui::EF_CONTROL_DOWN));
113
114  DialogDelegate::CreateDialogWidget(this, NULL, NULL);
115  // Since the User Manager can be the only top level window, we don't
116  // want to accidentally quit all of Chrome if the user is just trying to
117  // unfocus the selected pod in the WebView.
118  GetDialogClientView()->RemoveAccelerator(
119      ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
120
121#if defined(OS_WIN)
122  // Set the app id for the task manager to the app id of its parent
123  ui::win::SetAppIdForWindow(
124      ShellIntegration::GetChromiumModelIdForProfile(
125          guest_profile->GetPath()),
126      views::HWNDForWidget(GetWidget()));
127#endif
128  GetWidget()->Show();
129
130  web_view_->LoadInitialURL(url);
131  web_view_->RequestFocus();
132}
133
134bool UserManagerView::AcceleratorPressed(const ui::Accelerator& accelerator) {
135  DCHECK_EQ(ui::VKEY_W, accelerator.key_code());
136  DCHECK_EQ(ui::EF_CONTROL_DOWN, accelerator.modifiers());
137  GetWidget()->Close();
138  return true;
139}
140
141gfx::Size UserManagerView::GetPreferredSize() const {
142  return gfx::Size(kWindowWidth, kWindowHeight);
143}
144
145bool UserManagerView::CanResize() const {
146  return true;
147}
148
149bool UserManagerView::CanMaximize() const {
150  return true;
151}
152
153base::string16 UserManagerView::GetWindowTitle() const {
154  return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
155}
156
157int UserManagerView::GetDialogButtons() const {
158  return ui::DIALOG_BUTTON_NONE;
159}
160
161void UserManagerView::WindowClosing() {
162  // Now that the window is closed, we can allow a new one to be opened.
163  // (WindowClosing comes in asynchronously from the call to Close() and we
164  // may have already opened a new instance).
165  if (instance_ == this)
166    instance_ = NULL;
167}
168
169bool UserManagerView::UseNewStyleForThisDialog() const {
170  return false;
171}
172