Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_result_view.cc

Issue 917333004: Answers in Suggest icon downloading (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cancel image fetch even if the next suggestion has no answer record Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/views/omnibox/omnibox_result_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // For WinDDK ATL compatibility, these ATL headers must come first. 5 // For WinDDK ATL compatibility, these ATL headers must come first.
6 #include "build/build_config.h" 6 #include "build/build_config.h"
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <atlbase.h> // NOLINT 8 #include <atlbase.h> // NOLINT
9 #include <atlwin.h> // NOLINT 9 #include <atlwin.h> // NOLINT
10 #endif 10 #endif
11 11
12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" 12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
13 13
14 #include <algorithm> // NOLINT 14 #include <algorithm> // NOLINT
15 15
16 #include "base/i18n/bidi_line_iterator.h" 16 #include "base/i18n/bidi_line_iterator.h"
17 #include "base/memory/scoped_vector.h" 17 #include "base/memory/scoped_vector.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
21 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" 22 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
21 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 23 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
22 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" 24 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h"
23 #include "chrome/grit/generated_resources.h" 25 #include "chrome/grit/generated_resources.h"
24 #include "components/omnibox/suggestion_answer.h" 26 #include "components/omnibox/suggestion_answer.h"
25 #include "grit/components_scaled_resources.h" 27 #include "grit/components_scaled_resources.h"
26 #include "grit/theme_resources.h" 28 #include "grit/theme_resources.h"
27 #include "ui/base/l10n/l10n_util.h" 29 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/theme_provider.h" 30 #include "ui/base/theme_provider.h"
29 #include "ui/gfx/canvas.h" 31 #include "ui/gfx/canvas.h"
30 #include "ui/gfx/color_utils.h" 32 #include "ui/gfx/color_utils.h"
31 #include "ui/gfx/image/image.h" 33 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/range/range.h" 34 #include "ui/gfx/range/range.h"
33 #include "ui/gfx/render_text.h" 35 #include "ui/gfx/render_text.h"
34 #include "ui/gfx/text_utils.h" 36 #include "ui/gfx/text_utils.h"
35 #include "ui/native_theme/native_theme.h" 37 #include "ui/native_theme/native_theme.h"
36 38
37 using ui::NativeTheme; 39 using ui::NativeTheme;
38 40
39 namespace { 41 namespace {
40 42
43 // Calls back to the OmniboxResultView when the requested image is downloaded.
44 // This is a separate class instead of being implemented on OmniboxResultView
45 // because BitmapFetcherService currently takes ownership of this object.
46 // TODO(dschuyler): Make BitmapFetcherService use the more typical non-owning
47 // ObserverList pattern and have OmniboxResultView implement the Observer call
48 // directly.
49 class AnswersImageObserverDesktop : public BitmapFetcherService::Observer {
Peter Kasting 2015/03/05 23:11:16 Nit: How come this has such a long name (and plura
dschuyler 2015/03/05 23:53:07 Done.
50 public:
51 explicit AnswersImageObserverDesktop(
52 const base::WeakPtr<OmniboxResultView>& view)
53 : view_(view) {}
54
55 void OnImageChanged(BitmapFetcherService::RequestId request_id,
56 const SkBitmap& image) override;
57
58 private:
59 const base::WeakPtr<OmniboxResultView> view_;
60 DISALLOW_COPY_AND_ASSIGN(AnswersImageObserverDesktop);
61 };
62
63 void AnswersImageObserverDesktop::OnImageChanged(
64 BitmapFetcherService::RequestId request_id,
65 const SkBitmap& image) {
66 DCHECK(!image.empty());
Peter Kasting 2015/03/05 23:11:16 It sounds like this DCHECK is safe per the code in
dschuyler 2015/03/05 23:53:07 I added a comment for me to follow up on whether t
67 if (view_)
68 view_->SetAnswerImage(gfx::ImageSkia::CreateFrom1xBitmap(image));
69 }
70
41 // The minimum distance between the top and bottom of the {icon|text} and the 71 // The minimum distance between the top and bottom of the {icon|text} and the
42 // top or bottom of the row. 72 // top or bottom of the row.
43 const int kMinimumIconVerticalPadding = 2; 73 const int kMinimumIconVerticalPadding = 2;
44 const int kMinimumTextVerticalPadding = 3; 74 const int kMinimumTextVerticalPadding = 3;
45 75
46 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to 76 // A mapping from OmniboxResultView's ResultViewState/ColorKind types to
47 // NativeTheme colors. 77 // NativeTheme colors.
48 struct TranslationTable { 78 struct TranslationTable {
49 ui::NativeTheme::ColorId id; 79 ui::NativeTheme::ColorId id;
50 OmniboxResultView::ResultViewState state; 80 OmniboxResultView::ResultViewState state;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model, 156 OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model,
127 int model_index, 157 int model_index,
128 LocationBarView* location_bar_view, 158 LocationBarView* location_bar_view,
129 const gfx::FontList& font_list) 159 const gfx::FontList& font_list)
130 : edge_item_padding_(LocationBarView::kItemPadding), 160 : edge_item_padding_(LocationBarView::kItemPadding),
131 item_padding_(LocationBarView::kItemPadding), 161 item_padding_(LocationBarView::kItemPadding),
132 minimum_text_vertical_padding_(kMinimumTextVerticalPadding), 162 minimum_text_vertical_padding_(kMinimumTextVerticalPadding),
133 model_(model), 163 model_(model),
134 model_index_(model_index), 164 model_index_(model_index),
135 location_bar_view_(location_bar_view), 165 location_bar_view_(location_bar_view),
166 image_service_(BitmapFetcherServiceFactory::GetForBrowserContext(
167 location_bar_view_->profile())),
136 font_list_(font_list), 168 font_list_(font_list),
137 font_height_( 169 font_height_(
138 std::max(font_list.GetHeight(), 170 std::max(font_list.GetHeight(),
139 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())), 171 font_list.DeriveWithStyle(gfx::Font::BOLD).GetHeight())),
140 mirroring_context_(new MirroringContext()), 172 mirroring_context_(new MirroringContext()),
141 keyword_icon_(new views::ImageView()), 173 keyword_icon_(new views::ImageView()),
142 animation_(new gfx::SlideAnimation(this)) { 174 animation_(new gfx::SlideAnimation(this)),
175 request_id_(BitmapFetcherService::REQUEST_ID_INVALID),
176 weak_ptr_factory_(this) {
143 CHECK_GE(model_index, 0); 177 CHECK_GE(model_index, 0);
144 if (default_icon_size_ == 0) { 178 if (default_icon_size_ == 0) {
145 default_icon_size_ = 179 default_icon_size_ =
146 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( 180 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed(
147 AutocompleteMatch::TypeToIcon( 181 AutocompleteMatch::TypeToIcon(
148 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width(); 182 AutocompleteMatchType::URL_WHAT_YOU_TYPED))->width();
149 } 183 }
150 keyword_icon_->set_owned_by_client(); 184 keyword_icon_->set_owned_by_client();
151 keyword_icon_->EnableCanvasFlippingForRTLUI(true); 185 keyword_icon_->EnableCanvasFlippingForRTLUI(true);
152 keyword_icon_->SetImage(GetKeywordIcon()); 186 keyword_icon_->SetImage(GetKeywordIcon());
153 keyword_icon_->SizeToPreferredSize(); 187 keyword_icon_->SizeToPreferredSize();
154 } 188 }
155 189
156 OmniboxResultView::~OmniboxResultView() { 190 OmniboxResultView::~OmniboxResultView() {
191 if (image_service_)
192 image_service_->CancelRequest(request_id_);
157 } 193 }
158 194
159 SkColor OmniboxResultView::GetColor( 195 SkColor OmniboxResultView::GetColor(
160 ResultViewState state, 196 ResultViewState state,
161 ColorKind kind) const { 197 ColorKind kind) const {
162 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) { 198 for (size_t i = 0; i < arraysize(kTranslationTable); ++i) {
163 if (kTranslationTable[i].state == state && 199 if (kTranslationTable[i].state == state &&
164 kTranslationTable[i].kind == kind) { 200 kTranslationTable[i].kind == kind) {
165 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id); 201 return GetNativeTheme()->GetSystemColor(kTranslationTable[i].id);
166 } 202 }
167 } 203 }
168 204
169 NOTREACHED(); 205 NOTREACHED();
170 return SK_ColorRED; 206 return SK_ColorRED;
171 } 207 }
172 208
173 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) { 209 void OmniboxResultView::SetMatch(const AutocompleteMatch& match) {
174 match_ = match; 210 match_ = match;
175 ResetRenderTexts(); 211 ResetRenderTexts();
176 animation_->Reset(); 212 animation_->Reset();
177 213
214 answer_image_ = gfx::ImageSkia();
215 if (image_service_) {
216 image_service_->CancelRequest(request_id_);
217 if (match_.answer)
Peter Kasting 2015/03/05 23:11:16 Nit: Need {} since body takes >1 physical line
dschuyler 2015/03/05 23:53:07 Done.
218 request_id_ = image_service_->RequestImage(
219 match_.answer->second_line().image_url(),
220 new AnswersImageObserverDesktop(weak_ptr_factory_.GetWeakPtr()));
221 }
222
178 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get(); 223 AutocompleteMatch* associated_keyword_match = match_.associated_keyword.get();
179 if (associated_keyword_match) { 224 if (associated_keyword_match) {
180 keyword_icon_->SetImage(GetKeywordIcon()); 225 keyword_icon_->SetImage(GetKeywordIcon());
181 if (!keyword_icon_->parent()) 226 if (!keyword_icon_->parent())
182 AddChildView(keyword_icon_.get()); 227 AddChildView(keyword_icon_.get());
183 } else if (keyword_icon_->parent()) { 228 } else if (keyword_icon_->parent()) {
184 RemoveChildView(keyword_icon_.get()); 229 RemoveChildView(keyword_icon_.get());
185 } 230 }
186 231
187 Layout(); 232 Layout();
(...skipping 27 matching lines...) Expand all
215 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const { 260 OmniboxResultView::ResultViewState OmniboxResultView::GetState() const {
216 if (model_->IsSelectedIndex(model_index_)) 261 if (model_->IsSelectedIndex(model_index_))
217 return SELECTED; 262 return SELECTED;
218 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; 263 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL;
219 } 264 }
220 265
221 int OmniboxResultView::GetTextHeight() const { 266 int OmniboxResultView::GetTextHeight() const {
222 return font_height_; 267 return font_height_;
223 } 268 }
224 269
225 void OmniboxResultView::PaintMatch( 270 void OmniboxResultView::PaintMatch(const AutocompleteMatch& match,
226 const AutocompleteMatch& match, 271 gfx::RenderText* contents,
227 gfx::RenderText* contents, 272 gfx::RenderText* description,
228 gfx::RenderText* description, 273 gfx::Canvas* canvas,
229 gfx::Canvas* canvas, 274 int x) const {
230 int x) const {
231 int y = text_bounds_.y(); 275 int y = text_bounds_.y();
232 276
233 if (!separator_rendertext_) { 277 if (!separator_rendertext_) {
234 const base::string16& separator = 278 const base::string16& separator =
235 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR); 279 l10n_util::GetStringUTF16(IDS_AUTOCOMPLETE_MATCH_DESCRIPTION_SEPARATOR);
236 separator_rendertext_.reset(CreateRenderText(separator).release()); 280 separator_rendertext_.reset(CreateRenderText(separator).release());
237 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT)); 281 separator_rendertext_->SetColor(GetColor(GetState(), DIMMED_TEXT));
238 separator_width_ = separator_rendertext_->GetContentWidth(); 282 separator_width_ = separator_rendertext_->GetContentWidth();
239 } 283 }
240 284
241 int contents_max_width, description_max_width; 285 int contents_max_width, description_max_width;
242 OmniboxPopupModel::ComputeMatchMaxWidths( 286 OmniboxPopupModel::ComputeMatchMaxWidths(
243 contents->GetContentWidth(), 287 contents->GetContentWidth(),
244 separator_width_, 288 separator_width_,
245 description ? description->GetContentWidth() : 0, 289 description ? description->GetContentWidth() : 0,
246 mirroring_context_->remaining_width(x), 290 mirroring_context_->remaining_width(x),
247 !AutocompleteMatch::IsSearchType(match.type), 291 !AutocompleteMatch::IsSearchType(match.type),
248 &contents_max_width, 292 &contents_max_width,
249 &description_max_width); 293 &description_max_width);
250 294
251 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width); 295 x = DrawRenderText(match, contents, true, canvas, x, y, contents_max_width);
252 296
253 if (description_max_width != 0) { 297 if (description_max_width != 0) {
254 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y, 298 x = DrawRenderText(match, separator_rendertext_.get(), false, canvas, x, y,
255 separator_width_); 299 separator_width_);
300
301 if (!answer_image_.size().IsEmpty()) {
Peter Kasting 2015/03/05 23:11:16 I wasn't thinking through this well enough. A sim
dschuyler 2015/03/05 23:53:07 Done.
302 canvas->DrawImageInt(answer_image_,
303 // Source x, y, w, h.
304 0, 0, answer_image_.width(), answer_image_.height(),
305 // Destination x, y, w, h.
306 GetMirroredXInView(x),
307 y + kMinimumIconVerticalPadding, default_icon_size_,
308 default_icon_size_, true);
309 x += default_icon_size_ + LocationBarView::kIconInternalPadding;
310 }
311
256 DrawRenderText(match, description, false, canvas, x, y, 312 DrawRenderText(match, description, false, canvas, x, y,
257 description_max_width); 313 description_max_width);
258 } 314 }
259 } 315 }
260 316
261 int OmniboxResultView::DrawRenderText( 317 int OmniboxResultView::DrawRenderText(
262 const AutocompleteMatch& match, 318 const AutocompleteMatch& match,
263 gfx::RenderText* render_text, 319 gfx::RenderText* render_text,
264 bool contents, 320 bool contents,
265 gfx::Canvas* canvas, 321 gfx::Canvas* canvas,
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range); 445 render_text->ApplyColor(GetColor(GetState(), color_kind), current_range);
390 } 446 }
391 return render_text.Pass(); 447 return render_text.Pass();
392 } 448 }
393 449
394 int OmniboxResultView::GetMatchContentsWidth() const { 450 int OmniboxResultView::GetMatchContentsWidth() const {
395 InitContentsRenderTextIfNecessary(); 451 InitContentsRenderTextIfNecessary();
396 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0; 452 return contents_rendertext_ ? contents_rendertext_->GetContentWidth() : 0;
397 } 453 }
398 454
455 void OmniboxResultView::SetAnswerImage(const gfx::ImageSkia& image) {
456 answer_image_ = image;
457 SchedulePaint();
458 }
459
399 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in 460 // TODO(skanuj): This is probably identical across all OmniboxResultView rows in
400 // the omnibox dropdown. Consider sharing the result. 461 // the omnibox dropdown. Consider sharing the result.
401 int OmniboxResultView::GetDisplayOffset( 462 int OmniboxResultView::GetDisplayOffset(
402 const AutocompleteMatch& match, 463 const AutocompleteMatch& match,
403 bool is_ui_rtl, 464 bool is_ui_rtl,
404 bool is_match_contents_rtl) const { 465 bool is_match_contents_rtl) const {
405 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL) 466 if (match.type != AutocompleteMatchType::SEARCH_SUGGEST_TAIL)
406 return 0; 467 return 0;
407 468
408 const base::string16& input_text = 469 const base::string16& input_text =
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 } 629 }
569 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(), 630 PaintMatch(*keyword_match, keyword_contents_rendertext_.get(),
570 keyword_description_rendertext_.get(), canvas, x); 631 keyword_description_rendertext_.get(), canvas, x);
571 } 632 }
572 } 633 }
573 634
574 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) { 635 void OmniboxResultView::AnimationProgressed(const gfx::Animation* animation) {
575 Layout(); 636 Layout();
576 SchedulePaint(); 637 SchedulePaint();
577 } 638 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/omnibox/omnibox_result_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698