| 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> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 class URLRequestContextGetter; | 25 class URLRequestContextGetter; |
| 26 } | 26 } |
| 27 | 27 |
| 28 namespace suggestions { | 28 namespace suggestions { |
| 29 | 29 |
| 30 class ImageData; | 30 class ImageData; |
| 31 class ImageEncoder; |
| 31 class ImageFetcher; | 32 class ImageFetcher; |
| 32 class SuggestionsProfile; | 33 class SuggestionsProfile; |
| 33 | 34 |
| 34 // A class used to fetch server images asynchronously and manage the caching | 35 // A class used to fetch server images asynchronously and manage the caching |
| 35 // layer (both in memory and on disk). | 36 // layer (both in memory and on disk). |
| 36 class ImageManager : public ImageFetcherDelegate { | 37 class ImageManager : public ImageFetcherDelegate { |
| 37 public: | 38 public: |
| 38 typedef std::vector<ImageData> ImageDataVector; | 39 typedef std::vector<ImageData> ImageDataVector; |
| 39 | 40 |
| 41 // This class takes ownership of |image_fetcher|, |image_encoder| and |
| 42 // |database|. |
| 40 ImageManager(scoped_ptr<ImageFetcher> image_fetcher, | 43 ImageManager(scoped_ptr<ImageFetcher> image_fetcher, |
| 44 scoped_ptr<ImageEncoder> image_encoder, |
| 41 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database, | 45 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database, |
| 42 const base::FilePath& database_dir); | 46 const base::FilePath& database_dir); |
| 43 virtual ~ImageManager(); | 47 virtual ~ImageManager(); |
| 44 | 48 |
| 45 virtual void Initialize(const SuggestionsProfile& suggestions); | 49 virtual void Initialize(const SuggestionsProfile& suggestions); |
| 46 | 50 |
| 47 // Should be called from the UI thread. | 51 // Should be called from the UI thread. |
| 48 virtual void GetImageForURL( | 52 virtual void GetImageForURL( |
| 49 const GURL& url, | 53 const GURL& url, |
| 50 base::Callback<void(const GURL&, const SkBitmap*)> callback); | 54 base::Callback<void(const GURL&, const SkBitmap*)> callback); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 void OnDatabaseInit(bool success); | 116 void OnDatabaseInit(bool success); |
| 113 // Will transfer the loaded |entries| in memory (|image_map_|). | 117 // Will transfer the loaded |entries| in memory (|image_map_|). |
| 114 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); | 118 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); |
| 115 void OnDatabaseSave(bool success); | 119 void OnDatabaseSave(bool success); |
| 116 | 120 |
| 117 // Take entries from the database and put them in the local cache. | 121 // Take entries from the database and put them in the local cache. |
| 118 void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries); | 122 void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries); |
| 119 | 123 |
| 120 void ServePendingCacheRequests(); | 124 void ServePendingCacheRequests(); |
| 121 | 125 |
| 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 | 126 // Map from URL to image URL. Should be kept up to date when a new |
| 128 // SuggestionsProfile is available. | 127 // SuggestionsProfile is available. |
| 129 std::map<GURL, GURL> image_url_map_; | 128 std::map<GURL, GURL> image_url_map_; |
| 130 | 129 |
| 131 // Map from website URL to request information, used for pending cache | 130 // Map from website URL to request information, used for pending cache |
| 132 // requests while the database hasn't loaded. | 131 // requests while the database hasn't loaded. |
| 133 ImageCacheRequestMap pending_cache_requests_; | 132 ImageCacheRequestMap pending_cache_requests_; |
| 134 | 133 |
| 135 // Holding the bitmaps in memory, keyed by website URL string. | 134 // Holding the bitmaps in memory, keyed by website URL string. |
| 136 ImageMap image_map_; | 135 ImageMap image_map_; |
| 137 | 136 |
| 138 scoped_ptr<ImageFetcher> image_fetcher_; | 137 scoped_ptr<ImageFetcher> image_fetcher_; |
| 139 | 138 |
| 139 scoped_ptr<ImageEncoder> image_encoder_; |
| 140 |
| 140 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_; | 141 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_; |
| 141 | 142 |
| 142 bool database_ready_; | 143 bool database_ready_; |
| 143 | 144 |
| 144 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; | 145 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; |
| 145 | 146 |
| 146 base::ThreadChecker thread_checker_; | 147 base::ThreadChecker thread_checker_; |
| 147 | 148 |
| 148 DISALLOW_COPY_AND_ASSIGN(ImageManager); | 149 DISALLOW_COPY_AND_ASSIGN(ImageManager); |
| 149 }; | 150 }; |
| 150 | 151 |
| 151 } // namespace suggestions | 152 } // namespace suggestions |
| 152 | 153 |
| 153 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ | 154 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ |
| OLD | NEW |