1 // Copyright 2018 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_CACHED_IMAGE_FETCHER_H_
6 #define COMPONENTS_IMAGE_FETCHER_CORE_CACHED_IMAGE_FETCHER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequence_checker.h"
14 #include "components/image_fetcher/core/image_decoder.h"
15 #include "components/image_fetcher/core/image_fetcher.h"
16 #include "components/image_fetcher/core/image_fetcher_types.h"
17 #include "net/traffic_annotation/network_traffic_annotation.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "url/gurl.h"
20 
21 namespace image_fetcher {
22 
23 class ImageCache;
24 class ImageFetcher;
25 struct RequestMetadata;
26 
27 // Encapsulates a request to simplify argument lists.
28 struct CachedImageFetcherRequest;
29 
30 // CachedImageFetcher takes care of fetching images from the network and caching
31 // them. Has a read-only mode which doesn't perform write operations on the
32 // cache.
33 class CachedImageFetcher : public ImageFetcher {
34  public:
35   CachedImageFetcher(ImageFetcher* image_fetcher,
36                      scoped_refptr<ImageCache> image_cache,
37                      bool read_only);
38   ~CachedImageFetcher() override;
39 
40   // ImageFetcher:
41   void FetchImageAndData(const GURL& image_url,
42                          ImageDataFetcherCallback image_data_callback,
43                          ImageFetcherCallback image_callback,
44                          ImageFetcherParams params) override;
45   ImageDecoder* GetImageDecoder() override;
46 
47  private:
48   // Cache
49   void OnImageFetchedFromCache(CachedImageFetcherRequest request,
50                                ImageDataFetcherCallback image_data_callback,
51                                ImageFetcherCallback image_callback,
52                                bool cache_result_needs_transcoding,
53                                std::string image_data);
54   void OnImageDecodedFromCache(CachedImageFetcherRequest request,
55                                ImageDataFetcherCallback image_data_callback,
56                                ImageFetcherCallback image_callback,
57                                bool cache_result_needs_transcoding,
58                                const gfx::Image& image);
59 
60   // Network
61   void EnqueueFetchImageFromNetwork(
62       CachedImageFetcherRequest request,
63       ImageDataFetcherCallback image_data_callback,
64       ImageFetcherCallback image_callback);
65   void FetchImageFromNetwork(
66       CachedImageFetcherRequest request,
67       ImageDataFetcherCallback image_data_callback,
68       ImageFetcherCallback image_callback);
69   void OnImageFetchedWithoutTranscoding(
70       CachedImageFetcherRequest request,
71       ImageDataFetcherCallback image_data_callback,
72       const std::string& image_data,
73       const RequestMetadata& request_metadata);
74   void OnImageFetchedForTranscoding(CachedImageFetcherRequest request,
75                                     ImageFetcherCallback image_data_callback,
76                                     const gfx::Image& image,
77                                     const RequestMetadata& request_metadata);
78   // Encode the given |image_data| and store it.
79   // |cache_result_needs_transcoding| is passed along for metrics purposes. It
80   // is true when the result 1) comes from cache and 2) wasn't transcoded when
81   // last time it was stored in the ImageCache.
82   // |is_image_data_transcoded| indicates whether the image to save has been
83   // transcoded. An image could be transcoded during network fetching, or loaded
84   // from the ImageCache without transcoding before.
85   void EncodeAndStoreData(bool cache_result_needs_transcoding,
86                           bool is_image_data_transcoded,
87                           CachedImageFetcherRequest request,
88                           const gfx::Image& image);
89   // Store the given |image_data| in the cache. |cache_result_needs_transcoding|
90   // is passed along for metrics purposes. It is true when the result 1) comes
91   // from cache and 2) wasn't transcoded when last time it was stored in the
92   // ImageCache.
93   void StoreData(bool cache_result_needs_transcoding,
94                  bool is_image_data_transcoded,
95                  CachedImageFetcherRequest request,
96                  std::string image_data);
97 
98   // Owned by ImageFetcherService.
99   ImageFetcher* image_fetcher_;
100   scoped_refptr<ImageCache> image_cache_;
101 
102   // Whether the ImageCache is allowed to be modified in any way from requests
103   // made by this CachedImageFetcher. This includes updating last used times,
104   // writing new data to the cache, or cleaning up unreadable data. Note that
105   // the ImageCache may still decide to perform eviction/reconciliation even
106   // when only read only CachedImageFetchers are using it.
107   bool read_only_;
108 
109   // Used to ensure that operations are performed on the sequence that this
110   // object was created on.
111   SEQUENCE_CHECKER(sequence_checker_);
112 
113   base::WeakPtrFactory<CachedImageFetcher> weak_ptr_factory_{this};
114 
115   DISALLOW_COPY_AND_ASSIGN(CachedImageFetcher);
116 };
117 
118 }  // namespace image_fetcher
119 
120 #endif  // COMPONENTS_IMAGE_FETCHER_CORE_CACHED_IMAGE_FETCHER_H_
121