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/autofill/autofill_popup_base_view.h"
6
7 #include <algorithm>
8 #include <memory>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "chrome/browser/platform_util.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/views/autofill/autofill_popup_view_utils.h"
19 #include "chrome/browser/ui/views/chrome_layout_provider.h"
20 #include "chrome/browser/ui/views/frame/browser_view.h"
21 #include "chrome/browser/ui/views/frame/contents_web_view.h"
22 #include "components/strings/grit/components_strings.h"
23 #include "ui/accessibility/ax_enums.mojom.h"
24 #include "ui/accessibility/platform/ax_platform_node.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/gfx/color_palette.h"
27 #include "ui/native_theme/native_theme.h"
28 #include "ui/views/accessibility/view_accessibility.h"
29 #include "ui/views/border.h"
30 #include "ui/views/bubble/bubble_border.h"
31 #include "ui/views/focus/focus_manager.h"
32 #include "ui/views/layout/fill_layout.h"
33
34 namespace autofill {
35
GetCornerRadius()36 int AutofillPopupBaseView::GetCornerRadius() {
37 return ChromeLayoutProvider::Get()->GetCornerRadiusMetric(
38 views::EMPHASIS_MEDIUM);
39 }
40
GetBackgroundColor()41 SkColor AutofillPopupBaseView::GetBackgroundColor() {
42 return GetNativeTheme()->GetSystemColor(
43 ui::NativeTheme::kColorId_DropdownBackgroundColor);
44 }
45
GetForegroundColor()46 SkColor AutofillPopupBaseView::GetForegroundColor() {
47 return GetNativeTheme()->GetSystemColor(
48 ui::NativeTheme::kColorId_DropdownForegroundColor);
49 }
50
GetSelectedBackgroundColor()51 SkColor AutofillPopupBaseView::GetSelectedBackgroundColor() {
52 return GetNativeTheme()->GetSystemColor(
53 ui::NativeTheme::kColorId_DropdownSelectedBackgroundColor);
54 }
55
GetSelectedForegroundColor()56 SkColor AutofillPopupBaseView::GetSelectedForegroundColor() {
57 return GetNativeTheme()->GetSystemColor(
58 ui::NativeTheme::kColorId_DropdownSelectedForegroundColor);
59 }
60
GetFooterBackgroundColor()61 SkColor AutofillPopupBaseView::GetFooterBackgroundColor() {
62 return GetNativeTheme()->GetSystemColor(
63 ui::NativeTheme::kColorId_BubbleFooterBackground);
64 }
65
GetSeparatorColor()66 SkColor AutofillPopupBaseView::GetSeparatorColor() {
67 return GetNativeTheme()->GetSystemColor(
68 ui::NativeTheme::kColorId_MenuSeparatorColor);
69 }
70
GetWarningColor()71 SkColor AutofillPopupBaseView::GetWarningColor() {
72 return GetNativeTheme()->GetSystemColor(
73 ui::NativeTheme::kColorId_AlertSeverityHigh);
74 }
75
AutofillPopupBaseView(AutofillPopupViewDelegate * delegate,views::Widget * parent_widget)76 AutofillPopupBaseView::AutofillPopupBaseView(
77 AutofillPopupViewDelegate* delegate,
78 views::Widget* parent_widget)
79 : delegate_(delegate), parent_widget_(parent_widget) {}
80
~AutofillPopupBaseView()81 AutofillPopupBaseView::~AutofillPopupBaseView() {
82 if (delegate_) {
83 delegate_->ViewDestroyed();
84
85 RemoveWidgetObservers();
86 }
87
88 CHECK(!IsInObserverList());
89 }
90
DoShow()91 void AutofillPopupBaseView::DoShow() {
92 const bool initialize_widget = !GetWidget();
93 if (initialize_widget) {
94 // On Mac Cocoa browser, |parent_widget_| is null (the parent is not a
95 // views::Widget).
96 // TODO(crbug.com/826862): Remove |parent_widget_|.
97 if (parent_widget_)
98 parent_widget_->AddObserver(this);
99
100 // The widget is destroyed by the corresponding NativeWidget, so we use
101 // a weak pointer to hold the reference and don't have to worry about
102 // deletion.
103 views::Widget* widget = new views::Widget;
104 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
105 params.delegate = this;
106 params.parent = parent_widget_ ? parent_widget_->GetNativeView()
107 : delegate_->container_view();
108 // Ensure the bubble border is not painted on an opaque background.
109 params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
110 params.shadow_type = views::Widget::InitParams::ShadowType::kNone;
111 widget->Init(std::move(params));
112 widget->AddObserver(this);
113
114 // No animation for popup appearance (too distracting).
115 widget->SetVisibilityAnimationTransition(views::Widget::ANIMATE_HIDE);
116
117 show_time_ = base::Time::Now();
118 }
119
120 GetWidget()->GetRootView()->SetBorder(CreateBorder());
121 bool enough_height = DoUpdateBoundsAndRedrawPopup();
122 // If there is insufficient height, DoUpdateBoundsAndRedrawPopup() hides and
123 // thus deletes |this|. Hence, there is nothing else to do.
124 if (!enough_height)
125 return;
126 GetWidget()->Show();
127
128 // Showing the widget can change native focus (which would result in an
129 // immediate hiding of the popup). Only start observing after shown.
130 if (initialize_widget)
131 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this);
132 }
133
DoHide()134 void AutofillPopupBaseView::DoHide() {
135 // The controller is no longer valid after it hides us.
136 delegate_ = nullptr;
137
138 RemoveWidgetObservers();
139
140 if (GetWidget()) {
141 // Don't call CloseNow() because some of the functions higher up the stack
142 // assume the the widget is still valid after this point.
143 // http://crbug.com/229224
144 // NOTE: This deletes |this|.
145 GetWidget()->Close();
146 } else {
147 delete this;
148 }
149 }
150
VisibilityChanged(View * starting_from,bool is_visible)151 void AutofillPopupBaseView::VisibilityChanged(View* starting_from,
152 bool is_visible) {
153 if (!is_visible) {
154 if (is_ax_menu_start_event_fired_) {
155 // Fire menu end event.
156 // The menu start event is delayed until the user
157 // navigates into the menu, otherwise some screen readers will ignore
158 // any focus events outside of the menu, including a focus event on
159 // the form control itself.
160 NotifyAccessibilityEvent(ax::mojom::Event::kMenuEnd, true);
161 GetViewAccessibility().EndPopupFocusOverride();
162 }
163 is_ax_menu_start_event_fired_ = false;
164 }
165 }
166
NotifyAXSelection(View * selected_view)167 void AutofillPopupBaseView::NotifyAXSelection(View* selected_view) {
168 DCHECK(selected_view);
169 if (!is_ax_menu_start_event_fired_) {
170 // Fire the menu start event once, right before the first item is selected.
171 // By firing these and the matching kMenuEnd events, we are telling screen
172 // readers that the focus is only changing temporarily, and the screen
173 // reader will restore the focus back to the appropriate textfield when the
174 // menu closes.
175 NotifyAccessibilityEvent(ax::mojom::Event::kMenuStart, true);
176 is_ax_menu_start_event_fired_ = true;
177 }
178 selected_view->GetViewAccessibility().SetPopupFocusOverride();
179 selected_view->NotifyAccessibilityEvent(ax::mojom::Event::kSelection, true);
180 }
181
OnWidgetBoundsChanged(views::Widget * widget,const gfx::Rect & new_bounds)182 void AutofillPopupBaseView::OnWidgetBoundsChanged(views::Widget* widget,
183 const gfx::Rect& new_bounds) {
184 DCHECK(widget == parent_widget_ || widget == GetWidget());
185 if (widget != parent_widget_)
186 return;
187
188 HideController(PopupHidingReason::kWidgetChanged);
189 }
190
OnWidgetDestroying(views::Widget * widget)191 void AutofillPopupBaseView::OnWidgetDestroying(views::Widget* widget) {
192 // On Windows, widgets can be destroyed in any order. Regardless of which
193 // widget is destroyed first, remove all observers and hide the popup.
194 DCHECK(widget == parent_widget_ || widget == GetWidget());
195
196 // Normally this happens at destruct-time or hide-time, but because it depends
197 // on |parent_widget_| (which is about to go away), it needs to happen sooner
198 // in this case.
199 RemoveWidgetObservers();
200
201 // Because the parent widget is about to be destroyed, we null out the weak
202 // reference to it and protect against possibly accessing it during
203 // destruction (e.g., by attempting to remove observers).
204 parent_widget_ = nullptr;
205
206 HideController(PopupHidingReason::kWidgetChanged);
207 }
208
RemoveWidgetObservers()209 void AutofillPopupBaseView::RemoveWidgetObservers() {
210 if (parent_widget_)
211 parent_widget_->RemoveObserver(this);
212 GetWidget()->RemoveObserver(this);
213
214 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this);
215 }
216
UpdateClipPath()217 void AutofillPopupBaseView::UpdateClipPath() {
218 SkRect local_bounds = gfx::RectToSkRect(GetLocalBounds());
219 SkScalar radius = SkIntToScalar(GetCornerRadius());
220 SkPath clip_path;
221 clip_path.addRoundRect(local_bounds, radius, radius);
222 SetClipPath(clip_path);
223 }
224
GetWindowBounds() const225 gfx::Rect AutofillPopupBaseView::GetWindowBounds() const {
226 views::Widget* widget = views::Widget::GetTopLevelWidgetForNativeView(
227 delegate()->container_view());
228 if (widget)
229 return widget->GetWindowBoundsInScreen();
230
231 // If the widget is null, simply return an empty rect. The most common reason
232 // to end up here is that the NativeView has been destroyed externally, which
233 // can happen at any time. This happens fairly commonly on Windows (e.g., at
234 // shutdown) in particular.
235 return gfx::Rect();
236 }
237
GetContentAreaBounds() const238 gfx::Rect AutofillPopupBaseView::GetContentAreaBounds() const {
239 content::WebContents* web_contents = delegate()->GetWebContents();
240 if (web_contents)
241 return web_contents->GetContainerBounds();
242
243 // If the |web_contents| is null, simply return an empty rect. The most common
244 // reason to end up here is that the |web_contents| has been destroyed
245 // externally, which can happen at any time. This happens fairly commonly on
246 // Windows (e.g., at shutdown) in particular.
247 return gfx::Rect();
248 }
249
DoUpdateBoundsAndRedrawPopup()250 bool AutofillPopupBaseView::DoUpdateBoundsAndRedrawPopup() {
251 gfx::Size preferred_size = GetPreferredSize();
252
253 // When a bubble border is shown, the contents area (inside the shadow) is
254 // supposed to be aligned with input element boundaries.
255 gfx::Rect element_bounds = gfx::ToEnclosingRect(delegate()->element_bounds());
256 element_bounds.Inset(/*horizontal=*/0, /*vertical=*/-kElementBorderPadding);
257
258 // At least one row of the popup should be shown in the bounds of the content
259 // area so that the user notices the presence of the popup.
260 int item_height =
261 children().size() > 0 ? children()[0]->GetPreferredSize().height() : 0;
262 if (!HasEnoughHeightForOneRow(item_height, GetContentAreaBounds(),
263 element_bounds)) {
264 HideController(PopupHidingReason::kInsufficientSpace);
265 return false;
266 }
267
268 gfx::Rect popup_bounds = CalculatePopupBounds(
269 preferred_size, GetWindowBounds(), element_bounds, delegate()->IsRTL());
270 // Account for the scroll view's border so that the content has enough space.
271 popup_bounds.Inset(-GetWidget()->GetRootView()->border()->GetInsets());
272 GetWidget()->SetBounds(popup_bounds);
273
274 Layout();
275 UpdateClipPath();
276 SchedulePaint();
277 return true;
278 }
279
OnNativeFocusChanged(gfx::NativeView focused_now)280 void AutofillPopupBaseView::OnNativeFocusChanged(gfx::NativeView focused_now) {
281 if (GetWidget() && GetWidget()->GetNativeView() != focused_now)
282 HideController(PopupHidingReason::kFocusChanged);
283 }
284
GetAccessibleNodeData(ui::AXNodeData * node_data)285 void AutofillPopupBaseView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
286 // TODO(aleventhal) The correct role spec-wise to use here is kMenu, however
287 // as of NVDA 2018.2.1, firing a menu event with kMenu breaks left/right
288 // arrow editing feedback in text field. If NVDA addresses this we should
289 // consider returning to using kMenu, so that users are notified that a
290 // menu popup has been shown.
291 node_data->role = ax::mojom::Role::kPane;
292 node_data->SetName(
293 l10n_util::GetStringUTF16(IDS_AUTOFILL_POPUP_ACCESSIBLE_NODE_DATA));
294 }
295
HideController(PopupHidingReason reason)296 void AutofillPopupBaseView::HideController(PopupHidingReason reason) {
297 if (delegate_)
298 delegate_->Hide(reason);
299 // This will eventually result in the deletion of |this|, as the delegate
300 // will hide |this|. See |DoHide| above for an explanation on why the precise
301 // timing of that deletion is tricky.
302 }
303
CreateBorder()304 std::unique_ptr<views::Border> AutofillPopupBaseView::CreateBorder() {
305 auto border = std::make_unique<views::BubbleBorder>(
306 views::BubbleBorder::NONE, views::BubbleBorder::SMALL_SHADOW,
307 SK_ColorWHITE);
308 border->SetCornerRadius(GetCornerRadius());
309 border->set_md_shadow_elevation(
310 ChromeLayoutProvider::Get()->GetShadowElevationMetric(
311 views::EMPHASIS_MEDIUM));
312 return border;
313 }
314
container_view()315 gfx::NativeView AutofillPopupBaseView::container_view() {
316 return delegate_->container_view();
317 }
318
319 } // namespace autofill
320