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_STORE_H_
6 #define CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_STORE_H_
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 "content/public/browser/android/ui_resource_client_android.h"
21 #include "content/public/browser/android/ui_resource_provider.h"
22 #include "third_party/skia/include/core/SkBitmap.h"
23 #include "ui/gfx/geometry/point.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gfx/geometry/size_f.h"
34 class ContentViewCore
;
37 typedef std::list
<TabId
> TabIdList
;
39 class ThumbnailStoreObserver
{
41 virtual void OnFinishedThumbnailRead(TabId tab_id
) = 0;
44 class ThumbnailStore
: ThumbnailDelegate
{
46 ThumbnailStore(const std::string
& disk_cache_path_str
,
47 size_t default_cache_size
,
48 size_t approximation_cache_size
,
49 size_t compression_queue_max_size
,
50 size_t write_queue_max_size
,
51 bool use_approximation_thumbnail
);
53 virtual ~ThumbnailStore();
55 void SetUIResourceProvider(content::UIResourceProvider
* ui_resource_provider
);
57 void AddThumbnailStoreObserver(ThumbnailStoreObserver
* observer
);
58 void RemoveThumbnailStoreObserver(
59 ThumbnailStoreObserver
* observer
);
61 void Put(TabId tab_id
, const SkBitmap
& bitmap
, float thumbnail_scale
);
62 void Remove(TabId tab_id
);
63 Thumbnail
* Get(TabId tab_id
, bool force_disk_read
);
65 void RemoveFromDiskAtAndAboveId(TabId min_id
);
66 void InvalidateThumbnailIfChanged(TabId tab_id
, const GURL
& url
);
67 bool CheckAndUpdateThumbnailMetaData(TabId tab_id
, const GURL
& url
);
68 void UpdateVisibleIds(const TabIdList
& priority
);
69 void DecompressThumbnailFromFile(
71 const base::Callback
<void(bool, SkBitmap
)>&
72 post_decompress_callback
);
74 // ThumbnailDelegate implementation
75 virtual void InvalidateCachedThumbnail(Thumbnail
* thumbnail
) OVERRIDE
;
78 class ThumbnailMetaData
{
81 ThumbnailMetaData(const base::Time
& current_time
, const GURL
& url
);
82 const GURL
& url() const { return url_
; }
83 base::Time
capture_time() const { return capture_time_
; }
86 base::Time capture_time_
;
90 typedef ScopedPtrExpiringCache
<TabId
, Thumbnail
> ExpiringThumbnailCache
;
91 typedef base::hash_map
<TabId
, ThumbnailMetaData
> ThumbnailMetaDataMap
;
93 void RemoveFromDisk(TabId tab_id
);
94 static void RemoveFromDiskTask(const base::FilePath
& file_path
);
95 static void RemoveFromDiskAtAndAboveIdTask(const base::FilePath
& dir_path
,
97 void WriteThumbnailIfNecessary(TabId tab_id
,
98 skia::RefPtr
<SkPixelRef
> compressed_data
,
100 const gfx::Size
& content_size
);
101 void CompressThumbnailIfNecessary(TabId tab_id
,
102 const base::Time
& time_stamp
,
103 const SkBitmap
& bitmap
,
105 void ReadNextThumbnail();
106 void MakeSpaceForNewItemIfNecessary(TabId tab_id
);
107 void RemoveFromReadQueue(TabId tab_id
);
108 base::FilePath
GetFilePath(TabId tab_id
) const;
109 static void WriteTask(const base::FilePath
& file_path
,
110 skia::RefPtr
<SkPixelRef
> compressed_data
,
112 const gfx::Size
& content_size
,
113 const base::Callback
<void()>& post_write_task
);
114 void PostWriteTask();
115 static void CompressionTask(
117 gfx::Size encoded_size
,
118 const base::Callback
<void(skia::RefPtr
<SkPixelRef
>, const gfx::Size
&)>&
119 post_compression_task
);
120 void PostCompressionTask(TabId tab_id
,
121 const base::Time
& time_stamp
,
123 skia::RefPtr
<SkPixelRef
> compressed_data
,
124 const gfx::Size
& content_size
);
125 static void DecompressionTask(
126 const base::Callback
<void(bool, SkBitmap
)>&
127 post_decompress_callback
,
128 skia::RefPtr
<SkPixelRef
> compressed_data
,
130 const gfx::Size
& encoded_size
);
131 static void ReadTask(
133 const base::FilePath
& file_path
,
134 const base::Callback
<
135 void(skia::RefPtr
<SkPixelRef
>, float, const gfx::Size
&)>&
137 void PostReadTask(TabId tab_id
,
138 skia::RefPtr
<SkPixelRef
> compressed_data
,
140 const gfx::Size
& content_size
);
141 void NotifyObserversOfThumbnailRead(TabId tab_id
);
142 void RemoveOnMatchedTimeStamp(TabId tab_id
, const base::Time
& time_stamp
);
143 static std::pair
<SkBitmap
, float> CreateApproximation(const SkBitmap
& bitmap
,
146 const base::FilePath disk_cache_path_
;
147 const size_t compression_queue_max_size_
;
148 const size_t write_queue_max_size_
;
149 const bool use_approximation_thumbnail_
;
151 size_t compression_tasks_count_
;
152 size_t write_tasks_count_
;
153 bool read_in_progress_
;
155 ExpiringThumbnailCache cache_
;
156 ExpiringThumbnailCache approximation_cache_
;
157 ObserverList
<ThumbnailStoreObserver
> observers_
;
158 ThumbnailMetaDataMap thumbnail_meta_data_
;
159 TabIdList read_queue_
;
160 TabIdList visible_ids_
;
162 content::UIResourceProvider
* ui_resource_provider_
;
164 base::WeakPtrFactory
<ThumbnailStore
> weak_factory_
;
166 DISALLOW_COPY_AND_ASSIGN(ThumbnailStore
);
169 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_STORE_H_