Chromium Code Reviews| 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 COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ | 5 #ifndef COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ |
| 6 #define COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ | 6 #define COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <utility> | |
|
huangs
2014/09/24 15:46:58
What's <utility> used for?
Mathieu
2014/09/24 19:31:30
Done.
| |
| 11 #include <vector> | |
| 12 | |
| 8 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/containers/hash_tables.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "base/threading/thread_checker.h" | |
| 19 #include "components/leveldb_proto/proto_database.h" | |
| 20 #include "components/suggestions/image_fetcher_delegate.h" | |
| 10 #include "components/suggestions/proto/suggestions.pb.h" | 21 #include "components/suggestions/proto/suggestions.pb.h" |
| 11 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
| 12 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 13 | 24 |
| 25 namespace net { | |
| 26 class URLRequestContextGetter; | |
| 27 } | |
| 28 | |
| 14 namespace suggestions { | 29 namespace suggestions { |
| 15 | 30 |
| 16 // An interface to retrieve images related to a specific URL. | 31 class ImageData; |
| 17 class ImageManager { | 32 class ImageFetcher; |
| 33 class SuggestionsProfile; | |
| 34 | |
| 35 // A class used to fetch server images asynchronously and manage the caching | |
| 36 // layer (both in memory and on disk). | |
| 37 class ImageManager : public ImageFetcherDelegate { | |
| 18 public: | 38 public: |
| 19 ImageManager() {} | 39 typedef std::vector<ImageData> ImageDataVector; |
| 20 virtual ~ImageManager() {} | |
| 21 | 40 |
| 22 // (Re)Initializes states using data received from a SuggestionService. We're | 41 ImageManager(scoped_ptr<ImageFetcher> image_fetcher, |
| 23 // not doing this in the constructor because an instance may be long-lived. | 42 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database, |
| 24 virtual void Initialize(const SuggestionsProfile& suggestions) = 0; | 43 const base::FilePath& database_dir); |
| 44 virtual ~ImageManager(); | |
| 25 | 45 |
| 26 // Retrieves stored image for website |url| asynchronously. Calls |callback| | 46 void Initialize(const SuggestionsProfile& suggestions); |
| 27 // with Bitmap pointer if found, and NULL otherwise. | 47 // Should be called from the UI thread. |
| 28 virtual void GetImageForURL( | 48 void GetImageForURL( |
| 29 const GURL& url, | 49 const GURL& url, |
| 30 base::Callback<void(const GURL&, const SkBitmap*)> callback) = 0; | 50 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
| 51 | |
| 52 protected: | |
| 53 // Perform additional tasks when an image has been fetched. | |
| 54 virtual void OnImageFetched(const GURL& url, const SkBitmap* bitmap) OVERRIDE; | |
| 31 | 55 |
| 32 private: | 56 private: |
| 57 friend class MockImageManager; | |
| 58 friend class ImageManagerTest; | |
| 59 FRIEND_TEST_ALL_PREFIXES(ImageFetcherImplBrowserTest, NormalFetch); | |
| 60 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); | |
| 61 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); | |
| 62 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, | |
| 63 GetImageForURLNetworkCacheNotInitialized); | |
| 64 | |
| 65 // Used for testing. | |
| 66 ImageManager(); | |
| 67 | |
| 68 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > | |
| 69 CallbackVector; | |
| 70 typedef base::hash_map<std::string, SkBitmap> ImageMap; | |
| 71 | |
| 72 // State related to an image fetch (associated website url, image_url, | |
| 73 // pending callbacks). | |
| 74 struct ImageCacheRequest { | |
| 75 ImageCacheRequest(); | |
| 76 ~ImageCacheRequest(); | |
| 77 | |
| 78 GURL url; | |
| 79 GURL image_url; | |
| 80 // Queue for pending callbacks, which may accumulate while the request is in | |
| 81 // flight. | |
| 82 CallbackVector callbacks; | |
| 83 }; | |
| 84 | |
| 85 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; | |
|
huangs
2014/09/24 15:46:58
Is there reason why you're using std::map here but
Mathieu
2014/09/24 19:31:30
GURL is not hashable
| |
| 86 | |
| 87 // Looks up image URL for |url|. If found, writes the result to |image_url| | |
| 88 // and returns true. Otherwise just returns false. | |
| 89 bool GetImageURL(const GURL& url, GURL* image_url); | |
| 90 | |
| 91 void QueueCacheRequest( | |
| 92 const GURL& url, const GURL& image_url, | |
| 93 base::Callback<void(const GURL&, const SkBitmap*)> callback); | |
| 94 | |
| 95 void ServeFromCacheOrNetwork( | |
| 96 const GURL& url, const GURL& image_url, | |
| 97 base::Callback<void(const GURL&, const SkBitmap*)> callback); | |
| 98 | |
| 99 // Will return false if no bitmap was found corresponding to |url|, else | |
| 100 // return true and call |callback| with the found bitmap. | |
| 101 bool ServeFromCache( | |
| 102 const GURL& url, | |
| 103 base::Callback<void(const GURL&, const SkBitmap*)> callback); | |
| 104 | |
| 105 // Returns null if the |url| had no entry in the cache. | |
| 106 SkBitmap* GetBitmapFromCache(const GURL& url); | |
| 107 | |
| 108 // Save the image bitmap in the cache and in the database. | |
| 109 void SaveImage(const GURL& url, const SkBitmap& bitmap); | |
| 110 | |
| 111 // Database callback methods. | |
| 112 // Will initiate loading the entries. | |
| 113 void OnDatabaseInit(bool success); | |
| 114 // Will transfer the loaded |entries| in memory (|image_map_|). | |
| 115 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); | |
| 116 void OnDatabaseSave(bool success); | |
| 117 | |
| 118 // Take entries from the database and put them in the local cache. | |
| 119 void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries); | |
| 120 | |
| 121 void ServePendingCacheRequests(); | |
| 122 | |
| 123 // From SkBitmap to the vector of JPEG-encoded bytes, |dst|. Visible only for | |
| 124 // testing. | |
| 125 static bool EncodeImage(const SkBitmap& bitmap, | |
| 126 std::vector<unsigned char>* dest); | |
| 127 | |
| 128 // Map from URL to image URL. Should be kept up to date when a new | |
| 129 // SuggestionsProfile is available. | |
| 130 std::map<GURL, GURL> image_url_map_; | |
| 131 | |
| 132 // Map from website URL to request information, used for pending cache | |
| 133 // requests while the database hasn't loaded. | |
| 134 ImageCacheRequestMap pending_cache_requests_; | |
| 135 | |
| 136 // Holding the bitmaps in memory, keyed by website URL string. | |
| 137 ImageMap image_map_; | |
| 138 | |
| 139 scoped_ptr<ImageFetcher> image_fetcher_; | |
| 140 | |
| 141 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_; | |
| 142 | |
| 143 bool database_ready_; | |
| 144 | |
| 145 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; | |
| 146 | |
| 147 base::ThreadChecker thread_checker_; | |
| 148 | |
| 33 DISALLOW_COPY_AND_ASSIGN(ImageManager); | 149 DISALLOW_COPY_AND_ASSIGN(ImageManager); |
| 34 }; | 150 }; |
| 35 | 151 |
| 36 } // namespace suggestions | 152 } // namespace suggestions |
| 37 | 153 |
| 38 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ | 154 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ |
| OLD | NEW |