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

Side by Side Diff: components/favicon/core/favicon_selector.h

Issue 2739173002: Always select best favicon bitmap (Closed)
Patch Set: Rebased. Created 3 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
OLDNEW
(Empty)
1 // Copyright (c) 2017 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 #ifndef COMPONENTS_FAVICON_CORE_FAVICON_SELECTOR_H_
6 #define COMPONENTS_FAVICON_CORE_FAVICON_SELECTOR_H_
7
8 #include <list>
9 #include <memory>
10 #include <utility>
11 #include <vector>
12
13 #include "base/macros.h"
14 #include "base/optional.h"
15 #include "components/favicon/core/favicon_url.h"
16 #include "ui/gfx/image/image_skia.h"
17
18 namespace favicon {
19
20 // Represents a queue contaning favicon bitmaps, sorted by descending score.
21 // Candidates can be dequeued one by one and the result of processing candidates
22 // can be fed back to the queue, such that it keeps track of the best candidate
23 // found so far.
24 class FaviconSelector {
25 public:
26 // Used to track a candidate favicon bitmap.
27 struct Candidate {
28 // Builds a scored candidate by selecting the best bitmap size.
29 static Candidate FromFaviconURL(const favicon::FaviconURL& favicon_url,
30 int target_pixel_size);
31
32 GURL icon_url;
33 favicon_base::IconType icon_type;
34 float score;
35 };
36
37 struct BestCandidate {
38 Candidate candidate;
39 gfx::Size original_size;
40 SkBitmap bitmap;
41 };
42
43 // Defines the goals or ideal pixel size that a particular
44 // FaviconSelector is looking for.
45 class TargetSizeSpec {
46 public:
47 static TargetSizeSpec ForLargest();
48 // Returns largest pixel sizes first.
49 static std::vector<TargetSizeSpec> For16x16Dips();
50
51 TargetSizeSpec(const TargetSizeSpec&);
52 ~TargetSizeSpec();
53
54 TargetSizeSpec(TargetSizeSpec&&);
55 TargetSizeSpec& operator=(TargetSizeSpec&&);
56
57 // Returns the target pixel size.
58 int pixel_size() const { return pixel_size_; }
59
60 // Returns the target scale factor.
61 float scale_factor() const { return scale_factor_; }
62
63 // Returns whether the spec is requiring the best-matching bitmap only.
64 // TODO(mastiz): Rename this?
65 bool WantsBestBitmapOnly() const;
66
67 // Checks if |bitmap_results| contains a bitmap satistying this spec.
68 bool HasSatisfyingResult(
69 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results)
70 const;
71
72 // Scores and sorts the entries in |favicon_urls|, best icon size matches
73 // first. Pruning means discarding all but the best bitmap per candidate.
74 // Returns the sorted, scored and pruned candidate list.
75 std::list<Candidate> SortAndPruneCandidates(
76 const std::vector<favicon::FaviconURL>& favicon_urls) const;
77
78 private:
79 TargetSizeSpec();
80
81 int pixel_size_;
82 float scale_factor_;
83
84 // If true, FaviconHandler will resample candidates whenever needed to
85 // ensure that bitmaps for all |pixel_sizes| are produced.
86 bool ensure_exact_size_;
87 };
88
89 // Convenience function to construct multiple selectors. |target_size_specs|
90 // must be in descending pixel size order.
91 static std::vector<FaviconSelector> BuildMultiple(
92 const std::vector<TargetSizeSpec>& target_size_specs,
93 const std::vector<favicon::FaviconURL>& candidates);
94
95 // |candidates| must be non-empty.
96 FaviconSelector(const TargetSizeSpec& target_size_spec,
97 const std::vector<favicon::FaviconURL>& candidates);
98 ~FaviconSelector();
99 FaviconSelector(FaviconSelector&&);
100 FaviconSelector& operator=(FaviconSelector&&);
101
102 const TargetSizeSpec& target_size_spec() const { return target_size_spec_; }
103
104 // Returns the next candidate to be processed, or an empty value if no
105 // further work is demanded, although clients are allowed to feed in new
106 // downloads via ProcessDownloadedImage().
107 base::Optional<Candidate> DequeueCandidate();
108
109 // Returns the current candidate without dequeueing it.
110 const Candidate* CurrentCandidate() const;
111
112 // Feeds in the bitmaps of a downloaded image, which will be compared against
113 // the best one processed so far. |bitmaps| and |original_sizes| must have the
114 // same size. Returns true if |icon_url| was chosen as the best one so far.
115 bool ProcessDownloadedImage(const GURL& icon_url,
116 favicon_base::IconType icon_type,
117 const std::vector<SkBitmap>& bitmaps,
118 const std::vector<gfx::Size>& original_sizes);
119
120 // Returns the best-matching bitmap downloaded/processed so far, if any.
121 const base::Optional<BestCandidate>& BestBitmap() const {
122 return best_candidate_;
123 }
124
125 private:
126 // Returns whether a good enough bitmap was found (i.e. no further work
127 // demanded by this selector, although callers are allowed to process and feed
128 // in more processed downloads)
129 bool IsSatisfied() const;
130
131 // Desired icon size.
132 class TargetSizeSpec target_size_spec_;
133
134 // The prioritized favicon candidates from the page. There cannot be
135 // multiple candidates for the same icon URL.
136 std::list<Candidate> pending_candidates_;
137
138 // Best bitmap we've seen so far, if any.
139 base::Optional<BestCandidate> best_candidate_;
140
141 DISALLOW_COPY_AND_ASSIGN(FaviconSelector);
142 };
143
144 } // namespace favicon
145
146 #endif // COMPONENTS_FAVICON_CORE_FAVICON_SELECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698