1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/cocoa/profiles/user_manager_mac.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util_mac.h"
// Default window size. Taken from the views implementation in
// chrome/browser/ui/views/user_manager_view.cc.
// TODO(noms): Figure out if this size can be computed dynamically or adjusted
// for smaller screens.
const int kWindowWidth = 900;
const int kWindowHeight = 700;
namespace chrome {
// Declared in browser_dialogs.h so others don't have to depend on this header.
void ShowUserManager(const base::FilePath& profile_path_to_focus) {
UserManagerMac::Show(
profile_path_to_focus, profiles::USER_MANAGER_NO_TUTORIAL);
}
void ShowUserManagerWithTutorial(profiles::UserManagerTutorialMode tutorial) {
UserManagerMac::Show(base::FilePath(), tutorial);
}
void HideUserManager() {
UserManagerMac::Hide();
}
} // namespace chrome
// Window controller for the User Manager view.
@interface UserManagerWindowController : NSWindowController <NSWindowDelegate> {
@private
scoped_ptr<content::WebContents> webContents_;
UserManagerMac* userManagerObserver_; // Weak.
}
- (void)windowWillClose:(NSNotification*)notification;
- (void)dealloc;
- (id)initWithProfile:(Profile*)profile
withObserver:(UserManagerMac*)userManagerObserver;
- (void)showURL:(const GURL&)url;
- (void)show;
- (void)close;
- (BOOL)isVisible;
@end
@implementation UserManagerWindowController
- (id)initWithProfile:(Profile*)profile
withObserver:(UserManagerMac*)userManagerObserver {
// Center the window on the primary screen.
CGFloat screenHeight =
[[[NSScreen screens] objectAtIndex:0] frame].size.height;
CGFloat screenWidth =
[[[NSScreen screens] objectAtIndex:0] frame].size.width;
NSRect contentRect = NSMakeRect((screenWidth - kWindowWidth) / 2,
(screenHeight - kWindowHeight) / 2,
kWindowWidth, kWindowHeight);
NSWindow* window = [[NSWindow alloc]
initWithContentRect:contentRect
styleMask:NSTitledWindowMask |
NSClosableWindowMask |
NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:l10n_util::GetNSString(IDS_USER_MANAGER_SCREEN_TITLE)];
[window setMinSize:NSMakeSize(kWindowWidth, kWindowHeight)];
if ((self = [super initWithWindow:window])) {
userManagerObserver_ = userManagerObserver;
// Initialize the web view.
webContents_.reset(content::WebContents::Create(
content::WebContents::CreateParams(profile)));
window.contentView = webContents_->GetView()->GetNativeView();
DCHECK(window.contentView);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification
object:self.window];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)showURL:(const GURL&)url {
webContents_->GetController().LoadURL(url, content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
[self show];
}
- (void)show {
[[self window] makeKeyAndOrderFront:self];
}
- (void)close {
[[self window] close];
}
-(BOOL)isVisible {
return [[self window] isVisible];
}
- (void)windowWillClose:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self];
DCHECK(userManagerObserver_);
userManagerObserver_->WindowWasClosed();
}
@end
// static
UserManagerMac* UserManagerMac::instance_ = NULL;
UserManagerMac::UserManagerMac(Profile* profile) {
window_controller_.reset([[UserManagerWindowController alloc]
initWithProfile:profile withObserver:this]);
}
UserManagerMac::~UserManagerMac() {
}
// static
void UserManagerMac::Show(const base::FilePath& profile_path_to_focus,
profiles::UserManagerTutorialMode tutorial_mode) {
if (instance_) {
// If there's a user manager window open already, just activate it.
[instance_->window_controller_ show];
return;
}
// Create the guest profile, if necessary, and open the User Manager
// from the guest profile.
profiles::CreateGuestProfileForUserManager(
profile_path_to_focus,
tutorial_mode,
base::Bind(&UserManagerMac::OnGuestProfileCreated));
}
// static
void UserManagerMac::Hide() {
if (instance_)
[instance_->window_controller_ close];
}
// static
bool UserManagerMac::IsShowing() {
return instance_ ? [instance_->window_controller_ isVisible]: false;
}
// static
void UserManagerMac::OnGuestProfileCreated(Profile* guest_profile,
const std::string& url) {
instance_ = new UserManagerMac(guest_profile);
[instance_->window_controller_ showURL:GURL(url)];
}
void UserManagerMac::WindowWasClosed() {
instance_ = NULL;
delete this;
}
|