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> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 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/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "components/image_fetcher/image_fetcher_delegate.h" | 21 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 22 #include "components/leveldb_proto/proto_database.h" | 22 #include "components/leveldb_proto/proto_database.h" |
| 23 #include "components/suggestions/proto/suggestions.pb.h" | 23 #include "components/suggestions/proto/suggestions.pb.h" |
| 24 #include "ui/gfx/image/image_skia.h" | 24 #include "ui/gfx/image/image_skia.h" |
|
Marc Treib
2016/05/20 14:40:04
Not your doing, but is this include needed?
markusheintz_
2016/05/23 13:20:47
Replaced with an include for SkBitmap
This requir
Marc Treib
2016/05/23 13:44:03
Ah, okay. If this requires extra approvals for the
markusheintz_
2016/05/24 08:32:31
Done.
| |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 | 26 |
| 27 namespace gfx { | |
| 28 class Image; | |
| 29 } | |
| 30 | |
| 27 namespace image_fetcher { | 31 namespace image_fetcher { |
| 28 class ImageFetcher; | 32 class ImageFetcher; |
| 29 } | 33 } |
| 30 | 34 |
| 31 namespace net { | 35 namespace net { |
| 32 class URLRequestContextGetter; | 36 class URLRequestContextGetter; |
| 33 } | 37 } |
| 34 | 38 |
| 35 namespace suggestions { | 39 namespace suggestions { |
| 36 | 40 |
| 37 class ImageData; | 41 class ImageData; |
| 38 class SuggestionsProfile; | 42 class SuggestionsProfile; |
| 39 | 43 |
| 40 // A class used to fetch server images asynchronously and manage the caching | 44 // A class used to fetch server images asynchronously and manage the caching |
| 41 // layer (both in memory and on disk). | 45 // layer (both in memory and on disk). |
| 42 class ImageManager : public image_fetcher::ImageFetcherDelegate { | 46 class ImageManager : public image_fetcher::ImageFetcherDelegate { |
|
noyau (Ping after 24h)
2016/05/20 15:15:59
I'm lost in this code, but from what I read, this
markusheintz_
2016/05/23 13:20:46
While I'm cleaning this up I'll try to identify su
| |
| 43 public: | 47 public: |
| 44 typedef std::vector<ImageData> ImageDataVector; | 48 typedef std::vector<ImageData> ImageDataVector; |
| 45 | 49 |
| 46 ImageManager( | 50 ImageManager( |
| 47 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 51 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 48 std::unique_ptr<leveldb_proto::ProtoDatabase<ImageData>> database, | 52 std::unique_ptr<leveldb_proto::ProtoDatabase<ImageData>> database, |
| 49 const base::FilePath& database_dir, | 53 const base::FilePath& database_dir, |
| 50 scoped_refptr<base::TaskRunner> background_task_runner); | 54 scoped_refptr<base::TaskRunner> background_task_runner); |
| 51 ~ImageManager() override; | 55 ~ImageManager() override; |
| 52 | 56 |
| 53 virtual void Initialize(const SuggestionsProfile& suggestions); | 57 virtual void Initialize(const SuggestionsProfile& suggestions); |
| 54 | 58 |
| 55 // Should be called from the UI thread. | 59 // Should be called from the UI thread. |
| 56 virtual void AddImageURL(const GURL& url, const GURL& image_url); | 60 virtual void AddImageURL(const GURL& url, const GURL& image_url); |
| 57 | 61 |
| 58 // Should be called from the UI thread. | 62 // Should be called from the UI thread. |
| 59 virtual void GetImageForURL( | 63 virtual void GetImageForURL( |
| 60 const GURL& url, | 64 const GURL& url, |
| 61 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 65 base::Callback<void(const GURL&, const gfx::Image&)> callback); |
| 62 | 66 |
| 63 protected: | 67 protected: |
| 64 // Methods inherited from image_fetcher::ImageFetcherDelegate | 68 // Methods inherited from image_fetcher::ImageFetcherDelegate |
| 65 | 69 |
| 66 // Perform additional tasks when an image has been fetched. | 70 // Perform additional tasks when an image has been fetched. |
| 67 void OnImageFetched(const GURL& url, const gfx::Image& image) override; | 71 void OnImageFetched(const GURL& url, const gfx::Image& image) override; |
| 68 | 72 |
| 69 private: | 73 private: |
| 70 friend class MockImageManager; | 74 friend class MockImageManager; |
| 71 friend class ImageManagerTest; | 75 friend class ImageManagerTest; |
| 72 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); | 76 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); |
| 73 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, AddImageURL); | 77 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, AddImageURL); |
| 74 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); | 78 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); |
| 75 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, | 79 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, |
| 76 GetImageForURLNetworkCacheNotInitialized); | 80 GetImageForURLNetworkCacheNotInitialized); |
| 77 | 81 |
| 78 // Used for testing. | 82 // Used for testing. |
| 79 ImageManager(); | 83 ImageManager(); |
| 80 | 84 |
| 81 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > | 85 typedef std::vector<base::Callback<void(const GURL&, const gfx::Image&)> > |
| 82 CallbackVector; | 86 CallbackVector; |
| 83 typedef base::hash_map<std::string, scoped_refptr<base::RefCountedMemory>> | 87 typedef base::hash_map<std::string, scoped_refptr<base::RefCountedMemory>> |
| 84 ImageMap; | 88 ImageMap; |
| 85 | 89 |
| 86 // State related to an image fetch (associated website url, image_url, | 90 // State related to an image fetch (associated website url, image_url, |
| 87 // pending callbacks). | 91 // pending callbacks). |
| 88 struct ImageCacheRequest { | 92 struct ImageCacheRequest { |
| 89 ImageCacheRequest(); | 93 ImageCacheRequest(); |
| 90 ImageCacheRequest(const ImageCacheRequest& other); | 94 ImageCacheRequest(const ImageCacheRequest& other); |
| 91 ~ImageCacheRequest(); | 95 ~ImageCacheRequest(); |
| 92 | 96 |
| 93 GURL url; | 97 GURL url; |
| 94 GURL image_url; | 98 GURL image_url; |
| 95 // Queue for pending callbacks, which may accumulate while the request is in | 99 // Queue for pending callbacks, which may accumulate while the request is in |
| 96 // flight. | 100 // flight. |
| 97 CallbackVector callbacks; | 101 CallbackVector callbacks; |
| 98 }; | 102 }; |
| 99 | 103 |
| 100 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; | 104 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; |
| 101 | 105 |
| 102 // Looks up image URL for |url|. If found, writes the result to |image_url| | 106 // Looks up image URL for |url|. If found, writes the result to |image_url| |
| 103 // and returns true. Otherwise just returns false. | 107 // and returns true. Otherwise just returns false. |
| 104 bool GetImageURL(const GURL& url, GURL* image_url); | 108 bool GetImageURL(const GURL& url, GURL* image_url); |
| 105 | 109 |
| 106 void QueueCacheRequest( | 110 void QueueCacheRequest( |
| 107 const GURL& url, const GURL& image_url, | 111 const GURL& url, const GURL& image_url, |
| 108 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 112 base::Callback<void(const GURL&, const gfx::Image&)> callback); |
|
noyau (Ping after 24h)
2016/05/20 15:15:59
You are using this callback type in 4 places just
markusheintz_
2016/05/23 13:20:47
Done
| |
| 109 | 113 |
| 110 void ServeFromCacheOrNetwork( | 114 void ServeFromCacheOrNetwork( |
| 111 const GURL& url, const GURL& image_url, | 115 const GURL& url, const GURL& image_url, |
| 112 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 116 base::Callback<void(const GURL&, const gfx::Image&)> callback); |
| 113 | 117 |
| 114 void OnCacheImageDecoded( | 118 void OnCacheImageDecoded( |
| 115 const GURL& url, | 119 const GURL& url, |
| 116 const GURL& image_url, | 120 const GURL& image_url, |
| 117 base::Callback<void(const GURL&, const SkBitmap*)> callback, | 121 const base::Callback<void(const GURL&, const gfx::Image&)>& callback, |
| 118 std::unique_ptr<SkBitmap> bitmap); | 122 std::unique_ptr<SkBitmap> bitmap); |
| 119 | 123 |
| 120 // Returns null if the |url| had no entry in the cache. | 124 // Returns null if the |url| had no entry in the cache. |
| 121 scoped_refptr<base::RefCountedMemory> GetEncodedImageFromCache( | 125 scoped_refptr<base::RefCountedMemory> GetEncodedImageFromCache( |
| 122 const GURL& url); | 126 const GURL& url); |
| 123 | 127 |
| 124 // Save the image bitmap in the cache and in the database. | 128 // Save the image bitmap in the cache and in the database. |
| 125 void SaveImage(const GURL& url, const SkBitmap& bitmap); | 129 void SaveImage(const GURL& url, const SkBitmap& bitmap); |
| 126 | 130 |
| 127 // Database callback methods. | 131 // Database callback methods. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 158 base::ThreadChecker thread_checker_; | 162 base::ThreadChecker thread_checker_; |
| 159 | 163 |
| 160 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; | 164 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; |
| 161 | 165 |
| 162 DISALLOW_COPY_AND_ASSIGN(ImageManager); | 166 DISALLOW_COPY_AND_ASSIGN(ImageManager); |
| 163 }; | 167 }; |
| 164 | 168 |
| 165 } // namespace suggestions | 169 } // namespace suggestions |
| 166 | 170 |
| 167 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ | 171 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ |
| OLD | NEW |