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/cocoa/user_manager_mac.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/ui/browser_dialogs.h"
11 #include "chrome/common/url_constants.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_view.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util_mac.h"
17 // Default window size. Taken from the views implementation in
18 // chrome/browser/ui/views/user_manager_view.cc.
19 // TODO(noms): Figure out if this size can be computed dynamically or adjusted
20 // for smaller screens.
21 const int kWindowWidth = 900;
22 const int kWindowHeight = 700;
26 // Declared in browser_dialogs.h so others don't have to depend on this header.
27 void ShowUserManager(const base::FilePath& profile_path_to_focus) {
28 UserManagerMac::Show(profile_path_to_focus);
31 void HideUserManager() {
32 UserManagerMac::Hide();
37 // Window controller for the User Manager view.
38 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
40 scoped_ptr<content::WebContents> webContents_;
41 UserManagerMac* userManagerObserver_; // Weak.
43 - (void)windowWillClose:(NSNotification*)notification;
45 - (id)initWithProfile:(Profile*)profile
46 withObserver:(UserManagerMac*)userManagerObserver;
47 - (void)showURL:(const GURL&)url;
53 @implementation UserManagerWindowController
55 - (id)initWithProfile:(Profile*)profile
56 withObserver:(UserManagerMac*)userManagerObserver {
58 // Center the window on the primary screen.
59 CGFloat screenHeight =
60 [[[NSScreen screens] objectAtIndex:0] frame].size.height;
62 [[[NSScreen screens] objectAtIndex:0] frame].size.width;
64 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2,
65 (screenHeight - kWindowHeight) / 2,
66 kWindowWidth, kWindowHeight);
67 NSWindow* window = [[NSWindow alloc]
68 initWithContentRect:contentRect
69 styleMask:NSTitledWindowMask |
70 NSClosableWindowMask |
72 backing:NSBackingStoreBuffered
74 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)];
76 if ((self = [super initWithWindow:window])) {
77 userManagerObserver_ = userManagerObserver;
79 // Initialize the web view.
80 webContents_.reset(content::WebContents::Create(
81 content::WebContents::CreateParams(profile)));
82 window.contentView = webContents_->GetView()->GetNativeView();
83 DCHECK(window.contentView);
85 [[NSNotificationCenter defaultCenter]
87 selector:@selector(windowWillClose:)
88 name:NSWindowWillCloseNotification
95 [[NSNotificationCenter defaultCenter] removeObserver:self];
99 - (void)showURL:(const GURL&)url {
100 webContents_->GetController().LoadURL(url, content::Referrer(),
101 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
107 [[self window] makeKeyAndOrderFront:self];
111 [[self window] close];
115 return [[self window] isVisible];
118 - (void)windowWillClose:(NSNotification*)notification {
119 [[NSNotificationCenter defaultCenter] removeObserver:self];
120 DCHECK(userManagerObserver_);
121 userManagerObserver_->WindowWasClosed();
127 UserManagerMac* UserManagerMac::instance_ = NULL;
129 UserManagerMac::UserManagerMac(Profile* profile) {
130 window_controller_.reset([[UserManagerWindowController alloc]
131 initWithProfile:profile withObserver:this]);
134 UserManagerMac::~UserManagerMac() {
138 void UserManagerMac::Show(const base::FilePath& profile_path_to_focus) {
140 [instance_->window_controller_ show];
144 // Create the guest profile, if necessary, and open the User Manager
145 // from the guest profile.
146 ProfileManager* profile_manager = g_browser_process->profile_manager();
147 profile_manager->CreateProfileAsync(
148 ProfileManager::GetGuestProfilePath(),
149 base::Bind(&UserManagerMac::OnGuestProfileCreated,
150 profile_path_to_focus),
157 void UserManagerMac::Hide() {
159 [instance_->window_controller_ close];
163 bool UserManagerMac::IsShowing() {
164 return instance_ ? [instance_->window_controller_ isVisible]: false;
167 void UserManagerMac::WindowWasClosed() {
172 void UserManagerMac::OnGuestProfileCreated(
173 const base::FilePath& profile_path_to_focus,
174 Profile* guest_profile,
175 Profile::CreateStatus status) {
176 if (status != Profile::CREATE_STATUS_INITIALIZED)
179 instance_ = new UserManagerMac(guest_profile);
181 // Tell the webui which user pod should be focused.
182 std::string page = chrome::kChromeUIUserManagerURL;
184 if (!profile_path_to_focus.empty()) {
185 ProfileInfoCache& cache =
186 g_browser_process->profile_manager()->GetProfileInfoCache();
187 size_t index = cache.GetIndexOfProfileWithPath(profile_path_to_focus);
188 if (index != std::string::npos) {
190 page += base::IntToString(index);
193 [instance_->window_controller_ showURL:GURL(page)];