• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(OS_WIN)
25 #include "chrome/browser/shell_integration.h"
26 #include "ui/base/win/shell.h"
27 #include "ui/views/win/hwnd_util.h"
28 #endif
29 
30 namespace {
31 
32 // Default window size.
33 const int kWindowWidth = 900;
34 const int kWindowHeight = 700;
35 
36 }
37 
38 namespace chrome {
39 
40 // Declared in browser_dialogs.h so others don't have to depend on this header.
ShowUserManager(const base::FilePath & profile_path_to_focus)41 void ShowUserManager(const base::FilePath& profile_path_to_focus) {
42   UserManagerView::Show(
43       profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
44 }
45 
ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial)46 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
47   UserManagerView::Show(base::FilePath(), tutorial);
48 }
49 
HideUserManager()50 void HideUserManager() {
51   UserManagerView::Hide();
52 }
53 
54 }  // namespace chrome
55 
56 // static
57 UserManagerView* UserManagerView::instance_ = NULL;
58 
UserManagerView()59 UserManagerView::UserManagerView()
60     : web_view_(NULL),
61       keep_alive_(new AutoKeepAlive(NULL)) {
62 }
63 
~UserManagerView()64 UserManagerView::~UserManagerView() {
65 }
66 
67 // static
Show(const base::FilePath & profile_path_to_focus,profiles::UserManagerTutorialMode tutorial_mode)68 void UserManagerView::Show(const base::FilePath& profile_path_to_focus,
69                            profiles::UserManagerTutorialMode tutorial_mode) {
70   ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::OPEN_USER_MANAGER);
71   if (instance_) {
72     // If there's a user manager window open already, just activate it.
73     instance_->GetWidget()->Activate();
74     return;
75   }
76 
77   // Create the guest profile, if necessary, and open the user manager
78   // from the guest profile.
79   profiles::CreateGuestProfileForUserManager(
80       profile_path_to_focus,
81       tutorial_mode,
82       base::Bind(&UserManagerView::OnGuestProfileCreated,
83                  base::Passed(make_scoped_ptr(new UserManagerView))));
84 }
85 
86 // static
Hide()87 void UserManagerView::Hide() {
88   if (instance_)
89     instance_->GetWidget()->Close();
90 }
91 
92 // static
IsShowing()93 bool UserManagerView::IsShowing() {
94   return instance_ ? instance_->GetWidget()->IsActive() : false;
95 }
96 
97 // static
OnGuestProfileCreated(scoped_ptr<UserManagerView> instance,Profile * guest_profile,const std::string & url)98 void UserManagerView::OnGuestProfileCreated(
99     scoped_ptr<UserManagerView> instance,
100     Profile* guest_profile,
101     const std::string& url) {
102   instance_ = instance.release();  // |instance_| takes over ownership.
103   instance_->Init(guest_profile, GURL(url));
104 }
105 
Init(Profile * guest_profile,const GURL & url)106 void UserManagerView::Init(Profile* guest_profile, const GURL& url) {
107   web_view_ = new views::WebView(guest_profile);
108   SetLayoutManager(new views::FillLayout);
109   AddChildView(web_view_);
110 
111   DialogDelegate::CreateDialogWidget(this, 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(GetWidget()));
119 #endif
120   GetWidget()->Show();
121 
122   web_view_->LoadInitialURL(url);
123   web_view_->RequestFocus();
124 }
125 
GetPreferredSize() const126 gfx::Size UserManagerView::GetPreferredSize() const {
127   return gfx::Size(kWindowWidth, kWindowHeight);
128 }
129 
CanResize() const130 bool UserManagerView::CanResize() const {
131   return true;
132 }
133 
CanMaximize() const134 bool UserManagerView::CanMaximize() const {
135   return true;
136 }
137 
GetWindowTitle() const138 base::string16 UserManagerView::GetWindowTitle() const {
139   return l10n_util::GetStringUTF16(IDS_USER_MANAGER_SCREEN_TITLE);
140 }
141 
GetDialogButtons() const142 int UserManagerView::GetDialogButtons() const {
143   return ui::DIALOG_BUTTON_NONE;
144 }
145 
WindowClosing()146 void UserManagerView::WindowClosing() {
147   // Now that the window is closed, we can allow a new one to be opened.
148   // (WindowClosing comes in asynchronously from the call to Close() and we
149   // may have already opened a new instance).
150   if (instance_ == this)
151     instance_ = NULL;
152 }
153 
UseNewStyleForThisDialog() const154 bool UserManagerView::UseNewStyleForThisDialog() const {
155   return false;
156 }
157