Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / user_manager_mac.mm
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.
4
5 #include "chrome/browser/ui/cocoa/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 "content/public/browser/web_contents_view.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util_mac.h"
14
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;
21
22 namespace chrome {
23
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) {
26   UserManagerMac::Show(
27       profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
28 }
29
30 void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
31   UserManagerMac::Show(base::FilePath(), tutorial);
32 }
33
34 void HideUserManager() {
35   UserManagerMac::Hide();
36 }
37
38 }  // namespace chrome
39
40 // Window controller for the User Manager view.
41 @interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
42  @private
43   scoped_ptr<content::WebContents> webContents_;
44   UserManagerMac* userManagerObserver_;  // Weak.
45 }
46 - (void)windowWillClose:(NSNotification*)notification;
47 - (void)dealloc;
48 - (id)initWithProfile:(Profile*)profile
49          withObserver:(UserManagerMac*)userManagerObserver;
50 - (void)showURL:(const GURL&)url;
51 - (void)show;
52 - (void)close;
53 - (BOOL)isVisible;
54 @end
55
56 @implementation UserManagerWindowController
57
58 - (id)initWithProfile:(Profile*)profile
59          withObserver:(UserManagerMac*)userManagerObserver {
60
61   // Center the window on the primary screen.
62   CGFloat screenHeight =
63       [[[NSScreen screens] objectAtIndex:0] frame].size.height;
64   CGFloat screenWidth =
65       [[[NSScreen screens] objectAtIndex:0] frame].size.width;
66
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 |
74                           NSResizableWindowMask
75                   backing:NSBackingStoreBuffered
76                     defer:NO];
77   [window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)];
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_->GetView()->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 }