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_THUMBNAIL_MANAGER_H_ |
6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ | 6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_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 "chrome/browser/search/suggestions/image_manager.h" |
18 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 19 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
19 #include "components/leveldb_proto/proto_database.h" | 20 #include "components/leveldb_proto/proto_database.h" |
20 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 class URLRequestContextGetter; | 25 class URLRequestContextGetter; |
25 } | 26 } |
26 | 27 |
27 namespace suggestions { | 28 namespace suggestions { |
28 | 29 |
29 class SuggestionsProfile; | 30 class SuggestionsProfile; |
30 class ThumbnailData; | 31 class ThumbnailData; |
31 | 32 |
32 // A class used to fetch server thumbnails asynchronously and manage the caching | 33 // A class used to fetch server thumbnails asynchronously and manage the caching |
33 // layer (both in memory and on disk). | 34 // layer (both in memory and on disk). |
34 class ThumbnailManager : public chrome::BitmapFetcherDelegate { | 35 class ThumbnailManager : public ImageManager, |
| 36 public chrome::BitmapFetcherDelegate { |
35 public: | 37 public: |
36 typedef std::vector<ThumbnailData> ThumbnailVector; | 38 typedef std::vector<ThumbnailData> ThumbnailVector; |
37 | 39 |
38 ThumbnailManager( | 40 ThumbnailManager( |
39 net::URLRequestContextGetter* url_request_context, | 41 net::URLRequestContextGetter* url_request_context, |
40 scoped_ptr<leveldb_proto::ProtoDatabase<ThumbnailData> > database, | 42 scoped_ptr<leveldb_proto::ProtoDatabase<ThumbnailData> > database, |
41 const base::FilePath& database_dir); | 43 const base::FilePath& database_dir); |
42 virtual ~ThumbnailManager(); | 44 virtual ~ThumbnailManager(); |
43 | 45 |
44 // Initializes the |thumbnail_url_map_| with the proper mapping from website | 46 // Overrides from ImageManager. |
45 // URL to thumbnail URL. | 47 virtual void Initialize(const SuggestionsProfile& suggestions) OVERRIDE; |
46 void InitializeThumbnailMap(const SuggestionsProfile& suggestions); | 48 // Should be called from the UI thread. |
47 | 49 virtual void GetImageForURL( |
48 // Retrieves stored thumbnail for website |url| asynchronously. Calls | |
49 // |callback| with Bitmap pointer if found, and NULL otherwise. Should be | |
50 // called from the UI thread. | |
51 void GetPageThumbnail( | |
52 const GURL& url, | 50 const GURL& url, |
53 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 51 base::Callback<void(const GURL&, const SkBitmap*)> callback) OVERRIDE; |
54 | 52 |
55 private: | 53 private: |
56 friend class MockThumbnailManager; | 54 friend class MockThumbnailManager; |
57 friend class ThumbnailManagerBrowserTest; | 55 friend class ThumbnailManagerBrowserTest; |
58 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerTest, InitializeThumbnailMapTest); | 56 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerTest, InitializeTest); |
59 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, | 57 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, |
60 GetPageThumbnailNetworkCacheHit); | 58 GetImageForURLNetworkCacheHit); |
61 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, | 59 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, |
62 GetPageThumbnailNetworkCacheNotInitialized); | 60 GetImageForURLNetworkCacheNotInitialized); |
63 | 61 |
64 // Used for testing. | 62 // Used for testing. |
65 ThumbnailManager(); | 63 ThumbnailManager(); |
66 | 64 |
67 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > | 65 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > |
68 CallbackVector; | 66 CallbackVector; |
69 typedef base::hash_map<std::string, SkBitmap> ThumbnailMap; | 67 typedef base::hash_map<std::string, SkBitmap> ThumbnailMap; |
70 | 68 |
71 // State related to a thumbnail fetch (associated website url, thumbnail_url, | 69 // State related to a thumbnail fetch (associated website url, thumbnail_url, |
72 // fetcher, pending callbacks). | 70 // fetcher, pending callbacks). |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 bool database_ready_; | 161 bool database_ready_; |
164 | 162 |
165 base::WeakPtrFactory<ThumbnailManager> weak_ptr_factory_; | 163 base::WeakPtrFactory<ThumbnailManager> weak_ptr_factory_; |
166 | 164 |
167 DISALLOW_COPY_AND_ASSIGN(ThumbnailManager); | 165 DISALLOW_COPY_AND_ASSIGN(ThumbnailManager); |
168 }; | 166 }; |
169 | 167 |
170 } // namespace suggestions | 168 } // namespace suggestions |
171 | 169 |
172 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ | 170 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ |
OLD | NEW |