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 CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_STORE_H_ 6 #define CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_STORE_H_ 7 8 #include <list> 9 #include <set> 10 #include <string> 11 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" 26 #include "url/gurl.h" 27 28 namespace base { 29 class File; 30 class Time; 31 } 32 33 namespace content { 34 class ContentViewCore; 35 } 36 37 typedef std::list<TabId> TabIdList; 38 39 class ThumbnailStoreObserver { 40 public: 41 virtual void OnFinishedThumbnailRead(TabId tab_id) = 0; 42 }; 43 44 class ThumbnailStore : ThumbnailDelegate { 45 public: 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); 52 53 virtual ~ThumbnailStore(); 54 55 void SetUIResourceProvider(content::UIResourceProvider* ui_resource_provider); 56 57 void AddThumbnailStoreObserver(ThumbnailStoreObserver* observer); 58 void RemoveThumbnailStoreObserver( 59 ThumbnailStoreObserver* observer); 60 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, bool allow_approximation); 64 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( 70 TabId tab_id, 71 const base::Callback<void(bool, SkBitmap)>& 72 post_decompress_callback); 73 74 // ThumbnailDelegate implementation 75 virtual void InvalidateCachedThumbnail(Thumbnail* thumbnail) OVERRIDE; 76 77 private: 78 class ThumbnailMetaData { 79 public: 80 ThumbnailMetaData(); 81 ThumbnailMetaData(const base::Time& current_time, const GURL& url); url()82 const GURL& url() const { return url_; } capture_time()83 base::Time capture_time() const { return capture_time_; } 84 85 private: 86 base::Time capture_time_; 87 GURL url_; 88 }; 89 90 typedef ScopedPtrExpiringCache<TabId, Thumbnail> ExpiringThumbnailCache; 91 typedef base::hash_map<TabId, ThumbnailMetaData> ThumbnailMetaDataMap; 92 93 void RemoveFromDisk(TabId tab_id); 94 static void RemoveFromDiskTask(const base::FilePath& file_path); 95 static void RemoveFromDiskAtAndAboveIdTask(const base::FilePath& dir_path, 96 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 base::FilePath GetFilePath(TabId tab_id) const; 109 static void WriteTask(const base::FilePath& file_path, 110 skia::RefPtr<SkPixelRef> compressed_data, 111 float scale, 112 const gfx::Size& content_size, 113 const base::Callback<void()>& post_write_task); 114 void PostWriteTask(); 115 static void CompressionTask( 116 SkBitmap raw_data, 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, 122 float scale, 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, 129 float scale, 130 const gfx::Size& encoded_size); 131 static void ReadTask( 132 bool decompress, 133 const base::FilePath& file_path, 134 const base::Callback< 135 void(skia::RefPtr<SkPixelRef>, float, const gfx::Size&)>& 136 post_read_task); 137 void PostReadTask(TabId tab_id, 138 skia::RefPtr<SkPixelRef> compressed_data, 139 float scale, 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, 144 float scale); 145 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_; 150 151 size_t compression_tasks_count_; 152 size_t write_tasks_count_; 153 bool read_in_progress_; 154 155 ExpiringThumbnailCache cache_; 156 ExpiringThumbnailCache approximation_cache_; 157 ObserverList<ThumbnailStoreObserver> observers_; 158 ThumbnailMetaDataMap thumbnail_meta_data_; 159 TabIdList read_queue_; 160 TabIdList visible_ids_; 161 162 content::UIResourceProvider* ui_resource_provider_; 163 164 base::WeakPtrFactory<ThumbnailStore> weak_factory_; 165 166 DISALLOW_COPY_AND_ASSIGN(ThumbnailStore); 167 }; 168 169 #endif // CHROME_BROWSER_ANDROID_THUMBNAIL_THUMBNAIL_STORE_H_ 170