Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(617)

Side by Side Diff: components/suggestions/image_manager.h

Issue 1156003008: Decode ImageManager cached images on demand. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Don't use MessageLoop API Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/suggestions/image_encoder_ios.mm ('k') | components/suggestions/image_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
18 #include "base/task_runner.h"
17 #include "base/threading/thread_checker.h" 19 #include "base/threading/thread_checker.h"
18 #include "components/leveldb_proto/proto_database.h" 20 #include "components/leveldb_proto/proto_database.h"
19 #include "components/suggestions/image_fetcher_delegate.h" 21 #include "components/suggestions/image_fetcher_delegate.h"
20 #include "components/suggestions/proto/suggestions.pb.h" 22 #include "components/suggestions/proto/suggestions.pb.h"
21 #include "ui/gfx/image/image_skia.h" 23 #include "ui/gfx/image/image_skia.h"
22 #include "url/gurl.h" 24 #include "url/gurl.h"
23 25
24 namespace net { 26 namespace net {
25 class URLRequestContextGetter; 27 class URLRequestContextGetter;
26 } 28 }
27 29
28 namespace suggestions { 30 namespace suggestions {
29 31
30 class ImageData; 32 class ImageData;
31 class ImageFetcher; 33 class ImageFetcher;
32 class SuggestionsProfile; 34 class SuggestionsProfile;
33 35
34 // A class used to fetch server images asynchronously and manage the caching 36 // A class used to fetch server images asynchronously and manage the caching
35 // layer (both in memory and on disk). 37 // layer (both in memory and on disk).
36 class ImageManager : public ImageFetcherDelegate { 38 class ImageManager : public ImageFetcherDelegate {
37 public: 39 public:
38 typedef std::vector<ImageData> ImageDataVector; 40 typedef std::vector<ImageData> ImageDataVector;
39 41
40 ImageManager(scoped_ptr<ImageFetcher> image_fetcher, 42 ImageManager(scoped_ptr<ImageFetcher> image_fetcher,
41 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database, 43 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData>> database,
42 const base::FilePath& database_dir); 44 const base::FilePath& database_dir,
45 scoped_refptr<base::TaskRunner> background_task_runner);
43 ~ImageManager() override; 46 ~ImageManager() override;
44 47
45 virtual void Initialize(const SuggestionsProfile& suggestions); 48 virtual void Initialize(const SuggestionsProfile& suggestions);
46 49
47 // Should be called from the UI thread. 50 // Should be called from the UI thread.
48 virtual void GetImageForURL( 51 virtual void GetImageForURL(
49 const GURL& url, 52 const GURL& url,
50 base::Callback<void(const GURL&, const SkBitmap*)> callback); 53 base::Callback<void(const GURL&, const SkBitmap*)> callback);
51 54
52 protected: 55 protected:
53 // Perform additional tasks when an image has been fetched. 56 // Perform additional tasks when an image has been fetched.
54 void OnImageFetched(const GURL& url, const SkBitmap* bitmap) override; 57 void OnImageFetched(const GURL& url, const SkBitmap* bitmap) override;
55 58
56 private: 59 private:
57 friend class MockImageManager; 60 friend class MockImageManager;
58 friend class ImageManagerTest; 61 friend class ImageManagerTest;
59 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); 62 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest);
60 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); 63 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit);
61 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, 64 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest,
62 GetImageForURLNetworkCacheNotInitialized); 65 GetImageForURLNetworkCacheNotInitialized);
63 66
64 // Used for testing. 67 // Used for testing.
65 ImageManager(); 68 ImageManager();
66 69
67 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > 70 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> >
68 CallbackVector; 71 CallbackVector;
69 typedef base::hash_map<std::string, SkBitmap> ImageMap; 72 typedef base::hash_map<std::string, scoped_refptr<base::RefCountedMemory>>
73 ImageMap;
70 74
71 // State related to an image fetch (associated website url, image_url, 75 // State related to an image fetch (associated website url, image_url,
72 // pending callbacks). 76 // pending callbacks).
73 struct ImageCacheRequest { 77 struct ImageCacheRequest {
74 ImageCacheRequest(); 78 ImageCacheRequest();
75 ~ImageCacheRequest(); 79 ~ImageCacheRequest();
76 80
77 GURL url; 81 GURL url;
78 GURL image_url; 82 GURL image_url;
79 // Queue for pending callbacks, which may accumulate while the request is in 83 // Queue for pending callbacks, which may accumulate while the request is in
80 // flight. 84 // flight.
81 CallbackVector callbacks; 85 CallbackVector callbacks;
82 }; 86 };
83 87
84 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; 88 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap;
85 89
86 // Looks up image URL for |url|. If found, writes the result to |image_url| 90 // Looks up image URL for |url|. If found, writes the result to |image_url|
87 // and returns true. Otherwise just returns false. 91 // and returns true. Otherwise just returns false.
88 bool GetImageURL(const GURL& url, GURL* image_url); 92 bool GetImageURL(const GURL& url, GURL* image_url);
89 93
90 void QueueCacheRequest( 94 void QueueCacheRequest(
91 const GURL& url, const GURL& image_url, 95 const GURL& url, const GURL& image_url,
92 base::Callback<void(const GURL&, const SkBitmap*)> callback); 96 base::Callback<void(const GURL&, const SkBitmap*)> callback);
93 97
94 void ServeFromCacheOrNetwork( 98 void ServeFromCacheOrNetwork(
95 const GURL& url, const GURL& image_url, 99 const GURL& url, const GURL& image_url,
96 base::Callback<void(const GURL&, const SkBitmap*)> callback); 100 base::Callback<void(const GURL&, const SkBitmap*)> callback);
97 101
98 // Will return false if no bitmap was found corresponding to |url|, else 102 void OnCacheImageDecoded(
99 // return true and call |callback| with the found bitmap.
100 bool ServeFromCache(
101 const GURL& url, 103 const GURL& url,
102 base::Callback<void(const GURL&, const SkBitmap*)> callback); 104 const GURL& image_url,
105 base::Callback<void(const GURL&, const SkBitmap*)> callback,
106 scoped_ptr<SkBitmap> bitmap);
103 107
104 // Returns null if the |url| had no entry in the cache. 108 // Returns null if the |url| had no entry in the cache.
105 SkBitmap* GetBitmapFromCache(const GURL& url); 109 scoped_refptr<base::RefCountedMemory> GetEncodedImageFromCache(
110 const GURL& url);
106 111
107 // Save the image bitmap in the cache and in the database. 112 // Save the image bitmap in the cache and in the database.
108 void SaveImage(const GURL& url, const SkBitmap& bitmap); 113 void SaveImage(const GURL& url, const SkBitmap& bitmap);
109 114
110 // Database callback methods. 115 // Database callback methods.
111 // Will initiate loading the entries. 116 // Will initiate loading the entries.
112 void OnDatabaseInit(bool success); 117 void OnDatabaseInit(bool success);
113 // Will transfer the loaded |entries| in memory (|image_map_|). 118 // Will transfer the loaded |entries| in memory (|image_map_|).
114 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); 119 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries);
115 void OnDatabaseSave(bool success); 120 void OnDatabaseSave(bool success);
(...skipping 11 matching lines...) Expand all
127 // requests while the database hasn't loaded. 132 // requests while the database hasn't loaded.
128 ImageCacheRequestMap pending_cache_requests_; 133 ImageCacheRequestMap pending_cache_requests_;
129 134
130 // Holding the bitmaps in memory, keyed by website URL string. 135 // Holding the bitmaps in memory, keyed by website URL string.
131 ImageMap image_map_; 136 ImageMap image_map_;
132 137
133 scoped_ptr<ImageFetcher> image_fetcher_; 138 scoped_ptr<ImageFetcher> image_fetcher_;
134 139
135 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_; 140 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_;
136 141
142 scoped_refptr<base::TaskRunner> background_task_runner_;
143
137 bool database_ready_; 144 bool database_ready_;
138 145
139 base::ThreadChecker thread_checker_; 146 base::ThreadChecker thread_checker_;
140 147
141 base::WeakPtrFactory<ImageManager> weak_ptr_factory_; 148 base::WeakPtrFactory<ImageManager> weak_ptr_factory_;
142 149
143 DISALLOW_COPY_AND_ASSIGN(ImageManager); 150 DISALLOW_COPY_AND_ASSIGN(ImageManager);
144 }; 151 };
145 152
146 } // namespace suggestions 153 } // namespace suggestions
147 154
148 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ 155 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_
OLDNEW
« no previous file with comments | « components/suggestions/image_encoder_ios.mm ('k') | components/suggestions/image_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698