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 COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_FETCHER_IMPL_H_
6 #define COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_FETCHER_IMPL_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "components/image_fetcher/core/image_data_fetcher.h"
18 #include "components/image_fetcher/core/image_decoder.h"
19 #include "components/image_fetcher/core/image_fetcher.h"
20 #include "net/traffic_annotation/network_traffic_annotation.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "url/gurl.h"
23 
24 namespace gfx {
25 class Image;
26 }
27 
28 namespace network {
29 class SharedURLLoaderFactory;
30 }
31 
32 namespace image_fetcher {
33 
34 // The standard (non-test) implementation of ImageFetcher.
35 class ImageFetcherImpl : public ImageFetcher {
36  public:
37   ImageFetcherImpl(
38       std::unique_ptr<ImageDecoder> image_decoder,
39       scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
40   ~ImageFetcherImpl() override;
41 
42   void FetchImageAndData(const GURL& image_url,
43                          ImageDataFetcherCallback image_data_callback,
44                          ImageFetcherCallback image_callback,
45                          ImageFetcherParams params) override;
46 
47   ImageDecoder* GetImageDecoder() override;
48 
49  private:
50   // State related to an image fetch (id, pending callbacks).
51   struct ImageRequest {
52     ImageRequest();
53     ~ImageRequest();
54     ImageRequest(ImageRequest&& other);
55 
56     std::string id;
57     // These have the default value if the image data has not yet been fetched.
58     RequestMetadata request_metadata;
59     std::string image_data;
60     // Queue for pending callbacks, which may accumulate while the request is in
61     // flight.
62     std::vector<ImageFetcherCallback> image_callbacks;
63     std::vector<ImageDataFetcherCallback> image_data_callbacks;
64 
65     DISALLOW_COPY_AND_ASSIGN(ImageRequest);
66   };
67 
68   using ImageRequestMap = std::map<GURL, ImageRequest>;
69 
70   // Processes image URL fetched events. This is the continuation method used
71   // for creating callbacks that are passed to the ImageDataFetcher.
72   void OnImageURLFetched(const GURL& image_url,
73                          ImageFetcherParams params,
74                          const std::string& image_data,
75                          const RequestMetadata& metadata);
76 
77   // Processes image decoded events. This is the continuation method used for
78   // creating callbacks that are passed to the ImageDecoder.
79   void OnImageDecoded(const GURL& image_url,
80                       const RequestMetadata& metadata,
81                       const gfx::Image& image);
82 
83   // Used to run |image_data_callback| only if |this| is still valid.
84   void RunImageDataCallback(ImageDataFetcherCallback image_data_callback,
85                             std::string image_data,
86                             RequestMetadata request_metadata);
87 
88   scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
89 
90   std::unique_ptr<ImageDecoder> image_decoder_;
91 
92   std::unique_ptr<ImageDataFetcher> image_data_fetcher_;
93 
94   // Map from each image URL to the request information (associated website
95   // url, fetcher, pending callbacks).
96   ImageRequestMap pending_net_requests_;
97 
98   base::WeakPtrFactory<ImageFetcherImpl> weak_ptr_factory_{this};
99 
100   DISALLOW_COPY_AND_ASSIGN(ImageFetcherImpl);
101 };
102 
103 }  // namespace image_fetcher
104 
105 #endif  // COMPONENTS_IMAGE_FETCHER_CORE_IMAGE_FETCHER_IMPL_H_
106