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. 4 5 #ifndef CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ 6 #define CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ 7 8 #include <map> 9 #include <utility> 10 #include <vector> 11 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" 16 #include "url/gurl.h" 17 18 class Profile; 19 20 namespace suggestions { 21 22 class SuggestionsProfile; 23 24 // A class used to fetch server thumbnails asynchronously. 25 class ThumbnailManager : public chrome::BitmapFetcherDelegate { 26 public: 27 explicit ThumbnailManager(Profile* profile); 28 virtual ~ThumbnailManager(); 29 30 // Initializes the |thumbnail_map_| with the proper mapping from website URL 31 // to thumbnail URL. 32 void InitializeThumbnailMap(const SuggestionsProfile& suggestions); 33 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( 38 const GURL& url, 39 base::Callback<void(const GURL&, const SkBitmap*)> callback); 40 41 private: 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); 47 48 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > 49 CallbackVector; 50 51 // State related to a thumbnail fetch (associated website url, fetcher, 52 // pending callbacks). 53 struct ThumbnailRequest { 54 ThumbnailRequest(); 55 explicit ThumbnailRequest(chrome::BitmapFetcher* f); 56 ~ThumbnailRequest(); 57 58 void swap(ThumbnailRequest* other) { 59 std::swap(url, other->url); 60 std::swap(callbacks, other->callbacks); 61 std::swap(fetcher, other->fetcher); 62 } 63 64 GURL url; 65 chrome::BitmapFetcher* fetcher; 66 // Queue for pending callbacks, which may accumulate while the request is in 67 // flight. 68 CallbackVector callbacks; 69 }; 70 71 typedef std::map<const GURL, ThumbnailRequest> ThumbnailRequestMap; 72 73 // Inherited from BitmapFetcherDelegate. Runs on the UI thread. 74 virtual void OnFetchComplete(const GURL thumbnail_url, 75 const SkBitmap* bitmap) OVERRIDE; 76 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); 80 81 // Used for substituting the request context during testing. 82 void set_request_context(net::URLRequestContextGetter* context) { 83 url_request_context_ = context; 84 } 85 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_; 89 90 // Map from each thumbnail URL to the request information (associated website 91 // url, fetcher, pending callbacks). 92 ThumbnailRequestMap pending_requests_; 93 94 net::URLRequestContextGetter* url_request_context_; 95 96 DISALLOW_COPY_AND_ASSIGN(ThumbnailManager); 97 }; 98 99 } // namespace suggestions 100 101 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ 102