Chromium Code Reviews| Index: chrome/browser/search/suggestions/thumbnail_manager.cc |
| diff --git a/chrome/browser/search/suggestions/thumbnail_manager.cc b/chrome/browser/search/suggestions/thumbnail_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6320601b41d2f931f727e92ae5841809b5437d8c |
| --- /dev/null |
| +++ b/chrome/browser/search/suggestions/thumbnail_manager.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/search/suggestions/thumbnail_manager.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| +#include "net/base/load_flags.h" |
| + |
| +namespace suggestions { |
| + |
| +ThumbnailManager::ThumbnailManager(Profile* profile) |
| + : url_request_context_(profile->GetRequestContext()) {} |
| + |
| +ThumbnailManager::~ThumbnailManager() {} |
| + |
| +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 :/
|
| + |
| +ThumbnailManager::Session::~Session() {} |
| + |
| +void ThumbnailManager::InitializeThumbnailMap( |
| + const SuggestionsProfile& suggestions) { |
| + thumbnail_map_.clear(); |
| + for (int i = 0; i < suggestions.suggestions_size(); ++i) { |
| + const ChromeSuggestion& suggestion = suggestions.suggestions(i); |
| + if (suggestion.has_thumbnail()) { |
| + thumbnail_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); |
| + } |
| + } |
| +} |
| + |
| +void ThumbnailManager::GetPageThumbnail(const GURL& url, |
| + BitmapResponseCallback callback) { |
|
huangs
2014/05/22 18:08:22
#include "content/public/browser/browser_thread.h"
Mathieu
2014/05/22 19:46:32
Done.
|
| + // 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.
|
| + // thumbnail for it. Will callback with an empty bitmap. |
| + GURL thumbnail_url; |
| + if (!GetThumbnailURL(url, &thumbnail_url)) { |
| + callback.Run(url, NULL); |
| + return; |
| + } |
| + |
| + // 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.
|
| + // set the website URL. |
| + ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url); |
| + if (it == session_map_.end()) { |
| + // 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.
|
| + // and initiate the fetch. |
| + Session* session = &session_map_[thumbnail_url]; |
| + session->url_ = url; |
| + session->callbacks_.push(callback); |
| + 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.
|
| + session->fetcher_->Start( |
| + url_request_context_, std::string(), |
| + net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| + net::LOAD_NORMAL); |
| + } else { |
| + // Fetch in progress. Register as an interested callback. |
| + it->second.callbacks_.push(callback); |
| + } |
| +} |
| + |
| +void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url, |
| + const SkBitmap* bitmap) { |
| + ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url); |
| + DCHECK(it != session_map_.end()); |
| + |
| + Session* session = &it->second; |
| + |
| + // Make sure the |fetcher| is deallocated. |
| + scoped_ptr<chrome::BitmapFetcher> fetcher_destructor(session->fetcher_); |
| + |
| + // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the |
| + // BitmapFetcher and which ceases to exist after this function. Pass the |
| + // un-owned pointer to the registered callbacks. |
| + std::queue<BitmapResponseCallback> callbacks; |
| + callbacks.swap(session->callbacks_); |
| + while (!callbacks.empty()) { |
| + callbacks.front().Run(session->url_, bitmap); |
| + callbacks.pop(); |
| + } |
| + session_map_.erase(it); |
| +} |
| + |
| +bool ThumbnailManager::GetThumbnailURL(const GURL& url, GURL* thumbnail_url) { |
| + std::map<GURL, GURL>::iterator it = thumbnail_map_.find(url); |
| + if (it == thumbnail_map_.end()) { |
| + // 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.
|
| + return false; |
| + } |
| + *thumbnail_url = it->second; |
| + return true; |
| +} |
| + |
| +} // namespace suggestions |