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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// 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.
#import "chrome/browser/ui/cocoa/profiles/avatar_base_controller.h"
#include "base/mac/foundation_util.h"
#include "base/macros.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/signin/chrome_signin_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/base_bubble_controller.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/profiles/avatar_menu_bubble_controller.h"
#import "chrome/browser/ui/cocoa/profiles/profile_chooser_controller.h"
#include "components/signin/core/browser/signin_error_controller.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "ui/base/cocoa/cocoa_base_utils.h"
// Space between the avatar icon and the avatar menu bubble.
const CGFloat kMenuYOffsetAdjust = 1.0;
// Offset needed to align the edge of the avatar bubble with the edge of the
// avatar button.
const CGFloat kMenuXOffsetAdjust = 2.0;
@interface AvatarBaseController (Private)
// Shows the avatar bubble.
- (IBAction)buttonClicked:(id)sender;
- (IBAction)buttonRightClicked:(id)sender;
- (void)bubbleWillClose:(NSNotification*)notif;
// Updates the profile name displayed by the avatar button. If |layoutParent| is
// yes, then the BrowserWindowController is notified to relayout the subviews,
// as the button needs to be repositioned.
- (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent;
// Displays an error icon if any accounts associated with this profile have an
// auth error.
- (void)updateErrorStatus:(BOOL)hasError;
@end
class ProfileAttributesUpdateObserver
: public ProfileAttributesStorage::Observer,
public SigninErrorController::Observer {
public:
ProfileAttributesUpdateObserver(Profile* profile,
AvatarBaseController* avatarController)
: profile_(profile),
avatarController_(avatarController) {
g_browser_process->profile_manager()->
GetProfileAttributesStorage().AddObserver(this);
// Subscribe to authentication error changes so that the avatar button
// can update itself.
SigninErrorController* errorController =
profiles::GetSigninErrorController(profile_);
if (errorController)
errorController->AddObserver(this);
}
~ProfileAttributesUpdateObserver() override {
g_browser_process->profile_manager()->
GetProfileAttributesStorage().RemoveObserver(this);
SigninErrorController* errorController =
profiles::GetSigninErrorController(profile_);
if (errorController)
errorController->RemoveObserver(this);
}
// ProfileAttributesStorage::Observer:
void OnProfileAdded(const base::FilePath& profile_path) override {
[avatarController_ updateAvatarButtonAndLayoutParent:YES];
}
void OnProfileWasRemoved(const base::FilePath& profile_path,
const base::string16& profile_name) override {
// If deleting the active profile, don't bother updating the avatar
// button, as the browser window is being closed anyway.
if (profile_->GetPath() != profile_path)
[avatarController_ updateAvatarButtonAndLayoutParent:YES];
}
void OnProfileNameChanged(const base::FilePath& profile_path,
const base::string16& old_profile_name) override {
if (profile_->GetPath() == profile_path)
[avatarController_ updateAvatarButtonAndLayoutParent:YES];
}
void OnProfileSupervisedUserIdChanged(
const base::FilePath& profile_path) override {
if (profile_->GetPath() == profile_path)
[avatarController_ updateAvatarButtonAndLayoutParent:YES];
}
// SigninErrorController::Observer:
void OnErrorChanged() override {
SigninErrorController* errorController =
profiles::GetSigninErrorController(profile_);
if (errorController)
[avatarController_ updateErrorStatus:errorController->HasError()];
}
private:
Profile* profile_;
AvatarBaseController* avatarController_; // Weak; owns this.
DISALLOW_COPY_AND_ASSIGN(ProfileAttributesUpdateObserver);
};
@implementation AvatarBaseController
- (id)initWithBrowser:(Browser*)browser {
if ((self = [super init])) {
browser_ = browser;
profileAttributesObserver_.reset(
new ProfileAttributesUpdateObserver(browser_->profile(), self));
}
return self;
}
- (void)dealloc {
[self browserWillBeDestroyed];
[super dealloc];
}
- (void)browserWillBeDestroyed {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSWindowWillCloseNotification
object:[menuController_ window]];
browser_ = nullptr;
}
- (NSButton*)buttonView {
CHECK(button_.get()); // Subclasses must set this.
return button_.get();
}
- (void)showAvatarBubbleAnchoredAt:(NSView*)anchor
withMode:(BrowserWindow::AvatarBubbleMode)mode
withServiceType:(signin::GAIAServiceType)serviceType
fromAccessPoint:(signin_metrics::AccessPoint)accessPoint {
if (menuController_) {
profiles::BubbleViewMode viewMode;
profiles::TutorialMode tutorialMode;
profiles::BubbleViewModeFromAvatarBubbleMode(
mode, &viewMode, &tutorialMode);
if (tutorialMode != profiles::TUTORIAL_MODE_NONE) {
ProfileChooserController* profileChooserController =
base::mac::ObjCCastStrict<ProfileChooserController>(
menuController_);
[profileChooserController setTutorialMode:tutorialMode];
[profileChooserController initMenuContentsWithView:viewMode];
}
return;
}
DCHECK(chrome::IsCommandEnabled(browser_, IDC_SHOW_AVATAR_MENU));
NSWindowController* wc =
[browser_->window()->GetNativeWindow() windowController];
if ([wc isKindOfClass:[BrowserWindowController class]]) {
[static_cast<BrowserWindowController*>(wc)
lockBarVisibilityForOwner:self withAnimation:NO delay:NO];
}
// The new avatar bubble does not have an arrow, and it should be anchored
// to the edge of the avatar button.
int anchorX = NSMaxX([anchor bounds]) - kMenuXOffsetAdjust;
NSPoint point = NSMakePoint(anchorX,
NSMaxY([anchor bounds]) + kMenuYOffsetAdjust);
point = [anchor convertPoint:point toView:nil];
point = ui::ConvertPointFromWindowToScreen([anchor window], point);
// |menuController_| will automatically release itself on close.
profiles::BubbleViewMode viewMode;
profiles::TutorialMode tutorialMode;
profiles::BubbleViewModeFromAvatarBubbleMode(
mode, &viewMode, &tutorialMode);
// Don't start creating the view if it would be an empty fast user switcher.
// It has to happen here to prevent the view system from creating an empty
// container.
if (viewMode == profiles::BUBBLE_VIEW_MODE_FAST_PROFILE_CHOOSER &&
!profiles::HasProfileSwitchTargets(browser_->profile())) {
return;
}
menuController_ =
[[ProfileChooserController alloc] initWithBrowser:browser_
anchoredAt:point
viewMode:viewMode
tutorialMode:tutorialMode
serviceType:serviceType
accessPoint:accessPoint];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(bubbleWillClose:)
name:NSWindowWillCloseNotification
object:[menuController_ window]];
[menuController_ showWindow:self];
ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ICON_AVATAR_BUBBLE);
}
- (BOOL)isCtrlPressed {
return [NSEvent modifierFlags] & NSControlKeyMask ? YES : NO;
}
- (IBAction)buttonClicked:(id)sender {
BrowserWindow::AvatarBubbleMode mode =
BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT;
if ([self isCtrlPressed])
mode = BrowserWindow::AVATAR_BUBBLE_MODE_FAST_USER_SWITCH;
[self showAvatarBubbleAnchoredAt:button_
withMode:mode
withServiceType:signin::GAIA_SERVICE_TYPE_NONE
fromAccessPoint:signin_metrics::AccessPoint::
ACCESS_POINT_AVATAR_BUBBLE_SIGN_IN];
}
- (IBAction)buttonRightClicked:(id)sender {
BrowserWindow::AvatarBubbleMode mode =
BrowserWindow::AVATAR_BUBBLE_MODE_FAST_USER_SWITCH;
[self showAvatarBubbleAnchoredAt:button_
withMode:mode
withServiceType:signin::GAIA_SERVICE_TYPE_NONE
fromAccessPoint:signin_metrics::AccessPoint::
ACCESS_POINT_AVATAR_BUBBLE_SIGN_IN];
}
- (void)bubbleWillClose:(NSNotification*)notif {
NSWindowController* wc =
[browser_->window()->GetNativeWindow() windowController];
if ([wc isKindOfClass:[BrowserWindowController class]]) {
[static_cast<BrowserWindowController*>(wc)
releaseBarVisibilityForOwner:self withAnimation:YES delay:NO];
}
menuController_ = nil;
}
- (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent {
NOTREACHED();
}
- (void)updateErrorStatus:(BOOL)hasError {
}
- (BaseBubbleController*)menuController {
return menuController_;
}
@end
|