NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / user_manager_mac.mm
blob1e132eb9bdf5584f48137acfe4e48598bd177e33
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;
24 namespace chrome {
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();
35 }  // namespace chrome
37 // Window controller for the User Manager view.
38 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
39  @private
40   scoped_ptr<content::WebContents> webContents_;
41   UserManagerMac* userManagerObserver_;  // Weak.
43 - (void)windowWillClose:(NSNotification*)notification;
44 - (void)dealloc;
45 - (id)initWithProfile:(Profile*)profile
46          withObserver:(UserManagerMac*)userManagerObserver;
47 - (void)showURL:(const GURL&)url;
48 - (void)show;
49 - (void)close;
50 - (BOOL)isVisible;
51 @end
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;
61   CGFloat screenWidth =
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 |
71                           NSResizableWindowMask
72                   backing:NSBackingStoreBuffered
73                     defer:NO];
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]
86         addObserver:self
87            selector:@selector(windowWillClose:)
88                name:NSWindowWillCloseNotification
89              object:self.window];
90   }
91   return self;
94 - (void)dealloc {
95   [[NSNotificationCenter defaultCenter] removeObserver:self];
96   [super dealloc];
99 - (void)showURL:(const GURL&)url {
100   webContents_->GetController().LoadURL(url, content::Referrer(),
101                                         content::PAGE_TRANSITION_AUTO_TOPLEVEL,
102                                         std::string());
103   [self show];
106 - (void)show {
107   [[self window] makeKeyAndOrderFront:self];
110 - (void)close {
111   [[self window] close];
114 -(BOOL)isVisible {
115   return [[self window] isVisible];
118 - (void)windowWillClose:(NSNotification*)notification {
119   [[NSNotificationCenter defaultCenter] removeObserver:self];
120   DCHECK(userManagerObserver_);
121   userManagerObserver_->WindowWasClosed();
124 @end
126 // static
127 UserManagerMac* UserManagerMac::instance_ = NULL;
129 UserManagerMac::UserManagerMac(Profile* profile) {
130   window_controller_.reset([[UserManagerWindowController alloc]
131       initWithProfile:profile withObserver:this]);
134 UserManagerMac::~UserManagerMac() {
137 // static
138 void UserManagerMac::Show(const base::FilePath& profile_path_to_focus) {
139   if (instance_) {
140     [instance_->window_controller_ show];
141     return;
142   }
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),
151       base::string16(),
152       base::string16(),
153       std::string());
156 // static
157 void UserManagerMac::Hide() {
158   if (instance_)
159     [instance_->window_controller_ close];
162 // static
163 bool UserManagerMac::IsShowing() {
164   return instance_ ? [instance_->window_controller_ isVisible]: false;
167 void UserManagerMac::WindowWasClosed() {
168   instance_ = NULL;
169   delete this;
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)
177     return;
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) {
189       page += "#";
190       page += base::IntToString(index);
191     }
192   }
193   [instance_->window_controller_ showURL:GURL(page)];