| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" | 5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/profiles/profile_attributes_entry.h" | 12 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/browser/profiles/profiles_state.h" | 14 #include "chrome/browser/profiles/profiles_state.h" |
| 15 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 16 #include "chrome/browser/ui/views/profiles/avatar_button_delegate.h" | 15 #include "chrome/browser/ui/views/profiles/avatar_button_delegate.h" |
| 17 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h" | 16 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h" |
| 18 #include "components/browser_sync/browser/profile_sync_service.h" | |
| 19 #include "components/signin/core/common/profile_management_switches.h" | 17 #include "components/signin/core/common/profile_management_switches.h" |
| 20 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/color_palette.h" | 21 #include "ui/gfx/color_palette.h" |
| 24 #include "ui/gfx/geometry/vector2d.h" | 22 #include "ui/gfx/geometry/vector2d.h" |
| 25 #include "ui/gfx/paint_vector_icon.h" | 23 #include "ui/gfx/paint_vector_icon.h" |
| 26 #include "ui/gfx/vector_icons_public.h" | 24 #include "ui/gfx/vector_icons_public.h" |
| 27 #include "ui/views/border.h" | 25 #include "ui/views/border.h" |
| 28 #include "ui/views/controls/button/label_button_border.h" | 26 #include "ui/views/controls/button/label_button_border.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 const int kTopInset = 2; | 44 const int kTopInset = 2; |
| 47 const int kBottomInset = 4; | 45 const int kBottomInset = 4; |
| 48 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, | 46 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, |
| 49 kBottomInset, kLeftRightInset)); | 47 kBottomInset, kLeftRightInset)); |
| 50 | 48 |
| 51 return std::move(border); | 49 return std::move(border); |
| 52 } | 50 } |
| 53 | 51 |
| 54 } // namespace | 52 } // namespace |
| 55 | 53 |
| 56 SyncErrorController* GetSyncErrorControllerIfNeeded(Profile* profile) { | 54 NewAvatarButton::AvatarSigninErrorObserver::AvatarSigninErrorObserver( |
| 57 if (!switches::IsMaterialDesignUserMenu()) | 55 Profile* profile, |
| 58 return nullptr; | 56 NewAvatarButton* parent_button) |
| 59 ProfileSyncService* sync_service = | 57 : AvatarSigninErrorController(profile), parent_button_(parent_button) {} |
| 60 ProfileSyncServiceFactory::GetForProfile(profile); | 58 |
| 61 return sync_service ? sync_service->sync_error_controller() : nullptr; | 59 void NewAvatarButton::AvatarSigninErrorObserver::OnErrorChanged() { |
| 60 AvatarSigninErrorController::OnErrorChanged(); |
| 61 parent_button_->Update(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 NewAvatarButton::SigninErrorObserver::SigninErrorObserver( | 64 NewAvatarButton::AvatarSyncErrorObserver::AvatarSyncErrorObserver( |
| 65 NewAvatarButton* parent_button, | 65 Profile* profile, |
| 66 Profile* profile) | 66 NewAvatarButton* parent_button) |
| 67 : parent_button_(parent_button), profile_(profile) { | 67 : AvatarSyncErrorController(profile), parent_button_(parent_button) {} |
| 68 // Subscribe to authentication error changes so that the avatar button can | |
| 69 // update itself. Note that guest mode profiles won't have a token service. | |
| 70 SigninErrorController* signin_error_controller = | |
| 71 profiles::GetSigninErrorController(profile_); | |
| 72 if (signin_error_controller) | |
| 73 signin_error_controller->AddObserver(this); | |
| 74 } | |
| 75 | 68 |
| 76 NewAvatarButton::SigninErrorObserver::~SigninErrorObserver() { | 69 void NewAvatarButton::AvatarSyncErrorObserver::OnErrorChanged() { |
| 77 SigninErrorController* signin_error_controller = | 70 AvatarSyncErrorController::OnErrorChanged(); |
| 78 profiles::GetSigninErrorController(profile_); | 71 parent_button_->Update(); |
| 79 if (signin_error_controller) | |
| 80 signin_error_controller->RemoveObserver(this); | |
| 81 } | |
| 82 | |
| 83 void NewAvatarButton::SigninErrorObserver::OnErrorChanged() { | |
| 84 parent_button_->OnErrorChanged(); | |
| 85 } | |
| 86 | |
| 87 NewAvatarButton::SyncErrorObserver::SyncErrorObserver( | |
| 88 NewAvatarButton* parent_button, | |
| 89 Profile* profile) | |
| 90 : parent_button_(parent_button), profile_(profile) { | |
| 91 SyncErrorController* sync_error_controller = | |
| 92 GetSyncErrorControllerIfNeeded(profile_); | |
| 93 if (sync_error_controller) | |
| 94 sync_error_controller->AddObserver(this); | |
| 95 } | |
| 96 | |
| 97 NewAvatarButton::SyncErrorObserver::~SyncErrorObserver() { | |
| 98 SyncErrorController* sync_error_controller = | |
| 99 GetSyncErrorControllerIfNeeded(profile_); | |
| 100 if (sync_error_controller) | |
| 101 sync_error_controller->RemoveObserver(this); | |
| 102 } | |
| 103 | |
| 104 void NewAvatarButton::SyncErrorObserver::OnErrorChanged() { | |
| 105 parent_button_->OnErrorChanged(); | |
| 106 } | 72 } |
| 107 | 73 |
| 108 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, | 74 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, |
| 109 AvatarButtonStyle button_style, | 75 AvatarButtonStyle button_style, |
| 110 Profile* profile) | 76 Profile* profile) |
| 111 : LabelButton(delegate, base::string16()), | 77 : LabelButton(delegate, base::string16()), |
| 112 signin_error_observer_(this, profile), | 78 signin_error_observer_(profile, this), |
| 113 sync_error_observer_(this, profile), | 79 sync_error_observer_(profile, this), |
| 114 delegate_(delegate), | 80 delegate_(delegate), |
| 115 profile_(profile), | 81 profile_(profile), |
| 116 has_error_(false), | |
| 117 suppress_mouse_released_action_(false) { | 82 suppress_mouse_released_action_(false) { |
| 118 set_triggerable_event_flags( | 83 set_triggerable_event_flags( |
| 119 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); | 84 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); |
| 120 set_animate_on_state_change(false); | 85 set_animate_on_state_change(false); |
| 121 SetEnabledTextColors(SK_ColorWHITE); | 86 SetEnabledTextColors(SK_ColorWHITE); |
| 122 SetTextSubpixelRenderingEnabled(false); | 87 SetTextSubpixelRenderingEnabled(false); |
| 123 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 88 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 124 | 89 |
| 125 // The largest text height that fits in the button. If the font list height | 90 // The largest text height that fits in the button. If the font list height |
| 126 // is larger than this, it will be shrunk to match it. | 91 // is larger than this, it will be shrunk to match it. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 153 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); | 118 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); |
| 154 | 119 |
| 155 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 120 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 156 generic_avatar_ = | 121 generic_avatar_ = |
| 157 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); | 122 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); |
| 158 } | 123 } |
| 159 | 124 |
| 160 g_browser_process->profile_manager()-> | 125 g_browser_process->profile_manager()-> |
| 161 GetProfileAttributesStorage().AddObserver(this); | 126 GetProfileAttributesStorage().AddObserver(this); |
| 162 | 127 |
| 163 OnErrorChanged(); | 128 signin_error_observer_.OnErrorChanged(); |
| 129 sync_error_observer_.OnErrorChanged(); |
| 130 |
| 164 SchedulePaint(); | 131 SchedulePaint(); |
| 165 } | 132 } |
| 166 | 133 |
| 167 NewAvatarButton::~NewAvatarButton() { | 134 NewAvatarButton::~NewAvatarButton() { |
| 168 g_browser_process->profile_manager()-> | 135 g_browser_process->profile_manager()-> |
| 169 GetProfileAttributesStorage().RemoveObserver(this); | 136 GetProfileAttributesStorage().RemoveObserver(this); |
| 170 } | 137 } |
| 171 | 138 |
| 172 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { | 139 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 173 // Prevent the bubble from being re-shown if it's already showing. | 140 // Prevent the bubble from being re-shown if it's already showing. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 186 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since | 153 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since |
| 187 // no other UI button based on CustomButton appears to handle mouse | 154 // no other UI button based on CustomButton appears to handle mouse |
| 188 // right-click. If other cases are identified, it may make sense to move this | 155 // right-click. If other cases are identified, it may make sense to move this |
| 189 // check to CustomButton. | 156 // check to CustomButton. |
| 190 if (event->type() == ui::ET_GESTURE_LONG_PRESS) | 157 if (event->type() == ui::ET_GESTURE_LONG_PRESS) |
| 191 NotifyClick(*event); | 158 NotifyClick(*event); |
| 192 else | 159 else |
| 193 LabelButton::OnGestureEvent(event); | 160 LabelButton::OnGestureEvent(event); |
| 194 } | 161 } |
| 195 | 162 |
| 196 void NewAvatarButton::OnErrorChanged() { | |
| 197 // If there is a signin error, show a warning icon. | |
| 198 const SigninErrorController* signin_error_controller = | |
| 199 profiles::GetSigninErrorController(profile_); | |
| 200 has_error_ = signin_error_controller && signin_error_controller->HasError(); | |
| 201 | |
| 202 // Also show a warning icon for sync errors for the material design user menu. | |
| 203 ProfileSyncService* sync_service = | |
| 204 ProfileSyncServiceFactory::GetForProfile(profile_); | |
| 205 if (switches::IsMaterialDesignUserMenu() && sync_service) { | |
| 206 SyncErrorController* sync_error_controller = | |
| 207 sync_service->sync_error_controller(); | |
| 208 ProfileSyncService::Status status; | |
| 209 sync_service->QueryDetailedSyncStatus(&status); | |
| 210 has_error_ |= | |
| 211 (sync_service->HasUnrecoverableError() || | |
| 212 status.sync_protocol_error.action == syncer::UPGRADE_CLIENT || | |
| 213 (sync_error_controller && sync_error_controller->HasError())); | |
| 214 } | |
| 215 | |
| 216 Update(); | |
| 217 } | |
| 218 | |
| 219 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { | 163 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| 220 Update(); | 164 Update(); |
| 221 } | 165 } |
| 222 | 166 |
| 223 void NewAvatarButton::OnProfileWasRemoved( | 167 void NewAvatarButton::OnProfileWasRemoved( |
| 224 const base::FilePath& profile_path, | 168 const base::FilePath& profile_path, |
| 225 const base::string16& profile_name) { | 169 const base::string16& profile_name) { |
| 226 // If deleting the active profile, don't bother updating the avatar | 170 // If deleting the active profile, don't bother updating the avatar |
| 227 // button, as the browser window is being closed anyway. | 171 // button, as the browser window is being closed anyway. |
| 228 if (profile_->GetPath() != profile_path) | 172 if (profile_->GetPath() != profile_path) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 use_generic_button | 208 use_generic_button |
| 265 ? gfx::ShadowValues() | 209 ? gfx::ShadowValues() |
| 266 : gfx::ShadowValues( | 210 : gfx::ShadowValues( |
| 267 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); | 211 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); |
| 268 | 212 |
| 269 // We want the button to resize if the new text is shorter. | 213 // We want the button to resize if the new text is shorter. |
| 270 SetMinSize(gfx::Size()); | 214 SetMinSize(gfx::Size()); |
| 271 | 215 |
| 272 if (use_generic_button) { | 216 if (use_generic_button) { |
| 273 SetImage(views::Button::STATE_NORMAL, generic_avatar_); | 217 SetImage(views::Button::STATE_NORMAL, generic_avatar_); |
| 274 } else if (has_error_) { | 218 } else if (signin_error_observer_.has_error() || |
| 219 sync_error_observer_.has_error()) { |
| 275 if (switches::IsMaterialDesignUserMenu()) { | 220 if (switches::IsMaterialDesignUserMenu()) { |
| 276 SetImage(views::Button::STATE_NORMAL, | 221 SetImage(views::Button::STATE_NORMAL, |
| 277 gfx::CreateVectorIcon(gfx::VectorIconId::SYNC_PROBLEM, 13, | 222 gfx::CreateVectorIcon(gfx::VectorIconId::SYNC_PROBLEM, 13, |
| 278 gfx::kGoogleRed700)); | 223 gfx::kGoogleRed700)); |
| 279 } else { | 224 } else { |
| 280 SetImage(views::Button::STATE_NORMAL, | 225 SetImage(views::Button::STATE_NORMAL, |
| 281 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13, | 226 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13, |
| 282 gfx::kGoogleYellow700)); | 227 gfx::kGoogleYellow700)); |
| 283 } | 228 } |
| 284 } else { | 229 } else { |
| 285 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); | 230 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
| 286 } | 231 } |
| 287 | 232 |
| 288 // If we are not using the generic button, then reset the spacing between | 233 // If we are not using the generic button, then reset the spacing between |
| 289 // the text and the possible authentication error icon. | 234 // the text and the possible authentication error icon. |
| 290 const int kDefaultImageTextSpacing = 5; | 235 const int kDefaultImageTextSpacing = 5; |
| 291 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); | 236 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
| 292 | 237 |
| 293 PreferredSizeChanged(); | 238 PreferredSizeChanged(); |
| 294 delegate_->ButtonPreferredSizeChanged(); | 239 delegate_->ButtonPreferredSizeChanged(); |
| 295 } | 240 } |
| OLD | NEW |