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

Side by Side Diff: components/image_fetcher/image_fetcher_impl.cc

Issue 2757643002: components/image_fetcher: Expose RequestMetadata from ImageFetcher (Closed)
Patch Set: logo_bridge Created 3 years, 9 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/image_fetcher/image_fetcher_impl.h" 5 #include "components/image_fetcher/image_fetcher_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "net/base/load_flags.h" 10 #include "net/base/load_flags.h"
(...skipping 28 matching lines...) Expand all
39 image_data_fetcher_->SetDataUseServiceName(data_use_service_name); 39 image_data_fetcher_->SetDataUseServiceName(data_use_service_name);
40 } 40 }
41 41
42 void ImageFetcherImpl::SetDesiredImageFrameSize(const gfx::Size& size) { 42 void ImageFetcherImpl::SetDesiredImageFrameSize(const gfx::Size& size) {
43 desired_image_frame_size_ = size; 43 desired_image_frame_size_ = size;
44 } 44 }
45 45
46 void ImageFetcherImpl::StartOrQueueNetworkRequest( 46 void ImageFetcherImpl::StartOrQueueNetworkRequest(
47 const std::string& id, 47 const std::string& id,
48 const GURL& image_url, 48 const GURL& image_url,
49 base::Callback<void(const std::string&, const gfx::Image&)> callback) { 49 const ImageFetcherCallback& callback) {
50 // Before starting to fetch the image. Look for a request in progress for 50 // Before starting to fetch the image. Look for a request in progress for
51 // |image_url|, and queue if appropriate. 51 // |image_url|, and queue if appropriate.
52 ImageRequestMap::iterator it = pending_net_requests_.find(image_url); 52 ImageRequestMap::iterator it = pending_net_requests_.find(image_url);
53 if (it == pending_net_requests_.end()) { 53 if (it == pending_net_requests_.end()) {
54 ImageRequest request; 54 ImageRequest request;
55 request.id = id; 55 request.id = id;
56 request.callbacks.push_back(callback); 56 request.callbacks.push_back(callback);
57 pending_net_requests_[image_url].swap(&request); 57 pending_net_requests_[image_url].swap(&request);
58 58
59 image_data_fetcher_->FetchImageData( 59 image_data_fetcher_->FetchImageData(
60 image_url, 60 image_url,
61 base::Bind(&ImageFetcherImpl::OnImageURLFetched, 61 base::Bind(&ImageFetcherImpl::OnImageURLFetched,
62 base::Unretained(this), image_url)); 62 base::Unretained(this), image_url));
63 } else { 63 } else {
64 // Request in progress. Register as an interested callback. 64 // Request in progress. Register as an interested callback.
65 it->second.callbacks.push_back(callback); 65 it->second.callbacks.push_back(callback);
66 } 66 }
67 } 67 }
68 68
69 void ImageFetcherImpl::OnImageURLFetched(const GURL& image_url, 69 void ImageFetcherImpl::OnImageURLFetched(const GURL& image_url,
70 const std::string& image_data, 70 const std::string& image_data,
71 const RequestMetadata& metadata) { 71 const RequestMetadata& metadata) {
72 // Inform the ImageFetcherDelegate. 72 // Inform the ImageFetcherDelegate.
73 if (delegate_) { 73 if (delegate_) {
74 auto it = pending_net_requests_.find(image_url); 74 auto it = pending_net_requests_.find(image_url);
75 DCHECK(it != pending_net_requests_.end()); 75 DCHECK(it != pending_net_requests_.end());
76 delegate_->OnImageDataFetched(it->second.id, image_data); 76 delegate_->OnImageDataFetched(it->second.id, image_data);
77 } 77 }
78 78
79 image_decoder_->DecodeImage(image_data, desired_image_frame_size_, 79 image_decoder_->DecodeImage(
80 base::Bind(&ImageFetcherImpl::OnImageDecoded, 80 image_data, desired_image_frame_size_,
81 base::Unretained(this), image_url)); 81 base::Bind(&ImageFetcherImpl::OnImageDecoded, base::Unretained(this),
82 image_url, metadata));
82 } 83 }
83 84
84 void ImageFetcherImpl::OnImageDecoded(const GURL& image_url, 85 void ImageFetcherImpl::OnImageDecoded(const GURL& image_url,
86 const RequestMetadata& metadata,
85 const gfx::Image& image) { 87 const gfx::Image& image) {
86 // Get request for the given image_url from the request queue. 88 // Get request for the given image_url from the request queue.
87 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url); 89 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url);
88 DCHECK(image_iter != pending_net_requests_.end()); 90 DCHECK(image_iter != pending_net_requests_.end());
89 ImageRequest* request = &image_iter->second; 91 ImageRequest* request = &image_iter->second;
90 92
91 // Run all callbacks 93 // Run all callbacks
92 for (const auto& callback : request->callbacks) { 94 for (const auto& callback : request->callbacks) {
93 callback.Run(request->id, image); 95 callback.Run(request->id, image, metadata);
94 } 96 }
95 97
96 // Inform the ImageFetcherDelegate. 98 // Inform the ImageFetcherDelegate.
97 if (delegate_) { 99 if (delegate_) {
98 delegate_->OnImageFetched(request->id, image); 100 delegate_->OnImageFetched(request->id, image);
99 } 101 }
100 102
101 // Erase the completed ImageRequest. 103 // Erase the completed ImageRequest.
102 pending_net_requests_.erase(image_iter); 104 pending_net_requests_.erase(image_iter);
103 } 105 }
104 106
105 ImageDecoder* ImageFetcherImpl::GetImageDecoder() { 107 ImageDecoder* ImageFetcherImpl::GetImageDecoder() {
106 return image_decoder_.get(); 108 return image_decoder_.get();
107 } 109 }
108 110
109 } // namespace image_fetcher 111 } // namespace image_fetcher
OLDNEW
« no previous file with comments | « components/image_fetcher/image_fetcher_impl.h ('k') | components/ntp_snippets/remote/remote_suggestions_provider_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698