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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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::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.
21
22 ThumbnailManager::Session::~Session() {}
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(const GURL& url,
36 BitmapResponseCallback callback) {
37 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
38 // If |url| is not found in |thumbnail_map_|, then invoke |callback| with NULL
39 // since there is no associated thumbnail.
40 GURL thumbnail_url;
41 if (!GetThumbnailURL(url, &thumbnail_url)) {
42 callback.Run(url, NULL);
43 return;
44 }
45
46 // Look for a fetch in progress for |thumbnail_url|.
47 ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url);
48 if (it == session_map_.end()) {
49 // |thumbnail_url| is not being fetched, so create a session and initiate
50 // the fetch.
51 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
52 session->url = url;
53 session->callbacks.push(callback);
54 // This is deallocated in OnFetchComplete().
55 session->fetcher = new chrome::BitmapFetcher(thumbnail_url, this);
56 session->fetcher->Start(
57 url_request_context_, std::string(),
58 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
59 net::LOAD_NORMAL);
60 } else {
61 // Fetch in progress. Register as an interested callback.
62 it->second.callbacks.push(callback);
63 }
64 }
65
66 void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url,
67 const SkBitmap* bitmap) {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
69
70 ThumbnailSessionMap::iterator it = session_map_.find(thumbnail_url);
71 DCHECK(it != session_map_.end());
72
73 Session* session = &it->second;
74
75 // Make sure the |fetcher| is deallocated.
76 scoped_ptr<chrome::BitmapFetcher> fetcher_destructor(session->fetcher);
77 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the
78 // BitmapFetcher and which ceases to exist after this function. Pass the
79 // un-owned pointer to the registered callbacks.
80 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
81 while (!callbacks.empty()) {
82 callbacks.front().Run(session->url, bitmap);
83 callbacks.pop();
84 }
85 session_map_.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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698