OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_ | |
6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | |
15 #include "components/image_fetcher/image_data_fetcher.h" | |
16 #include "components/image_fetcher/image_decoder.h" | |
17 #include "components/image_fetcher/image_fetcher.h" | |
18 #include "url/gurl.h" | |
19 | |
20 namespace gfx { | |
21 class Image; | |
22 } | |
23 | |
24 namespace net { | |
25 class URLRequestContextGetter; | |
26 } | |
27 | |
28 // TODO(markusheintz): Move the ImageFetcherImpl to components/image_fetcher | |
29 namespace suggestions { | |
30 | |
31 // image_fetcher::ImageFetcher implementation. | |
32 class ImageFetcherImpl : public image_fetcher::ImageFetcher { | |
33 public: | |
34 explicit ImageFetcherImpl(net::URLRequestContextGetter* url_request_context); | |
35 ~ImageFetcherImpl() override; | |
36 | |
37 void SetImageFetcherDelegate( | |
38 image_fetcher::ImageFetcherDelegate* delegate) override; | |
39 | |
40 void StartOrQueueNetworkRequest( | |
41 const std::string& id, | |
42 const GURL& image_url, | |
43 base::Callback<void(const std::string&, const gfx::Image&)> callback) | |
44 override; | |
45 | |
46 private: | |
47 using CallbackVector = | |
48 std::vector<base::Callback<void(const std::string&, const gfx::Image&)>>; | |
49 | |
50 // State related to an image fetch (id, pending callbacks). | |
51 struct ImageRequest { | |
52 ImageRequest(); | |
53 ImageRequest(const ImageRequest& other); | |
54 ~ImageRequest(); | |
55 | |
56 void swap(ImageRequest* other) { | |
57 std::swap(id, other->id); | |
58 std::swap(callbacks, other->callbacks); | |
59 } | |
60 | |
61 std::string id; | |
62 // Queue for pending callbacks, which may accumulate while the request is in | |
63 // flight. | |
64 CallbackVector callbacks; | |
65 }; | |
66 | |
67 using ImageRequestMap = std::map<const GURL, ImageRequest>; | |
68 | |
69 // Processes image URL fetched events. This is the continuation method used | |
70 // for creating callbacks that are passed to the ImageDataFetcher. | |
71 void OnImageURLFetched(const GURL& image_url, const std::string& image_data); | |
72 | |
73 // Processes image decoded events. This is the continuation method used for | |
74 // creating callbacks that are passed to the ImageDecoder. | |
75 void OnImageDecoded(const GURL& image_url, const gfx::Image& image); | |
76 | |
77 | |
78 image_fetcher::ImageFetcherDelegate* delegate_; | |
79 | |
80 net::URLRequestContextGetter* url_request_context_; | |
81 | |
82 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | |
83 | |
84 std::unique_ptr<image_fetcher::ImageDataFetcher> image_data_fetcher_; | |
85 | |
86 // Map from each image URL to the request information (associated website | |
87 // url, fetcher, pending callbacks). | |
88 ImageRequestMap pending_net_requests_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(ImageFetcherImpl); | |
91 }; | |
92 | |
93 } // namespace suggestions | |
94 | |
95 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_FETCHER_IMPL_H_ | |
OLD | NEW |