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 #include "chrome/browser/search/suggestions/thumbnail_manager.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "chrome/browser/profiles/profile.h" | |
9 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | |
10 #include "net/base/load_flags.h" | |
11 | |
12 namespace suggestions { | |
13 | |
14 ThumbnailManager::ThumbnailManager(Profile* profile) | |
15 : profile_(profile), url_request_context_(profile->GetRequestContext()) {} | |
16 | |
17 ThumbnailManager::~ThumbnailManager() {} | |
18 | |
19 void ThumbnailManager::InitializeThumbnailMap( | |
20 const SuggestionsProfile& suggestions) { | |
21 thumbnail_map_.clear(); | |
22 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | |
23 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | |
24 if (suggestion.has_thumbnail()) { | |
huangs
2014/05/22 06:17:35
What if there are multiple suggestions with the sa
Mathieu
2014/05/22 15:45:51
I feel like the server should worry about this. In
| |
25 thumbnail_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); | |
26 } | |
27 } | |
28 } | |
29 | |
30 void ThumbnailManager::GetURLThumbnail(const GURL& url, | |
31 BitmapResponseCallback callback) { | |
32 // If the |url| is not in the |thumbnail_map_|, there is no associated | |
33 // thumbnail for it. Will callback with an empty bitmap. | |
34 GURL thumbnail_url; | |
35 if (!GetThumbnailURL(url, &thumbnail_url)) { | |
36 callback.Run(url, NULL); | |
37 return; | |
38 } | |
39 | |
40 // If a fetch of the |thumbnail_url| is already in progress, register as an | |
41 // interested callback. Will allocate a new queue if not present. | |
42 pending_callbacks_[thumbnail_url].push(callback); | |
43 | |
44 // If we are the first ones requesting this |thumbnail_url|, start the fetch. | |
45 if (pending_callbacks_[thumbnail_url].size() == 1) { | |
huangs
2014/05/22 06:17:35
I'd prefer using queue<>::empty() instead of check
Mathieu
2014/05/22 15:45:51
Reworked this, let me know
| |
46 chrome::BitmapFetcher* fetcher = | |
47 new chrome::BitmapFetcher(thumbnail_url, this); | |
48 fetcher_map_[thumbnail_url] = std::make_pair(url, fetcher); | |
49 fetcher->Start( | |
50 url_request_context_, std::string(), | |
huangs
2014/05/22 06:17:35
NIT: 1 parameter per line.
Mathieu
2014/05/22 15:45:51
clang-format would disagree!
| |
51 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | |
52 net::LOAD_NORMAL); | |
53 } | |
54 } | |
55 | |
56 void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url, | |
57 const SkBitmap* bitmap) { | |
58 // Make sure the BitmapFetcher gets destroyed. | |
59 scoped_ptr<chrome::BitmapFetcher> fetcher_destructor( | |
huangs
2014/05/22 06:17:35
You only need to do one lookup if you store the it
Mathieu
2014/05/22 15:45:51
Done. Have a look!
| |
60 fetcher_map_[thumbnail_url].second); | |
61 GURL url(fetcher_map_[thumbnail_url].first); | |
62 fetcher_map_.erase(fetcher_map_.find(thumbnail_url)); | |
63 | |
64 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the | |
65 // BitmapFetcher and which ceases to exist after this function. Pass the | |
66 // un-owned pointer to the registered callbacks. | |
67 std::queue<BitmapResponseCallback> callbacks = | |
huangs
2014/05/22 06:17:35
I think it's cleaner to just swap data data:
std:
Mathieu
2014/05/22 15:45:51
Done.
| |
68 pending_callbacks_[thumbnail_url]; | |
69 while (!callbacks.empty()) { | |
70 callbacks.front().Run(url, bitmap); | |
71 callbacks.pop(); | |
72 } | |
73 pending_callbacks_.erase(pending_callbacks_.find(thumbnail_url)); | |
huangs
2014/05/22 06:17:35
You can just direct erase with the key:
pending_c
Mathieu
2014/05/22 15:45:51
Have a look at how it is now?
| |
74 } | |
75 | |
76 bool ThumbnailManager::GetThumbnailURL(const GURL& url, | |
77 GURL* thumbnail_url) { | |
78 std::map<GURL, GURL>::iterator it = thumbnail_map_.find(url); | |
79 if (it == thumbnail_map_.end()) { | |
80 // Not found. | |
81 return false; | |
82 } | |
83 *thumbnail_url = it->second; | |
84 return true; | |
85 } | |
86 | |
87 } // namespace suggestions | |
OLD | NEW |