• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ui/views/user_manager_view.h"
6 
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"
23 
24 #if defined(USE_ASH)
25 #include "ash/wm/window_util.h"
26 #endif
27 
28 #if defined(OS_WIN)
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"
33 #endif
34 
35 namespace {
36 
37 // Default window size.
38 const int kWindowWidth = 900;
39 const int kWindowHeight = 700;
40 
41 }
42 
43 namespace chrome {
44 
45 // Declared in browser_dialogs.h so others don't have to depend on this header.
ShowUserManager(const base::FilePath & profile_path_to_focus)46 void ShowUserManager(const base::FilePath& profile_path_to_focus) {
47   UserManagerView::Show(profile_path_to_focus);
48 }
49 
HideUserManager()50 void HideUserManager() {
51   UserManagerView::Hide();
52 }
53 
54 }  // namespace chrome
55 
56 // static
57 UserManagerView* UserManagerView::instance_ = NULL;
58 
UserManagerView(Profile * profile)59 UserManagerView::UserManagerView(Profile* profile)
60     : web_view_(new views::WebView(profile)) {
61   SetLayoutManager(new views::FillLayout);
62   AddChildView(web_view_);
63 }
64 
~UserManagerView()65 UserManagerView::~UserManagerView() {
66   chrome::EndKeepAlive();  // Remove shutdown prevention.
67 }
68 
69 // static
Show(const base::FilePath & profile_path_to_focus)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();
73 
74   if (instance_) {
75     // If there's a user manager window open already, just activate it.
76     instance_->GetWidget()->Activate();
77     return;
78   }
79 
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),
87       base::string16(),
88       base::string16(),
89       std::string());
90 }
91 
92 // static
Hide()93 void UserManagerView::Hide() {
94   if (instance_)
95     instance_->GetWidget()->Close();
96 }
97 
98 // static
IsShowing()99 bool UserManagerView::IsShowing() {
100   return instance_ ? instance_->GetWidget()->IsActive() : false;
101 }
102 
OnGuestProfileCreated(const base::FilePath & profile_path_to_focus,Profile * guest_profile,Profile::CreateStatus status)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)
108     return;
109 
110   instance_ = new UserManagerView(guest_profile);
111   DialogDelegate::CreateDialogWidget(instance_, 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(instance_->GetWidget()));
119 #endif
120   instance_->GetWidget()->Show();
121 
122   // Tell the webui which user pod should be focused.
123   std::string page = chrome::kChromeUIUserManagerURL;
124 
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) {
130       page += "#";
131       page += base::IntToString(index);
132     }
133   }
134 
135   instance_->web_view_->LoadInitialURL(GURL(page));
136   instance_->web_view_->RequestFocus();
137 }
138 
GetPreferredSize()139 gfx::Size UserManagerView::GetPreferredSize() {
140   return gfx::Size(kWindowWidth, kWindowHeight);
141 }
142 
CanResize() const143 bool UserManagerView::CanResize() const {
144   return true;
145 }
146 
CanMaximize() const147 bool UserManagerView::CanMaximize() const {
148   return true;
149 }
150 
GetWindowTitle() const151 base::string16 UserManagerView::GetWindowTitle() const {
152   return l10n_util::GetStringUTF16(IDS_USER_MANAGER_SCREEN_TITLE);
153 }
154 
GetDialogButtons() const155 int UserManagerView::GetDialogButtons() const {
156   return ui::DIALOG_BUTTON_NONE;
157 }
158 
WindowClosing()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)
164     instance_ = NULL;
165 }
166 
UseNewStyleForThisDialog() const167 bool UserManagerView::UseNewStyleForThisDialog() const {
168   return false;
169 }
170