Chromium Code Reviews| 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 : url_request_context_(profile->GetRequestContext()) {} | |
| 16 | |
| 17 ThumbnailManager::~ThumbnailManager() {} | |
| 18 | |
| 19 ThumbnailManager::Session::Session() {} | |
|
huangs
2014/05/22 18:08:22
Can erase these, per. comment in .h file.
Mathieu
2014/05/22 19:46:32
I don't think so :/
| |
| 20 | |
| 21 ThumbnailManager::Session::~Session() {} | |
| 22 | |
| 23 void ThumbnailManager::InitializeThumbnailMap( | |
| 24 const SuggestionsProfile& suggestions) { | |
| 25 thumbnail_map_.clear(); | |
| 26 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | |
| 27 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | |
| 28 if (suggestion.has_thumbnail()) { | |
| 29 thumbnail_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); | |
| 30 } | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 void ThumbnailManager::GetPageThumbnail(const GURL& url, | |
| 35 BitmapResponseCallback callback) { | |
|
huangs
2014/05/22 18:08:22
#include "content/public/browser/browser_thread.h"
Mathieu
2014/05/22 19:46:32
Done.
| |
| 36 // If the |url| is not in the |thumbnail_map_|, there is no associated | |
|
huangs
2014/05/22 18:08:22
Perhaps:
// If |url| is not found in |thumbnail_m
Mathieu
2014/05/22 19:46:32
Done.
| |
| 37 // thumbnail for it. Will callback with an empty bitmap. | |
| 38 GURL thumbnail_url; | |
| 39 if (!GetThumbnailURL(url, &thumbnail_url)) { | |
| 40 callback.Run(url, NULL); | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 // Get or create the ThumbnailManagerSession. In the case of a creation, | |
|
huangs
2014/05/22 18:08:22
// Look for a fetch in progress for |thumbnail_url
Mathieu
2014/05/22 19:46:32
Done.
| |
| 45 // set the website URL. | |
| 46 ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url); | |
| 47 if (it == session_map_.end()) { | |
| 48 // There are no fetches in progress for |thumbnail_url|. Create a session | |
|
huangs
2014/05/22 18:08:22
// |thumbnail_url| is not being fetched, so create
Mathieu
2014/05/22 19:46:32
Done.
| |
| 49 // and initiate the fetch. | |
| 50 Session* session = &session_map_[thumbnail_url]; | |
| 51 session->url_ = url; | |
| 52 session->callbacks_.push(callback); | |
| 53 session->fetcher_ = new chrome::BitmapFetcher(thumbnail_url, this); | |
|
huangs
2014/05/22 18:08:22
// This is deallocated in OnFetchComplete().
Mathieu
2014/05/22 19:46:32
Done.
| |
| 54 session->fetcher_->Start( | |
| 55 url_request_context_, std::string(), | |
| 56 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | |
| 57 net::LOAD_NORMAL); | |
| 58 } else { | |
| 59 // Fetch in progress. Register as an interested callback. | |
| 60 it->second.callbacks_.push(callback); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url, | |
| 65 const SkBitmap* bitmap) { | |
| 66 ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url); | |
| 67 DCHECK(it != session_map_.end()); | |
| 68 | |
| 69 Session* session = &it->second; | |
| 70 | |
| 71 // Make sure the |fetcher| is deallocated. | |
| 72 scoped_ptr<chrome::BitmapFetcher> fetcher_destructor(session->fetcher_); | |
| 73 | |
| 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 std::queue<BitmapResponseCallback> callbacks; | |
| 78 callbacks.swap(session->callbacks_); | |
| 79 while (!callbacks.empty()) { | |
| 80 callbacks.front().Run(session->url_, bitmap); | |
| 81 callbacks.pop(); | |
| 82 } | |
| 83 session_map_.erase(it); | |
| 84 } | |
| 85 | |
| 86 bool ThumbnailManager::GetThumbnailURL(const GURL& url, GURL* thumbnail_url) { | |
| 87 std::map<GURL, GURL>::iterator it = thumbnail_map_.find(url); | |
| 88 if (it == thumbnail_map_.end()) { | |
| 89 // Not found. | |
|
huangs
2014/05/22 18:08:22
NIT: can be shortened to 2 liens:
if (it == thumb
Mathieu
2014/05/22 19:46:32
Done.
| |
| 90 return false; | |
| 91 } | |
| 92 *thumbnail_url = it->second; | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 } // namespace suggestions | |
| OLD | NEW |