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