Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / profiles / user_manager_mac.mm
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.
4
5 #include "chrome/browser/ui/cocoa/profiles/user_manager_mac.h"
6
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"
13
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;
20
21 namespace chrome {
22
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);
27 }
28
29 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
30   UserManagerMac::Show(base::FilePath(), tutorial);
31 }
32
33 void HideUserManager() {
34   UserManagerMac::Hide();
35 }
36
37 }  // namespace chrome
38
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.
44 }
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
54
55 @implementation UserManagerWindowController
56
57 - (id)initWithProfile:(Profile*)profile
58          withObserver:(UserManagerMac*)userManagerObserver {
59
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;
65
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)];
78
79   if ((self = [super initWithWindow:window])) {
80     userManagerObserver_ = userManagerObserver;
81
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);
87
88     [[NSNotificationCenter defaultCenter]
89         addObserver:self
90            selector:@selector(windowWillClose:)
91                name:NSWindowWillCloseNotification
92              object:self.window];
93   }
94   return self;
95 }
96
97 - (void)dealloc {
98   [[NSNotificationCenter defaultCenter] removeObserver:self];
99   [super dealloc];
100 }
101
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];
107 }
108
109 - (void)show {
110   [[self window] makeKeyAndOrderFront:self];
111 }
112
113 - (void)close {
114   [[self window] close];
115 }
116
117 -(BOOL)isVisible {
118   return [[self window] isVisible];
119 }
120
121 - (void)windowWillClose:(NSNotification*)notification {
122   [[NSNotificationCenter defaultCenter] removeObserver:self];
123   DCHECK(userManagerObserver_);
124   userManagerObserver_->WindowWasClosed();
125 }
126
127 @end
128
129 // static
130 UserManagerMac* UserManagerMac::instance_ = NULL;
131
132 UserManagerMac::UserManagerMac(Profile* profile) {
133   window_controller_.reset([[UserManagerWindowController alloc]
134       initWithProfile:profile withObserver:this]);
135 }
136
137 UserManagerMac::~UserManagerMac() {
138 }
139
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   }
148
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));
155 }
156
157 // static
158 void UserManagerMac::Hide() {
159   if (instance_)
160     [instance_->window_controller_ close];
161 }
162
163 // static
164 bool UserManagerMac::IsShowing() {
165   return instance_ ? [instance_->window_controller_ isVisible]: false;
166 }
167
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)];
173 }
174
175 void UserManagerMac::WindowWasClosed() {
176   instance_ = NULL;
177   delete this;
178 }