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

Side by Side Diff: chrome/browser/search/suggestions/image_fetcher_impl.cc

Issue 543753002: [Suggestions] Move ImageManager to the component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pointer fix Created 6 years, 3 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
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/image_fetcher_impl.h"
6
huangs 2014/09/24 15:46:57 #include <string>
Mathieu 2014/09/24 19:31:30 Done.
7 #include "content/public/browser/browser_thread.h"
8 #include "net/base/load_flags.h"
9 #include "net/url_request/url_request_context_getter.h"
10
11 namespace suggestions {
12
13 ImageFetcherImpl::ImageFetcherImpl(
14 net::URLRequestContextGetter* url_request_context)
15 : url_request_context_(url_request_context) {}
16
17 ImageFetcherImpl::~ImageFetcherImpl() {}
18
19 ImageFetcherImpl::ImageRequest::ImageRequest() : fetcher(NULL) {}
20
21 ImageFetcherImpl::ImageRequest::ImageRequest(chrome::BitmapFetcher* f)
22 : fetcher(f) {}
23
24 ImageFetcherImpl::ImageRequest::~ImageRequest() { delete fetcher; }
25
26 void ImageFetcherImpl::SetImageFetcherDelegate(ImageFetcherDelegate* delegate) {
27 DCHECK(delegate);
28 delegate_ = delegate;
29 }
30
31 void ImageFetcherImpl::StartOrQueueNetworkRequest(
32 const GURL& url, const GURL& image_url,
33 base::Callback<void(const GURL&, const SkBitmap*)> callback) {
34 // Before starting to fetch the image. Look for a request in progress for
35 // |image_url|, and queue if appropriate.
36 ImageRequestMap::iterator it = pending_net_requests_.find(image_url);
37 if (it == pending_net_requests_.end()) {
38 // |image_url| is not being fetched, so create a request and initiate
39 // the fetch.
40 ImageRequest request(new chrome::BitmapFetcher(image_url, this));
41 request.url = url;
42 request.callbacks.push_back(callback);
43 request.fetcher->Start(
44 url_request_context_, std::string(),
45 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
46 net::LOAD_NORMAL);
47 pending_net_requests_[image_url].swap(&request);
48 } else {
49 // Request in progress. Register as an interested callback.
50 it->second.callbacks.push_back(callback);
51 }
52 }
53
54 void ImageFetcherImpl::OnFetchComplete(const GURL image_url,
55 const SkBitmap* bitmap) {
56 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
57
58 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url);
59 DCHECK(image_iter != pending_net_requests_.end());
60
61 ImageRequest* request = &image_iter->second;
62
63 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the
64 // BitmapFetcher and which ceases to exist after this function. Pass the
65 // un-owned pointer to the registered callbacks.
66 for (CallbackVector::iterator callback_iter = request->callbacks.begin();
67 callback_iter != request->callbacks.end(); ++callback_iter) {
68 callback_iter->Run(request->url, bitmap);
69 }
70
71 // Inform the ImageFetcherDelegate.
72 if (delegate_) {
73 delegate_->OnImageFetched(request->url, bitmap);
74 }
75
76 // Erase the completed ImageRequest.
77 pending_net_requests_.erase(image_iter);
78 }
79
80 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698