Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / suggestions / image_manager.h
blob99ae55e1a34c54a43751a4c6a27f01dff1093417
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_
6 #define COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.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"
20 #include "components/suggestions/proto/suggestions.pb.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "url/gurl.h"
24 namespace net {
25 class URLRequestContextGetter;
28 namespace suggestions {
30 class ImageData;
31 class ImageFetcher;
32 class SuggestionsProfile;
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 {
37 public:
38 typedef std::vector<ImageData> ImageDataVector;
40 ImageManager(scoped_ptr<ImageFetcher> image_fetcher,
41 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database,
42 const base::FilePath& database_dir);
43 ~ImageManager() override;
45 virtual void Initialize(const SuggestionsProfile& suggestions);
47 // Should be called from the UI thread.
48 virtual void GetImageForURL(
49 const GURL& url,
50 base::Callback<void(const GURL&, const SkBitmap*)> callback);
52 protected:
53 // Perform additional tasks when an image has been fetched.
54 void OnImageFetched(const GURL& url, const SkBitmap* bitmap) override;
56 private:
57 friend class MockImageManager;
58 friend class ImageManagerTest;
59 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest);
60 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit);
61 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest,
62 GetImageForURLNetworkCacheNotInitialized);
64 // Used for testing.
65 ImageManager();
67 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> >
68 CallbackVector;
69 typedef base::hash_map<std::string, SkBitmap> ImageMap;
71 // State related to an image fetch (associated website url, image_url,
72 // pending callbacks).
73 struct ImageCacheRequest {
74 ImageCacheRequest();
75 ~ImageCacheRequest();
77 GURL url;
78 GURL image_url;
79 // Queue for pending callbacks, which may accumulate while the request is in
80 // flight.
81 CallbackVector callbacks;
84 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap;
86 // Looks up image URL for |url|. If found, writes the result to |image_url|
87 // and returns true. Otherwise just returns false.
88 bool GetImageURL(const GURL& url, GURL* image_url);
90 void QueueCacheRequest(
91 const GURL& url, const GURL& image_url,
92 base::Callback<void(const GURL&, const SkBitmap*)> callback);
94 void ServeFromCacheOrNetwork(
95 const GURL& url, const GURL& image_url,
96 base::Callback<void(const GURL&, const SkBitmap*)> callback);
98 // Will return false if no bitmap was found corresponding to |url|, else
99 // return true and call |callback| with the found bitmap.
100 bool ServeFromCache(
101 const GURL& url,
102 base::Callback<void(const GURL&, const SkBitmap*)> callback);
104 // Returns null if the |url| had no entry in the cache.
105 SkBitmap* GetBitmapFromCache(const GURL& url);
107 // Save the image bitmap in the cache and in the database.
108 void SaveImage(const GURL& url, const SkBitmap& bitmap);
110 // Database callback methods.
111 // Will initiate loading the entries.
112 void OnDatabaseInit(bool success);
113 // Will transfer the loaded |entries| in memory (|image_map_|).
114 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries);
115 void OnDatabaseSave(bool success);
117 // Take entries from the database and put them in the local cache.
118 void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries);
120 void ServePendingCacheRequests();
122 // Map from URL to image URL. Should be kept up to date when a new
123 // SuggestionsProfile is available.
124 std::map<GURL, GURL> image_url_map_;
126 // Map from website URL to request information, used for pending cache
127 // requests while the database hasn't loaded.
128 ImageCacheRequestMap pending_cache_requests_;
130 // Holding the bitmaps in memory, keyed by website URL string.
131 ImageMap image_map_;
133 scoped_ptr<ImageFetcher> image_fetcher_;
135 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_;
137 bool database_ready_;
139 base::ThreadChecker thread_checker_;
141 base::WeakPtrFactory<ImageManager> weak_ptr_factory_;
143 DISALLOW_COPY_AND_ASSIGN(ImageManager);
146 } // namespace suggestions
148 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_