File: favicon_downloader.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (106 lines) | stat: -rw-r--r-- 3,632 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_
#define CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_

#include <map>
#include <set>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/web_contents_observer.h"

class SkBitmap;

namespace content {
struct FaviconURL;
}

namespace gfx {
class Size;
}

// Class to help download all favicons for a tab.
class FaviconDownloader : public content::WebContentsObserver {
 public:
  typedef std::map<GURL, std::vector<SkBitmap> > FaviconMap;
  typedef base::Callback<void(
          bool,  /* success */
          /* A map of icon urls to the bitmaps provided by that url. */
          const FaviconMap&)>
      FaviconDownloaderCallback;
  // |extra_favicon_urls| allows callers to provide icon urls that aren't
  // provided by the renderer (e.g touch icons on non-android environments).
  // |skip_page_favicons| instructs the downloader to not query the page
  // for favicons (e.g. when a favicon URL has already been provided in
  // |extra_favicon_urls|).
  FaviconDownloader(content::WebContents* web_contents,
                    const std::vector<GURL>& extra_favicon_urls,
                    FaviconDownloaderCallback callback);
  ~FaviconDownloader() override;

  void SkipPageFavicons();

  void Start();

 private:
  friend class TestFaviconDownloader;

  // Initiates a download of the image at |url| and returns the download id.
  // This is overridden in testing.
  virtual int DownloadImage(const GURL& url);

  // Queries FaviconDriver for the page's current favicon URLs.
  // This is overridden in testing.
  virtual std::vector<content::FaviconURL> GetFaviconURLsFromWebContents();

  // Fetches icons for the given urls.
  // |callback_| is run when all downloads complete.
  void FetchIcons(const std::vector<content::FaviconURL>& favicon_urls);
  void FetchIcons(const std::vector<GURL>& urls);

  // Icon download callback.
  void DidDownloadFavicon(int id,
                          int http_status_code,
                          const GURL& image_url,
                          const std::vector<SkBitmap>& bitmaps,
                          const std::vector<gfx::Size>& original_bitmap_sizes);

  // content::WebContentsObserver overrides:
  void DidNavigateMainFrame(
      const content::LoadCommittedDetails& details,
      const content::FrameNavigateParams& params) override;
  void DidUpdateFaviconURL(
      const std::vector<content::FaviconURL>& candidates) override;

  // Whether we need to fetch favicons from the renderer.
  bool need_favicon_urls_;

  // URLs that aren't given by WebContentsObserver::DidUpdateFaviconURL() that
  // should be used for this favicon. This is necessary in order to get touch
  // icons on non-android environments.
  std::vector<GURL> extra_favicon_urls_;

  // The icons which were downloaded. Populated by FetchIcons().
  FaviconMap favicon_map_;

  // Request ids of in-progress requests.
  std::set<int> in_progress_requests_;

  // Urls for which a download has already been initiated. Used to prevent
  // duplicate downloads of the same url.
  std::set<GURL> processed_urls_;

  // Callback to run on favicon download completion.
  FaviconDownloaderCallback callback_;

  base::WeakPtrFactory<FaviconDownloader> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(FaviconDownloader);
};

#endif  // CHROME_BROWSER_EXTENSIONS_FAVICON_DOWNLOADER_H_