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 CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_
6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
15 #include "ui/gfx/image/image_skia.h"
20 namespace suggestions {
22 class SuggestionsProfile;
24 // A class used to fetch server thumbnails asynchronously.
25 class ThumbnailManager : public chrome::BitmapFetcherDelegate {
27 explicit ThumbnailManager(Profile* profile);
28 virtual ~ThumbnailManager();
30 // Initializes the |thumbnail_map_| with the proper mapping from website URL
32 void InitializeThumbnailMap(const SuggestionsProfile& suggestions);
34 // Retrieves stored thumbnail for website |url| asynchronously. Calls
35 // |callback| with Bitmap pointer if found, and NULL otherwise. Should be
36 // called from the UI thread.
37 void GetPageThumbnail(
39 base::Callback<void(const GURL&, const SkBitmap*)> callback);
42 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerTest, InitializeThumbnailMapTest);
43 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, FetchThumbnails);
44 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, FetchThumbnailsInvalid);
45 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest,
46 FetchThumbnailsMultiple);
48 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> >
51 // State related to a thumbnail fetch (associated website url, fetcher,
52 // pending callbacks).
53 struct ThumbnailRequest {
55 explicit ThumbnailRequest(chrome::BitmapFetcher* f);
58 void swap(ThumbnailRequest* other) {
59 std::swap(url, other->url);
60 std::swap(callbacks, other->callbacks);
61 std::swap(fetcher, other->fetcher);
65 chrome::BitmapFetcher* fetcher;
66 // Queue for pending callbacks, which may accumulate while the request is in
68 CallbackVector callbacks;
71 typedef std::map<const GURL, ThumbnailRequest> ThumbnailRequestMap;
73 // Inherited from BitmapFetcherDelegate. Runs on the UI thread.
74 virtual void OnFetchComplete(const GURL thumbnail_url,
75 const SkBitmap* bitmap) OVERRIDE;
77 // Looks up thumbnail for |url|. If found, writes the result to
78 // |thumbnail_url| and returns true. Otherwise just returns false.
79 bool GetThumbnailURL(const GURL& url, GURL* thumbnail_url);
81 // Used for substituting the request context during testing.
82 void set_request_context(net::URLRequestContextGetter* context) {
83 url_request_context_ = context;
86 // Map from URL to thumbnail URL. Should be kept up to date when a new
87 // SuggestionsProfile is available.
88 std::map<GURL, GURL> thumbnail_map_;
90 // Map from each thumbnail URL to the request information (associated website
91 // url, fetcher, pending callbacks).
92 ThumbnailRequestMap pending_requests_;
94 net::URLRequestContextGetter* url_request_context_;
96 DISALLOW_COPY_AND_ASSIGN(ThumbnailManager);
99 } // namespace suggestions
101 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_