Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / suggestions / image_manager.h
blob851d1cdd4ad6d2af01b7f0c7022be7a0f28fec62
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/ref_counted_memory.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/task_runner.h"
19 #include "base/threading/thread_checker.h"
20 #include "components/leveldb_proto/proto_database.h"
21 #include "components/suggestions/image_fetcher_delegate.h"
22 #include "components/suggestions/proto/suggestions.pb.h"
23 #include "ui/gfx/image/image_skia.h"
24 #include "url/gurl.h"
26 namespace net {
27 class URLRequestContextGetter;
30 namespace suggestions {
32 class ImageData;
33 class ImageFetcher;
34 class SuggestionsProfile;
36 // A class used to fetch server images asynchronously and manage the caching
37 // layer (both in memory and on disk).
38 class ImageManager : public ImageFetcherDelegate {
39 public:
40 typedef std::vector<ImageData> ImageDataVector;
42 ImageManager(scoped_ptr<ImageFetcher> image_fetcher,
43 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData>> database,
44 const base::FilePath& database_dir,
45 scoped_refptr<base::TaskRunner> background_task_runner);
46 ~ImageManager() override;
48 virtual void Initialize(const SuggestionsProfile& suggestions);
50 // Should be called from the UI thread.
51 virtual void GetImageForURL(
52 const GURL& url,
53 base::Callback<void(const GURL&, const SkBitmap*)> callback);
55 protected:
56 // Perform additional tasks when an image has been fetched.
57 void OnImageFetched(const GURL& url, const SkBitmap* bitmap) override;
59 private:
60 friend class MockImageManager;
61 friend class ImageManagerTest;
62 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest);
63 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit);
64 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest,
65 GetImageForURLNetworkCacheNotInitialized);
67 // Used for testing.
68 ImageManager();
70 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> >
71 CallbackVector;
72 typedef base::hash_map<std::string, scoped_refptr<base::RefCountedMemory>>
73 ImageMap;
75 // State related to an image fetch (associated website url, image_url,
76 // pending callbacks).
77 struct ImageCacheRequest {
78 ImageCacheRequest();
79 ~ImageCacheRequest();
81 GURL url;
82 GURL image_url;
83 // Queue for pending callbacks, which may accumulate while the request is in
84 // flight.
85 CallbackVector callbacks;
88 typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap;
90 // Looks up image URL for |url|. If found, writes the result to |image_url|
91 // and returns true. Otherwise just returns false.
92 bool GetImageURL(const GURL& url, GURL* image_url);
94 void QueueCacheRequest(
95 const GURL& url, const GURL& image_url,
96 base::Callback<void(const GURL&, const SkBitmap*)> callback);
98 void ServeFromCacheOrNetwork(
99 const GURL& url, const GURL& image_url,
100 base::Callback<void(const GURL&, const SkBitmap*)> callback);
102 void OnCacheImageDecoded(
103 const GURL& url,
104 const GURL& image_url,
105 base::Callback<void(const GURL&, const SkBitmap*)> callback,
106 scoped_ptr<SkBitmap> bitmap);
108 // Returns null if the |url| had no entry in the cache.
109 scoped_refptr<base::RefCountedMemory> GetEncodedImageFromCache(
110 const GURL& url);
112 // Save the image bitmap in the cache and in the database.
113 void SaveImage(const GURL& url, const SkBitmap& bitmap);
115 // Database callback methods.
116 // Will initiate loading the entries.
117 void OnDatabaseInit(bool success);
118 // Will transfer the loaded |entries| in memory (|image_map_|).
119 void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries);
120 void OnDatabaseSave(bool success);
122 // Take entries from the database and put them in the local cache.
123 void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries);
125 void ServePendingCacheRequests();
127 // Map from URL to image URL. Should be kept up to date when a new
128 // SuggestionsProfile is available.
129 std::map<GURL, GURL> image_url_map_;
131 // Map from website URL to request information, used for pending cache
132 // requests while the database hasn't loaded.
133 ImageCacheRequestMap pending_cache_requests_;
135 // Holding the bitmaps in memory, keyed by website URL string.
136 ImageMap image_map_;
138 scoped_ptr<ImageFetcher> image_fetcher_;
140 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_;
142 scoped_refptr<base::TaskRunner> background_task_runner_;
144 bool database_ready_;
146 base::ThreadChecker thread_checker_;
148 base::WeakPtrFactory<ImageManager> weak_ptr_factory_;
150 DISALLOW_COPY_AND_ASSIGN(ImageManager);
153 } // namespace suggestions
155 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_