Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / profiles / profile_chooser_view.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h"
6
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/lifetime/application_lifetime.h"
11 #include "chrome/browser/prefs/incognito_mode_prefs.h"
12 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
13 #include "chrome/browser/profiles/profile_info_cache.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/profiles/profile_metrics.h"
16 #include "chrome/browser/profiles/profile_window.h"
17 #include "chrome/browser/profiles/profiles_state.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19 #include "chrome/browser/signin/signin_header_helper.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/signin/signin_promo.h"
22 #include "chrome/browser/signin/signin_ui_util.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_commands.h"
25 #include "chrome/browser/ui/browser_dialogs.h"
26 #include "chrome/browser/ui/chrome_pages.h"
27 #include "chrome/browser/ui/singleton_tabs.h"
28 #include "chrome/browser/ui/views/profiles/user_manager_view.h"
29 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
30 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/url_constants.h"
33 #include "components/signin/core/browser/mutable_profile_oauth2_token_service.h"
34 #include "components/signin/core/browser/profile_oauth2_token_service.h"
35 #include "components/signin/core/browser/signin_error_controller.h"
36 #include "components/signin/core/browser/signin_manager.h"
37 #include "components/signin/core/common/profile_management_switches.h"
38 #include "grit/chromium_strings.h"
39 #include "grit/generated_resources.h"
40 #include "grit/theme_resources.h"
41 #include "third_party/skia/include/core/SkColor.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/resource/resource_bundle.h"
44 #include "ui/gfx/canvas.h"
45 #include "ui/gfx/image/image.h"
46 #include "ui/gfx/image/image_skia.h"
47 #include "ui/gfx/path.h"
48 #include "ui/gfx/skia_util.h"
49 #include "ui/gfx/text_elider.h"
50 #include "ui/native_theme/native_theme.h"
51 #include "ui/views/controls/button/blue_button.h"
52 #include "ui/views/controls/button/image_button.h"
53 #include "ui/views/controls/button/label_button.h"
54 #include "ui/views/controls/button/menu_button.h"
55 #include "ui/views/controls/label.h"
56 #include "ui/views/controls/link.h"
57 #include "ui/views/controls/separator.h"
58 #include "ui/views/controls/styled_label.h"
59 #include "ui/views/controls/textfield/textfield.h"
60 #include "ui/views/controls/webview/webview.h"
61 #include "ui/views/layout/grid_layout.h"
62 #include "ui/views/layout/layout_constants.h"
63 #include "ui/views/widget/widget.h"
64
65 namespace {
66
67 // Helpers --------------------------------------------------------------------
68
69 const int kFixedMenuWidth = 250;
70 const int kButtonHeight = 32;
71 const int kFixedGaiaViewHeight = 440;
72 const int kFixedGaiaViewWidth = 360;
73 const int kFixedAccountRemovalViewWidth = 280;
74 const int kFixedSwitchUserViewWidth = 320;
75 const int kLargeImageSide = 88;
76
77 const int kVerticalSpacing = 16;
78
79 // Creates a GridLayout with a single column. This ensures that all the child
80 // views added get auto-expanded to fill the full width of the bubble.
81 views::GridLayout* CreateSingleColumnLayout(views::View* view, int width) {
82   views::GridLayout* layout = new views::GridLayout(view);
83   view->SetLayoutManager(layout);
84
85   views::ColumnSet* columns = layout->AddColumnSet(0);
86   columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0,
87                      views::GridLayout::FIXED, width, width);
88   return layout;
89 }
90
91 views::Link* CreateLink(const base::string16& link_text,
92                         views::LinkListener* listener) {
93   views::Link* link_button = new views::Link(link_text);
94   link_button->SetHorizontalAlignment(gfx::ALIGN_LEFT);
95   link_button->SetUnderline(false);
96   link_button->set_listener(listener);
97   return link_button;
98 }
99
100 gfx::ImageSkia CreateSquarePlaceholderImage(int size) {
101   SkBitmap bitmap;
102   bitmap.allocPixels(SkImageInfo::MakeA8(size, size));
103   bitmap.eraseARGB(0, 0, 0, 0);
104   return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
105 }
106
107 bool HasAuthError(Profile* profile) {
108   const SigninErrorController* error =
109       profiles::GetSigninErrorController(profile);
110   return error && error->HasError();
111 }
112
113 std::string GetAuthErrorAccountId(Profile* profile) {
114   const SigninErrorController* error =
115       profiles::GetSigninErrorController(profile);
116   if (!error)
117     return std::string();
118
119   return error->error_account_id();
120 }
121
122 std::string GetAuthErrorUsername(Profile* profile) {
123   const SigninErrorController* error =
124       profiles::GetSigninErrorController(profile);
125   if (!error)
126     return std::string();
127
128   return error->error_username();
129 }
130
131 // BackgroundColorHoverButton -------------------------------------------------
132
133 // A custom button that allows for setting a background color when hovered over.
134 class BackgroundColorHoverButton : public views::LabelButton {
135  public:
136   BackgroundColorHoverButton(views::ButtonListener* listener,
137                              const base::string16& text,
138                              const gfx::ImageSkia& icon)
139       : views::LabelButton(listener, text) {
140     SetImageLabelSpacing(views::kItemLabelSpacing);
141     SetBorder(views::Border::CreateEmptyBorder(
142         0, views::kButtonHEdgeMarginNew, 0, views::kButtonHEdgeMarginNew));
143     SetMinSize(gfx::Size(0,
144         kButtonHeight + views::kRelatedControlVerticalSpacing));
145     SetImage(STATE_NORMAL, icon);
146     SetFocusable(true);
147   }
148
149   virtual ~BackgroundColorHoverButton() {}
150
151  private:
152   // views::LabelButton:
153   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
154     if ((state() == STATE_PRESSED) ||
155         (state() == STATE_HOVERED) ||
156         HasFocus()) {
157       canvas->DrawColor(GetNativeTheme()->GetSystemColor(
158           ui::NativeTheme::kColorId_ButtonHoverBackgroundColor));
159     }
160     LabelButton::OnPaint(canvas);
161   }
162
163   DISALLOW_COPY_AND_ASSIGN(BackgroundColorHoverButton);
164 };
165
166 // SizedContainer -------------------------------------------------
167
168 // A simple container view that takes an explicit preferred size.
169 class SizedContainer : public views::View {
170  public:
171   explicit SizedContainer(const gfx::Size& preferred_size)
172       : preferred_size_(preferred_size) {}
173
174   virtual gfx::Size GetPreferredSize() const OVERRIDE {
175     return preferred_size_;
176   }
177
178  private:
179   gfx::Size preferred_size_;
180 };
181
182 }  // namespace
183
184 // RightAlignedIconLabelButton -------------------------------------------------
185
186 // A custom LabelButton that has a centered text and right aligned icon.
187 class RightAlignedIconLabelButton : public views::LabelButton {
188  public:
189   RightAlignedIconLabelButton(views::ButtonListener* listener,
190                               const base::string16& text)
191       : views::LabelButton(listener, text) {
192   }
193
194  protected:
195   virtual void Layout() OVERRIDE {
196     // This layout trick keeps the text left-aligned and the icon right-aligned.
197     SetHorizontalAlignment(gfx::ALIGN_RIGHT);
198     views::LabelButton::Layout();
199     label()->SetHorizontalAlignment(gfx::ALIGN_CENTER);
200   }
201
202   DISALLOW_COPY_AND_ASSIGN(RightAlignedIconLabelButton);
203 };
204
205 // EditableProfilePhoto -------------------------------------------------
206
207 // A custom Image control that shows a "change" button when moused over.
208 class EditableProfilePhoto : public views::LabelButton {
209  public:
210   EditableProfilePhoto(views::ButtonListener* listener,
211                        const gfx::Image& icon,
212                        bool is_editing_allowed,
213                        const gfx::Rect& bounds)
214       : views::LabelButton(listener, base::string16()),
215         photo_overlay_(NULL) {
216     gfx::Image image = profiles::GetSizedAvatarIcon(
217         icon, true, kLargeImageSide, kLargeImageSide);
218     SetImage(views::LabelButton::STATE_NORMAL, *image.ToImageSkia());
219     SetBorder(views::Border::NullBorder());
220     SetBoundsRect(bounds);
221
222     // Calculate the circular mask that will be used to display the photo.
223     circular_mask_.addCircle(SkIntToScalar(bounds.width() / 2),
224                              SkIntToScalar(bounds.height() / 2),
225                              SkIntToScalar(bounds.width() / 2));
226
227     if (!is_editing_allowed) {
228       SetEnabled(false);
229       return;
230     }
231
232     SetFocusable(true);
233     set_notify_enter_exit_on_child(true);
234
235     // Photo overlay that appears when hovering over the button.
236     photo_overlay_ = new views::ImageView();
237
238     const SkColor kBackgroundColor = SkColorSetARGB(65, 255, 255, 255);
239     photo_overlay_->set_background(
240         views::Background::CreateSolidBackground(kBackgroundColor));
241     photo_overlay_->SetImage(*ui::ResourceBundle::GetSharedInstance().
242         GetImageSkiaNamed(IDR_ICON_PROFILES_EDIT_CAMERA));
243
244     photo_overlay_->SetSize(bounds.size());
245     photo_overlay_->SetVisible(false);
246     AddChildView(photo_overlay_);
247   }
248
249   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
250     // Display the profile picture as a circle.
251     canvas->ClipPath(circular_mask_, true);
252     views::LabelButton::OnPaint(canvas);
253   }
254
255   virtual void PaintChildren(gfx::Canvas* canvas,
256                              const views::CullSet& cull_set) OVERRIDE {
257     // Display any children (the "change photo" overlay) as a circle.
258     canvas->ClipPath(circular_mask_, true);
259     View::PaintChildren(canvas, cull_set);
260   }
261
262  private:
263   // views::CustomButton:
264   virtual void StateChanged() OVERRIDE {
265     bool show_overlay =
266         (state() == STATE_PRESSED || state() == STATE_HOVERED || HasFocus());
267     if (photo_overlay_)
268       photo_overlay_->SetVisible(show_overlay);
269   }
270
271   virtual void OnFocus() OVERRIDE {
272     views::LabelButton::OnFocus();
273     if (photo_overlay_)
274       photo_overlay_->SetVisible(true);
275   }
276
277   virtual void OnBlur() OVERRIDE {
278     views::LabelButton::OnBlur();
279     // Don't hide the overlay if it's being shown as a result of a mouseover.
280     if (photo_overlay_ && state() != STATE_HOVERED)
281       photo_overlay_->SetVisible(false);
282   }
283
284   gfx::Path circular_mask_;
285
286   // Image that is shown when hovering over the image button. Can be NULL if
287   // the photo isn't allowed to be edited (e.g. for guest profiles).
288   views::ImageView* photo_overlay_;
289
290   DISALLOW_COPY_AND_ASSIGN(EditableProfilePhoto);
291 };
292
293 // EditableProfileName -------------------------------------------------
294
295 // A custom text control that turns into a textfield for editing when clicked.
296 class EditableProfileName : public RightAlignedIconLabelButton,
297                             public views::ButtonListener {
298  public:
299   EditableProfileName(views::TextfieldController* controller,
300                       const base::string16& text,
301                       bool is_editing_allowed)
302       : RightAlignedIconLabelButton(this, text),
303         profile_name_textfield_(NULL) {
304     ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
305     const gfx::FontList& medium_font_list =
306         rb->GetFontList(ui::ResourceBundle::MediumFont);
307     SetFontList(medium_font_list);
308     SetHorizontalAlignment(gfx::ALIGN_CENTER);
309
310     if (!is_editing_allowed) {
311       SetBorder(views::Border::CreateEmptyBorder(2, 0, 2, 0));
312       return;
313     }
314
315     SetFocusable(true);
316     // Show an "edit" pencil icon when hovering over. In the default state,
317     // we need to create an empty placeholder of the correct size, so that
318     // the text doesn't jump around when the hovered icon appears.
319     gfx::ImageSkia hover_image =
320         *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_EDIT_HOVER);
321     SetImage(STATE_NORMAL, CreateSquarePlaceholderImage(hover_image.width()));
322     SetImage(STATE_HOVERED, hover_image);
323     SetImage(STATE_PRESSED,
324              *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_EDIT_PRESSED));
325     // To center the text, we need to offest it by the width of the icon we
326     // are adding and its padding. We need to also add a small top/bottom
327     // padding to account for the textfield's border.
328     const int kIconTextLabelButtonSpacing = 5;
329     SetBorder(views::Border::CreateEmptyBorder(
330         2, hover_image.width() + kIconTextLabelButtonSpacing, 2, 0));
331
332     // Textfield that overlaps the button.
333     profile_name_textfield_ = new views::Textfield();
334     profile_name_textfield_->set_controller(controller);
335     profile_name_textfield_->SetFontList(medium_font_list);
336     profile_name_textfield_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
337
338     profile_name_textfield_->SetVisible(false);
339     AddChildView(profile_name_textfield_);
340   }
341
342   views::Textfield* profile_name_textfield() {
343     return profile_name_textfield_;
344   }
345
346   // Hide the editable textfield to show the profile name button instead.
347   void ShowReadOnlyView() {
348     if (profile_name_textfield_)
349       profile_name_textfield_->SetVisible(false);
350   }
351
352  private:
353   // views::ButtonListener:
354   virtual void ButtonPressed(views::Button* sender,
355                             const ui::Event& event) OVERRIDE {
356     if (profile_name_textfield_) {
357       profile_name_textfield_->SetVisible(true);
358       profile_name_textfield_->SetText(GetText());
359       profile_name_textfield_->SelectAll(false);
360       profile_name_textfield_->RequestFocus();
361     }
362   }
363
364   // views::LabelButton:
365   virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE {
366     // Override CustomButton's implementation, which presses the button when
367     // you press space and clicks it when you release space, as the space can be
368     // part of the new profile name typed in the textfield.
369     return false;
370   }
371
372   virtual void Layout() OVERRIDE {
373     if (profile_name_textfield_)
374       profile_name_textfield_->SetBounds(0, 0, width(), height());
375     RightAlignedIconLabelButton::Layout();
376   }
377
378   virtual void OnFocus() OVERRIDE {
379     RightAlignedIconLabelButton::OnFocus();
380     SetState(STATE_HOVERED);
381   }
382
383   virtual void OnBlur() OVERRIDE {
384     RightAlignedIconLabelButton::OnBlur();
385     SetState(STATE_NORMAL);
386   }
387
388   // Textfield that is shown when editing the profile name. Can be NULL if
389   // the profile name isn't allowed to be edited (e.g. for guest profiles).
390   views::Textfield* profile_name_textfield_;
391
392   DISALLOW_COPY_AND_ASSIGN(EditableProfileName);
393 };
394
395 // A title card with one back button right aligned and one label center aligned.
396 class TitleCard : public views::View {
397  public:
398   TitleCard(const base::string16& message, views::ButtonListener* listener,
399             views::ImageButton** back_button) {
400     back_button_ = new views::ImageButton(listener);
401     back_button_->SetImageAlignment(views::ImageButton::ALIGN_LEFT,
402                                     views::ImageButton::ALIGN_MIDDLE);
403     ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
404     back_button_->SetImage(views::ImageButton::STATE_NORMAL,
405                            rb->GetImageSkiaNamed(IDR_BACK));
406     back_button_->SetImage(views::ImageButton::STATE_HOVERED,
407                            rb->GetImageSkiaNamed(IDR_BACK_H));
408     back_button_->SetImage(views::ImageButton::STATE_PRESSED,
409                            rb->GetImageSkiaNamed(IDR_BACK_P));
410     back_button_->SetImage(views::ImageButton::STATE_DISABLED,
411                            rb->GetImageSkiaNamed(IDR_BACK_D));
412     back_button_->SetFocusable(true);
413     *back_button = back_button_;
414
415     title_label_ = new views::Label(message);
416     title_label_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
417     const gfx::FontList& medium_font_list =
418         rb->GetFontList(ui::ResourceBundle::MediumFont);
419     title_label_->SetFontList(medium_font_list);
420
421     AddChildView(back_button_);
422     AddChildView(title_label_);
423   }
424
425   // Creates a new view that has the |title_card| with horizontal padding at the
426   // top, an edge-to-edge separator below, and the specified |view| at the
427   // bottom.
428   static views::View* AddPaddedTitleCard(views::View* view,
429                                          TitleCard* title_card,
430                                          int width) {
431     views::View* titled_view = new views::View();
432     views::GridLayout* layout = new views::GridLayout(titled_view);
433     titled_view->SetLayoutManager(layout);
434
435     // Column set 0 is a single column layout with horizontal padding at left
436     // and right, and column set 1 is a single column layout with no padding.
437     views::ColumnSet* columns = layout->AddColumnSet(0);
438     columns->AddPaddingColumn(1, views::kButtonHEdgeMarginNew);
439     int available_width = width - 2 * views::kButtonHEdgeMarginNew;
440     columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0,
441         views::GridLayout::FIXED, available_width, available_width);
442     columns->AddPaddingColumn(1, views::kButtonHEdgeMarginNew);
443     layout->AddColumnSet(1)->AddColumn(views::GridLayout::FILL,
444         views::GridLayout::FILL, 0,views::GridLayout::FIXED, width, width);
445
446     layout->StartRowWithPadding(1, 0, 0, kVerticalSpacing);
447     layout->AddView(title_card);
448     layout->StartRowWithPadding(1, 1, 0, kVerticalSpacing);
449     layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
450
451     layout->StartRow(1, 1);
452     layout->AddView(view);
453
454     return titled_view;
455   }
456
457  private:
458   virtual void Layout() OVERRIDE{
459     int back_button_width = back_button_->GetPreferredSize().width();
460     back_button_->SetBounds(0, 0, back_button_width, height());
461     int label_padding = back_button_width + views::kButtonHEdgeMarginNew;
462     int label_width = width() - 2 * label_padding;
463     DCHECK_GT(label_width, 0);
464     title_label_->SetBounds(label_padding, 0, label_width, height());
465   }
466
467   virtual gfx::Size GetPreferredSize() const OVERRIDE{
468     int height = std::max(title_label_->GetPreferredSize().height(),
469         back_button_->GetPreferredSize().height());
470     return gfx::Size(width(), height);
471   }
472
473   views::ImageButton* back_button_;
474   views::Label* title_label_;
475
476   DISALLOW_COPY_AND_ASSIGN(TitleCard);
477 };
478
479 // ProfileChooserView ---------------------------------------------------------
480
481 // static
482 ProfileChooserView* ProfileChooserView::profile_bubble_ = NULL;
483 bool ProfileChooserView::close_on_deactivate_for_testing_ = true;
484
485 // static
486 void ProfileChooserView::ShowBubble(
487     profiles::BubbleViewMode view_mode,
488     profiles::TutorialMode tutorial_mode,
489     const signin::ManageAccountsParams& manage_accounts_params,
490     views::View* anchor_view,
491     views::BubbleBorder::Arrow arrow,
492     views::BubbleBorder::BubbleAlignment border_alignment,
493     Browser* browser) {
494   if (IsShowing()) {
495     if (tutorial_mode != profiles::TUTORIAL_MODE_NONE) {
496       profile_bubble_->tutorial_mode_ = tutorial_mode;
497       profile_bubble_->ShowView(view_mode, profile_bubble_->avatar_menu_.get());
498     }
499     return;
500   }
501
502   profile_bubble_ = new ProfileChooserView(anchor_view, arrow, browser,
503       view_mode, tutorial_mode, manage_accounts_params.service_type);
504   views::BubbleDelegateView::CreateBubble(profile_bubble_);
505   profile_bubble_->set_close_on_deactivate(close_on_deactivate_for_testing_);
506   profile_bubble_->SetAlignment(border_alignment);
507   profile_bubble_->GetWidget()->Show();
508   profile_bubble_->SetArrowPaintType(views::BubbleBorder::PAINT_NONE);
509 }
510
511 // static
512 bool ProfileChooserView::IsShowing() {
513   return profile_bubble_ != NULL;
514 }
515
516 // static
517 void ProfileChooserView::Hide() {
518   if (IsShowing())
519     profile_bubble_->GetWidget()->Close();
520 }
521
522 ProfileChooserView::ProfileChooserView(views::View* anchor_view,
523                                        views::BubbleBorder::Arrow arrow,
524                                        Browser* browser,
525                                        profiles::BubbleViewMode view_mode,
526                                        profiles::TutorialMode tutorial_mode,
527                                        signin::GAIAServiceType service_type)
528     : BubbleDelegateView(anchor_view, arrow),
529       browser_(browser),
530       view_mode_(view_mode),
531       tutorial_mode_(tutorial_mode),
532       gaia_service_type_(service_type) {
533   // Reset the default margins inherited from the BubbleDelegateView.
534   // Add a small bottom inset so that the bubble's rounded corners show up.
535   set_margins(gfx::Insets(0, 0, 1, 0));
536   set_background(views::Background::CreateSolidBackground(
537       GetNativeTheme()->GetSystemColor(
538           ui::NativeTheme::kColorId_DialogBackground)));
539   ResetView();
540
541   avatar_menu_.reset(new AvatarMenu(
542       &g_browser_process->profile_manager()->GetProfileInfoCache(),
543       this,
544       browser_));
545   avatar_menu_->RebuildMenu();
546
547   ProfileOAuth2TokenService* oauth2_token_service =
548       ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile());
549   if (oauth2_token_service)
550     oauth2_token_service->AddObserver(this);
551 }
552
553 ProfileChooserView::~ProfileChooserView() {
554   ProfileOAuth2TokenService* oauth2_token_service =
555       ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile());
556   if (oauth2_token_service)
557     oauth2_token_service->RemoveObserver(this);
558 }
559
560 void ProfileChooserView::ResetView() {
561   open_other_profile_indexes_map_.clear();
562   delete_account_button_map_.clear();
563   reauth_account_button_map_.clear();
564   manage_accounts_link_ = NULL;
565   signin_current_profile_link_ = NULL;
566   auth_error_email_button_ = NULL;
567   current_profile_photo_ = NULL;
568   current_profile_name_ = NULL;
569   users_button_ = NULL;
570   go_incognito_button_ = NULL;
571   lock_button_ = NULL;
572   add_account_link_ = NULL;
573   gaia_signin_cancel_button_ = NULL;
574   remove_account_button_ = NULL;
575   account_removal_cancel_button_ = NULL;
576   add_person_button_ = NULL;
577   disconnect_button_ = NULL;
578   switch_user_cancel_button_ = NULL;
579   tutorial_sync_settings_ok_button_ = NULL;
580   tutorial_close_button_ = NULL;
581   tutorial_sync_settings_link_ = NULL;
582   tutorial_see_whats_new_button_ = NULL;
583   tutorial_not_you_link_ = NULL;
584   tutorial_learn_more_link_ = NULL;
585 }
586
587 void ProfileChooserView::Init() {
588   // If view mode is PROFILE_CHOOSER but there is an auth error, force
589   // ACCOUNT_MANAGEMENT mode.
590   if (view_mode_ == profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER &&
591       HasAuthError(browser_->profile()) &&
592       switches::IsEnableAccountConsistency() &&
593       avatar_menu_->GetItemAt(avatar_menu_->GetActiveProfileIndex()).
594           signed_in) {
595     view_mode_ = profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT;
596   }
597
598   ShowView(view_mode_, avatar_menu_.get());
599 }
600
601 void ProfileChooserView::OnAvatarMenuChanged(
602     AvatarMenu* avatar_menu) {
603   // Do not refresh the avatar menu if the user is on a signin related view.
604   if (view_mode_ == profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN ||
605       view_mode_ == profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT ||
606       view_mode_ == profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH) {
607     return;
608   }
609
610   // Refresh the view with the new menu. We can't just update the local copy
611   // as this may have been triggered by a sign out action, in which case
612   // the view is being destroyed.
613   ShowView(profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER, avatar_menu);
614 }
615
616 void ProfileChooserView::OnRefreshTokenAvailable(
617     const std::string& account_id) {
618   if (view_mode_ == profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT ||
619       view_mode_ == profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT ||
620       view_mode_ == profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH) {
621     // The account management UI is only available through the
622     // --enable-account-consistency flag.
623     ShowView(switches::IsEnableAccountConsistency() ?
624         profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT :
625         profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER, avatar_menu_.get());
626   }
627 }
628
629 void ProfileChooserView::OnRefreshTokenRevoked(const std::string& account_id) {
630   // Refresh the account management view when an account is removed from the
631   // profile.
632   if (view_mode_ == profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT)
633     ShowView(profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT, avatar_menu_.get());
634 }
635
636 void ProfileChooserView::ShowView(profiles::BubbleViewMode view_to_display,
637                                   AvatarMenu* avatar_menu) {
638   // The account management view should only be displayed if the active profile
639   // is signed in.
640   if (view_to_display == profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT) {
641     DCHECK(switches::IsEnableAccountConsistency());
642     const AvatarMenu::Item& active_item = avatar_menu->GetItemAt(
643         avatar_menu->GetActiveProfileIndex());
644     DCHECK(active_item.signed_in);
645   }
646
647   if (browser_->profile()->IsSupervised() &&
648       (view_to_display == profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT ||
649        view_to_display == profiles::BUBBLE_VIEW_MODE_ACCOUNT_REMOVAL)) {
650     LOG(WARNING) << "Supervised user attempted to add/remove account";
651     return;
652   }
653
654   ResetView();
655   RemoveAllChildViews(true);
656   view_mode_ = view_to_display;
657
658   views::GridLayout* layout;
659   views::View* sub_view;
660   switch (view_mode_) {
661     case profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN:
662     case profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT:
663     case profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH:
664       layout = CreateSingleColumnLayout(this, kFixedGaiaViewWidth);
665       sub_view = CreateGaiaSigninView();
666       break;
667     case profiles::BUBBLE_VIEW_MODE_ACCOUNT_REMOVAL:
668       layout = CreateSingleColumnLayout(this, kFixedAccountRemovalViewWidth);
669       sub_view = CreateAccountRemovalView();
670       break;
671     case profiles::BUBBLE_VIEW_MODE_SWITCH_USER:
672       layout = CreateSingleColumnLayout(this, kFixedSwitchUserViewWidth);
673       sub_view = CreateSwitchUserView();
674       ProfileMetrics::LogProfileNewAvatarMenuNotYou(
675           ProfileMetrics::PROFILE_AVATAR_MENU_NOT_YOU_VIEW);
676       break;
677     default:
678       layout = CreateSingleColumnLayout(this, kFixedMenuWidth);
679       sub_view = CreateProfileChooserView(avatar_menu);
680   }
681   // Clears tutorial mode for all non-profile-chooser views.
682   if (view_mode_ != profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER)
683     tutorial_mode_ = profiles::TUTORIAL_MODE_NONE;
684
685   layout->StartRow(1, 0);
686   layout->AddView(sub_view);
687   Layout();
688   if (GetBubbleFrameView())
689     SizeToContents();
690 }
691
692 void ProfileChooserView::WindowClosing() {
693   DCHECK_EQ(profile_bubble_, this);
694   profile_bubble_ = NULL;
695
696   if (tutorial_mode_ == profiles::TUTORIAL_MODE_CONFIRM_SIGNIN) {
697     LoginUIServiceFactory::GetForProfile(browser_->profile())->
698         SyncConfirmationUIClosed(false /* configure_sync_first */);
699   }
700 }
701
702 void ProfileChooserView::ButtonPressed(views::Button* sender,
703                                        const ui::Event& event) {
704   // Disable button after clicking so that it doesn't get clicked twice and
705   // start a second action... which can crash Chrome.  But don't disable if it
706   // has no parent (like in tests) because that will also crash.
707   if (sender->parent())
708     sender->SetEnabled(false);
709
710   if (sender == users_button_) {
711     // If this is a guest session, also close all the guest browser windows.
712     if (browser_->profile()->IsGuestSession()) {
713       chrome::ShowUserManager(base::FilePath());
714       profiles::CloseGuestProfileWindows();
715     } else {
716       chrome::ShowUserManager(browser_->profile()->GetPath());
717     }
718     PostActionPerformed(ProfileMetrics::PROFILE_DESKTOP_MENU_OPEN_USER_MANAGER);
719   } else if (sender == go_incognito_button_) {
720     DCHECK(ShouldShowGoIncognito());
721     chrome::NewIncognitoWindow(browser_);
722   } else if (sender == lock_button_) {
723     profiles::LockProfile(browser_->profile());
724     PostActionPerformed(ProfileMetrics::PROFILE_DESKTOP_MENU_LOCK);
725   } else if (sender == auth_error_email_button_) {
726     ShowView(profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH, avatar_menu_.get());
727   } else if (sender == tutorial_sync_settings_ok_button_) {
728     LoginUIServiceFactory::GetForProfile(browser_->profile())->
729         SyncConfirmationUIClosed(false /* configure_sync_first */);
730     DismissTutorial();
731     ProfileMetrics::LogProfileNewAvatarMenuSignin(
732         ProfileMetrics::PROFILE_AVATAR_MENU_SIGNIN_OK);
733   } else if (sender == tutorial_close_button_) {
734     DCHECK(tutorial_mode_ != profiles::TUTORIAL_MODE_NONE &&
735            tutorial_mode_ != profiles::TUTORIAL_MODE_CONFIRM_SIGNIN);
736     DismissTutorial();
737   } else if (sender == tutorial_see_whats_new_button_) {
738     ProfileMetrics::LogProfileNewAvatarMenuUpgrade(
739         ProfileMetrics::PROFILE_AVATAR_MENU_UPGRADE_WHATS_NEW);
740     chrome::ShowUserManagerWithTutorial(
741         profiles::USER_MANAGER_TUTORIAL_OVERVIEW);
742   } else if (sender == remove_account_button_) {
743     RemoveAccount();
744   } else if (sender == account_removal_cancel_button_) {
745     account_id_to_remove_.clear();
746     ShowView(profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT, avatar_menu_.get());
747   } else if (sender == gaia_signin_cancel_button_) {
748     std::string primary_account =
749         SigninManagerFactory::GetForProfile(browser_->profile())->
750         GetAuthenticatedUsername();
751     // The account management view is only available with the
752     // --enable-account-consistency flag.
753     bool account_management_available = !primary_account.empty() &&
754         switches::IsEnableAccountConsistency();
755     ShowView(account_management_available ?
756         profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT :
757         profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER, avatar_menu_.get());
758   } else if (sender == current_profile_photo_) {
759     avatar_menu_->EditProfile(avatar_menu_->GetActiveProfileIndex());
760     PostActionPerformed(ProfileMetrics::PROFILE_DESKTOP_MENU_EDIT_IMAGE);
761   } else if (sender == signin_current_profile_link_) {
762     ShowView(profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN, avatar_menu_.get());
763   } else if (sender == add_person_button_) {
764     ProfileMetrics::LogProfileNewAvatarMenuNotYou(
765         ProfileMetrics::PROFILE_AVATAR_MENU_NOT_YOU_ADD_PERSON);
766     chrome::ShowUserManager(browser_->profile()->GetPath());
767   } else if (sender == disconnect_button_) {
768     ProfileMetrics::LogProfileNewAvatarMenuNotYou(
769         ProfileMetrics::PROFILE_AVATAR_MENU_NOT_YOU_DISCONNECT);
770     chrome::ShowSettings(browser_);
771   } else if (sender == switch_user_cancel_button_) {
772     ShowView(profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER, avatar_menu_.get());
773     ProfileMetrics::LogProfileNewAvatarMenuNotYou(
774         ProfileMetrics::PROFILE_AVATAR_MENU_NOT_YOU_BACK);
775   } else {
776     // Either one of the "other profiles", or one of the profile accounts
777     // buttons was pressed.
778     ButtonIndexes::const_iterator profile_match =
779         open_other_profile_indexes_map_.find(sender);
780     if (profile_match != open_other_profile_indexes_map_.end()) {
781       avatar_menu_->SwitchToProfile(
782           profile_match->second,
783           ui::DispositionFromEventFlags(event.flags()) == NEW_WINDOW,
784           ProfileMetrics::SWITCH_PROFILE_ICON);
785     } else {
786       // This was a profile accounts button.
787       AccountButtonIndexes::const_iterator account_match =
788           delete_account_button_map_.find(sender);
789       if (account_match != delete_account_button_map_.end()) {
790         account_id_to_remove_ = account_match->second;
791         ShowView(profiles::BUBBLE_VIEW_MODE_ACCOUNT_REMOVAL,
792             avatar_menu_.get());
793       } else {
794         account_match = reauth_account_button_map_.find(sender);
795         DCHECK(account_match != reauth_account_button_map_.end());
796         ShowView(profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH, avatar_menu_.get());
797       }
798     }
799   }
800 }
801
802 void ProfileChooserView::RemoveAccount() {
803   DCHECK(!account_id_to_remove_.empty());
804   MutableProfileOAuth2TokenService* oauth2_token_service =
805       ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(
806       browser_->profile());
807   if (oauth2_token_service) {
808     oauth2_token_service->RevokeCredentials(account_id_to_remove_);
809     PostActionPerformed(ProfileMetrics::PROFILE_DESKTOP_MENU_REMOVE_ACCT);
810   }
811   account_id_to_remove_.clear();
812
813   ShowView(profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT, avatar_menu_.get());
814 }
815
816 void ProfileChooserView::LinkClicked(views::Link* sender, int event_flags) {
817   if (sender == manage_accounts_link_) {
818     // This link can either mean show/hide the account management view,
819     // depending on which view it is displayed. ShowView() will DCHECK if
820     // the account management view is displayed for non signed-in users.
821     ShowView(
822         view_mode_ == profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT ?
823             profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER :
824             profiles::BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT,
825         avatar_menu_.get());
826   } else if (sender == add_account_link_) {
827     ShowView(profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT, avatar_menu_.get());
828     PostActionPerformed(ProfileMetrics::PROFILE_DESKTOP_MENU_ADD_ACCT);
829   } else if (sender == tutorial_sync_settings_link_) {
830     LoginUIServiceFactory::GetForProfile(browser_->profile())->
831         SyncConfirmationUIClosed(true /* configure_sync_first */);
832     tutorial_mode_ = profiles::TUTORIAL_MODE_NONE;
833     ProfileMetrics::LogProfileNewAvatarMenuSignin(
834         ProfileMetrics::PROFILE_AVATAR_MENU_SIGNIN_SETTINGS);
835   } else if (sender == tutorial_not_you_link_){
836     ProfileMetrics::LogProfileNewAvatarMenuUpgrade(
837         ProfileMetrics::PROFILE_AVATAR_MENU_UPGRADE_NOT_YOU);
838     ShowView(profiles::BUBBLE_VIEW_MODE_SWITCH_USER, avatar_menu_.get());
839   } else {
840     DCHECK(sender == tutorial_learn_more_link_);
841     signin_ui_util::ShowSigninErrorLearnMorePage(browser_->profile());
842   }
843 }
844
845 void ProfileChooserView::StyledLabelLinkClicked(
846     const gfx::Range& range, int event_flags) {
847   chrome::ShowSettings(browser_);
848 }
849
850 bool ProfileChooserView::HandleKeyEvent(views::Textfield* sender,
851                                         const ui::KeyEvent& key_event) {
852   views::Textfield* name_textfield =
853       current_profile_name_->profile_name_textfield();
854   DCHECK(sender == name_textfield);
855
856   if (key_event.key_code() == ui::VKEY_RETURN ||
857       key_event.key_code() == ui::VKEY_TAB) {
858     // Pressing Tab/Enter commits the new profile name, unless it's empty.
859     base::string16 new_profile_name = name_textfield->text();
860     base::TrimWhitespace(new_profile_name, base::TRIM_ALL, &new_profile_name);
861     if (new_profile_name.empty())
862       return true;
863
864     const AvatarMenu::Item& active_item = avatar_menu_->GetItemAt(
865         avatar_menu_->GetActiveProfileIndex());
866     Profile* profile = g_browser_process->profile_manager()->GetProfile(
867         active_item.profile_path);
868     DCHECK(profile);
869
870     if (profile->IsSupervised())
871       return true;
872
873     profiles::UpdateProfileName(profile, new_profile_name);
874     PostActionPerformed(ProfileMetrics::PROFILE_DESKTOP_MENU_EDIT_NAME);
875     current_profile_name_->ShowReadOnlyView();
876     return true;
877   }
878   return false;
879 }
880
881 views::View* ProfileChooserView::CreateProfileChooserView(
882     AvatarMenu* avatar_menu) {
883   views::View* view = new views::View();
884   views::GridLayout* layout = CreateSingleColumnLayout(view, kFixedMenuWidth);
885   // Separate items into active and alternatives.
886   Indexes other_profiles;
887   views::View* tutorial_view = NULL;
888   views::View* current_profile_view = NULL;
889   views::View* current_profile_accounts = NULL;
890   views::View* option_buttons_view = NULL;
891   for (size_t i = 0; i < avatar_menu->GetNumberOfItems(); ++i) {
892     const AvatarMenu::Item& item = avatar_menu->GetItemAt(i);
893     if (item.active) {
894       option_buttons_view = CreateOptionsView(
895           switches::IsNewProfileManagement() && item.signed_in);
896       current_profile_view = CreateCurrentProfileView(item, false);
897       if (view_mode_ == profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER) {
898         switch (tutorial_mode_) {
899           case profiles::TUTORIAL_MODE_NONE:
900           case profiles::TUTORIAL_MODE_WELCOME_UPGRADE:
901             tutorial_view = CreateWelcomeUpgradeTutorialViewIfNeeded(
902                 tutorial_mode_ == profiles::TUTORIAL_MODE_WELCOME_UPGRADE,
903                 item);
904             break;
905           case profiles::TUTORIAL_MODE_CONFIRM_SIGNIN:
906             tutorial_view = CreateSigninConfirmationView();
907             break;
908           case profiles::TUTORIAL_MODE_SHOW_ERROR:
909             tutorial_view = CreateSigninErrorView();
910             break;
911         }
912       } else {
913         current_profile_accounts = CreateCurrentProfileAccountsView(item);
914       }
915     } else {
916       other_profiles.push_back(i);
917     }
918   }
919
920   if (tutorial_view) {
921     // TODO(mlerman): update UMA stats for the new tutorial.
922     layout->StartRow(1, 0);
923     layout->AddView(tutorial_view);
924   } else {
925     tutorial_mode_ = profiles::TUTORIAL_MODE_NONE;
926   }
927
928   if (!current_profile_view) {
929     // Guest windows don't have an active profile.
930     current_profile_view = CreateGuestProfileView();
931     option_buttons_view = CreateOptionsView(false);
932   }
933
934   layout->StartRow(1, 0);
935   layout->AddView(current_profile_view);
936
937   if (view_mode_ != profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER) {
938     DCHECK(current_profile_accounts);
939     layout->StartRow(0, 0);
940     layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
941     layout->StartRow(1, 0);
942     layout->AddView(current_profile_accounts);
943   }
944
945   if (browser_->profile()->IsSupervised()) {
946     layout->StartRow(0, 0);
947     layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
948     layout->StartRow(1, 0);
949     layout->AddView(CreateSupervisedUserDisclaimerView());
950   }
951
952   if (view_mode_ == profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER) {
953     layout->StartRow(1, 0);
954     if (switches::IsFastUserSwitching())
955       layout->AddView(CreateOtherProfilesView(other_profiles));
956   }
957
958   layout->StartRow(0, 0);
959   layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
960
961   if (option_buttons_view) {
962     layout->StartRow(0, 0);
963     layout->AddView(option_buttons_view);
964   }
965
966   return view;
967 }
968
969 void ProfileChooserView::DismissTutorial() {
970   // Never shows the upgrade tutorial again if manually closed.
971   if (tutorial_mode_ == profiles::TUTORIAL_MODE_WELCOME_UPGRADE) {
972     browser_->profile()->GetPrefs()->SetInteger(
973         prefs::kProfileAvatarTutorialShown,
974         signin_ui_util::kUpgradeWelcomeTutorialShowMax + 1);
975   }
976
977   tutorial_mode_ = profiles::TUTORIAL_MODE_NONE;
978   ShowView(profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER, avatar_menu_.get());
979 }
980
981 views::View* ProfileChooserView::CreateTutorialView(
982     profiles::TutorialMode tutorial_mode,
983     const base::string16& title_text,
984     const base::string16& content_text,
985     const base::string16& link_text,
986     const base::string16& button_text,
987     bool stack_button,
988     views::Link** link,
989     views::LabelButton** button,
990     views::ImageButton** close_button) {
991   tutorial_mode_ = tutorial_mode;
992
993   views::View* view = new views::View();
994   view->set_background(views::Background::CreateSolidBackground(
995       profiles::kAvatarTutorialBackgroundColor));
996   views::GridLayout* layout = CreateSingleColumnLayout(view,
997       kFixedMenuWidth - 2 * views::kButtonHEdgeMarginNew);
998   // Creates a second column set for buttons and links.
999   views::ColumnSet* button_columns = layout->AddColumnSet(1);
1000   button_columns->AddColumn(views::GridLayout::LEADING,
1001       views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
1002   button_columns->AddPaddingColumn(
1003       1, views::kUnrelatedControlHorizontalSpacing);
1004   button_columns->AddColumn(views::GridLayout::TRAILING,
1005       views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0);
1006   layout->SetInsets(views::kButtonVEdgeMarginNew,
1007                     views::kButtonHEdgeMarginNew,
1008                     views::kButtonVEdgeMarginNew,
1009                     views::kButtonHEdgeMarginNew);
1010
1011   // Adds title and close button if needed.
1012   views::Label* title_label = new views::Label(title_text);
1013   title_label->SetMultiLine(true);
1014   title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1015   title_label->SetAutoColorReadabilityEnabled(false);
1016   title_label->SetEnabledColor(SK_ColorWHITE);
1017   title_label->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
1018       ui::ResourceBundle::MediumFont));
1019
1020   if (close_button) {
1021     layout->StartRow(1, 1);
1022     layout->AddView(title_label);
1023     *close_button = new views::ImageButton(this);
1024     (*close_button)->SetImageAlignment(views::ImageButton::ALIGN_RIGHT,
1025                                        views::ImageButton::ALIGN_MIDDLE);
1026     ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1027     (*close_button)->SetImage(views::ImageButton::STATE_NORMAL,
1028                               rb->GetImageSkiaNamed(IDR_CLOSE_1));
1029     (*close_button)->SetImage(views::ImageButton::STATE_HOVERED,
1030                               rb->GetImageSkiaNamed(IDR_CLOSE_1_H));
1031     (*close_button)->SetImage(views::ImageButton::STATE_PRESSED,
1032                               rb->GetImageSkiaNamed(IDR_CLOSE_1_P));
1033     layout->AddView(*close_button);
1034   } else {
1035     layout->StartRow(1, 0);
1036     layout->AddView(title_label);
1037   }
1038
1039   // Adds body content.
1040   views::Label* content_label = new views::Label(content_text);
1041   content_label->SetMultiLine(true);
1042   content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1043   content_label->SetAutoColorReadabilityEnabled(false);
1044   content_label->SetEnabledColor(profiles::kAvatarTutorialContentTextColor);
1045   layout->StartRowWithPadding(1, 0, 0, views::kRelatedControlVerticalSpacing);
1046   layout->AddView(content_label);
1047
1048   // Adds links and buttons.
1049   bool has_button = !button_text.empty();
1050   if (has_button) {
1051     *button = new views::LabelButton(this, button_text);
1052     (*button)->SetHorizontalAlignment(gfx::ALIGN_CENTER);
1053     (*button)->SetStyle(views::Button::STYLE_BUTTON);
1054   }
1055
1056   bool has_link = !link_text.empty();
1057   if (has_link) {
1058     *link = CreateLink(link_text, this);
1059     (*link)->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1060     (*link)->SetAutoColorReadabilityEnabled(false);
1061     (*link)->SetEnabledColor(SK_ColorWHITE);
1062   }
1063
1064   if (stack_button) {
1065     DCHECK(has_button);
1066     layout->StartRowWithPadding(
1067         1, 0, 0, views::kUnrelatedControlVerticalSpacing);
1068     layout->AddView(*button);
1069     if (has_link) {
1070       layout->StartRowWithPadding(
1071           1, 0, 0, views::kRelatedControlVerticalSpacing);
1072       (*link)->SetHorizontalAlignment(gfx::ALIGN_CENTER);
1073       layout->AddView(*link);
1074     }
1075   } else {
1076     DCHECK(has_link || has_button);
1077     layout->StartRowWithPadding(
1078         1, 1, 0, views::kUnrelatedControlVerticalSpacing);
1079     if (has_link)
1080       layout->AddView(*link);
1081     else
1082       layout->SkipColumns(1);
1083     if (has_button)
1084       layout->AddView(*button);
1085     else
1086       layout->SkipColumns(1);
1087   }
1088
1089   return view;
1090 }
1091
1092 views::View* ProfileChooserView::CreateCurrentProfileView(
1093     const AvatarMenu::Item& avatar_item,
1094     bool is_guest) {
1095   views::View* view = new views::View();
1096   int column_width = kFixedMenuWidth - 2 * views::kButtonHEdgeMarginNew;
1097   views::GridLayout* layout = CreateSingleColumnLayout(view, column_width);
1098   layout->SetInsets(views::kButtonVEdgeMarginNew,
1099                     views::kButtonHEdgeMarginNew,
1100                     views::kUnrelatedControlVerticalSpacing,
1101                     views::kButtonHEdgeMarginNew);
1102
1103   // Profile icon, centered.
1104   int x_offset = (column_width - kLargeImageSide) / 2;
1105   current_profile_photo_ = new EditableProfilePhoto(
1106       this, avatar_item.icon, !is_guest,
1107       gfx::Rect(x_offset, 0, kLargeImageSide, kLargeImageSide));
1108   SizedContainer* profile_icon_container =
1109       new SizedContainer(gfx::Size(column_width, kLargeImageSide));
1110   profile_icon_container->AddChildView(current_profile_photo_);
1111
1112   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1113   if (browser_->profile()->IsSupervised()) {
1114     views::ImageView* supervised_icon = new views::ImageView();
1115     supervised_icon->SetImage(
1116         rb->GetImageSkiaNamed(IDR_ICON_PROFILES_MENU_SUPERVISED));
1117     gfx::Size preferred_size = supervised_icon->GetPreferredSize();
1118     gfx::Rect parent_bounds = current_profile_photo_->bounds();
1119     supervised_icon->SetBounds(
1120         parent_bounds.right() - preferred_size.width(),
1121         parent_bounds.bottom() - preferred_size.height(),
1122         preferred_size.width(),
1123         preferred_size.height());
1124     profile_icon_container->AddChildView(supervised_icon);
1125   }
1126
1127   layout->StartRow(1, 0);
1128   layout->AddView(profile_icon_container);
1129
1130   // Profile name, centered.
1131   bool editing_allowed = !is_guest && !browser_->profile()->IsSupervised();
1132   current_profile_name_ = new EditableProfileName(
1133       this,
1134       profiles::GetAvatarNameForProfile(browser_->profile()->GetPath()),
1135       editing_allowed);
1136   layout->StartRowWithPadding(1, 0, 0,
1137                               views::kRelatedControlSmallVerticalSpacing);
1138   layout->StartRow(1, 0);
1139   layout->AddView(current_profile_name_);
1140
1141   if (is_guest)
1142     return view;
1143
1144   // The available links depend on the type of profile that is active.
1145   if (avatar_item.signed_in) {
1146     layout->StartRow(1, 0);
1147     if (switches::IsEnableAccountConsistency()) {
1148       base::string16 link_title = l10n_util::GetStringUTF16(
1149           view_mode_ == profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER ?
1150               IDS_PROFILES_PROFILE_MANAGE_ACCOUNTS_BUTTON :
1151               IDS_PROFILES_PROFILE_HIDE_MANAGE_ACCOUNTS_BUTTON);
1152       manage_accounts_link_ = CreateLink(link_title, this);
1153       manage_accounts_link_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
1154       layout->AddView(manage_accounts_link_);
1155     } else {
1156       // Add a small padding between the email button and the profile name.
1157       layout->StartRowWithPadding(1, 0, 0, 2);
1158       // Badge the email address if there's an authentication error.
1159       if (HasAuthError(browser_->profile())) {
1160         const gfx::ImageSkia warning_image = *rb->GetImageNamed(
1161             IDR_ICON_PROFILES_ACCOUNT_BUTTON_ERROR).ToImageSkia();
1162         auth_error_email_button_ =
1163             new RightAlignedIconLabelButton(this, avatar_item.sync_state);
1164         auth_error_email_button_->SetElideBehavior(gfx::ELIDE_EMAIL);
1165         auth_error_email_button_->SetBorder(views::Border::NullBorder());
1166         auth_error_email_button_->SetImage(
1167             views::LabelButton::STATE_NORMAL, warning_image);
1168         auth_error_email_button_->SetTextColor(
1169             views::LabelButton::STATE_NORMAL,
1170             views::Link::GetDefaultEnabledColor());
1171         auth_error_email_button_->SetFocusable(true);
1172         layout->AddView(auth_error_email_button_);
1173       } else {
1174         views::Label* email_label = new views::Label(avatar_item.sync_state);
1175         email_label->SetElideBehavior(gfx::ELIDE_EMAIL);
1176         email_label->SetEnabled(false);
1177         layout->AddView(email_label);
1178       }
1179     }
1180   } else {
1181     SigninManagerBase* signin_manager = SigninManagerFactory::GetForProfile(
1182         browser_->profile()->GetOriginalProfile());
1183     if (signin_manager->IsSigninAllowed()) {
1184       views::Label* promo = new views::Label(
1185           l10n_util::GetStringUTF16(IDS_PROFILES_SIGNIN_PROMO));
1186       promo->SetMultiLine(true);
1187       promo->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1188       layout->StartRowWithPadding(1, 0, 0,
1189                                   views::kRelatedControlSmallVerticalSpacing);
1190       layout->StartRow(1, 0);
1191       layout->AddView(promo);
1192
1193       signin_current_profile_link_ = new views::BlueButton(
1194         this, l10n_util::GetStringFUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL,
1195             l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)));
1196       layout->StartRowWithPadding(1, 0, 0,
1197                                   views::kRelatedControlVerticalSpacing);
1198       layout->StartRow(1, 0);
1199       layout->AddView(signin_current_profile_link_);
1200     }
1201   }
1202
1203   return view;
1204 }
1205
1206 views::View* ProfileChooserView::CreateGuestProfileView() {
1207   gfx::Image guest_icon =
1208       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
1209           profiles::GetPlaceholderAvatarIconResourceID());
1210   AvatarMenu::Item guest_avatar_item(0, 0, guest_icon);
1211   guest_avatar_item.active = true;
1212   guest_avatar_item.name = l10n_util::GetStringUTF16(
1213       IDS_PROFILES_GUEST_PROFILE_NAME);
1214   guest_avatar_item.signed_in = false;
1215
1216   return CreateCurrentProfileView(guest_avatar_item, true);
1217 }
1218
1219 views::View* ProfileChooserView::CreateOtherProfilesView(
1220     const Indexes& avatars_to_show) {
1221   views::View* view = new views::View();
1222   views::GridLayout* layout = CreateSingleColumnLayout(view, kFixedMenuWidth);
1223
1224   int num_avatars_to_show = avatars_to_show.size();
1225   for (int i = 0; i < num_avatars_to_show; ++i) {
1226     const size_t index = avatars_to_show[i];
1227     const AvatarMenu::Item& item = avatar_menu_->GetItemAt(index);
1228     const int kSmallImageSide = 32;
1229
1230     gfx::Image image = profiles::GetSizedAvatarIcon(
1231         item.icon, true, kSmallImageSide, kSmallImageSide);
1232
1233     views::LabelButton* button = new BackgroundColorHoverButton(
1234         this,
1235         item.name,
1236         *image.ToImageSkia());
1237     open_other_profile_indexes_map_[button] = index;
1238
1239     layout->StartRow(1, 0);
1240     layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
1241     layout->StartRow(1, 0);
1242     layout->AddView(button);
1243   }
1244
1245   return view;
1246 }
1247
1248 views::View* ProfileChooserView::CreateOptionsView(bool enable_lock) {
1249   views::View* view = new views::View();
1250   views::GridLayout* layout = CreateSingleColumnLayout(view, kFixedMenuWidth);
1251
1252   base::string16 text = browser_->profile()->IsGuestSession() ?
1253       l10n_util::GetStringUTF16(IDS_PROFILES_EXIT_GUEST) :
1254       l10n_util::GetStringUTF16(IDS_PROFILES_SWITCH_USERS_BUTTON);
1255   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1256   users_button_ = new BackgroundColorHoverButton(
1257       this,
1258       text,
1259       *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_MENU_AVATAR));
1260   layout->StartRow(1, 0);
1261   layout->AddView(users_button_);
1262
1263   if (ShouldShowGoIncognito()) {
1264     layout->StartRow(1, 0);
1265     layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
1266
1267     go_incognito_button_ = new BackgroundColorHoverButton(
1268         this,
1269         l10n_util::GetStringUTF16(IDS_PROFILES_GO_INCOGNITO_BUTTON),
1270         *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_MENU_INCOGNITO));
1271     layout->StartRow(1, 0);
1272     layout->AddView(go_incognito_button_);
1273   }
1274
1275   if (enable_lock) {
1276     layout->StartRow(1, 0);
1277     layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
1278
1279     lock_button_ = new BackgroundColorHoverButton(
1280         this,
1281         l10n_util::GetStringUTF16(IDS_PROFILES_PROFILE_SIGNOUT_BUTTON),
1282         *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_MENU_LOCK));
1283     layout->StartRow(1, 0);
1284     layout->AddView(lock_button_);
1285   }
1286   return view;
1287 }
1288
1289 views::View* ProfileChooserView::CreateSupervisedUserDisclaimerView() {
1290   views::View* view = new views::View();
1291   views::GridLayout* layout = CreateSingleColumnLayout(
1292       view, kFixedMenuWidth - 2 * views::kButtonHEdgeMarginNew);
1293   layout->SetInsets(views::kRelatedControlVerticalSpacing,
1294                     views::kButtonHEdgeMarginNew,
1295                     views::kRelatedControlVerticalSpacing,
1296                     views::kButtonHEdgeMarginNew);
1297   views::Label* disclaimer = new views::Label(
1298       avatar_menu_->GetSupervisedUserInformation());
1299   disclaimer->SetMultiLine(true);
1300   disclaimer->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1301   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1302   disclaimer->SetFontList(rb->GetFontList(ui::ResourceBundle::SmallFont));
1303   layout->StartRow(1, 0);
1304   layout->AddView(disclaimer);
1305
1306   return view;
1307 }
1308
1309 views::View* ProfileChooserView::CreateCurrentProfileAccountsView(
1310     const AvatarMenu::Item& avatar_item) {
1311   DCHECK(avatar_item.signed_in);
1312   views::View* view = new views::View();
1313   view->set_background(views::Background::CreateSolidBackground(
1314       profiles::kAvatarBubbleAccountsBackgroundColor));
1315   views::GridLayout* layout = CreateSingleColumnLayout(view, kFixedMenuWidth);
1316
1317   Profile* profile = browser_->profile();
1318   std::string primary_account =
1319       SigninManagerFactory::GetForProfile(profile)->GetAuthenticatedUsername();
1320   DCHECK(!primary_account.empty());
1321   std::vector<std::string>accounts =
1322       profiles::GetSecondaryAccountsForProfile(profile, primary_account);
1323
1324   // Get state of authentication error, if any.
1325   std::string error_account_id = GetAuthErrorAccountId(profile);
1326
1327   // The primary account should always be listed first.
1328   // TODO(rogerta): we still need to further differentiate the primary account
1329   // from the others in the UI, so more work is likely required here:
1330   // crbug.com/311124.
1331   CreateAccountButton(layout, primary_account, true,
1332                       error_account_id == primary_account, kFixedMenuWidth);
1333   for (size_t i = 0; i < accounts.size(); ++i)
1334     CreateAccountButton(layout, accounts[i], false,
1335                         error_account_id == accounts[i], kFixedMenuWidth);
1336
1337   if (!profile->IsSupervised()) {
1338     layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
1339
1340     add_account_link_ = CreateLink(l10n_util::GetStringFUTF16(
1341         IDS_PROFILES_PROFILE_ADD_ACCOUNT_BUTTON, avatar_item.name), this);
1342     add_account_link_->SetBorder(views::Border::CreateEmptyBorder(
1343         0, views::kButtonVEdgeMarginNew,
1344         views::kRelatedControlVerticalSpacing, 0));
1345     layout->StartRow(1, 0);
1346     layout->AddView(add_account_link_);
1347   }
1348
1349   return view;
1350 }
1351
1352 void ProfileChooserView::CreateAccountButton(views::GridLayout* layout,
1353                                              const std::string& account,
1354                                              bool is_primary_account,
1355                                              bool reauth_required,
1356                                              int width) {
1357   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1358   const gfx::ImageSkia* delete_default_image =
1359       rb->GetImageNamed(IDR_CLOSE_1).ToImageSkia();
1360   const int kDeleteButtonWidth = delete_default_image->width();
1361   const gfx::ImageSkia warning_default_image = reauth_required ?
1362       *rb->GetImageNamed(IDR_ICON_PROFILES_ACCOUNT_BUTTON_ERROR).ToImageSkia() :
1363       gfx::ImageSkia();
1364   const int kWarningButtonWidth = reauth_required ?
1365       warning_default_image.width() + views::kRelatedButtonHSpacing : 0;
1366   int available_width = width - 2 * views::kButtonHEdgeMarginNew
1367       - kDeleteButtonWidth - kWarningButtonWidth;
1368   views::LabelButton* email_button = new BackgroundColorHoverButton(
1369       reauth_required ? this : NULL,
1370       base::UTF8ToUTF16(account),
1371       warning_default_image);
1372   email_button->SetElideBehavior(gfx::ELIDE_EMAIL);
1373   email_button->SetMinSize(gfx::Size(0, kButtonHeight));
1374   email_button->SetMaxSize(gfx::Size(available_width, kButtonHeight));
1375   layout->StartRow(1, 0);
1376   layout->AddView(email_button);
1377
1378   if (reauth_required)
1379     reauth_account_button_map_[email_button] = account;
1380
1381   // Delete button.
1382   if (!browser_->profile()->IsSupervised()) {
1383     views::ImageButton* delete_button = new views::ImageButton(this);
1384     delete_button->SetImageAlignment(views::ImageButton::ALIGN_RIGHT,
1385                                      views::ImageButton::ALIGN_MIDDLE);
1386     delete_button->SetImage(views::ImageButton::STATE_NORMAL,
1387                             delete_default_image);
1388     delete_button->SetImage(views::ImageButton::STATE_HOVERED,
1389                             rb->GetImageSkiaNamed(IDR_CLOSE_1_H));
1390     delete_button->SetImage(views::ImageButton::STATE_PRESSED,
1391                             rb->GetImageSkiaNamed(IDR_CLOSE_1_P));
1392     delete_button->SetBounds(
1393         width - views::kButtonHEdgeMarginNew - kDeleteButtonWidth,
1394         0, kDeleteButtonWidth, kButtonHeight);
1395
1396     email_button->set_notify_enter_exit_on_child(true);
1397     email_button->AddChildView(delete_button);
1398
1399     // Save the original email address, as the button text could be elided.
1400     delete_account_button_map_[delete_button] = account;
1401   }
1402 }
1403
1404 views::View* ProfileChooserView::CreateGaiaSigninView() {
1405   GURL url;
1406   int message_id;
1407
1408   switch (view_mode_) {
1409     case profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN:
1410       url = signin::GetPromoURL(signin::SOURCE_AVATAR_BUBBLE_SIGN_IN,
1411                                 false /* auto_close */,
1412                                 true /* is_constrained */);
1413       message_id = IDS_PROFILES_GAIA_SIGNIN_TITLE;
1414       break;
1415     case profiles::BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT:
1416       url = signin::GetPromoURL(signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT,
1417                                 false /* auto_close */,
1418                                 true /* is_constrained */);
1419       message_id = IDS_PROFILES_GAIA_ADD_ACCOUNT_TITLE;
1420       break;
1421     case profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH: {
1422       DCHECK(HasAuthError(browser_->profile()));
1423       url = signin::GetReauthURL(browser_->profile(),
1424                                  GetAuthErrorUsername(browser_->profile()));
1425       message_id = IDS_PROFILES_GAIA_REAUTH_TITLE;
1426       break;
1427     }
1428     default:
1429       NOTREACHED() << "Called with invalid mode=" << view_mode_;
1430       return NULL;
1431   }
1432
1433   // Adds Gaia signin webview
1434   Profile* profile = browser_->profile();
1435   views::WebView* web_view = new views::WebView(profile);
1436   web_view->LoadInitialURL(url);
1437   web_view->SetPreferredSize(
1438       gfx::Size(kFixedGaiaViewWidth, kFixedGaiaViewHeight));
1439
1440   TitleCard* title_card = new TitleCard(l10n_util::GetStringUTF16(message_id),
1441                                         this,
1442                                         &gaia_signin_cancel_button_);
1443   return TitleCard::AddPaddedTitleCard(
1444       web_view, title_card, kFixedGaiaViewWidth);
1445 }
1446
1447 views::View* ProfileChooserView::CreateAccountRemovalView() {
1448   views::View* view = new views::View();
1449   views::GridLayout* layout = CreateSingleColumnLayout(
1450       view, kFixedAccountRemovalViewWidth - 2 * views::kButtonHEdgeMarginNew);
1451   layout->SetInsets(0,
1452                     views::kButtonHEdgeMarginNew,
1453                     views::kButtonVEdgeMarginNew,
1454                     views::kButtonHEdgeMarginNew);
1455
1456   const std::string& primary_account = SigninManagerFactory::GetForProfile(
1457       browser_->profile())->GetAuthenticatedUsername();
1458   bool is_primary_account = primary_account == account_id_to_remove_;
1459
1460   // Adds main text.
1461   layout->StartRowWithPadding(1, 0, 0, views::kUnrelatedControlVerticalSpacing);
1462   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1463   const gfx::FontList& small_font_list =
1464       rb->GetFontList(ui::ResourceBundle::SmallFont);
1465
1466   if (is_primary_account) {
1467     std::vector<size_t> offsets;
1468     const base::string16 settings_text =
1469         l10n_util::GetStringUTF16(IDS_PROFILES_SETTINGS_LINK);
1470     const base::string16 primary_account_removal_text =
1471         l10n_util::GetStringFUTF16(IDS_PROFILES_PRIMARY_ACCOUNT_REMOVAL_TEXT,
1472             base::UTF8ToUTF16(account_id_to_remove_), settings_text, &offsets);
1473     views::StyledLabel* primary_account_removal_label =
1474         new views::StyledLabel(primary_account_removal_text, this);
1475     primary_account_removal_label->AddStyleRange(
1476         gfx::Range(offsets[1], offsets[1] + settings_text.size()),
1477         views::StyledLabel::RangeStyleInfo::CreateForLink());
1478     primary_account_removal_label->SetBaseFontList(small_font_list);
1479     layout->AddView(primary_account_removal_label);
1480   } else {
1481     views::Label* content_label = new views::Label(
1482         l10n_util::GetStringUTF16(IDS_PROFILES_ACCOUNT_REMOVAL_TEXT));
1483     content_label->SetMultiLine(true);
1484     content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1485     content_label->SetFontList(small_font_list);
1486     layout->AddView(content_label);
1487   }
1488
1489   // Adds button.
1490   if (!is_primary_account) {
1491     remove_account_button_ = new views::BlueButton(
1492         this, l10n_util::GetStringUTF16(IDS_PROFILES_ACCOUNT_REMOVAL_BUTTON));
1493     remove_account_button_->SetHorizontalAlignment(
1494         gfx::ALIGN_CENTER);
1495     layout->StartRowWithPadding(
1496         1, 0, 0, views::kUnrelatedControlVerticalSpacing);
1497     layout->AddView(remove_account_button_);
1498   } else {
1499     layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
1500   }
1501
1502   TitleCard* title_card = new TitleCard(
1503       l10n_util::GetStringUTF16(IDS_PROFILES_ACCOUNT_REMOVAL_TITLE),
1504       this, &account_removal_cancel_button_);
1505   return TitleCard::AddPaddedTitleCard(view, title_card,
1506       kFixedAccountRemovalViewWidth);
1507 }
1508
1509 views::View* ProfileChooserView::CreateWelcomeUpgradeTutorialViewIfNeeded(
1510     bool tutorial_shown, const AvatarMenu::Item& avatar_item){
1511   Profile* profile = browser_->profile();
1512
1513   const int show_count = profile->GetPrefs()->GetInteger(
1514       prefs::kProfileAvatarTutorialShown);
1515   // Do not show the tutorial if user has dismissed it.
1516   if (show_count > signin_ui_util::kUpgradeWelcomeTutorialShowMax)
1517     return NULL;
1518
1519   if (!tutorial_shown) {
1520     if (show_count == signin_ui_util::kUpgradeWelcomeTutorialShowMax)
1521       return NULL;
1522     profile->GetPrefs()->SetInteger(
1523         prefs::kProfileAvatarTutorialShown, show_count + 1);
1524   }
1525   ProfileMetrics::LogProfileNewAvatarMenuUpgrade(
1526       ProfileMetrics::PROFILE_AVATAR_MENU_UPGRADE_VIEW);
1527
1528   // For local profiles, the "Not you" link doesn't make sense.
1529   base::string16 link_message = avatar_item.signed_in ?
1530       l10n_util::GetStringFUTF16(IDS_PROFILES_NOT_YOU, avatar_item.name) :
1531       base::string16();
1532
1533   return CreateTutorialView(
1534       profiles::TUTORIAL_MODE_WELCOME_UPGRADE,
1535       l10n_util::GetStringUTF16(
1536           IDS_PROFILES_WELCOME_UPGRADE_TUTORIAL_TITLE),
1537       l10n_util::GetStringUTF16(
1538           IDS_PROFILES_WELCOME_UPGRADE_TUTORIAL_CONTENT_TEXT),
1539       link_message,
1540       l10n_util::GetStringUTF16(IDS_PROFILES_TUTORIAL_WHATS_NEW_BUTTON),
1541       true /* stack_button */,
1542       &tutorial_not_you_link_,
1543       &tutorial_see_whats_new_button_,
1544       &tutorial_close_button_);
1545 }
1546
1547 views::View* ProfileChooserView::CreateSigninConfirmationView() {
1548   ProfileMetrics::LogProfileNewAvatarMenuSignin(
1549       ProfileMetrics::PROFILE_AVATAR_MENU_SIGNIN_VIEW);
1550
1551   return CreateTutorialView(
1552       profiles::TUTORIAL_MODE_CONFIRM_SIGNIN,
1553       l10n_util::GetStringUTF16(IDS_PROFILES_CONFIRM_SIGNIN_TUTORIAL_TITLE),
1554       l10n_util::GetStringUTF16(
1555           IDS_PROFILES_CONFIRM_SIGNIN_TUTORIAL_CONTENT_TEXT),
1556       l10n_util::GetStringUTF16(IDS_PROFILES_SYNC_SETTINGS_LINK),
1557       l10n_util::GetStringUTF16(IDS_PROFILES_TUTORIAL_OK_BUTTON),
1558       false /* stack_button */,
1559       &tutorial_sync_settings_link_,
1560       &tutorial_sync_settings_ok_button_,
1561       NULL /* close_button*/);
1562 }
1563
1564 views::View* ProfileChooserView::CreateSigninErrorView() {
1565   LoginUIService* login_ui_service =
1566       LoginUIServiceFactory::GetForProfile(browser_->profile());
1567   base::string16 last_login_result(login_ui_service->GetLastLoginResult());
1568   return CreateTutorialView(
1569       profiles::TUTORIAL_MODE_SHOW_ERROR,
1570       l10n_util::GetStringUTF16(IDS_PROFILES_ERROR_TUTORIAL_TITLE),
1571       last_login_result,
1572       l10n_util::GetStringUTF16(IDS_PROFILES_PROFILE_TUTORIAL_LEARN_MORE),
1573       base::string16(),
1574       false /* stack_button */,
1575       &tutorial_learn_more_link_,
1576       NULL,
1577       &tutorial_close_button_);
1578 }
1579
1580 views::View* ProfileChooserView::CreateSwitchUserView() {
1581   views::View* view = new views::View();
1582   views::GridLayout* layout = CreateSingleColumnLayout(
1583       view, kFixedSwitchUserViewWidth);
1584   views::ColumnSet* columns = layout->AddColumnSet(1);
1585   columns->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
1586   int label_width =
1587       kFixedSwitchUserViewWidth - 2 * views::kButtonHEdgeMarginNew;
1588   columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0,
1589                      views::GridLayout::FIXED, label_width, label_width);
1590   columns->AddPaddingColumn(0, views::kButtonHEdgeMarginNew);
1591
1592   // Adds main text.
1593   layout->StartRowWithPadding(1, 1, 0, views::kUnrelatedControlVerticalSpacing);
1594   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1595   const gfx::FontList& small_font_list =
1596       rb->GetFontList(ui::ResourceBundle::SmallFont);
1597   const AvatarMenu::Item& avatar_item =
1598       avatar_menu_->GetItemAt(avatar_menu_->GetActiveProfileIndex());
1599   views::Label* content_label = new views::Label(
1600       l10n_util::GetStringFUTF16(
1601           IDS_PROFILES_NOT_YOU_CONTENT_TEXT, avatar_item.name));
1602   content_label->SetMultiLine(true);
1603   content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1604   content_label->SetFontList(small_font_list);
1605   layout->AddView(content_label);
1606
1607   // Adds "Add person" button.
1608   layout->StartRowWithPadding(1, 0, 0, views::kUnrelatedControlVerticalSpacing);
1609   layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
1610
1611   add_person_button_ = new BackgroundColorHoverButton(
1612       this,
1613       l10n_util::GetStringUTF16(IDS_PROFILES_ADD_PERSON_BUTTON),
1614       *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_MENU_AVATAR));
1615   layout->StartRow(1, 0);
1616   layout->AddView(add_person_button_);
1617
1618   // Adds "Disconnect your Google Account" button.
1619   layout->StartRow(1, 0);
1620   layout->AddView(new views::Separator(views::Separator::HORIZONTAL));
1621
1622   disconnect_button_ = new BackgroundColorHoverButton(
1623       this,
1624       l10n_util::GetStringUTF16(IDS_PROFILES_DISCONNECT_BUTTON),
1625       *rb->GetImageSkiaNamed(IDR_ICON_PROFILES_MENU_DISCONNECT));
1626   layout->StartRow(1, 0);
1627   layout->AddView(disconnect_button_);
1628
1629   TitleCard* title_card = new TitleCard(
1630       l10n_util::GetStringFUTF16(IDS_PROFILES_NOT_YOU, avatar_item.name),
1631       this, &switch_user_cancel_button_);
1632   return TitleCard::AddPaddedTitleCard(view, title_card,
1633       kFixedSwitchUserViewWidth);
1634 }
1635
1636 bool ProfileChooserView::ShouldShowGoIncognito() const {
1637   bool incognito_available =
1638       IncognitoModePrefs::GetAvailability(browser_->profile()->GetPrefs()) !=
1639           IncognitoModePrefs::DISABLED;
1640   return incognito_available && !browser_->profile()->IsGuestSession();
1641 }
1642
1643 void ProfileChooserView::PostActionPerformed(
1644     ProfileMetrics::ProfileDesktopMenu action_performed) {
1645   ProfileMetrics::LogProfileDesktopMenu(action_performed, gaia_service_type_);
1646   gaia_service_type_ = signin::GAIA_SERVICE_TYPE_NONE;
1647 }