OLD | NEW |
---|---|
(Empty) | |
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.h" | |
15 #include "ui/gfx/image/image_skia.h" | |
16 #include "url/gurl.h" | |
17 | |
18 class Profile; | |
19 | |
20 namespace suggestions { | |
21 | |
22 typedef base::Callback<void(const GURL&, const SkBitmap*)> | |
23 BitmapResponseCallback; | |
24 | |
25 class SuggestionsProfile; | |
26 | |
27 // A class used to fetch server thumbnails asynchronously. | |
28 class ThumbnailManager : public chrome::BitmapFetcherDelegate { | |
29 public: | |
30 explicit ThumbnailManager(Profile* profile); | |
31 virtual ~ThumbnailManager(); | |
32 | |
33 // Initializes the |thumbnail_map_| with the proper mapping from website URL | |
34 // to thumbnail URL. | |
35 void InitializeThumbnailMap(const SuggestionsProfile& suggestions); | |
36 | |
37 // Retrieves stored thumbnail for website |url| asynchronously. Calls | |
38 // |callback| with Bitmap pointer if found, and NULL otherwise. Should be | |
39 // called from the UI thread. | |
40 void GetPageThumbnail( | |
41 const GURL& url, | |
42 base::Callback<void(const GURL&, const SkBitmap*)> callback); | |
Jered
2014/05/23 21:25:00
This doesn't use the typedef above?
huangs
2014/05/23 22:34:26
We decided to not use
typedef BitmapResponseCallb
Mathieu
2014/05/23 22:40:25
Oops I meant to remove the typedef. I want to avoi
| |
43 | |
44 | |
Jered
2014/05/23 21:25:00
nit: omit this extra blank line
Mathieu
2014/05/23 22:40:25
Done.
| |
45 private: | |
46 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerTest, InitializeThumbnailMapTest); | |
47 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, FetchThumbnails); | |
48 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, FetchThumbnailsInvalid); | |
49 FRIEND_TEST_ALL_PREFIXES(ThumbnailManagerBrowserTest, | |
50 FetchThumbnailsMultiple); | |
51 | |
52 typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > | |
53 CallbackVector; | |
54 | |
55 // State related to a thumbnail fetch (associated website url, fetcher, | |
56 // pending callbacks). | |
57 struct ThumbnailRequest { | |
58 ThumbnailRequest(); | |
59 ~ThumbnailRequest(); | |
60 | |
61 GURL url; | |
62 chrome::BitmapFetcher* fetcher; | |
63 // Queue for pending callbacks, which may accumulate while the request is in | |
64 // flight. | |
65 CallbackVector callbacks; | |
66 }; | |
67 | |
68 typedef std::map<const GURL, ThumbnailRequest> ThumbnailRequestMap; | |
69 | |
70 // Inherited from BitmapFetcherDelegate. Runs on the UI thread. | |
71 virtual void OnFetchComplete(const GURL thumbnail_url, | |
72 const SkBitmap* bitmap) OVERRIDE; | |
73 | |
74 // Looks up thumbnail for |url|. If found, writes the result to | |
75 // |thumbnail_url| and returns true. Otherwise just returns false. | |
76 bool GetThumbnailURL(const GURL& url, GURL* thumbnail_url); | |
77 | |
78 // Used for substituting the request context during testing. | |
79 void set_request_context(net::URLRequestContextGetter* context) { | |
80 url_request_context_ = context; | |
81 } | |
82 | |
83 // Map from URL to thumbnail URL. Should be kept up to date when a new | |
84 // SuggestionsProfile is available. | |
85 std::map<GURL, GURL> thumbnail_map_; | |
86 | |
87 // Map from each thumbnail URL to the request information (associated website | |
88 // url, fetcher, pending callbacks). | |
89 ThumbnailRequestMap pending_requests_; | |
90 | |
91 net::URLRequestContextGetter* url_request_context_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ThumbnailManager); | |
94 }; | |
95 | |
96 } // namespace suggestions | |
97 | |
98 #endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_THUMBNAIL_MANAGER_H_ | |
OLD | NEW |