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