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

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

Issue 2074153002: Pass the ImageDecoder to the ImageFetcher at creation time. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments round2 treib@ & added missing imports *sigh* Created 4 years, 5 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
7 #include <string>
8
9 #include "base/bind.h"
10 #include "chrome/browser/search/suggestions/image_decoder_impl.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/load_flags.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "ui/gfx/image/image.h"
15
16 namespace suggestions {
17
18 ImageFetcherImpl::ImageFetcherImpl(
19 net::URLRequestContextGetter* url_request_context)
20 : delegate_(nullptr), url_request_context_(url_request_context),
21 image_decoder_(new suggestions::ImageDecoderImpl()),
22 image_data_fetcher_(
23 new image_fetcher::ImageDataFetcher(url_request_context_)) {
24 }
25
26 ImageFetcherImpl::~ImageFetcherImpl() {}
27
28 ImageFetcherImpl::ImageRequest::ImageRequest() {}
29
30 ImageFetcherImpl::ImageRequest::ImageRequest(const ImageRequest& other) =
31 default;
32
33 ImageFetcherImpl::ImageRequest::~ImageRequest() { }
34
35 void ImageFetcherImpl::SetImageFetcherDelegate(
36 image_fetcher::ImageFetcherDelegate* delegate) {
37 DCHECK(delegate);
38 delegate_ = delegate;
39 }
40
41 void ImageFetcherImpl::StartOrQueueNetworkRequest(
42 const std::string& id,
43 const GURL& image_url,
44 base::Callback<void(const std::string&, const gfx::Image&)> callback) {
45 // Before starting to fetch the image. Look for a request in progress for
46 // |image_url|, and queue if appropriate.
47 ImageRequestMap::iterator it = pending_net_requests_.find(image_url);
48 if (it == pending_net_requests_.end()) {
49 ImageRequest request;
50 request.id = id;
51 request.callbacks.push_back(callback);
52 pending_net_requests_[image_url].swap(&request);
53
54 image_data_fetcher_->FetchImageData(
55 image_url,
56 base::Bind(&ImageFetcherImpl::OnImageURLFetched,
57 base::Unretained(this), image_url));
58 } else {
59 // Request in progress. Register as an interested callback.
60 it->second.callbacks.push_back(callback);
61 }
62 }
63
64 void ImageFetcherImpl::OnImageURLFetched(const GURL& image_url,
65 const std::string& image_data) {
66 // Inform the ImageFetcherDelegate.
67 if (delegate_) {
68 auto it = pending_net_requests_.find(image_url);
69 DCHECK(it != pending_net_requests_.end());
70 delegate_->OnImageDataFetched(it->second.id, image_data);
71 }
72
73 image_decoder_->DecodeImage(
74 image_data,
75 base::Bind(&ImageFetcherImpl::OnImageDecoded,
76 base::Unretained(this), image_url));
77 }
78
79 void ImageFetcherImpl::OnImageDecoded(const GURL& image_url,
80 const gfx::Image& image) {
81 // Get request for the given image_url from the request queue.
82 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url);
83 DCHECK(image_iter != pending_net_requests_.end());
84 ImageRequest* request = &image_iter->second;
85
86 // Run all callbacks
87 for (const auto& callback : request->callbacks) {
88 callback.Run(request->id, image);
89 }
90
91 // Inform the ImageFetcherDelegate.
92 if (delegate_) {
93 delegate_->OnImageFetched(request->id, image);
94 }
95
96 // Erase the completed ImageRequest.
97 pending_net_requests_.erase(image_iter);
98 }
99
100 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698