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_
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"
27 class URLRequestContextGetter
;
30 namespace suggestions
{
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
{
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 AddImageURL(const GURL
& url
, const GURL
& image_url
);
53 // Should be called from the UI thread.
54 virtual void GetImageForURL(
56 base::Callback
<void(const GURL
&, const SkBitmap
*)> callback
);
59 // Perform additional tasks when an image has been fetched.
60 void OnImageFetched(const GURL
& url
, const SkBitmap
* bitmap
) override
;
63 friend class MockImageManager
;
64 friend class ImageManagerTest
;
65 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest
, InitializeTest
);
66 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest
, AddImageURL
);
67 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest
, GetImageForURLNetworkCacheHit
);
68 FRIEND_TEST_ALL_PREFIXES(ImageManagerTest
,
69 GetImageForURLNetworkCacheNotInitialized
);
74 typedef std::vector
<base::Callback
<void(const GURL
&, const SkBitmap
*)> >
76 typedef base::hash_map
<std::string
, scoped_refptr
<base::RefCountedMemory
>>
79 // State related to an image fetch (associated website url, image_url,
80 // pending callbacks).
81 struct ImageCacheRequest
{
87 // Queue for pending callbacks, which may accumulate while the request is in
89 CallbackVector callbacks
;
92 typedef std::map
<const GURL
, ImageCacheRequest
> ImageCacheRequestMap
;
94 // Looks up image URL for |url|. If found, writes the result to |image_url|
95 // and returns true. Otherwise just returns false.
96 bool GetImageURL(const GURL
& url
, GURL
* image_url
);
98 void QueueCacheRequest(
99 const GURL
& url
, const GURL
& image_url
,
100 base::Callback
<void(const GURL
&, const SkBitmap
*)> callback
);
102 void ServeFromCacheOrNetwork(
103 const GURL
& url
, const GURL
& image_url
,
104 base::Callback
<void(const GURL
&, const SkBitmap
*)> callback
);
106 void OnCacheImageDecoded(
108 const GURL
& image_url
,
109 base::Callback
<void(const GURL
&, const SkBitmap
*)> callback
,
110 scoped_ptr
<SkBitmap
> bitmap
);
112 // Returns null if the |url| had no entry in the cache.
113 scoped_refptr
<base::RefCountedMemory
> GetEncodedImageFromCache(
116 // Save the image bitmap in the cache and in the database.
117 void SaveImage(const GURL
& url
, const SkBitmap
& bitmap
);
119 // Database callback methods.
120 // Will initiate loading the entries.
121 void OnDatabaseInit(bool success
);
122 // Will transfer the loaded |entries| in memory (|image_map_|).
123 void OnDatabaseLoad(bool success
, scoped_ptr
<ImageDataVector
> entries
);
124 void OnDatabaseSave(bool success
);
126 // Take entries from the database and put them in the local cache.
127 void LoadEntriesInCache(scoped_ptr
<ImageDataVector
> entries
);
129 void ServePendingCacheRequests();
131 // Map from URL to image URL. Should be kept up to date when a new
132 // SuggestionsProfile is available.
133 std::map
<GURL
, GURL
> image_url_map_
;
135 // Map from website URL to request information, used for pending cache
136 // requests while the database hasn't loaded.
137 ImageCacheRequestMap pending_cache_requests_
;
139 // Holding the bitmaps in memory, keyed by website URL string.
142 scoped_ptr
<ImageFetcher
> image_fetcher_
;
144 scoped_ptr
<leveldb_proto::ProtoDatabase
<ImageData
> > database_
;
146 scoped_refptr
<base::TaskRunner
> background_task_runner_
;
148 bool database_ready_
;
150 base::ThreadChecker thread_checker_
;
152 base::WeakPtrFactory
<ImageManager
> weak_ptr_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(ImageManager
);
157 } // namespace suggestions
159 #endif // COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_