Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1460)

Side by Side Diff: chrome/browser/ui/views/profiles/new_avatar_button.cc

Issue 2179283002: Refactored signin/sync error controllers for the avatar button (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Prototype Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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::AvatarSyncErrorObserver::AvatarSyncErrorObserver(
57 if (!switches::IsMaterialDesignUserMenu()) 55 Profile* profile,
58 return nullptr; 56 NewAvatarButton* parent_button)
59 ProfileSyncService* sync_service = 57 : AvatarSyncError(profile), parent_button_(parent_button) {}
60 ProfileSyncServiceFactory::GetForProfile(profile); 58
61 return sync_service ? sync_service->sync_error_controller() : nullptr; 59 void NewAvatarButton::AvatarSyncErrorObserver::OnErrorChanged() {
60 AvatarSyncError::OnErrorChanged();
61 parent_button_->OnErrorChanged();
62 } 62 }
63 63
64 NewAvatarButton::SigninErrorObserver::SigninErrorObserver( 64 NewAvatarButton::SigninErrorObserver::SigninErrorObserver(
65 NewAvatarButton* parent_button, 65 NewAvatarButton* parent_button,
66 Profile* profile) 66 Profile* profile)
67 : parent_button_(parent_button), profile_(profile) { 67 : parent_button_(parent_button), profile_(profile) {
68 // Subscribe to authentication error changes so that the avatar button can 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. 69 // update itself. Note that guest mode profiles won't have a token service.
70 SigninErrorController* signin_error_controller = 70 SigninErrorController* signin_error_controller =
71 profiles::GetSigninErrorController(profile_); 71 profiles::GetSigninErrorController(profile_);
72 if (signin_error_controller) 72 if (signin_error_controller)
73 signin_error_controller->AddObserver(this); 73 signin_error_controller->AddObserver(this);
74 } 74 }
75 75
76 NewAvatarButton::SigninErrorObserver::~SigninErrorObserver() { 76 NewAvatarButton::SigninErrorObserver::~SigninErrorObserver() {
77 SigninErrorController* signin_error_controller = 77 SigninErrorController* signin_error_controller =
78 profiles::GetSigninErrorController(profile_); 78 profiles::GetSigninErrorController(profile_);
79 if (signin_error_controller) 79 if (signin_error_controller)
80 signin_error_controller->RemoveObserver(this); 80 signin_error_controller->RemoveObserver(this);
81 } 81 }
82 82
83 void NewAvatarButton::SigninErrorObserver::OnErrorChanged() { 83 void NewAvatarButton::SigninErrorObserver::OnErrorChanged() {
84 parent_button_->OnErrorChanged(); 84 parent_button_->OnErrorChanged();
85 } 85 }
86 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 }
107
108 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, 87 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate,
109 AvatarButtonStyle button_style, 88 AvatarButtonStyle button_style,
110 Profile* profile) 89 Profile* profile)
111 : LabelButton(delegate, base::string16()), 90 : LabelButton(delegate, base::string16()),
112 signin_error_observer_(this, profile), 91 signin_error_observer_(this, profile),
113 sync_error_observer_(this, profile), 92 avatar_sync_error_(profile, this),
114 delegate_(delegate), 93 delegate_(delegate),
115 profile_(profile), 94 profile_(profile),
116 has_error_(false), 95 has_signin_error_(false),
96 has_sync_error_(false),
117 suppress_mouse_released_action_(false) { 97 suppress_mouse_released_action_(false) {
118 set_triggerable_event_flags( 98 set_triggerable_event_flags(
119 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); 99 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON);
120 set_animate_on_state_change(false); 100 set_animate_on_state_change(false);
121 SetEnabledTextColors(SK_ColorWHITE); 101 SetEnabledTextColors(SK_ColorWHITE);
122 SetTextSubpixelRenderingEnabled(false); 102 SetTextSubpixelRenderingEnabled(false);
123 SetHorizontalAlignment(gfx::ALIGN_CENTER); 103 SetHorizontalAlignment(gfx::ALIGN_CENTER);
124 104
125 // The largest text height that fits in the button. If the font list height 105 // 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. 106 // is larger than this, it will be shrunk to match it.
(...skipping 26 matching lines...) Expand all
153 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); 133 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED);
154 134
155 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); 135 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
156 generic_avatar_ = 136 generic_avatar_ =
157 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); 137 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia();
158 } 138 }
159 139
160 g_browser_process->profile_manager()-> 140 g_browser_process->profile_manager()->
161 GetProfileAttributesStorage().AddObserver(this); 141 GetProfileAttributesStorage().AddObserver(this);
162 142
143 avatar_sync_error_.OnErrorChanged();
163 OnErrorChanged(); 144 OnErrorChanged();
164 SchedulePaint(); 145 SchedulePaint();
165 } 146 }
166 147
167 NewAvatarButton::~NewAvatarButton() { 148 NewAvatarButton::~NewAvatarButton() {
168 g_browser_process->profile_manager()-> 149 g_browser_process->profile_manager()->
169 GetProfileAttributesStorage().RemoveObserver(this); 150 GetProfileAttributesStorage().RemoveObserver(this);
170 } 151 }
171 152
172 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { 153 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) {
(...skipping 17 matching lines...) Expand all
190 if (event->type() == ui::ET_GESTURE_LONG_PRESS) 171 if (event->type() == ui::ET_GESTURE_LONG_PRESS)
191 NotifyClick(*event); 172 NotifyClick(*event);
192 else 173 else
193 LabelButton::OnGestureEvent(event); 174 LabelButton::OnGestureEvent(event);
194 } 175 }
195 176
196 void NewAvatarButton::OnErrorChanged() { 177 void NewAvatarButton::OnErrorChanged() {
197 // If there is a signin error, show a warning icon. 178 // If there is a signin error, show a warning icon.
198 const SigninErrorController* signin_error_controller = 179 const SigninErrorController* signin_error_controller =
199 profiles::GetSigninErrorController(profile_); 180 profiles::GetSigninErrorController(profile_);
200 has_error_ = signin_error_controller && signin_error_controller->HasError(); 181 has_signin_error_ =
182 signin_error_controller && signin_error_controller->HasError();
201 183
202 // Also show a warning icon for sync errors for the material design user menu. 184 has_sync_error_ = avatar_sync_error_.HasSyncError();
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 185
216 Update(); 186 Update();
217 } 187 }
218 188
219 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { 189 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) {
220 Update(); 190 Update();
221 } 191 }
222 192
223 void NewAvatarButton::OnProfileWasRemoved( 193 void NewAvatarButton::OnProfileWasRemoved(
224 const base::FilePath& profile_path, 194 const base::FilePath& profile_path,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 use_generic_button 234 use_generic_button
265 ? gfx::ShadowValues() 235 ? gfx::ShadowValues()
266 : gfx::ShadowValues( 236 : gfx::ShadowValues(
267 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY))); 237 10, gfx::ShadowValue(gfx::Vector2d(), 1.0f, SK_ColorDKGRAY)));
268 238
269 // We want the button to resize if the new text is shorter. 239 // We want the button to resize if the new text is shorter.
270 SetMinSize(gfx::Size()); 240 SetMinSize(gfx::Size());
271 241
272 if (use_generic_button) { 242 if (use_generic_button) {
273 SetImage(views::Button::STATE_NORMAL, generic_avatar_); 243 SetImage(views::Button::STATE_NORMAL, generic_avatar_);
274 } else if (has_error_) { 244 } else if (has_signin_error_ || has_sync_error_) {
275 if (switches::IsMaterialDesignUserMenu()) { 245 if (switches::IsMaterialDesignUserMenu()) {
276 SetImage(views::Button::STATE_NORMAL, 246 SetImage(views::Button::STATE_NORMAL,
277 gfx::CreateVectorIcon(gfx::VectorIconId::SYNC_PROBLEM, 13, 247 gfx::CreateVectorIcon(gfx::VectorIconId::SYNC_PROBLEM, 13,
278 gfx::kGoogleRed700)); 248 gfx::kGoogleRed700));
279 } else { 249 } else {
280 SetImage(views::Button::STATE_NORMAL, 250 SetImage(views::Button::STATE_NORMAL,
281 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13, 251 gfx::CreateVectorIcon(gfx::VectorIconId::WARNING, 13,
282 gfx::kGoogleYellow700)); 252 gfx::kGoogleYellow700));
283 } 253 }
284 } else { 254 } else {
285 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); 255 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia());
286 } 256 }
287 257
288 // If we are not using the generic button, then reset the spacing between 258 // If we are not using the generic button, then reset the spacing between
289 // the text and the possible authentication error icon. 259 // the text and the possible authentication error icon.
290 const int kDefaultImageTextSpacing = 5; 260 const int kDefaultImageTextSpacing = 5;
291 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); 261 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing);
292 262
293 PreferredSizeChanged(); 263 PreferredSizeChanged();
294 delegate_->ButtonPreferredSizeChanged(); 264 delegate_->ButtonPreferredSizeChanged();
295 } 265 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698