[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / profiles / user_manager_mac.mm
blob3087ac4b94c39afc5047ac53e0b36a3f2ebde880
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;
21 namespace chrome {
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) {
25   UserManagerMac::Show(
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();
37 }  // namespace chrome
39 // Window controller for the User Manager view.
40 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
41  @private
42   scoped_ptr<content::WebContents> webContents_;
43   UserManagerMac* userManagerObserver_;  // Weak.
45 - (void)windowWillClose:(NSNotification*)notification;
46 - (void)dealloc;
47 - (id)initWithProfile:(Profile*)profile
48          withObserver:(UserManagerMac*)userManagerObserver;
49 - (void)showURL:(const GURL&)url;
50 - (void)show;
51 - (void)close;
52 - (BOOL)isVisible;
53 @end
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;
63   CGFloat screenWidth =
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 |
73                           NSResizableWindowMask
74                   backing:NSBackingStoreBuffered
75                     defer:NO];
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]
89         addObserver:self
90            selector:@selector(windowWillClose:)
91                name:NSWindowWillCloseNotification
92              object:self.window];
93   }
94   return self;
97 - (void)dealloc {
98   [[NSNotificationCenter defaultCenter] removeObserver:self];
99   [super dealloc];
102 - (void)showURL:(const GURL&)url {
103   webContents_->GetController().LoadURL(url, content::Referrer(),
104                                         content::PAGE_TRANSITION_AUTO_TOPLEVEL,
105                                         std::string());
106   [self show];
109 - (void)show {
110   [[self window] makeKeyAndOrderFront:self];
113 - (void)close {
114   [[self window] close];
117 -(BOOL)isVisible {
118   return [[self window] isVisible];
121 - (void)windowWillClose:(NSNotification*)notification {
122   [[NSNotificationCenter defaultCenter] removeObserver:self];
123   DCHECK(userManagerObserver_);
124   userManagerObserver_->WindowWasClosed();
127 @end
129 // static
130 UserManagerMac* UserManagerMac::instance_ = NULL;
132 UserManagerMac::UserManagerMac(Profile* profile) {
133   window_controller_.reset([[UserManagerWindowController alloc]
134       initWithProfile:profile withObserver:this]);
137 UserManagerMac::~UserManagerMac() {
140 // static
141 void UserManagerMac::Show(const base::FilePath& profile_path_to_focus,
142                           profiles::UserManagerTutorialMode tutorial_mode) {
143   if (instance_) {
144     // If there's a user manager window open already, just activate it.
145     [instance_->window_controller_ show];
146     return;
147   }
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,
153       tutorial_mode,
154       base::Bind(&UserManagerMac::OnGuestProfileCreated));
157 // static
158 void UserManagerMac::Hide() {
159   if (instance_)
160     [instance_->window_controller_ close];
163 // static
164 bool UserManagerMac::IsShowing() {
165   return instance_ ? [instance_->window_controller_ isVisible]: false;
168 // static
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() {
176   instance_ = NULL;
177   delete this;