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

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: delete pointer on destruction 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 "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h"
9 #include "content/public/browser/browser_thread.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::ThumbnailRequest::ThumbnailRequest() {}
Jered 2014/05/23 22:57:38 say :fetcher(NULL) here
Mathieu 2014/05/23 23:14:51 Done.
20
21 ThumbnailManager::ThumbnailRequest::ThumbnailRequest(chrome::BitmapFetcher* f)
22 : fetcher(f) {}
23
24 ThumbnailManager::ThumbnailRequest::~ThumbnailRequest() {
25 if (fetcher) delete fetcher;
Jered 2014/05/23 22:57:38 delete NULL is fine
Mathieu 2014/05/23 23:14:51 Done.
26 }
27
28 void ThumbnailManager::InitializeThumbnailMap(
29 const SuggestionsProfile& suggestions) {
30 thumbnail_map_.clear();
31 for (int i = 0; i < suggestions.suggestions_size(); ++i) {
32 const ChromeSuggestion& suggestion = suggestions.suggestions(i);
33 if (suggestion.has_thumbnail()) {
34 thumbnail_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail());
35 }
36 }
37 }
38
39 void ThumbnailManager::GetPageThumbnail(
40 const GURL& url,
41 base::Callback<void(const GURL&, const SkBitmap*)> callback) {
42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
43 // If |url| is not found in |thumbnail_map_|, then invoke |callback| with NULL
44 // since there is no associated thumbnail.
45 GURL thumbnail_url;
46 if (!GetThumbnailURL(url, &thumbnail_url)) {
47 callback.Run(url, NULL);
48 return;
49 }
50
51 // Look for a request in progress for |thumbnail_url|.
52 ThumbnailRequestMap::iterator it = pending_requests_.find(thumbnail_url);
53 if (it == pending_requests_.end()) {
54 // |thumbnail_url| is not being fetched, so create a request and initiate
55 // the fetch.
56 ThumbnailRequest request(new chrome::BitmapFetcher(thumbnail_url, this));
57 request.url = url;
58 request.callbacks.push_back(callback);
59 // This is deallocated in OnFetchComplete().
60 request.fetcher->Start(
61 url_request_context_, std::string(),
62 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
63 net::LOAD_NORMAL);
64 pending_requests_[thumbnail_url] = request;
65 } else {
66 // Request in progress. Register as an interested callback.
67 it->second.callbacks.push_back(callback);
68 }
69 }
70
71 void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url,
72 const SkBitmap* bitmap) {
73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
74
75 ThumbnailRequestMap::iterator it = pending_requests_.find(thumbnail_url);
76 DCHECK(it != pending_requests_.end());
77
78 ThumbnailRequest* request = &it->second;
79
80 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the
81 // BitmapFetcher and which ceases to exist after this function. Pass the
82 // un-owned pointer to the registered callbacks.
83 for (CallbackVector::iterator it = request->callbacks.begin();
84 it != request->callbacks.end(); ++it) {
85 it->Run(request->url, bitmap);
86 }
87 pending_requests_.erase(it);
88 }
89
90 bool ThumbnailManager::GetThumbnailURL(const GURL& url, GURL* thumbnail_url) {
91 std::map<GURL, GURL>::iterator it = thumbnail_map_.find(url);
92 if (it == thumbnail_map_.end()) return false; // Not found.
93 *thumbnail_url = it->second;
94 return true;
95 }
96
97 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698