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/ui/views/user_manager_view.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/lifetime/application_lifetime.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "chrome/browser/profiles/profile_metrics.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/common/url_constants.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_view.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/views/controls/webview/webview.h"
21 #include "ui/views/layout/fill_layout.h"
22 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h"
26 #include "ash/wm/window_util.h"
30 #include "chrome/browser/shell_integration.h"
31 #include "ui/base/win/shell.h"
32 #include "ui/views/win/hwnd_util.h"
33 #include "win8/util/win8_util.h"
38 // Default window size.
39 const int kWindowWidth
= 900;
40 const int kWindowHeight
= 700;
46 // Declared in browser_dialogs.h so others don't have to depend on this header.
47 void ShowUserManager(const base::FilePath
& profile_path_to_focus
) {
48 UserManagerView::Show(profile_path_to_focus
);
51 void HideUserManager() {
52 UserManagerView::Hide();
58 UserManagerView
* UserManagerView::instance_
= NULL
;
60 UserManagerView::UserManagerView(Profile
* profile
)
61 : web_view_(new views::WebView(profile
)) {
62 SetLayoutManager(new views::FillLayout
);
63 AddChildView(web_view_
);
66 UserManagerView::~UserManagerView() {
67 chrome::EndKeepAlive(); // Remove shutdown prevention.
71 void UserManagerView::Show(const base::FilePath
& profile_path_to_focus
) {
72 // Prevent the browser process from shutting down while this window is open.
73 chrome::StartKeepAlive();
75 ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::OPEN_USER_MANAGER
);
77 // If there's a user manager window open already, just activate it.
78 instance_
->GetWidget()->Activate();
82 // Create the guest profile, if necessary, and open the user manager
83 // from the guest profile.
84 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
85 profile_manager
->CreateProfileAsync(
86 ProfileManager::GetGuestProfilePath(),
87 base::Bind(&UserManagerView::OnGuestProfileCreated
,
88 profile_path_to_focus
),
95 void UserManagerView::Hide() {
97 instance_
->GetWidget()->Close();
101 bool UserManagerView::IsShowing() {
102 return instance_
? instance_
->GetWidget()->IsActive() : false;
105 void UserManagerView::OnGuestProfileCreated(
106 const base::FilePath
& profile_path_to_focus
,
107 Profile
* guest_profile
,
108 Profile::CreateStatus status
) {
109 if (status
!= Profile::CREATE_STATUS_INITIALIZED
)
112 instance_
= new UserManagerView(guest_profile
);
113 DialogDelegate::CreateDialogWidget(instance_
, NULL
, NULL
);
116 // Set the app id for the task manager to the app id of its parent
117 ui::win::SetAppIdForWindow(
118 ShellIntegration::GetChromiumModelIdForProfile(
119 guest_profile
->GetPath()),
120 views::HWNDForWidget(instance_
->GetWidget()));
122 instance_
->GetWidget()->Show();
124 // Tell the webui which user pod should be focused.
125 std::string page
= chrome::kChromeUIUserManagerURL
;
127 if (!profile_path_to_focus
.empty()) {
128 ProfileInfoCache
& cache
=
129 g_browser_process
->profile_manager()->GetProfileInfoCache();
130 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path_to_focus
);
131 if (index
!= std::string::npos
) {
133 page
+= base::IntToString(index
);
137 instance_
->web_view_
->LoadInitialURL(GURL(page
));
138 instance_
->web_view_
->RequestFocus();
141 gfx::Size
UserManagerView::GetPreferredSize() {
142 return gfx::Size(kWindowWidth
, kWindowHeight
);
145 bool UserManagerView::CanResize() const {
149 bool UserManagerView::CanMaximize() const {
153 base::string16
UserManagerView::GetWindowTitle() const {
154 return l10n_util::GetStringUTF16(IDS_USER_MANAGER_SCREEN_TITLE
);
157 int UserManagerView::GetDialogButtons() const {
158 return ui::DIALOG_BUTTON_NONE
;
161 void 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)
169 bool UserManagerView::UseNewStyleForThisDialog() const {