ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / search / suggestions / image_fetcher_impl.h
blob82379f28f88f4079514f77b562d8acfc98346ab0
1 // Copyright 2014 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_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_
6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_
8 #include <map>
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
15 #include "components/suggestions/image_fetcher.h"
16 #include "ui/gfx/image/image_skia.h"
17 #include "url/gurl.h"
19 namespace net {
20 class URLRequestContextGetter;
23 namespace suggestions {
25 // A class used to fetch server images. It can be called from any thread and the
26 // callback will be called on the thread which initiated the fetch.
27 class ImageFetcherImpl : public ImageFetcher,
28 public chrome::BitmapFetcherDelegate {
29 public:
30 explicit ImageFetcherImpl(net::URLRequestContextGetter* url_request_context);
31 ~ImageFetcherImpl() override;
33 void SetImageFetcherDelegate(ImageFetcherDelegate* delegate) override;
35 void StartOrQueueNetworkRequest(
36 const GURL& url,
37 const GURL& image_url,
38 base::Callback<void(const GURL&, const SkBitmap*)> callback) override;
40 private:
41 // Inherited from BitmapFetcherDelegate.
42 void OnFetchComplete(const GURL& image_url, const SkBitmap* bitmap) override;
44 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> >
45 CallbackVector;
47 // State related to an image fetch (associated website url, image_url,
48 // fetcher, pending callbacks).
49 struct ImageRequest {
50 ImageRequest();
51 // Struct takes ownership of |f|.
52 explicit ImageRequest(chrome::BitmapFetcher* f);
53 ~ImageRequest();
55 void swap(ImageRequest* other) {
56 std::swap(url, other->url);
57 std::swap(image_url, other->image_url);
58 std::swap(callbacks, other->callbacks);
59 std::swap(fetcher, other->fetcher);
62 GURL url;
63 GURL image_url;
64 chrome::BitmapFetcher* fetcher;
65 // Queue for pending callbacks, which may accumulate while the request is in
66 // flight.
67 CallbackVector callbacks;
70 typedef std::map<const GURL, ImageRequest> ImageRequestMap;
72 // Map from each image URL to the request information (associated website
73 // url, fetcher, pending callbacks).
74 ImageRequestMap pending_net_requests_;
76 ImageFetcherDelegate* delegate_;
78 net::URLRequestContextGetter* url_request_context_;
80 DISALLOW_COPY_AND_ASSIGN(ImageFetcherImpl);
83 } // namespace suggestions
85 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_