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 "content/public/browser/browser_thread.h" |
| 11 #include "net/base/load_flags.h" |
| 12 |
| 13 namespace suggestions { |
| 14 |
| 15 ThumbnailManager::ThumbnailManager(Profile* profile) |
| 16 : url_request_context_(profile->GetRequestContext()) {} |
| 17 |
| 18 ThumbnailManager::~ThumbnailManager() {} |
| 19 |
| 20 void ThumbnailManager::InitializeThumbnailMap( |
| 21 const SuggestionsProfile& suggestions) { |
| 22 thumbnail_map_.clear(); |
| 23 for (int i = 0; i < suggestions.suggestions_size(); ++i) { |
| 24 const ChromeSuggestion& suggestion = suggestions.suggestions(i); |
| 25 if (suggestion.has_thumbnail()) { |
| 26 thumbnail_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); |
| 27 } |
| 28 } |
| 29 } |
| 30 |
| 31 void ThumbnailManager::GetPageThumbnail( |
| 32 const GURL& url, |
| 33 base::Callback<void(const GURL&, const SkBitmap*)> callback) { |
| 34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 35 // If |url| is not found in |thumbnail_map_|, then invoke |callback| with NULL |
| 36 // since there is no associated thumbnail. |
| 37 GURL thumbnail_url; |
| 38 if (!GetThumbnailURL(url, &thumbnail_url)) { |
| 39 callback.Run(url, NULL); |
| 40 return; |
| 41 } |
| 42 |
| 43 // Look for a request in progress for |thumbnail_url|. |
| 44 ThumbnailRequestMap::iterator it = pending_requests_.find(thumbnail_url); |
| 45 if (it == pending_requests_.end()) { |
| 46 // |thumbnail_url| is not being fetched, so create a request and initiate |
| 47 // the fetch. |
| 48 ThumbnailRequest* request = &pending_requests_[thumbnail_url]; |
| 49 request->url = url; |
| 50 request->callbacks.push_back(callback); |
| 51 // This is deallocated in OnFetchComplete(). |
| 52 request->fetcher = new chrome::BitmapFetcher(thumbnail_url, this); |
| 53 request->fetcher->Start( |
| 54 url_request_context_, std::string(), |
| 55 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 56 net::LOAD_NORMAL); |
| 57 } else { |
| 58 // Request in progress. Register as an interested callback. |
| 59 it->second.callbacks.push_back(callback); |
| 60 } |
| 61 } |
| 62 |
| 63 void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url, |
| 64 const SkBitmap* bitmap) { |
| 65 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 66 |
| 67 ThumbnailRequestMap::iterator it = pending_requests_.find(thumbnail_url); |
| 68 DCHECK(it != pending_requests_.end()); |
| 69 |
| 70 ThumbnailRequest* request = &it->second; |
| 71 |
| 72 // Make sure the request fetcher is deallocated. |
| 73 scoped_ptr<chrome::BitmapFetcher> fetcher_destructor(request->fetcher); |
| 74 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the |
| 75 // BitmapFetcher and which ceases to exist after this function. Pass the |
| 76 // un-owned pointer to the registered callbacks. |
| 77 for (CallbackVector::iterator it = request->callbacks.begin(); |
| 78 it != request->callbacks.end(); ++it) { |
| 79 it->Run(request->url, bitmap); |
| 80 } |
| 81 pending_requests_.erase(it); |
| 82 } |
| 83 |
| 84 bool ThumbnailManager::GetThumbnailURL(const GURL& url, GURL* thumbnail_url) { |
| 85 std::map<GURL, GURL>::iterator it = thumbnail_map_.find(url); |
| 86 if (it == thumbnail_map_.end()) |
| 87 return false; // Not found. |
| 88 *thumbnail_url = it->second; |
| 89 return true; |
| 90 } |
| 91 |
| 92 } // namespace suggestions |
OLD | NEW |