Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / manifest / manifest_icon_downloader.h
blobfa7b38f74c5db752c0c18ccfef9bef78646519cf
1 // Copyright 2015 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 #ifndef CHROME_BROWSER_MANIFEST_MANIFEST_ICON_DOWNLOADER_H_
6 #define CHROME_BROWSER_MANIFEST_MANIFEST_ICON_DOWNLOADER_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
14 namespace content {
15 class WebContents;
16 } // namespace content
18 namespace gfx {
19 class Size;
20 } // namespace gfx
22 class GURL;
24 // Helper class which downloads the icon located at a specified. If the icon
25 // file contains multiple icons then it attempts to pick the one closest in size
26 // bigger than or equal to ideal_icon_size_in_dp, taking into account the
27 // density of the device. If a bigger icon is chosen then, the icon is scaled
28 // down to be equal to ideal_icon_size_in_dp. Smaller icons will be chosen down
29 // to the value specified by |minimum_icon_size_in_dp|.
30 class ManifestIconDownloader final {
31 public:
32 using IconFetchCallback = base::Callback<void(const SkBitmap&)>;
34 ManifestIconDownloader() = delete;
35 ~ManifestIconDownloader() = delete;
37 // Returns whether the download has started.
38 // It will return false if the current context or information do not allow to
39 // download the image.
40 static bool Download(content::WebContents* web_contents,
41 const GURL& icon_url,
42 int ideal_icon_size_in_dp,
43 int minimum_icon_size_in_dp,
44 const IconFetchCallback& callback);
46 private:
47 // Callback run after the manifest icon downloaded successfully or the
48 // download failed.
49 static void OnIconFetched(int ideal_icon_size_in_px,
50 int minimum_icon_size_in_px,
51 const IconFetchCallback& callback,
52 int id,
53 int http_status_code,
54 const GURL& url,
55 const std::vector<SkBitmap>& bitmaps,
56 const std::vector<gfx::Size>& sizes);
58 static void ScaleIcon(int ideal_icon_size_in_px,
59 const SkBitmap& bitmap,
60 const IconFetchCallback& callback);
62 static int FindClosestBitmapIndex(int ideal_icon_size_in_px,
63 int minimum_icon_size_in_px,
64 const std::vector<SkBitmap>& bitmaps);
66 friend class ManifestIconDownloaderTest;
68 DISALLOW_COPY_AND_ASSIGN(ManifestIconDownloader);
71 #endif // CHROME_BROWSER_MANIFEST_MANIFEST_ICON_DOWNLOADER_H_