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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/favicon/core/favicon_selector.h
diff --git a/components/favicon/core/favicon_selector.h b/components/favicon/core/favicon_selector.h
new file mode 100644
index 0000000000000000000000000000000000000000..346a3b2fd25687eff515a77effadc72bc2ad5cf4
--- /dev/null
+++ b/components/favicon/core/favicon_selector.h
@@ -0,0 +1,146 @@
+// Copyright (c) 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_FAVICON_CORE_FAVICON_SELECTOR_H_
+#define COMPONENTS_FAVICON_CORE_FAVICON_SELECTOR_H_
+
+#include <list>
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "base/macros.h"
+#include "base/optional.h"
+#include "components/favicon/core/favicon_url.h"
+#include "ui/gfx/image/image_skia.h"
+
+namespace favicon {
+
+// Represents a queue contaning favicon bitmaps, sorted by descending score.
+// Candidates can be dequeued one by one and the result of processing candidates
+// can be fed back to the queue, such that it keeps track of the best candidate
+// found so far.
+class FaviconSelector {
+ public:
+ // Used to track a candidate favicon bitmap.
+ struct Candidate {
+ // Builds a scored candidate by selecting the best bitmap size.
+ static Candidate FromFaviconURL(const favicon::FaviconURL& favicon_url,
+ int target_pixel_size);
+
+ GURL icon_url;
+ favicon_base::IconType icon_type;
+ float score;
+ };
+
+ struct BestCandidate {
+ Candidate candidate;
+ gfx::Size original_size;
+ SkBitmap bitmap;
+ };
+
+ // Defines the goals or ideal pixel size that a particular
+ // FaviconSelector is looking for.
+ class TargetSizeSpec {
+ public:
+ static TargetSizeSpec ForLargest();
+ // Returns largest pixel sizes first.
+ static std::vector<TargetSizeSpec> For16x16Dips();
+
+ TargetSizeSpec(const TargetSizeSpec&);
+ ~TargetSizeSpec();
+
+ TargetSizeSpec(TargetSizeSpec&&);
+ TargetSizeSpec& operator=(TargetSizeSpec&&);
+
+ // Returns the target pixel size.
+ int pixel_size() const { return pixel_size_; }
+
+ // Returns the target scale factor.
+ float scale_factor() const { return scale_factor_; }
+
+ // Returns whether the spec is requiring the best-matching bitmap only.
+ // TODO(mastiz): Rename this?
+ bool WantsBestBitmapOnly() const;
+
+ // Checks if |bitmap_results| contains a bitmap satistying this spec.
+ bool HasSatisfyingResult(
+ const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_results)
+ const;
+
+ // Scores and sorts the entries in |favicon_urls|, best icon size matches
+ // first. Pruning means discarding all but the best bitmap per candidate.
+ // Returns the sorted, scored and pruned candidate list.
+ std::list<Candidate> SortAndPruneCandidates(
+ const std::vector<favicon::FaviconURL>& favicon_urls) const;
+
+ private:
+ TargetSizeSpec();
+
+ int pixel_size_;
+ float scale_factor_;
+
+ // If true, FaviconHandler will resample candidates whenever needed to
+ // ensure that bitmaps for all |pixel_sizes| are produced.
+ bool ensure_exact_size_;
+ };
+
+ // Convenience function to construct multiple selectors. |target_size_specs|
+ // must be in descending pixel size order.
+ static std::vector<FaviconSelector> BuildMultiple(
+ const std::vector<TargetSizeSpec>& target_size_specs,
+ const std::vector<favicon::FaviconURL>& candidates);
+
+ // |candidates| must be non-empty.
+ FaviconSelector(const TargetSizeSpec& target_size_spec,
+ const std::vector<favicon::FaviconURL>& candidates);
+ ~FaviconSelector();
+ FaviconSelector(FaviconSelector&&);
+ FaviconSelector& operator=(FaviconSelector&&);
+
+ const TargetSizeSpec& target_size_spec() const { return target_size_spec_; }
+
+ // Returns the next candidate to be processed, or an empty value if no
+ // further work is demanded, although clients are allowed to feed in new
+ // downloads via ProcessDownloadedImage().
+ base::Optional<Candidate> DequeueCandidate();
+
+ // Returns the current candidate without dequeueing it.
+ const Candidate* CurrentCandidate() const;
+
+ // Feeds in the bitmaps of a downloaded image, which will be compared against
+ // the best one processed so far. |bitmaps| and |original_sizes| must have the
+ // same size. Returns true if |icon_url| was chosen as the best one so far.
+ bool ProcessDownloadedImage(const GURL& icon_url,
+ favicon_base::IconType icon_type,
+ const std::vector<SkBitmap>& bitmaps,
+ const std::vector<gfx::Size>& original_sizes);
+
+ // Returns the best-matching bitmap downloaded/processed so far, if any.
+ const base::Optional<BestCandidate>& BestBitmap() const {
+ return best_candidate_;
+ }
+
+ private:
+ // Returns whether a good enough bitmap was found (i.e. no further work
+ // demanded by this selector, although callers are allowed to process and feed
+ // in more processed downloads)
+ bool IsSatisfied() const;
+
+ // Desired icon size.
+ class TargetSizeSpec target_size_spec_;
+
+ // The prioritized favicon candidates from the page. There cannot be
+ // multiple candidates for the same icon URL.
+ std::list<Candidate> pending_candidates_;
+
+ // Best bitmap we've seen so far, if any.
+ base::Optional<BestCandidate> best_candidate_;
+
+ DISALLOW_COPY_AND_ASSIGN(FaviconSelector);
+};
+
+} // namespace favicon
+
+#endif // COMPONENTS_FAVICON_CORE_FAVICON_SELECTOR_H_

Powered by Google App Engine
This is Rietveld 408576698