summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search/suggestions/image_fetcher_impl.h
blob: 167865a0a2207cdc7a09bcb4cf5c64ae843642c4 (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
75
76
77
78
79
80
81
82
83
84
85
// 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_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_
#define CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_

#include <map>
#include <utility>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
#include "components/suggestions/image_fetcher.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"

namespace net {
class URLRequestContextGetter;
}

namespace suggestions {

// A class used to fetch server images. It can be called from any thread and the
// callback will be called on the thread which initiated the fetch.
class ImageFetcherImpl : public ImageFetcher,
                         public chrome::BitmapFetcherDelegate {
 public:
  explicit ImageFetcherImpl(net::URLRequestContextGetter* url_request_context);
  ~ImageFetcherImpl() override;

  void SetImageFetcherDelegate(ImageFetcherDelegate* delegate) override;

  void StartOrQueueNetworkRequest(
      const GURL& url,
      const GURL& image_url,
      base::Callback<void(const GURL&, const SkBitmap*)> callback) override;

 private:
  // Inherited from BitmapFetcherDelegate.
  void OnFetchComplete(const GURL& image_url, const SkBitmap* bitmap) override;

  typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> >
      CallbackVector;

  // State related to an image fetch (associated website url, image_url,
  // fetcher, pending callbacks).
  struct ImageRequest {
    ImageRequest();
    // Struct takes ownership of |f|.
    explicit ImageRequest(chrome::BitmapFetcher* f);
    ~ImageRequest();

    void swap(ImageRequest* other) {
      std::swap(url, other->url);
      std::swap(image_url, other->image_url);
      std::swap(callbacks, other->callbacks);
      std::swap(fetcher, other->fetcher);
    }

    GURL url;
    GURL image_url;
    chrome::BitmapFetcher* fetcher;
    // Queue for pending callbacks, which may accumulate while the request is in
    // flight.
    CallbackVector callbacks;
  };

  typedef std::map<const GURL, ImageRequest> ImageRequestMap;

  // Map from each image URL to the request information (associated website
  // url, fetcher, pending callbacks).
  ImageRequestMap pending_net_requests_;

  ImageFetcherDelegate* delegate_;

  net::URLRequestContextGetter* url_request_context_;

  DISALLOW_COPY_AND_ASSIGN(ImageFetcherImpl);
};

}  // namespace suggestions

#endif  // CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_