Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / android / thumbnail / thumbnail_cache.h
blob0ab16b4dda9829535b8375bdd1242dbef351fc9c
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_ANDROID_THUMBNAIL_THUMBNAIL_CACHE_H_
6 #define CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_CACHE_H_
8 #include <list>
9 #include <set>
10 #include <string>
12 #include "base/bind.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/observer_list.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/android/thumbnail/scoped_ptr_expiring_cache.h"
19 #include "chrome/browser/android/thumbnail/thumbnail.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "ui/android/resources/ui_resource_client_android.h"
22 #include "ui/android/resources/ui_resource_provider.h"
23 #include "ui/gfx/geometry/point.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gfx/geometry/size_f.h"
26 #include "url/gurl.h"
28 namespace base {
29 class File;
30 class Time;
33 namespace content {
34 class ContentViewCore;
37 typedef std::list<TabId> TabIdList;
39 class ThumbnailCacheObserver {
40 public:
41 virtual void OnFinishedThumbnailRead(TabId tab_id) = 0;
44 class ThumbnailCache : ThumbnailDelegate {
45 public:
46 ThumbnailCache(size_t default_cache_size,
47 size_t approximation_cache_size,
48 size_t compression_queue_max_size,
49 size_t write_queue_max_size,
50 bool use_approximation_thumbnail);
52 ~ThumbnailCache() override;
54 void SetUIResourceProvider(ui::UIResourceProvider* ui_resource_provider);
56 void AddThumbnailCacheObserver(ThumbnailCacheObserver* observer);
57 void RemoveThumbnailCacheObserver(
58 ThumbnailCacheObserver* observer);
60 void Put(TabId tab_id, const SkBitmap& bitmap, float thumbnail_scale);
61 void Remove(TabId tab_id);
62 Thumbnail* Get(TabId tab_id, bool force_disk_read, bool allow_approximation);
64 void RemoveFromDiskAtAndAboveId(TabId min_id);
65 void InvalidateThumbnailIfChanged(TabId tab_id, const GURL& url);
66 bool CheckAndUpdateThumbnailMetaData(TabId tab_id, const GURL& url);
67 void UpdateVisibleIds(const TabIdList& priority);
68 void DecompressThumbnailFromFile(
69 TabId tab_id,
70 const base::Callback<void(bool, SkBitmap)>&
71 post_decompress_callback);
73 // ThumbnailDelegate implementation
74 void InvalidateCachedThumbnail(Thumbnail* thumbnail) override;
75 static base::FilePath GetCacheDirectory();
76 static base::FilePath GetFilePath(TabId tab_id);
78 private:
79 class ThumbnailMetaData {
80 public:
81 ThumbnailMetaData();
82 ThumbnailMetaData(const base::Time& current_time, const GURL& url);
83 const GURL& url() const { return url_; }
84 base::Time capture_time() const { return capture_time_; }
86 private:
87 base::Time capture_time_;
88 GURL url_;
91 typedef ScopedPtrExpiringCache<TabId, Thumbnail> ExpiringThumbnailCache;
92 typedef base::hash_map<TabId, ThumbnailMetaData> ThumbnailMetaDataMap;
94 void RemoveFromDisk(TabId tab_id);
95 static void RemoveFromDiskTask(TabId tab_id);
96 static void RemoveFromDiskAtAndAboveIdTask(TabId min_id);
97 void WriteThumbnailIfNecessary(TabId tab_id,
98 skia::RefPtr<SkPixelRef> compressed_data,
99 float scale,
100 const gfx::Size& content_size);
101 void CompressThumbnailIfNecessary(TabId tab_id,
102 const base::Time& time_stamp,
103 const SkBitmap& bitmap,
104 float scale);
105 void ReadNextThumbnail();
106 void MakeSpaceForNewItemIfNecessary(TabId tab_id);
107 void RemoveFromReadQueue(TabId tab_id);
108 static void WriteTask(TabId tab_id,
109 skia::RefPtr<SkPixelRef> compressed_data,
110 float scale,
111 const gfx::Size& content_size,
112 const base::Callback<void()>& post_write_task);
113 void PostWriteTask();
114 static void CompressionTask(
115 SkBitmap raw_data,
116 gfx::Size encoded_size,
117 const base::Callback<void(skia::RefPtr<SkPixelRef>, const gfx::Size&)>&
118 post_compression_task);
119 void PostCompressionTask(TabId tab_id,
120 const base::Time& time_stamp,
121 float scale,
122 skia::RefPtr<SkPixelRef> compressed_data,
123 const gfx::Size& content_size);
124 static void DecompressionTask(
125 const base::Callback<void(bool, SkBitmap)>&
126 post_decompress_callback,
127 skia::RefPtr<SkPixelRef> compressed_data,
128 float scale,
129 const gfx::Size& encoded_size);
130 static void ReadTask(
131 bool decompress,
132 TabId tab_id,
133 const base::Callback<
134 void(skia::RefPtr<SkPixelRef>, float, const gfx::Size&)>&
135 post_read_task);
136 void PostReadTask(TabId tab_id,
137 skia::RefPtr<SkPixelRef> compressed_data,
138 float scale,
139 const gfx::Size& content_size);
140 void NotifyObserversOfThumbnailRead(TabId tab_id);
141 void RemoveOnMatchedTimeStamp(TabId tab_id, const base::Time& time_stamp);
142 static std::pair<SkBitmap, float> CreateApproximation(const SkBitmap& bitmap,
143 float scale);
145 const size_t compression_queue_max_size_;
146 const size_t write_queue_max_size_;
147 const bool use_approximation_thumbnail_;
149 size_t compression_tasks_count_;
150 size_t write_tasks_count_;
151 bool read_in_progress_;
153 ExpiringThumbnailCache cache_;
154 ExpiringThumbnailCache approximation_cache_;
155 base::ObserverList<ThumbnailCacheObserver> observers_;
156 ThumbnailMetaDataMap thumbnail_meta_data_;
157 TabIdList read_queue_;
158 TabIdList visible_ids_;
160 ui::UIResourceProvider* ui_resource_provider_;
162 base::WeakPtrFactory<ThumbnailCache> weak_factory_;
164 DISALLOW_COPY_AND_ASSIGN(ThumbnailCache);
167 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_CACHE_H_