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

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: clean 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..b5ae194756854ebbf7710b90e247a773f5315174
--- /dev/null
+++ b/chrome/browser/search/suggestions/thumbnail_manager.cc
@@ -0,0 +1,87 @@
+// 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)
+ : profile_(profile), url_request_context_(profile->GetRequestContext()) {}
+
+ThumbnailManager::~ThumbnailManager() {}
+
+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()) {
huangs 2014/05/22 06:17:35 What if there are multiple suggestions with the sa
Mathieu 2014/05/22 15:45:51 I feel like the server should worry about this. In
+ thumbnail_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail());
+ }
+ }
+}
+
+void ThumbnailManager::GetURLThumbnail(const GURL& url,
+ BitmapResponseCallback callback) {
+ // If the |url| is not in the |thumbnail_map_|, there is no associated
+ // thumbnail for it. Will callback with an empty bitmap.
+ GURL thumbnail_url;
+ if (!GetThumbnailURL(url, &thumbnail_url)) {
+ callback.Run(url, NULL);
+ return;
+ }
+
+ // If a fetch of the |thumbnail_url| is already in progress, register as an
+ // interested callback. Will allocate a new queue if not present.
+ pending_callbacks_[thumbnail_url].push(callback);
+
+ // If we are the first ones requesting this |thumbnail_url|, start the fetch.
+ if (pending_callbacks_[thumbnail_url].size() == 1) {
huangs 2014/05/22 06:17:35 I'd prefer using queue<>::empty() instead of check
Mathieu 2014/05/22 15:45:51 Reworked this, let me know
+ chrome::BitmapFetcher* fetcher =
+ new chrome::BitmapFetcher(thumbnail_url, this);
+ fetcher_map_[thumbnail_url] = std::make_pair(url, fetcher);
+ fetcher->Start(
+ url_request_context_, std::string(),
huangs 2014/05/22 06:17:35 NIT: 1 parameter per line.
Mathieu 2014/05/22 15:45:51 clang-format would disagree!
+ net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
+ net::LOAD_NORMAL);
+ }
+}
+
+void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url,
+ const SkBitmap* bitmap) {
+ // Make sure the BitmapFetcher gets destroyed.
+ scoped_ptr<chrome::BitmapFetcher> fetcher_destructor(
huangs 2014/05/22 06:17:35 You only need to do one lookup if you store the it
Mathieu 2014/05/22 15:45:51 Done. Have a look!
+ fetcher_map_[thumbnail_url].second);
+ GURL url(fetcher_map_[thumbnail_url].first);
+ fetcher_map_.erase(fetcher_map_.find(thumbnail_url));
+
+ // 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 =
huangs 2014/05/22 06:17:35 I think it's cleaner to just swap data data: std:
Mathieu 2014/05/22 15:45:51 Done.
+ pending_callbacks_[thumbnail_url];
+ while (!callbacks.empty()) {
+ callbacks.front().Run(url, bitmap);
+ callbacks.pop();
+ }
+ pending_callbacks_.erase(pending_callbacks_.find(thumbnail_url));
huangs 2014/05/22 06:17:35 You can just direct erase with the key: pending_c
Mathieu 2014/05/22 15:45:51 Have a look at how it is now?
+}
+
+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.
+ return false;
+ }
+ *thumbnail_url = it->second;
+ return true;
+}
+
+} // namespace suggestions

Powered by Google App Engine
This is Rietveld 408576698