OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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/extensions/suspicious_extension_bubble_view.h" | |
6 | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "base/strings/string_util.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "chrome/browser/extensions/extension_prefs.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/extension_system.h" | |
13 #include "chrome/browser/extensions/suspicious_extension_bubble_controller.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "grit/locale_settings.h" | |
17 #include "ui/base/accessibility/accessible_view_state.h" | |
18 #include "ui/base/resource/resource_bundle.h" | |
19 #include "ui/views/controls/button/label_button.h" | |
20 #include "ui/views/controls/label.h" | |
21 #include "ui/views/controls/link.h" | |
22 #include "ui/views/layout/grid_layout.h" | |
23 #include "ui/views/widget/widget.h" | |
24 | |
25 namespace { | |
26 | |
27 // Layout constants. | |
28 const int kExtensionListPadding = 10; | |
29 const int kInsetBottomRight = 13; | |
30 const int kInsetLeft = 14; | |
31 const int kInsetTop = 9; | |
32 const int kHeadlineMessagePadding = 4; | |
33 const int kHeadlineRowPadding = 10; | |
34 const int kMessageBubblePadding = 11; | |
35 | |
36 // How many extensions to show in the bubble (max). | |
37 const size_t kMaxExtensionsToShow = 7; | |
38 | |
39 // How long to wait until showing the bubble (in seconds). | |
40 const int kBubbleAppearanceWaitTime = 5; | |
41 | |
42 } // namespace | |
43 | |
44 //////////////////////////////////////////////////////////////////////////////// | |
45 // SuspiciousExtensionBubbleView | |
46 | |
47 namespace extensions { | |
48 | |
49 SuspiciousExtensionBubbleView::SuspiciousExtensionBubbleView( | |
50 views::View* anchor_view, | |
51 SuspiciousExtensionBubbleController* controller) | |
52 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), | |
53 weak_factory_(this), | |
54 controller_(controller), | |
55 headline_(NULL), | |
56 learn_more_(NULL), | |
57 dismiss_button_(NULL), | |
58 link_clicked_(false) { | |
59 DCHECK(anchor_view->GetWidget()); | |
60 set_close_on_deactivate(false); | |
61 set_move_with_anchor(true); | |
62 set_close_on_esc(true); | |
63 | |
64 // Compensate for built-in vertical padding in the anchor view's image. | |
65 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); | |
66 } | |
67 | |
68 // static | |
69 void SuspiciousExtensionBubbleView::MaybeShow( | |
70 Browser* browser, | |
71 views::View* anchor_view) { | |
72 SuspiciousExtensionBubbleController* controller = | |
73 extensions::SuspiciousExtensionBubbleController::Get( | |
74 browser->profile()); | |
75 if (controller->HasSuspiciousExtensions()) { | |
76 SuspiciousExtensionBubbleView* bubble_delegate = | |
77 new SuspiciousExtensionBubbleView(anchor_view, controller); | |
78 views::BubbleDelegateView::CreateBubble(bubble_delegate); | |
79 controller->Show(bubble_delegate); | |
80 } | |
81 } | |
82 | |
83 void SuspiciousExtensionBubbleView::OnButtonClicked( | |
84 const base::Closure& callback) { | |
85 button_callback_ = callback; | |
86 } | |
87 | |
88 void SuspiciousExtensionBubbleView::OnLinkClicked( | |
89 const base::Closure& callback) { | |
90 link_callback_ = callback; | |
91 } | |
92 | |
93 void SuspiciousExtensionBubbleView::Show() { | |
94 // Not showing the bubble right away (during startup) has a few benefits: | |
95 // We don't have to worry about focus being lost due to the Omnibox (or to | |
96 // other things that want focus at startup). This allows Esc to work to close | |
97 // the bubble and also solves the keyboard accessibility problem that comes | |
98 // with focus being lost (we don't have a good generic mechanism of injecting | |
99 // bubbles into the focus cycle). Another benefit of delaying the show is | |
100 // that fade-in works (the fade-in isn't apparent if the the bubble appears at | |
101 // startup). | |
102 base::MessageLoop::current()->PostDelayedTask( | |
103 FROM_HERE, | |
104 base::Bind(&SuspiciousExtensionBubbleView::ShowBubble, | |
105 weak_factory_.GetWeakPtr()), | |
106 base::TimeDelta::FromSeconds(kBubbleAppearanceWaitTime)); | |
107 } | |
108 | |
109 void SuspiciousExtensionBubbleView::OnWidgetDestroying(views::Widget* widget) { | |
110 // To catch Esc, we monitor destroy message. Unless the link has been clicked, | |
111 // we assume Dismiss was the action taken. | |
112 if (!link_clicked_) | |
113 button_callback_.Run(); | |
114 } | |
115 | |
116 //////////////////////////////////////////////////////////////////////////////// | |
117 // SuspiciousExtensionBubbleView - private. | |
118 | |
119 SuspiciousExtensionBubbleView::~SuspiciousExtensionBubbleView() { | |
120 } | |
121 | |
122 void SuspiciousExtensionBubbleView::ShowBubble() { | |
123 StartFade(true); | |
124 } | |
125 | |
126 void SuspiciousExtensionBubbleView::Init() { | |
127 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
128 | |
129 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
130 layout->SetInsets(kInsetTop, kInsetLeft, | |
131 kInsetBottomRight, kInsetBottomRight); | |
132 SetLayoutManager(layout); | |
133 | |
134 const int headline_column_set_id = 0; | |
135 views::ColumnSet* top_columns = layout->AddColumnSet(headline_column_set_id); | |
136 top_columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, | |
137 0, views::GridLayout::USE_PREF, 0, 0); | |
138 top_columns->AddPaddingColumn(1, 0); | |
139 layout->StartRow(0, headline_column_set_id); | |
140 | |
141 headline_ = new views::Label(); | |
142 headline_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); | |
143 headline_->SetText(controller_->GetTitle()); | |
144 layout->AddView(headline_); | |
145 | |
146 layout->AddPaddingRow(0, kHeadlineRowPadding); | |
147 | |
148 const int text_column_set_id = 1; | |
149 views::ColumnSet* upper_columns = layout->AddColumnSet(text_column_set_id); | |
150 upper_columns->AddColumn( | |
151 views::GridLayout::LEADING, views::GridLayout::LEADING, | |
152 0, views::GridLayout::USE_PREF, 0, 0); | |
153 layout->StartRow(0, text_column_set_id); | |
154 | |
155 views::Label* message = new views::Label(); | |
156 message->SetMultiLine(true); | |
157 message->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
158 message->SetText(controller_->GetMessageBody()); | |
159 message->SizeToFit(views::Widget::GetLocalizedContentsWidth( | |
160 IDS_EXTENSION_WIPEOUT_BUBBLE_WIDTH_CHARS)); | |
161 layout->AddView(message); | |
162 | |
163 const int extension_list_column_set_id = 2; | |
164 views::ColumnSet* middle_columns = | |
165 layout->AddColumnSet(extension_list_column_set_id); | |
166 middle_columns->AddPaddingColumn(0, kExtensionListPadding); | |
167 middle_columns->AddColumn( | |
168 views::GridLayout::LEADING, views::GridLayout::CENTER, | |
169 0, views::GridLayout::USE_PREF, 0, 0); | |
170 | |
171 layout->StartRowWithPadding(0, extension_list_column_set_id, | |
172 0, kHeadlineMessagePadding); | |
173 views::Label* extensions = new views::Label(); | |
174 extensions->SetMultiLine(true); | |
175 extensions->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
176 | |
177 std::vector<string16> extension_list; | |
178 char16 bullet_point = 0x2022; | |
179 | |
180 std::vector<string16> suspicious = controller_->GetSuspiciousExtensionNames(); | |
181 size_t i = 0; | |
182 for (; i < suspicious.size() && i < kMaxExtensionsToShow; ++i) { | |
183 // Add each extension with bullet point. | |
184 extension_list.push_back(bullet_point + ASCIIToUTF16(" ") + suspicious[i]); | |
185 } | |
186 | |
187 if (i > kMaxExtensionsToShow) { | |
188 string16 difference = base::IntToString16(i - kMaxExtensionsToShow); | |
189 extension_list.push_back(bullet_point + ASCIIToUTF16(" ") + | |
190 controller_->GetOverflowText(difference)); | |
191 } | |
192 | |
193 extensions->SetText(JoinString(extension_list, ASCIIToUTF16("\n"))); | |
194 extensions->SizeToFit(views::Widget::GetLocalizedContentsWidth( | |
195 IDS_EXTENSION_WIPEOUT_BUBBLE_WIDTH_CHARS)); | |
196 layout->AddView(extensions); | |
197 | |
198 const int action_row_column_set_id = 3; | |
199 views::ColumnSet* bottom_columns = | |
200 layout->AddColumnSet(action_row_column_set_id); | |
201 bottom_columns->AddColumn(views::GridLayout::LEADING, | |
202 views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0); | |
203 bottom_columns->AddPaddingColumn(1, 0); | |
204 bottom_columns->AddColumn(views::GridLayout::TRAILING, | |
205 views::GridLayout::CENTER, 0, views::GridLayout::USE_PREF, 0, 0); | |
206 layout->StartRowWithPadding(0, action_row_column_set_id, | |
207 0, kMessageBubblePadding); | |
208 | |
209 learn_more_ = new views::Link(controller_->GetLearnMoreLabel()); | |
210 learn_more_->set_listener(this); | |
211 layout->AddView(learn_more_); | |
212 | |
213 dismiss_button_ = new views::LabelButton(this, | |
214 controller_->GetDismissButtonLabel()); | |
215 dismiss_button_->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); | |
216 layout->AddView(dismiss_button_); | |
217 } | |
218 | |
219 void SuspiciousExtensionBubbleView::ButtonPressed(views::Button* sender, | |
220 const ui::Event& event) { | |
221 DCHECK_EQ(dismiss_button_, sender); | |
222 GetWidget()->Close(); | |
223 } | |
224 | |
225 void SuspiciousExtensionBubbleView::LinkClicked(views::Link* source, | |
226 int event_flags) { | |
227 DCHECK_EQ(learn_more_, source); | |
228 link_clicked_ = true; | |
229 link_callback_.Run(); | |
230 GetWidget()->Close(); | |
231 } | |
232 | |
233 void SuspiciousExtensionBubbleView::GetAccessibleState( | |
234 ui::AccessibleViewState* state) { | |
235 state->role = ui::AccessibilityTypes::ROLE_ALERT; | |
236 } | |
237 | |
238 void SuspiciousExtensionBubbleView::ViewHierarchyChanged( | |
239 const ViewHierarchyChangedDetails& details) { | |
240 if (details.is_add && details.child == this) | |
241 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_ALERT, true); | |
242 } | |
243 | |
244 } // namespace extensions | |
OLD | NEW |