Revert "Only store leading 13 bits of password hash."
[chromium-blink-merge.git] / chrome / browser / ui / views / omnibox / omnibox_popup_contents_view.cc
blob6c45dd4c5ddaeaae01226c97be013253cd813725
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h"
7 #include <algorithm>
9 #include "chrome/browser/search/search.h"
10 #include "chrome/browser/themes/theme_properties.h"
11 #include "chrome/browser/ui/omnibox/omnibox_view.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
14 #include "ui/base/theme_provider.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/path.h"
18 #include "ui/resources/grit/ui_resources.h"
19 #include "ui/views/controls/image_view.h"
20 #include "ui/views/view_targeter.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/views/window/non_client_view.h"
24 namespace {
26 // This is the number of pixels in the border image interior to the actual
27 // border.
28 const int kBorderInterior = 6;
30 } // namespace
32 class OmniboxPopupContentsView::AutocompletePopupWidget
33 : public views::Widget,
34 public base::SupportsWeakPtr<AutocompletePopupWidget> {
35 public:
36 AutocompletePopupWidget() {}
37 ~AutocompletePopupWidget() override {}
39 private:
40 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget);
43 ////////////////////////////////////////////////////////////////////////////////
44 // OmniboxPopupContentsView, public:
46 OmniboxPopupView* OmniboxPopupContentsView::Create(
47 const gfx::FontList& font_list,
48 OmniboxView* omnibox_view,
49 OmniboxEditModel* edit_model,
50 LocationBarView* location_bar_view) {
51 OmniboxPopupContentsView* view = NULL;
52 view = new OmniboxPopupContentsView(
53 font_list, omnibox_view, edit_model, location_bar_view);
54 view->Init();
55 return view;
58 OmniboxPopupContentsView::OmniboxPopupContentsView(
59 const gfx::FontList& font_list,
60 OmniboxView* omnibox_view,
61 OmniboxEditModel* edit_model,
62 LocationBarView* location_bar_view)
63 : model_(new OmniboxPopupModel(this, edit_model)),
64 omnibox_view_(omnibox_view),
65 location_bar_view_(location_bar_view),
66 font_list_(font_list),
67 ignore_mouse_drag_(false),
68 size_animation_(this),
69 left_margin_(0),
70 right_margin_(0),
71 outside_vertical_padding_(0) {
72 // The contents is owned by the LocationBarView.
73 set_owned_by_client();
75 ui::ThemeProvider* theme = location_bar_view_->GetThemeProvider();
76 bottom_shadow_ = theme->GetImageSkiaNamed(IDR_BUBBLE_B);
78 SetEventTargeter(
79 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
82 void OmniboxPopupContentsView::Init() {
83 // This can't be done in the constructor as at that point we aren't
84 // necessarily our final class yet, and we may have subclasses
85 // overriding CreateResultView.
86 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
87 OmniboxResultView* result_view = CreateResultView(i, font_list_);
88 result_view->SetVisible(false);
89 AddChildViewAt(result_view, static_cast<int>(i));
93 OmniboxPopupContentsView::~OmniboxPopupContentsView() {
94 // We don't need to do anything with |popup_| here. The OS either has already
95 // closed the window, in which case it's been deleted, or it will soon, in
96 // which case there's nothing we need to do.
99 gfx::Rect OmniboxPopupContentsView::GetPopupBounds() const {
100 if (!size_animation_.is_animating())
101 return target_bounds_;
103 gfx::Rect current_frame_bounds = start_bounds_;
104 int total_height_delta = target_bounds_.height() - start_bounds_.height();
105 // Round |current_height_delta| instead of truncating so we won't leave single
106 // white pixels at the bottom of the popup as long when animating very small
107 // height differences.
108 int current_height_delta = static_cast<int>(
109 size_animation_.GetCurrentValue() * total_height_delta - 0.5);
110 current_frame_bounds.set_height(
111 current_frame_bounds.height() + current_height_delta);
112 return current_frame_bounds;
115 void OmniboxPopupContentsView::LayoutChildren() {
116 gfx::Rect contents_rect = GetContentsBounds();
118 contents_rect.Inset(left_margin_,
119 views::NonClientFrameView::kClientEdgeThickness +
120 outside_vertical_padding_,
121 right_margin_, outside_vertical_padding_);
122 int top = contents_rect.y();
123 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
124 View* v = child_at(i);
125 if (v->visible()) {
126 v->SetBounds(contents_rect.x(), top, contents_rect.width(),
127 v->GetPreferredSize().height());
128 top = v->bounds().bottom();
133 ////////////////////////////////////////////////////////////////////////////////
134 // OmniboxPopupContentsView, OmniboxPopupView overrides:
136 bool OmniboxPopupContentsView::IsOpen() const {
137 return popup_ != NULL;
140 void OmniboxPopupContentsView::InvalidateLine(size_t line) {
141 OmniboxResultView* result = result_view_at(line);
142 result->Invalidate();
144 if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) {
145 result->ShowKeyword(IsSelectedIndex(line) &&
146 model_->selected_line_state() == OmniboxPopupModel::KEYWORD);
150 void OmniboxPopupContentsView::UpdatePopupAppearance() {
151 const size_t hidden_matches = model_->result().ShouldHideTopMatch() ? 1 : 0;
152 if (model_->result().size() <= hidden_matches ||
153 omnibox_view_->IsImeShowingPopup()) {
154 // No matches or the IME is showing a popup window which may overlap
155 // the omnibox popup window. Close any existing popup.
156 if (popup_ != NULL) {
157 size_animation_.Stop();
159 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
160 // triggered by the popup receiving a message (e.g. LBUTTONUP), and
161 // destroying the popup would cause us to read garbage when we unwind back
162 // to that level.
163 popup_->Close(); // This will eventually delete the popup.
164 popup_.reset();
166 return;
169 // Update the match cached by each row, in the process of doing so make sure
170 // we have enough row views.
171 const size_t result_size = model_->result().size();
172 max_match_contents_width_ = 0;
173 for (size_t i = 0; i < result_size; ++i) {
174 OmniboxResultView* view = result_view_at(i);
175 const AutocompleteMatch& match = GetMatchAtIndex(i);
176 view->SetMatch(match);
177 view->SetVisible(i >= hidden_matches);
178 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE) {
179 max_match_contents_width_ = std::max(
180 max_match_contents_width_, view->GetMatchContentsWidth());
184 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i)
185 child_at(i)->SetVisible(false);
187 gfx::Point top_left_screen_coord;
188 int width;
189 location_bar_view_->GetOmniboxPopupPositioningInfo(
190 &top_left_screen_coord, &width, &left_margin_, &right_margin_);
191 gfx::Rect new_target_bounds(top_left_screen_coord,
192 gfx::Size(width, CalculatePopupHeight()));
194 // If we're animating and our target height changes, reset the animation.
195 // NOTE: If we just reset blindly on _every_ update, then when the user types
196 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
197 // last few pixels to get to one visible result.
198 if (new_target_bounds.height() != target_bounds_.height())
199 size_animation_.Reset();
200 target_bounds_ = new_target_bounds;
202 if (popup_ == NULL) {
203 views::Widget* popup_parent = location_bar_view_->GetWidget();
205 // If the popup is currently closed, we need to create it.
206 popup_ = (new AutocompletePopupWidget)->AsWeakPtr();
207 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
208 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
209 params.parent = popup_parent->GetNativeView();
210 params.bounds = GetPopupBounds();
211 params.context = popup_parent->GetNativeWindow();
212 popup_->Init(params);
213 // Third-party software such as DigitalPersona identity verification can
214 // hook the underlying window creation methods and use SendMessage to
215 // synchronously change focus/activation, resulting in the popup being
216 // destroyed by the time control returns here. Bail out in this case to
217 // avoid a NULL dereference.
218 if (!popup_.get())
219 return;
220 popup_->SetVisibilityAnimationTransition(views::Widget::ANIMATE_NONE);
221 popup_->SetContentsView(this);
222 popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup());
223 if (!popup_.get()) {
224 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
225 // focus, thereby closing (and destroying) the popup.
226 // TODO(sky): this won't be needed once we close the omnibox on input
227 // window showing.
228 return;
230 popup_->ShowInactive();
231 } else {
232 // Animate the popup shrinking, but don't animate growing larger since that
233 // would make the popup feel less responsive.
234 start_bounds_ = GetWidget()->GetWindowBoundsInScreen();
235 if (target_bounds_.height() < start_bounds_.height())
236 size_animation_.Show();
237 else
238 start_bounds_ = target_bounds_;
239 popup_->SetBounds(GetPopupBounds());
242 Layout();
245 gfx::Rect OmniboxPopupContentsView::GetTargetBounds() {
246 return target_bounds_;
249 void OmniboxPopupContentsView::PaintUpdatesNow() {
250 // TODO(beng): remove this from the interface.
253 void OmniboxPopupContentsView::OnDragCanceled() {
254 ignore_mouse_drag_ = true;
257 ////////////////////////////////////////////////////////////////////////////////
258 // OmniboxPopupContentsView, OmniboxResultViewModel implementation:
260 bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const {
261 return index == model_->selected_line();
264 bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const {
265 return index == model_->hovered_line();
268 gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch(
269 size_t index) const {
270 if (!HasMatchAt(index))
271 return gfx::Image();
272 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index));
275 bool OmniboxPopupContentsView::IsStarredMatch(
276 const AutocompleteMatch& match) const {
277 return model_->IsStarredMatch(match);
280 ////////////////////////////////////////////////////////////////////////////////
281 // OmniboxPopupContentsView, AnimationDelegate implementation:
283 void OmniboxPopupContentsView::AnimationProgressed(
284 const gfx::Animation* animation) {
285 // We should only be running the animation when the popup is already visible.
286 DCHECK(popup_ != NULL);
287 popup_->SetBounds(GetPopupBounds());
290 ////////////////////////////////////////////////////////////////////////////////
291 // OmniboxPopupContentsView, views::View overrides:
293 void OmniboxPopupContentsView::Layout() {
294 // Size our children to the available content area.
295 LayoutChildren();
297 // We need to manually schedule a paint here since we are a layered window and
298 // won't implicitly require painting until we ask for one.
299 SchedulePaint();
302 views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
303 const gfx::Point& point) {
304 return NULL;
307 bool OmniboxPopupContentsView::OnMousePressed(
308 const ui::MouseEvent& event) {
309 ignore_mouse_drag_ = false; // See comment on |ignore_mouse_drag_| in header.
310 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
311 UpdateLineEvent(event, event.IsLeftMouseButton());
312 return true;
315 bool OmniboxPopupContentsView::OnMouseDragged(
316 const ui::MouseEvent& event) {
317 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
318 UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton());
319 return true;
322 void OmniboxPopupContentsView::OnMouseReleased(
323 const ui::MouseEvent& event) {
324 if (ignore_mouse_drag_) {
325 OnMouseCaptureLost();
326 return;
329 if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) {
330 OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB :
331 NEW_BACKGROUND_TAB);
335 void OmniboxPopupContentsView::OnMouseCaptureLost() {
336 ignore_mouse_drag_ = false;
339 void OmniboxPopupContentsView::OnMouseMoved(
340 const ui::MouseEvent& event) {
341 model_->SetHoveredLine(GetIndexForPoint(event.location()));
344 void OmniboxPopupContentsView::OnMouseEntered(
345 const ui::MouseEvent& event) {
346 model_->SetHoveredLine(GetIndexForPoint(event.location()));
349 void OmniboxPopupContentsView::OnMouseExited(
350 const ui::MouseEvent& event) {
351 model_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
354 void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent* event) {
355 switch (event->type()) {
356 case ui::ET_GESTURE_TAP_DOWN:
357 case ui::ET_GESTURE_SCROLL_BEGIN:
358 case ui::ET_GESTURE_SCROLL_UPDATE:
359 UpdateLineEvent(*event, true);
360 break;
361 case ui::ET_GESTURE_TAP:
362 case ui::ET_GESTURE_SCROLL_END:
363 OpenSelectedLine(*event, CURRENT_TAB);
364 break;
365 default:
366 return;
368 event->SetHandled();
371 ////////////////////////////////////////////////////////////////////////////////
372 // OmniboxPopupContentsView, protected:
374 void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) {
375 canvas->DrawColor(result_view_at(0)->GetColor(
376 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND));
377 View::PaintChildren(canvas, views::CullSet());
380 int OmniboxPopupContentsView::CalculatePopupHeight() {
381 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size());
382 int popup_height = 0;
383 for (size_t i = model_->result().ShouldHideTopMatch() ? 1 : 0;
384 i < model_->result().size(); ++i)
385 popup_height += child_at(i)->GetPreferredSize().height();
387 // Add enough space on the top and bottom so it looks like there is the same
388 // amount of space between the text and the popup border as there is in the
389 // interior between each row of text.
391 // Discovering the exact amount of leading and padding around the font is
392 // a bit tricky and platform-specific, but this computation seems to work in
393 // practice.
394 OmniboxResultView* result_view = result_view_at(0);
395 outside_vertical_padding_ =
396 (result_view->GetPreferredSize().height() -
397 result_view->GetTextHeight());
399 return popup_height +
400 views::NonClientFrameView::kClientEdgeThickness + // Top border.
401 outside_vertical_padding_ * 2 + // Padding.
402 bottom_shadow_->height() - kBorderInterior; // Bottom border.
405 OmniboxResultView* OmniboxPopupContentsView::CreateResultView(
406 int model_index,
407 const gfx::FontList& font_list) {
408 return new OmniboxResultView(this, model_index, location_bar_view_,
409 font_list);
412 ////////////////////////////////////////////////////////////////////////////////
413 // OmniboxPopupContentsView, views::View overrides, private:
415 const char* OmniboxPopupContentsView::GetClassName() const {
416 return "OmniboxPopupContentsView";
419 void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) {
420 gfx::Rect contents_bounds = GetContentsBounds();
421 contents_bounds.set_height(
422 contents_bounds.height() - bottom_shadow_->height() + kBorderInterior);
424 gfx::Path path;
425 MakeContentsPath(&path, contents_bounds);
426 canvas->Save();
427 canvas->sk_canvas()->clipPath(path,
428 SkRegion::kIntersect_Op,
429 true /* doAntialias */);
430 PaintResultViews(canvas);
431 canvas->Restore();
433 // Top border.
434 canvas->FillRect(
435 gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness),
436 ThemeProperties::GetDefaultColor(
437 ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
439 // Bottom border.
440 canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(),
441 width(), bottom_shadow_->height());
444 void OmniboxPopupContentsView::PaintChildren(gfx::Canvas* canvas,
445 const views::CullSet& cull_set) {
446 // We paint our children inside OnPaint().
449 ////////////////////////////////////////////////////////////////////////////////
450 // OmniboxPopupContentsView, private:
452 views::View* OmniboxPopupContentsView::TargetForRect(views::View* root,
453 const gfx::Rect& rect) {
454 CHECK_EQ(root, this);
455 return this;
458 bool OmniboxPopupContentsView::HasMatchAt(size_t index) const {
459 return index < model_->result().size();
462 const AutocompleteMatch& OmniboxPopupContentsView::GetMatchAtIndex(
463 size_t index) const {
464 return model_->result().match_at(index);
467 void OmniboxPopupContentsView::MakeContentsPath(
468 gfx::Path* path,
469 const gfx::Rect& bounding_rect) {
470 SkRect rect;
471 rect.set(SkIntToScalar(bounding_rect.x()),
472 SkIntToScalar(bounding_rect.y()),
473 SkIntToScalar(bounding_rect.right()),
474 SkIntToScalar(bounding_rect.bottom()));
475 path->addRect(rect);
478 size_t OmniboxPopupContentsView::GetIndexForPoint(
479 const gfx::Point& point) {
480 if (!HitTestPoint(point))
481 return OmniboxPopupModel::kNoMatch;
483 int nb_match = model_->result().size();
484 DCHECK(nb_match <= child_count());
485 for (int i = 0; i < nb_match; ++i) {
486 views::View* child = child_at(i);
487 gfx::Point point_in_child_coords(point);
488 View::ConvertPointToTarget(this, child, &point_in_child_coords);
489 if (child->visible() && child->HitTestPoint(point_in_child_coords))
490 return i;
492 return OmniboxPopupModel::kNoMatch;
495 void OmniboxPopupContentsView::UpdateLineEvent(
496 const ui::LocatedEvent& event,
497 bool should_set_selected_line) {
498 size_t index = GetIndexForPoint(event.location());
499 model_->SetHoveredLine(index);
500 if (HasMatchAt(index) && should_set_selected_line)
501 model_->SetSelectedLine(index, false, false);
504 void OmniboxPopupContentsView::OpenSelectedLine(
505 const ui::LocatedEvent& event,
506 WindowOpenDisposition disposition) {
507 size_t index = GetIndexForPoint(event.location());
508 if (!HasMatchAt(index))
509 return;
510 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition,
511 GURL(), base::string16(), index);
514 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) {
515 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i)));