summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/profiles/avatar_base_controller.mm
blob: d05be0a0d480491e575ba6ef0a48bcea68bb7bc1 (plain)
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
// 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 "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_info_cache_observer.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/signin/signin_header_helper.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/browser/profiles/profile_window.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/resource/resource_bundle.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;

- (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 ProfileInfoUpdateObserver : public ProfileInfoCacheObserver,
                                  public SigninErrorController::Observer {
 public:
  ProfileInfoUpdateObserver(Profile* profile,
                            AvatarBaseController* avatarController)
      : profile_(profile),
        avatarController_(avatarController) {
    g_browser_process->profile_manager()->
        GetProfileInfoCache().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);
  }

  virtual ~ProfileInfoUpdateObserver() {
    g_browser_process->profile_manager()->
        GetProfileInfoCache().RemoveObserver(this);
    SigninErrorController* errorController =
        profiles::GetSigninErrorController(profile_);
    if (errorController)
      errorController->RemoveObserver(this);
  }

  // ProfileInfoCacheObserver:
  virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE {
    [avatarController_ updateAvatarButtonAndLayoutParent:YES];
  }

  virtual void OnProfileWasRemoved(
      const base::FilePath& profile_path,
      const base::string16& profile_name) OVERRIDE {
    [avatarController_ updateAvatarButtonAndLayoutParent:YES];
  }

  virtual void OnProfileNameChanged(
      const base::FilePath& profile_path,
      const base::string16& old_profile_name) OVERRIDE {
    [avatarController_ updateAvatarButtonAndLayoutParent:YES];
  }

  virtual void OnProfileAvatarChanged(
      const base::FilePath& profile_path) OVERRIDE {
    [avatarController_ updateAvatarButtonAndLayoutParent:YES];
  }

  virtual void OnProfileSupervisedUserIdChanged(
      const base::FilePath& profile_path) OVERRIDE {
    [avatarController_ updateAvatarButtonAndLayoutParent:YES];
  }

  // SigninErrorController::Observer:
  virtual 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(ProfileInfoUpdateObserver);
};

@implementation AvatarBaseController

- (id)initWithBrowser:(Browser*)browser {
  if ((self = [super init])) {
    browser_ = browser;
    profileInfoObserver_.reset(
        new ProfileInfoUpdateObserver(browser_->profile(), self));
  }
  return self;
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter]
      removeObserver:self
                name:NSWindowWillCloseNotification
              object:[menuController_ window]];
  [super dealloc];
}

- (NSButton*)buttonView {
  CHECK(button_.get());  // Subclasses must set this.
  return button_.get();
}

- (void)showAvatarBubble:(NSView*)anchor
                withMode:(BrowserWindow::AvatarBubbleMode)mode
         withServiceType:(signin::GAIAServiceType)serviceType {
  if (menuController_)
    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 = switches::IsNewAvatarMenu() ?
      NSMaxX([anchor bounds]) - kMenuXOffsetAdjust :
      NSMidX([anchor bounds]);
  NSPoint point = NSMakePoint(anchorX,
                              NSMaxY([anchor bounds]) - kMenuYOffsetAdjust);
  point = [anchor convertPoint:point toView:nil];
  point = [[anchor window] convertBaseToScreen:point];

  // |menuController_| will automatically release itself on close.
  if (switches::IsNewAvatarMenu()) {
    profiles::BubbleViewMode viewMode =
        profiles::BubbleViewModeFromAvatarBubbleMode(mode);
    menuController_ =
        [[ProfileChooserController alloc] initWithBrowser:browser_
                                               anchoredAt:point
                                                 withMode:viewMode
                                          withServiceType:serviceType];
  } else {
    menuController_ =
      [[AvatarMenuBubbleController alloc] initWithBrowser:browser_
                                               anchoredAt:point];
  }

  [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(bubbleWillClose:)
             name:NSWindowWillCloseNotification
           object:[menuController_ window]];
  [menuController_ showWindow:self];

  ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ICON_AVATAR_BUBBLE);
}

- (IBAction)buttonClicked:(id)sender {
  DCHECK_EQ(sender, button_.get());
  [self showAvatarBubble:button_
                withMode:BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT
         withServiceType:signin::GAIA_SERVICE_TYPE_NONE];
}

- (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