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/ui/browser.h"
12 #include "chrome/browser/ui/browser_dialogs.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_view.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"
25 #include "ash/wm/window_util.h"
29 #include "chrome/browser/shell_integration.h"
30 #include "ui/base/win/shell.h"
31 #include "ui/views/win/hwnd_util.h"
32 #include "win8/util/win8_util.h"
37 // Default window size.
38 const int kWindowWidth
= 900;
39 const int kWindowHeight
= 700;
45 // Declared in browser_dialogs.h so others don't have to depend on this header.
46 void ShowUserManager(const base::FilePath
& profile_path_to_focus
) {
47 UserManagerView::Show(profile_path_to_focus
);
50 void HideUserManager() {
51 UserManagerView::Hide();
57 UserManagerView
* UserManagerView::instance_
= NULL
;
59 UserManagerView::UserManagerView(Profile
* profile
)
60 : web_view_(new views::WebView(profile
)) {
61 SetLayoutManager(new views::FillLayout
);
62 AddChildView(web_view_
);
65 UserManagerView::~UserManagerView() {
66 chrome::EndKeepAlive(); // Remove shutdown prevention.
70 void UserManagerView::Show(const base::FilePath
& profile_path_to_focus
) {
71 // Prevent the browser process from shutting down while this window is open.
72 chrome::StartKeepAlive();
75 // If there's a user manager window open already, just activate it.
76 instance_
->GetWidget()->Activate();
80 // Create the guest profile, if necessary, and open the user manager
81 // from the guest profile.
82 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
83 profile_manager
->CreateProfileAsync(
84 ProfileManager::GetGuestProfilePath(),
85 base::Bind(&UserManagerView::OnGuestProfileCreated
,
86 profile_path_to_focus
),
93 void UserManagerView::Hide() {
95 instance_
->GetWidget()->Close();
99 bool UserManagerView::IsShowing() {
100 return instance_
? instance_
->GetWidget()->IsActive() : false;
103 void UserManagerView::OnGuestProfileCreated(
104 const base::FilePath
& profile_path_to_focus
,
105 Profile
* guest_profile
,
106 Profile::CreateStatus status
) {
107 if (status
!= Profile::CREATE_STATUS_INITIALIZED
)
110 instance_
= new UserManagerView(guest_profile
);
111 DialogDelegate::CreateDialogWidget(instance_
, NULL
, NULL
);
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(instance_
->GetWidget()));
120 instance_
->GetWidget()->Show();
122 // Tell the webui which user pod should be focused.
123 std::string page
= chrome::kChromeUIUserManagerURL
;
125 if (!profile_path_to_focus
.empty()) {
126 ProfileInfoCache
& cache
=
127 g_browser_process
->profile_manager()->GetProfileInfoCache();
128 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path_to_focus
);
129 if (index
!= std::string::npos
) {
131 page
+= base::IntToString(index
);
135 instance_
->web_view_
->LoadInitialURL(GURL(page
));
136 instance_
->web_view_
->RequestFocus();
139 gfx::Size
UserManagerView::GetPreferredSize() {
140 return gfx::Size(kWindowWidth
, kWindowHeight
);
143 bool UserManagerView::CanResize() const {
147 bool UserManagerView::CanMaximize() const {
151 base::string16
UserManagerView::GetWindowTitle() const {
152 return l10n_util::GetStringUTF16(IDS_USER_MANAGER_SCREEN_TITLE
);
155 int UserManagerView::GetDialogButtons() const {
156 return ui::DIALOG_BUTTON_NONE
;
159 void UserManagerView::WindowClosing() {
160 // Now that the window is closed, we can allow a new one to be opened.
161 // (WindowClosing comes in asynchronously from the call to Close() and we
162 // may have already opened a new instance).
163 if (instance_
== this)
167 bool UserManagerView::UseNewStyleForThisDialog() const {