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