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.
5 #include "chrome/browser/ui/cocoa/profiles/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 "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
14 // Default window size. Taken from the views implementation in
15 // chrome/browser/ui/views/user_manager_view.cc.
16 // TODO(noms): Figure out if this size can be computed dynamically or adjusted
17 // for smaller screens.
18 const int kWindowWidth = 900;
19 const int kWindowHeight = 700;
23 // Declared in browser_dialogs.h so others don't have to depend on this header.
24 void ShowUserManager(const base::FilePath& profile_path_to_focus) {
26 profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
29 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
30 UserManagerMac::Show(base::FilePath(), tutorial);
33 void HideUserManager() {
34 UserManagerMac::Hide();
39 // Window controller for the User Manager view.
40 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
42 scoped_ptr<content::WebContents> webContents_;
43 UserManagerMac* userManagerObserver_; // Weak.
45 - (void)windowWillClose:(NSNotification*)notification;
47 - (id)initWithProfile:(Profile*)profile
48 withObserver:(UserManagerMac*)userManagerObserver;
49 - (void)showURL:(const GURL&)url;
55 @implementation UserManagerWindowController
57 - (id)initWithProfile:(Profile*)profile
58 withObserver:(UserManagerMac*)userManagerObserver {
60 // Center the window on the primary screen.
61 CGFloat screenHeight =
62 [[[NSScreen screens] objectAtIndex:0] frame].size.height;
64 [[[NSScreen screens] objectAtIndex:0] frame].size.width;
66 NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2,
67 (screenHeight - kWindowHeight) / 2,
68 kWindowWidth, kWindowHeight);
69 NSWindow* window = [[NSWindow alloc]
70 initWithContentRect:contentRect
71 styleMask:NSTitledWindowMask |
72 NSClosableWindowMask |
74 backing:NSBackingStoreBuffered
76 [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)];
77 [window setMinSize:NSMakeSize(kWindowWidth, kWindowHeight)];
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_->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() {