• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #ifndef CEF_TESTS_CEFCLIENT_BROWSER_IMAGE_CACHE_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_IMAGE_CACHE_H_
7 #pragma once
8 
9 #include <map>
10 #include <vector>
11 
12 #include "include/base/cef_callback.h"
13 #include "include/base/cef_ref_counted.h"
14 #include "include/cef_image.h"
15 #include "include/wrapper/cef_closure_task.h"
16 #include "include/wrapper/cef_helpers.h"
17 
18 namespace client {
19 
20 // Simple image caching implementation.
21 class ImageCache
22     : public base::RefCountedThreadSafe<ImageCache, CefDeleteOnUIThread> {
23  public:
24   ImageCache();
25 
26   // Image representation at a specific scale factor.
27   struct ImageRep {
28     ImageRep(const std::string& path, float scale_factor);
29 
30     // Full file system path.
31     std::string path_;
32 
33     // Image scale factor (usually 1.0f or 2.0f).
34     float scale_factor_;
35   };
36   using ImageRepSet = std::vector<ImageRep>;
37 
38   // Unique image that may have multiple representations.
39   struct ImageInfo {
40     ImageInfo(const std::string& id,
41               const ImageRepSet& reps,
42               bool internal,
43               bool force_reload);
44 
45     // Helper for returning an empty image.
46     static ImageInfo Empty();
47 
48     // Helpers for creating common representations.
49     static ImageInfo Create1x(const std::string& id,
50                               const std::string& path_1x,
51                               bool internal);
52     static ImageInfo Create2x(const std::string& id,
53                               const std::string& path_1x,
54                               const std::string& path_2x,
55                               bool internal);
56     static ImageInfo Create2x(const std::string& id);
57 
58     // Image unique ID.
59     std::string id_;
60 
61     // Image representations to load.
62     ImageRepSet reps_;
63 
64     // True if the image is internal (loaded via LoadBinaryResource).
65     bool internal_;
66 
67     // True to force reload.
68     bool force_reload_;
69   };
70   using ImageInfoSet = std::vector<ImageInfo>;
71 
72   using ImageSet = std::vector<CefRefPtr<CefImage>>;
73 
74   using LoadImagesCallback =
75       base::OnceCallback<void(const ImageSet& /*images*/)>;
76 
77   // Loads the images represented by |image_info|. Executes |callback|
78   // either synchronously or asychronously on the UI thread after completion.
79   void LoadImages(const ImageInfoSet& image_info, LoadImagesCallback callback);
80 
81   // Returns an image that has already been cached. Must be called on the
82   // UI thread.
83   CefRefPtr<CefImage> GetCachedImage(const std::string& image_id);
84 
85  private:
86   // Only allow deletion via scoped_refptr.
87   friend struct CefDeleteOnThread<TID_UI>;
88   friend class base::RefCountedThreadSafe<ImageCache, CefDeleteOnUIThread>;
89 
90   ~ImageCache();
91 
92   enum ImageType {
93     TYPE_NONE,
94     TYPE_PNG,
95     TYPE_JPEG,
96   };
97 
98   static ImageType GetImageType(const std::string& path);
99 
100   struct ImageContent;
101   using ImageContentSet = std::vector<ImageContent>;
102 
103   // Load missing image contents on the FILE thread.
104   void LoadMissing(const ImageInfoSet& image_info,
105                    const ImageSet& images,
106                    LoadImagesCallback callback);
107   static bool LoadImageContents(const ImageInfo& info, ImageContent* content);
108   static bool LoadImageContents(const std::string& path,
109                                 bool internal,
110                                 ImageType* type,
111                                 std::string* contents);
112 
113   // Create missing CefImage representations on the UI thread.
114   void UpdateCache(const ImageInfoSet& image_info,
115                    const ImageContentSet& contents,
116                    LoadImagesCallback callback);
117   static CefRefPtr<CefImage> CreateImage(const std::string& image_id,
118                                          const ImageContent& content);
119 
120   // Map image ID to image representation. Only accessed on the UI thread.
121   using ImageMap = std::map<std::string, CefRefPtr<CefImage>>;
122   ImageMap image_map_;
123 };
124 
125 }  // namespace client
126 
127 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_IMAGE_CACHE_H_
128