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

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

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

Powered by Google App Engine
This is Rietveld 408576698