Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / user_manager_view.cc
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/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"
24
25 #if defined(USE_ASH)
26 #include "ash/wm/window_util.h"
27 #endif
28
29 #if defined(OS_WIN)
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"
34 #endif
35
36 namespace {
37
38 // Default window size.
39 const int kWindowWidth = 900;
40 const int kWindowHeight = 700;
41
42 }
43
44 namespace chrome {
45
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);
49 }
50
51 void HideUserManager() {
52   UserManagerView::Hide();
53 }
54
55 }  // namespace chrome
56
57 // static
58 UserManagerView* UserManagerView::instance_ = NULL;
59
60 UserManagerView::UserManagerView(Profile* profile)
61     : web_view_(new views::WebView(profile)) {
62   SetLayoutManager(new views::FillLayout);
63   AddChildView(web_view_);
64 }
65
66 UserManagerView::~UserManagerView() {
67   chrome::EndKeepAlive();  // Remove shutdown prevention.
68 }
69
70 // static
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();
74
75   ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::OPEN_USER_MANAGER);
76   if (instance_) {
77     // If there's a user manager window open already, just activate it.
78     instance_->GetWidget()->Activate();
79     return;
80   }
81
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),
89       base::string16(),
90       base::string16(),
91       std::string());
92 }
93
94 // static
95 void UserManagerView::Hide() {
96   if (instance_)
97     instance_->GetWidget()->Close();
98 }
99
100 // static
101 bool UserManagerView::IsShowing() {
102   return instance_ ? instance_->GetWidget()->IsActive() : false;
103 }
104
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)
110     return;
111
112   instance_ = new UserManagerView(guest_profile);
113   DialogDelegate::CreateDialogWidget(instance_, NULL, NULL);
114
115 #if defined(OS_WIN)
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()));
121 #endif
122   instance_->GetWidget()->Show();
123
124   // Tell the webui which user pod should be focused.
125   std::string page = chrome::kChromeUIUserManagerURL;
126
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) {
132       page += "#";
133       page += base::IntToString(index);
134     }
135   }
136
137   instance_->web_view_->LoadInitialURL(GURL(page));
138   instance_->web_view_->RequestFocus();
139 }
140
141 gfx::Size UserManagerView::GetPreferredSize() {
142   return gfx::Size(kWindowWidth, kWindowHeight);
143 }
144
145 bool UserManagerView::CanResize() const {
146   return true;
147 }
148
149 bool UserManagerView::CanMaximize() const {
150   return true;
151 }
152
153 base::string16 UserManagerView::GetWindowTitle() const {
154   return l10n_util::GetStringUTF16(IDS_USER_MANAGER_SCREEN_TITLE);
155 }
156
157 int UserManagerView::GetDialogButtons() const {
158   return ui::DIALOG_BUTTON_NONE;
159 }
160
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)
166     instance_ = NULL;
167 }
168
169 bool UserManagerView::UseNewStyleForThisDialog() const {
170   return false;
171 }