summaryrefslogtreecommitdiffstats
path: root/chrome/browser/image_holder.h
blob: 2609eaacac1ef7e8a93c0233aeda5759672e4955 (plain)
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
// Copyright 2014 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_IMAGE_HOLDER_H_
#define CHROME_BROWSER_IMAGE_HOLDER_H_

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"

class Profile;

namespace chrome {

// This provides a callback so the ImageHolder can inform its parent when a
// bitmap arrives.
class ImageHolderDelegate {
 public:
  virtual void OnFetchComplete() = 0;
};

// This class encapsulates the action of fetching a bitmap, reporting when it is
// fetched, and holding onto the bitmap until no longer needed.
class ImageHolder : public chrome::BitmapFetcherDelegate {
 public:
  ImageHolder(const GURL& low_dpi_url,
              const GURL& high_dpi_url,
              Profile* profile,
              ImageHolderDelegate* delegate);
  ~ImageHolder() override;

  // Begin fetching of the URLs we have.
  void StartFetch();

  // Check whether we have a response from the server for these resources,
  // even if the response is a failed fetch.
  bool IsFetchingDone() const;

  // Inherited from BitmapFetcherDelegate
  void OnFetchComplete(const GURL& url, const SkBitmap* bitmap) override;

  // Accessors:
  GURL low_dpi_url() const { return low_dpi_url_; }
  GURL high_dpi_url() const { return high_dpi_url_; }
  gfx::Image low_dpi_image() { return gfx::Image(image_); }

 private:
  // Helper function to create a bitmap fetcher (but not start the fetch).
  void CreateBitmapFetcher(const GURL& url);

  GURL low_dpi_url_;
  GURL high_dpi_url_;
  bool low_dpi_fetched_;
  bool high_dpi_fetched_;
  gfx::ImageSkia image_;
  ImageHolderDelegate* delegate_;
  ScopedVector<chrome::BitmapFetcher> fetchers_;
  Profile* profile_;

  FRIEND_TEST_ALL_PREFIXES(ImageHolderTest, CreateBitmapFetcherTest);
  FRIEND_TEST_ALL_PREFIXES(ImageHolderTest, OnFetchCompleteTest);
  FRIEND_TEST_ALL_PREFIXES(ImageHolderTest, IsFetchingDoneTest);

  DISALLOW_COPY_AND_ASSIGN(ImageHolder);
};

}  // namespace chrome.

#endif  // CHROME_BROWSER_IMAGE_HOLDER_H_