| 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 <string> | 9 #include <string> |
| 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/ref_counted_memory.h" |
| 15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 17 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 18 #include "components/leveldb_proto/proto_database.h" | 19 #include "components/leveldb_proto/proto_database.h" |
| 19 #include "components/suggestions/image_fetcher_delegate.h" | 20 #include "components/suggestions/image_fetcher_delegate.h" |
| 20 #include "components/suggestions/proto/suggestions.pb.h" | 21 #include "components/suggestions/proto/suggestions.pb.h" |
| 21 #include "ui/gfx/image/image_skia.h" | 22 #include "ui/gfx/image/image_skia.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); | 60 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); |
| 60 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); | 61 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); |
| 61 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, | 62 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, |
| 62 GetImageForURLNetworkCacheNotInitialized); | 63 GetImageForURLNetworkCacheNotInitialized); |
| 63 | 64 |
| 64 // Used for testing. | 65 // Used for testing. |
| 65 ImageManager(); | 66 ImageManager(); |
| 66 | 67 |
| 67 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > | 68 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > |
| 68 CallbackVector; | 69 CallbackVector; |
| 69 typedef base::hash_map<std::string, SkBitmap> ImageMap; | 70 typedef base::hash_map<std::string, scoped_refptr<base::RefCountedMemory> > |
| 71 ImageMap; |
| 70 | 72 |
| 71 // State related to an image fetch (associated website url, image_url, | 73 // State related to an image fetch (associated website url, image_url, |
| 72 // pending callbacks). | 74 // pending callbacks). |
| 73 struct ImageCacheRequest { | 75 struct ImageCacheRequest { |
| 74 ImageCacheRequest(); | 76 ImageCacheRequest(); |
| 75 ~ImageCacheRequest(); | 77 ~ImageCacheRequest(); |
| 76 | 78 |
| 77 GURL url; | 79 GURL url; |
| 78 GURL image_url; | 80 GURL image_url; |
| 79 // Queue for pending callbacks, which may accumulate while the request is in | 81 // Queue for pending callbacks, which may accumulate while the request is in |
| 80 // flight. | 82 // flight. |
| 81 CallbackVector callbacks; | 83 CallbackVector callbacks; |
| 82 }; | 84 }; |
| 83 | 85 |
| 84 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; | 86 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; |
| 85 | 87 |
| 86 // Looks up image URL for |url|. If found, writes the result to |image_url| | 88 // Looks up image URL for |url|. If found, writes the result to |image_url| |
| 87 // and returns true. Otherwise just returns false. | 89 // and returns true. Otherwise just returns false. |
| 88 bool GetImageURL(const GURL& url, GURL* image_url); | 90 bool GetImageURL(const GURL& url, GURL* image_url); |
| 89 | 91 |
| 90 void QueueCacheRequest( | 92 void QueueCacheRequest( |
| 91 const GURL& url, const GURL& image_url, | 93 const GURL& url, const GURL& image_url, |
| 92 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 94 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
| 93 | 95 |
| 94 void ServeFromCacheOrNetwork( | 96 void ServeFromCacheOrNetwork( |
| 95 const GURL& url, const GURL& image_url, | 97 const GURL& url, const GURL& image_url, |
| 96 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 98 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
| 97 | 99 |
| 98 // Will return false if no bitmap was found corresponding to |url|, else | 100 void OnCacheImageDecoded( |
| 99 // return true and call |callback| with the found bitmap. | 101 const GURL& url, const GURL& image_url, |
| 100 bool ServeFromCache( | 102 base::Callback<void(const GURL&, const SkBitmap*)> callback, |
| 101 const GURL& url, | 103 scoped_ptr<SkBitmap> bitmap); |
| 102 base::Callback<void(const GURL&, const SkBitmap*)> callback); | |
| 103 | 104 |
| 104 // Returns null if the |url| had no entry in the cache. | 105 // Returns null if the |url| had no entry in the cache. |
| 105 SkBitmap* GetBitmapFromCache(const GURL& url); | 106 scoped_refptr<base::RefCountedMemory> GetEncodedImageFromCache( |
| 107 const GURL& url); |
| 106 | 108 |
| 107 // Save the image bitmap in the cache and in the database. | 109 // Save the image bitmap in the cache and in the database. |
| 108 void SaveImage(const GURL& url, const SkBitmap& bitmap); | 110 void SaveImage(const GURL& url, const SkBitmap& bitmap); |
| 109 | 111 |
| 110 // Database callback methods. | 112 // Database callback methods. |
| 111 // Will initiate loading the entries. | 113 // Will initiate loading the entries. |
| 112 void OnDatabaseInit(bool success); | 114 void OnDatabaseInit(bool success); |
| 113 // Will transfer the loaded |entries| in memory (|image_map_|). | 115 // Will transfer the loaded |entries| in memory (|image_map_|). |
| 114 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); | 116 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); |
| 115 void OnDatabaseSave(bool success); | 117 void OnDatabaseSave(bool success); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 139 base::ThreadChecker thread_checker_; | 141 base::ThreadChecker thread_checker_; |
| 140 | 142 |
| 141 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; | 143 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; |
| 142 | 144 |
| 143 DISALLOW_COPY_AND_ASSIGN(ImageManager); | 145 DISALLOW_COPY_AND_ASSIGN(ImageManager); |
| 144 }; | 146 }; |
| 145 | 147 |
| 146 } // namespace suggestions | 148 } // namespace suggestions |
| 147 | 149 |
| 148 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ | 150 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ |
| OLD | NEW |