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 "chrome/browser/browser_process.h"
8 #include "chrome/browser/profiles/profile_manager.h"
9 #include "chrome/browser/ui/browser_dialogs.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_view.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util_mac.h"
15 // Default window size. Taken from the views implementation in
16 // chrome/browser/ui/views/user_manager_view.cc.
17 // TODO(noms): Figure out if this size can be computed dynamically or adjusted
18 // for smaller screens.
19 const int kWindowWidth = 900;
20 const int kWindowHeight = 700;
24 // Declared in browser_dialogs.h so others don't have to depend on this header.
25 void ShowUserManager(const base::FilePath& profile_path_to_focus) {
27 profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
30 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
31 UserManagerMac::Show(base::FilePath(), tutorial);
34 void HideUserManager() {
35 UserManagerMac::Hide();
40 // Window controller for the User Manager view.
41 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
43 scoped_ptr<content::WebContents> webContents_;
44 UserManagerMac* userManagerObserver_; // Weak.
46 - (void)windowWillClose:(NSNotification*)notification;
48 - (id)initWithProfile:(Profile*)profile
49 withObserver:(UserManagerMac*)userManagerObserver;
50 - (void)showURL:(const GURL&)url;
56 @implementation UserManagerWindowController
58 - (id)initWithProfile:(Profile*)profile
59 withObserver:(UserManagerMac*)userManagerObserver {
61 // Center the window on the primary screen.
62 CGFloat screenHeight =
63 [[[NSScreen screens] objectAtIndex:0] frame].size.height;
65 [[[NSScreen screens] objectAtIndex:0] frame].size.width;
67 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2,
68 (screenHeight - kWindowHeight) / 2,
69 kWindowWidth, kWindowHeight);
70 NSWindow* window = [[NSWindow alloc]
71 initWithContentRect:contentRect
72 styleMask:NSTitledWindowMask |
73 NSClosableWindowMask |
75 backing:NSBackingStoreBuffered
77 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)];
79 if ((self = [super initWithWindow:window])) {
80 userManagerObserver_ = userManagerObserver;
82 // Initialize the web view.
83 webContents_.reset(content::WebContents::Create(
84 content::WebContents::CreateParams(profile)));
85 window.contentView = webContents_->GetView()->GetNativeView();
86 DCHECK(window.contentView);
88 [[NSNotificationCenter defaultCenter]
90 selector:@selector(windowWillClose:)
91 name:NSWindowWillCloseNotification
98 [[NSNotificationCenter defaultCenter] removeObserver:self];
102 - (void)showURL:(const GURL&)url {
103 webContents_->GetController().LoadURL(url, content::Referrer(),
104 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
110 [[self window] makeKeyAndOrderFront:self];
114 [[self window] close];
118 return [[self window] isVisible];
121 - (void)windowWillClose:(NSNotification*)notification {
122 [[NSNotificationCenter defaultCenter] removeObserver:self];
123 DCHECK(userManagerObserver_);
124 userManagerObserver_->WindowWasClosed();
130 UserManagerMac* UserManagerMac::instance_ = NULL;
132 UserManagerMac::UserManagerMac(Profile* profile) {
133 window_controller_.reset([[UserManagerWindowController alloc]
134 initWithProfile:profile withObserver:this]);
137 UserManagerMac::~UserManagerMac() {
141 void UserManagerMac::Show(const base::FilePath& profile_path_to_focus,
142 profiles::UserManagerTutorialMode tutorial_mode) {
144 // If there's a user manager window open already, just activate it.
145 [instance_->window_controller_ show];
149 // Create the guest profile, if necessary, and open the User Manager
150 // from the guest profile.
151 profiles::CreateGuestProfileForUserManager(
152 profile_path_to_focus,
154 base::Bind(&UserManagerMac::OnGuestProfileCreated));
158 void UserManagerMac::Hide() {
160 [instance_->window_controller_ close];
164 bool UserManagerMac::IsShowing() {
165 return instance_ ? [instance_->window_controller_ isVisible]: false;
169 void UserManagerMac::OnGuestProfileCreated(Profile* guest_profile,
170 const std::string& url) {
171 instance_ = new UserManagerMac(guest_profile);
172 [instance_->window_controller_ showURL:GURL(url)];
175 void UserManagerMac::WindowWasClosed() {