OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_MANAGER_IMPL_H_ |
6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ | 6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_MANAGER_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 17 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
18 #include "components/leveldb_proto/proto_database.h" | 18 #include "components/leveldb_proto/proto_database.h" |
19 #include "components/suggestions/image_manager.h" | 19 #include "components/suggestions/image_manager.h" |
20 #include "components/suggestions/proto/suggestions.pb.h" | 20 #include "components/suggestions/proto/suggestions.pb.h" |
21 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 class URLRequestContextGetter; | 25 class URLRequestContextGetter; |
26 } | 26 } |
27 | 27 |
28 namespace suggestions { | 28 namespace suggestions { |
29 | 29 |
| 30 class ImageData; |
30 class SuggestionsProfile; | 31 class SuggestionsProfile; |
31 class ThumbnailData; | |
32 | 32 |
33 // A class used to fetch server thumbnails asynchronously and manage the caching | 33 // A class used to fetch server images asynchronously and manage the caching |
34 // layer (both in memory and on disk). | 34 // layer (both in memory and on disk). |
35 class ThumbnailManager : public ImageManager, | 35 class ImageManagerImpl : public ImageManager, |
36 public chrome::BitmapFetcherDelegate { | 36 public chrome::BitmapFetcherDelegate { |
37 public: | 37 public: |
38 typedef std::vector<ThumbnailData> ThumbnailVector; | 38 typedef std::vector<ImageData> ImageDataVector; |
39 | 39 |
40 ThumbnailManager( | 40 ImageManagerImpl( |
41 net::URLRequestContextGetter* url_request_context, | 41 net::URLRequestContextGetter* url_request_context, |
42 scoped_ptr<leveldb_proto::ProtoDatabase<ThumbnailData> > database, | 42 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database, |
43 const base::FilePath& database_dir); | 43 const base::FilePath& database_dir); |
44 virtual ~ThumbnailManager(); | 44 virtual ~ImageManagerImpl(); |
45 | 45 |
46 // Overrides from ImageManager. | 46 // Overrides from ImageManager. |
47 virtual void Initialize(const SuggestionsProfile& suggestions) OVERRIDE; | 47 virtual void Initialize(const SuggestionsProfile& suggestions) OVERRIDE; |
48 // Should be called from the UI thread. | 48 // Should be called from the UI thread. |
49 virtual void GetImageForURL( | 49 virtual void GetImageForURL( |
50 const GURL& url, | 50 const GURL& url, |
51 base::Callback<void(const GURL&, const SkBitmap*)> callback) OVERRIDE; | 51 base::Callback<void(const GURL&, const SkBitmap*)> callback) OVERRIDE; |
52 | 52 |
53 private: | 53 private: |
54 friend class MockThumbnailManager; | 54 friend class MockImageManagerImpl; |
55 friend class ThumbnailManagerBrowserTest; | 55 friend class ImageManagerImplBrowserTest; |
56 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerTest, InitializeTest); | 56 FRIEND_TEST_ALL_PREFIXES(ImageManagerImplTest, InitializeTest); |
57 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, | 57 FRIEND_TEST_ALL_PREFIXES(ImageManagerImplBrowserTest, |
58 GetImageForURLNetworkCacheHit); | 58 GetImageForURLNetworkCacheHit); |
59 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, | 59 FRIEND_TEST_ALL_PREFIXES(ImageManagerImplBrowserTest, |
60 GetImageForURLNetworkCacheNotInitialized); | 60 GetImageForURLNetworkCacheNotInitialized); |
61 | 61 |
62 // Used for testing. | 62 // Used for testing. |
63 ThumbnailManager(); | 63 ImageManagerImpl(); |
64 | 64 |
65 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > | 65 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > |
66 CallbackVector; | 66 CallbackVector; |
67 typedef base::hash_map<std::string, SkBitmap> ThumbnailMap; | 67 typedef base::hash_map<std::string, SkBitmap> ImageMap; |
68 | 68 |
69 // State related to a thumbnail fetch (associated website url, thumbnail_url, | 69 // State related to an image fetch (associated website url, image_url, |
70 // fetcher, pending callbacks). | 70 // fetcher, pending callbacks). |
71 struct ThumbnailRequest { | 71 struct ImageRequest { |
72 ThumbnailRequest(); | 72 ImageRequest(); |
73 explicit ThumbnailRequest(chrome::BitmapFetcher* f); | 73 explicit ImageRequest(chrome::BitmapFetcher* f); |
74 ~ThumbnailRequest(); | 74 ~ImageRequest(); |
75 | 75 |
76 void swap(ThumbnailRequest* other) { | 76 void swap(ImageRequest* other) { |
77 std::swap(url, other->url); | 77 std::swap(url, other->url); |
78 std::swap(thumbnail_url, other->thumbnail_url); | 78 std::swap(image_url, other->image_url); |
79 std::swap(callbacks, other->callbacks); | 79 std::swap(callbacks, other->callbacks); |
80 std::swap(fetcher, other->fetcher); | 80 std::swap(fetcher, other->fetcher); |
81 } | 81 } |
82 | 82 |
83 GURL url; | 83 GURL url; |
84 GURL thumbnail_url; | 84 GURL image_url; |
85 chrome::BitmapFetcher* fetcher; | 85 chrome::BitmapFetcher* fetcher; |
86 // Queue for pending callbacks, which may accumulate while the request is in | 86 // Queue for pending callbacks, which may accumulate while the request is in |
87 // flight. | 87 // flight. |
88 CallbackVector callbacks; | 88 CallbackVector callbacks; |
89 }; | 89 }; |
90 | 90 |
91 typedef std::map<const GURL, ThumbnailRequest> ThumbnailRequestMap; | 91 typedef std::map<const GURL, ImageRequest> ImageRequestMap; |
92 | 92 |
93 // Looks up thumbnail for |url|. If found, writes the result to | 93 // Looks up image URL for |url|. If found, writes the result to |image_url| |
94 // |thumbnail_url| and returns true. Otherwise just returns false. | 94 // and returns true. Otherwise just returns false. |
95 bool GetThumbnailURL(const GURL& url, GURL* thumbnail_url); | 95 bool GetImageURL(const GURL& url, GURL* image_url); |
96 | 96 |
97 void QueueCacheRequest( | 97 void QueueCacheRequest( |
98 const GURL& url, const GURL& thumbnail_url, | 98 const GURL& url, const GURL& image_url, |
99 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 99 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
100 | 100 |
101 void ServeFromCacheOrNetwork( | 101 void ServeFromCacheOrNetwork( |
102 const GURL& url, const GURL& thumbnail_url, | 102 const GURL& url, const GURL& image_url, |
103 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 103 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
104 | 104 |
105 // Will return false if no bitmap was found corresponding to |url|, else | 105 // Will return false if no bitmap was found corresponding to |url|, else |
106 // return true and call |callback| with the found bitmap. | 106 // return true and call |callback| with the found bitmap. |
107 bool ServeFromCache( | 107 bool ServeFromCache( |
108 const GURL& url, | 108 const GURL& url, |
109 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 109 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
110 | 110 |
111 // Returns null if the |url| had no entry in the cache. | 111 // Returns null if the |url| had no entry in the cache. |
112 SkBitmap* GetBitmapFromCache(const GURL& url); | 112 SkBitmap* GetBitmapFromCache(const GURL& url); |
113 | 113 |
114 void StartOrQueueNetworkRequest( | 114 void StartOrQueueNetworkRequest( |
115 const GURL& url, const GURL& thumbnail_url, | 115 const GURL& url, const GURL& image_url, |
116 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 116 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
117 | 117 |
118 // Inherited from BitmapFetcherDelegate. Runs on the UI thread. | 118 // Inherited from BitmapFetcherDelegate. Runs on the UI thread. |
119 virtual void OnFetchComplete(const GURL thumbnail_url, | 119 virtual void OnFetchComplete(const GURL image_url, |
120 const SkBitmap* bitmap) OVERRIDE; | 120 const SkBitmap* bitmap) OVERRIDE; |
121 | 121 |
122 // Save the thumbnail bitmap in the cache and in the database. | 122 // Save the image bitmap in the cache and in the database. |
123 void SaveThumbnail(const GURL& url, const SkBitmap& bitmap); | 123 void SaveImage(const GURL& url, const SkBitmap& bitmap); |
124 | 124 |
125 // Database callback methods. | 125 // Database callback methods. |
126 // Will initiate loading the entries. | 126 // Will initiate loading the entries. |
127 void OnDatabaseInit(bool success); | 127 void OnDatabaseInit(bool success); |
128 // Will transfer the loaded |entries| in memory (|thumbnail_map_|). | 128 // Will transfer the loaded |entries| in memory (|image_map_|). |
129 void OnDatabaseLoad(bool success, scoped_ptr<ThumbnailVector> entries); | 129 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); |
130 void OnDatabaseSave(bool success); | 130 void OnDatabaseSave(bool success); |
131 | 131 |
132 // Take entries from the database and put them in the local cache. | 132 // Take entries from the database and put them in the local cache. |
133 void LoadEntriesInCache(scoped_ptr<ThumbnailVector> entries); | 133 void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries); |
134 | 134 |
135 void ServePendingCacheRequests(); | 135 void ServePendingCacheRequests(); |
136 | 136 |
137 // From SkBitmap to the vector of JPEG-encoded bytes, |dst|. Visible only for | 137 // From SkBitmap to the vector of JPEG-encoded bytes, |dst|. Visible only for |
138 // testing. | 138 // testing. |
139 static bool EncodeThumbnail(const SkBitmap& bitmap, | 139 static bool EncodeImage(const SkBitmap& bitmap, |
140 std::vector<unsigned char>* dest); | 140 std::vector<unsigned char>* dest); |
141 | 141 |
142 // Map from URL to thumbnail URL. Should be kept up to date when a new | 142 // Map from URL to image URL. Should be kept up to date when a new |
143 // SuggestionsProfile is available. | 143 // SuggestionsProfile is available. |
144 std::map<GURL, GURL> thumbnail_url_map_; | 144 std::map<GURL, GURL> image_url_map_; |
145 | 145 |
146 // Map from each thumbnail URL to the request information (associated website | 146 // Map from each image URL to the request information (associated website |
147 // url, fetcher, pending callbacks). | 147 // url, fetcher, pending callbacks). |
148 ThumbnailRequestMap pending_net_requests_; | 148 ImageRequestMap pending_net_requests_; |
149 | 149 |
150 // Map from website URL to request information, used for pending cache | 150 // Map from website URL to request information, used for pending cache |
151 // requests while the database hasn't loaded. | 151 // requests while the database hasn't loaded. |
152 ThumbnailRequestMap pending_cache_requests_; | 152 ImageRequestMap pending_cache_requests_; |
153 | 153 |
154 // Holding the bitmaps in memory, keyed by website URL string. | 154 // Holding the bitmaps in memory, keyed by website URL string. |
155 ThumbnailMap thumbnail_map_; | 155 ImageMap image_map_; |
156 | 156 |
157 net::URLRequestContextGetter* url_request_context_; | 157 net::URLRequestContextGetter* url_request_context_; |
158 | 158 |
159 scoped_ptr<leveldb_proto::ProtoDatabase<ThumbnailData> > database_; | 159 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_; |
160 | 160 |
161 bool database_ready_; | 161 bool database_ready_; |
162 | 162 |
163 base::WeakPtrFactory<ThumbnailManager> weak_ptr_factory_; | 163 base::WeakPtrFactory<ImageManagerImpl> weak_ptr_factory_; |
164 | 164 |
165 DISALLOW_COPY_AND_ASSIGN(ThumbnailManager); | 165 DISALLOW_COPY_AND_ASSIGN(ImageManagerImpl); |
166 }; | 166 }; |
167 | 167 |
168 } // namespace suggestions | 168 } // namespace suggestions |
169 | 169 |
170 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ | 170 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_IMAGE_MANAGER_IMPL_H_ |
OLD | NEW |