| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_data_fetcher.h" | 5 #include "components/image_fetcher/image_data_fetcher.h" |
| 6 | 6 |
| 7 #include "net/base/load_flags.h" | 7 #include "net/base/load_flags.h" |
| 8 #include "net/url_request/url_fetcher.h" | 8 #include "net/url_request/url_fetcher.h" |
| 9 #include "net/url_request/url_request.h" | 9 #include "net/url_request/url_request.h" |
| 10 #include "net/url_request/url_request_context_getter.h" | 10 #include "net/url_request/url_request_context_getter.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 // The callback to run after the image data was fetched. The callback will | 26 // The callback to run after the image data was fetched. The callback will |
| 27 // be run even if the image data could not be fetched successfully. | 27 // be run even if the image data could not be fetched successfully. |
| 28 ImageDataFetcherCallback callback; | 28 ImageDataFetcherCallback callback; |
| 29 | 29 |
| 30 std::unique_ptr<net::URLFetcher> url_fetcher; | 30 std::unique_ptr<net::URLFetcher> url_fetcher; |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 ImageDataFetcher::ImageDataFetcher( | 33 ImageDataFetcher::ImageDataFetcher( |
| 34 net::URLRequestContextGetter* url_request_context_getter) | 34 net::URLRequestContextGetter* url_request_context_getter) |
| 35 : url_request_context_getter_(url_request_context_getter) {} | 35 : url_request_context_getter_(url_request_context_getter), |
| 36 next_url_fetcher_id_(0) {} |
| 36 | 37 |
| 37 ImageDataFetcher::~ImageDataFetcher() {} | 38 ImageDataFetcher::~ImageDataFetcher() {} |
| 38 | 39 |
| 39 void ImageDataFetcher::FetchImageData( | 40 void ImageDataFetcher::FetchImageData( |
| 40 const GURL& url, const ImageDataFetcherCallback& callback) { | 41 const GURL& url, const ImageDataFetcherCallback& callback) { |
| 41 std::unique_ptr<net::URLFetcher> url_fetcher = | 42 std::unique_ptr<net::URLFetcher> url_fetcher = |
| 42 net::URLFetcher::Create(url, net::URLFetcher::GET, this); | 43 net::URLFetcher::Create( |
| 44 next_url_fetcher_id_++, url, net::URLFetcher::GET, this); |
| 43 | 45 |
| 44 std::unique_ptr<ImageDataFetcherRequest> request( | 46 std::unique_ptr<ImageDataFetcherRequest> request( |
| 45 new ImageDataFetcherRequest(callback, std::move(url_fetcher))); | 47 new ImageDataFetcherRequest(callback, std::move(url_fetcher))); |
| 46 request->url_fetcher->SetRequestContext(url_request_context_getter_.get()); | 48 request->url_fetcher->SetRequestContext(url_request_context_getter_.get()); |
| 47 request->url_fetcher->Start(); | 49 request->url_fetcher->Start(); |
| 48 | 50 |
| 49 pending_requests_[request->url_fetcher.get()] = std::move(request); | 51 pending_requests_[request->url_fetcher.get()] = std::move(request); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 54 void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 53 auto request_iter = pending_requests_.find(source); | 55 auto request_iter = pending_requests_.find(source); |
| 54 DCHECK(request_iter != pending_requests_.end()); | 56 DCHECK(request_iter != pending_requests_.end()); |
| 55 | 57 |
| 56 std::string image_data; | 58 std::string image_data; |
| 57 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS) { | 59 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS) { |
| 58 source->GetResponseAsString(&image_data); | 60 source->GetResponseAsString(&image_data); |
| 59 } | 61 } |
| 60 request_iter->second->callback.Run(image_data); | 62 request_iter->second->callback.Run(image_data); |
| 61 | 63 |
| 62 // Remove the finished request. | 64 // Remove the finished request. |
| 63 pending_requests_.erase(request_iter); | 65 pending_requests_.erase(request_iter); |
| 64 } | 66 } |
| 65 | 67 |
| 66 } // namespace image_fetcher | 68 } // namespace image_fetcher |
| OLD | NEW |