OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/password_generation_bubble_view.h" | 5 #include "chrome/browser/ui/views/password_generation_bubble_view.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/autofill/password_generator.h" | 8 #include "chrome/browser/autofill/password_generator.h" |
9 #include "chrome/browser/password_manager/password_manager.h" | 9 #include "chrome/browser/password_manager/password_manager.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
12 #include "chrome/common/autofill_messages.h" | 12 #include "chrome/common/autofill_messages.h" |
13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
14 #include "content/public/browser/page_navigator.h" | 14 #include "content/public/browser/page_navigator.h" |
15 #include "content/public/browser/render_view_host.h" | 15 #include "content/public/browser/render_view_host.h" |
16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
17 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
18 #include "grit/theme_resources_standard.h" | |
19 #include "ui/base/theme_provider.h" | |
20 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/views/controls/button/image_button.h" | |
22 #include "ui/views/controls/button/text_button.h" | 19 #include "ui/views/controls/button/text_button.h" |
23 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
24 #include "ui/views/controls/link.h" | 21 #include "ui/views/controls/link.h" |
25 #include "ui/views/controls/textfield/textfield.h" | 22 #include "ui/views/controls/textfield/textfield.h" |
26 #include "ui/views/layout/grid_layout.h" | 23 #include "ui/views/layout/grid_layout.h" |
27 #include "ui/views/layout/layout_constants.h" | 24 #include "ui/views/layout/layout_constants.h" |
28 | 25 |
29 using views::ColumnSet; | 26 using views::ColumnSet; |
30 using views::GridLayout; | 27 using views::GridLayout; |
31 | 28 |
32 PasswordGenerationBubbleView::PasswordGenerationBubbleView( | 29 PasswordGenerationBubbleView::PasswordGenerationBubbleView( |
33 const gfx::Rect& anchor_rect, | 30 const gfx::Rect& anchor_rect, |
34 const webkit::forms::PasswordForm& form, | 31 const webkit::forms::PasswordForm& form, |
35 views::View* anchor_view, | 32 views::View* anchor_view, |
36 content::RenderViewHost* render_view_host, | 33 content::RenderViewHost* render_view_host, |
37 autofill::PasswordGenerator* password_generator, | 34 autofill::PasswordGenerator* password_generator, |
38 content::PageNavigator* navigator, | 35 content::PageNavigator* navigator, |
39 PasswordManager* password_manager, | 36 PasswordManager* password_manager) |
40 ui::ThemeProvider* theme_provider) | |
41 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), | 37 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT), |
42 accept_button_(NULL), | 38 accept_button_(NULL), |
43 regenerate_button_(NULL), | |
44 text_field_(NULL), | 39 text_field_(NULL), |
45 anchor_rect_(anchor_rect), | 40 anchor_rect_(anchor_rect), |
46 form_(form), | 41 form_(form), |
47 render_view_host_(render_view_host), | 42 render_view_host_(render_view_host), |
48 password_generator_(password_generator), | 43 password_generator_(password_generator), |
49 navigator_(navigator), | 44 navigator_(navigator), |
50 password_manager_(password_manager), | 45 password_manager_(password_manager) {} |
51 theme_provider_(theme_provider) {} | |
52 | 46 |
53 PasswordGenerationBubbleView::~PasswordGenerationBubbleView() {} | 47 PasswordGenerationBubbleView::~PasswordGenerationBubbleView() {} |
54 | 48 |
55 void PasswordGenerationBubbleView::Init() { | 49 void PasswordGenerationBubbleView::Init() { |
56 // TODO(gcasto): Localize text after we have finalized the UI. | 50 // TODO(gcasto): Localize text after we have finalized the UI. |
57 // crbug.com/118062 | 51 // crbug.com/118062 |
58 accept_button_ = new views::NativeTextButton(this, | 52 accept_button_ = new views::NativeTextButton(this, |
59 ASCIIToUTF16("Try It")); | 53 ASCIIToUTF16("Try It")); |
60 | 54 |
61 regenerate_button_ = new views::ImageButton(this); | |
62 regenerate_button_->SetImage(views::CustomButton::BS_NORMAL, | |
63 theme_provider_->GetImageSkiaNamed(IDR_RELOAD)); | |
64 regenerate_button_->SetImage(views::CustomButton::BS_HOT, | |
65 theme_provider_->GetImageSkiaNamed(IDR_RELOAD_H)); | |
66 regenerate_button_->SetImage(views::CustomButton::BS_PUSHED, | |
67 theme_provider_->GetImageSkiaNamed(IDR_RELOAD_P)); | |
68 regenerate_button_->SetTooltipText(ASCIIToUTF16("Regenerate")); | |
69 | |
70 text_field_ = new views::Textfield(); | 55 text_field_ = new views::Textfield(); |
71 text_field_->SetText( | 56 text_field_->SetText( |
72 ASCIIToUTF16(password_generator_->Generate())); | 57 ASCIIToUTF16(password_generator_->Generate())); |
73 | 58 |
74 views::Label* title_label = new views::Label( | 59 views::Label* title_label = new views::Label( |
75 ASCIIToUTF16("Password Suggestion")); | 60 ASCIIToUTF16("Password Suggestion")); |
76 | 61 |
77 views::Link* learn_more_link = new views::Link( | 62 views::Link* learn_more_link = new views::Link( |
78 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); | 63 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); |
79 learn_more_link->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 64 learn_more_link->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
80 learn_more_link->set_listener(this); | 65 learn_more_link->set_listener(this); |
81 | 66 |
82 GridLayout* layout = new GridLayout(this); | 67 GridLayout* layout = new GridLayout(this); |
83 SetLayoutManager(layout); | 68 SetLayoutManager(layout); |
84 | 69 |
85 // Title row. | 70 // Title row. |
86 ColumnSet* cs = layout->AddColumnSet(0); | 71 ColumnSet* cs = layout->AddColumnSet(0); |
87 cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | 72 cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, |
88 GridLayout::USE_PREF, 0, 100); | 73 GridLayout::USE_PREF, 0, 100); |
89 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); | 74 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); |
90 cs->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0, | 75 cs->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0, |
91 GridLayout::USE_PREF, 0, 0); | 76 GridLayout::USE_PREF, 0, 0); |
92 | 77 |
93 // Input row | 78 // Input row |
94 cs = layout->AddColumnSet(1); | 79 cs = layout->AddColumnSet(1); |
95 cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | 80 cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, |
96 GridLayout::USE_PREF, 0, 100); | 81 GridLayout::USE_PREF, 0, 100); |
97 cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, GridLayout::FIXED, | |
98 regenerate_button_->GetPreferredSize().width(), 100); | |
99 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); | 82 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); |
100 cs->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0, | 83 cs->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0, |
101 GridLayout::USE_PREF, 0, 0); | 84 GridLayout::USE_PREF, 0, 0); |
102 | 85 |
103 layout->StartRow(0, 0); | 86 layout->StartRow(0, 0); |
104 layout->AddView(title_label); | 87 layout->AddView(title_label); |
105 layout->AddView(learn_more_link); | 88 layout->AddView(learn_more_link); |
106 | 89 |
107 layout->StartRow(0, 1); | 90 layout->StartRow(0, 1); |
108 layout->AddView(text_field_); | 91 layout->AddView(text_field_); |
109 layout->AddView(regenerate_button_); | |
110 layout->AddView(accept_button_); | 92 layout->AddView(accept_button_); |
111 } | 93 } |
112 | 94 |
113 gfx::Rect PasswordGenerationBubbleView::GetAnchorRect() { | 95 gfx::Rect PasswordGenerationBubbleView::GetAnchorRect() { |
114 return anchor_rect_; | 96 return anchor_rect_; |
115 } | 97 } |
116 | 98 |
117 void PasswordGenerationBubbleView::ButtonPressed(views::Button* sender, | 99 void PasswordGenerationBubbleView::ButtonPressed(views::Button* sender, |
118 const views::Event& event) { | 100 const views::Event& event) { |
119 if (sender == accept_button_) { | 101 if (sender == accept_button_) { |
120 render_view_host_->Send(new AutofillMsg_GeneratedPasswordAccepted( | 102 render_view_host_->Send(new AutofillMsg_GeneratedPasswordAccepted( |
121 render_view_host_->GetRoutingID(), text_field_->text())); | 103 render_view_host_->GetRoutingID(), text_field_->text())); |
122 password_manager_->SetFormHasGeneratedPassword(form_); | 104 password_manager_->SetFormHasGeneratedPassword(form_); |
123 StartFade(false); | 105 StartFade(false); |
124 } else if (sender == regenerate_button_) { | |
125 text_field_->SetText( | |
126 ASCIIToUTF16(password_generator_->Generate())); | |
127 } | 106 } |
128 } | 107 } |
129 | 108 |
130 void PasswordGenerationBubbleView::LinkClicked(views::Link* source, | 109 void PasswordGenerationBubbleView::LinkClicked(views::Link* source, |
131 int event_flags) { | 110 int event_flags) { |
132 content::OpenURLParams params( | 111 content::OpenURLParams params( |
133 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), | 112 GURL(chrome::kAutoPasswordGenerationLearnMoreURL), content::Referrer(), |
134 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); | 113 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false); |
135 navigator_->OpenURL(params); | 114 navigator_->OpenURL(params); |
136 StartFade(false); | 115 StartFade(false); |
137 } | 116 } |
138 | 117 |
139 views::View* PasswordGenerationBubbleView::GetInitiallyFocusedView() { | 118 views::View* PasswordGenerationBubbleView::GetInitiallyFocusedView() { |
140 return text_field_; | 119 return text_field_; |
141 } | 120 } |
OLD | NEW |