Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Unified Diff: chrome/browser/search/suggestions/thumbnail_manager.cc

Issue 299713007: [Suggestions] Adding a Thumbnail Manager for the Suggestions Service (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: removed C++11 statement Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8f2425db5d46c43cce93dad345ffc61367815aab
--- /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 "content/public/browser/browser_thread.h"
+#include "net/base/load_flags.h"
+
+namespace suggestions {
+
+ThumbnailManager::ThumbnailManager(Profile* profile)
+ : url_request_context_(profile->GetRequestContext()) {}
+
+ThumbnailManager::~ThumbnailManager() {}
+
+ThumbnailManager::Session::Session() {}
Jered 2014/05/23 16:57:48 Initialize the struct fields.
huangs 2014/05/23 17:23:35 Per discussion, can remove ThumbnailManager::Sessi
Mathieu 2014/05/23 19:21:58 Actually removed this.
Mathieu 2014/05/23 19:21:58 Done.
+
+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) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ // If |url| is not found in |thumbnail_map_|, then invoke |callback| with NULL
+ // since there is no associated thumbnail.
+ GURL thumbnail_url;
+ if (!GetThumbnailURL(url, &thumbnail_url)) {
+ callback.Run(url, NULL);
+ return;
+ }
+
+ // Look for a fetch in progress for |thumbnail_url|.
+ ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url);
+ if (it == session_map_.end()) {
+ // |thumbnail_url| is not being fetched, so create a session and initiate
+ // the fetch.
+ Session* session = &session_map_[thumbnail_url];
Jered 2014/05/23 16:57:48 A different approach here that would help you to c
Mathieu 2014/05/23 19:21:58 I actually like the approach Sam and I had come up
huangs 2014/05/23 20:16:22 To Jered: Since we use std::map<>, the suggested c
Jered 2014/05/23 21:25:00 A default constructor seems reasonable. map.erase(
Jered 2014/05/23 21:25:00 This is one of my pet peeves in C++... correctness
huangs 2014/05/23 22:34:26 We tried this, but std::map<GURL, ThumbnailSession
Jered 2014/05/23 22:37:01 Even if we can't use scoped_ptr, a raw pointer wit
Mathieu 2014/05/23 22:40:25 Indeed we tried this along with many variations (S
Mathieu 2014/05/23 22:40:25 Ack.
Mathieu 2014/05/23 22:54:58 Have a look at the latest version. Had to leave th
+ session->url = url;
+ session->callbacks.push(callback);
+ // This is deallocated in OnFetchComplete().
+ session->fetcher = new chrome::BitmapFetcher(thumbnail_url, this);
+ 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) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ 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 = session->callbacks;
huangs 2014/05/23 17:23:35 Comment that you're purposefully creating a copy o
Mathieu 2014/05/23 19:21:58 No copying sounds good since we're on the UI threa
+ 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())
+ return false; // Not found.
+ *thumbnail_url = it->second;
+ return true;
+}
+
+} // namespace suggestions

Powered by Google App Engine
This is Rietveld 408576698