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

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

Issue 1974013002: Replace SkBitmap with gfx::Image in the ImageFetcher API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync again Created 4 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
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 "chrome/browser/search/suggestions/image_fetcher_impl.h" 5 #include "chrome/browser/search/suggestions/image_fetcher_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "content/public/browser/browser_thread.h" 9 #include "content/public/browser/browser_thread.h"
10 #include "net/base/load_flags.h" 10 #include "net/base/load_flags.h"
11 #include "net/url_request/url_request_context_getter.h" 11 #include "net/url_request/url_request_context_getter.h"
12 #include "ui/gfx/image/image.h"
12 13
13 namespace suggestions { 14 namespace suggestions {
14 15
15 ImageFetcherImpl::ImageFetcherImpl( 16 ImageFetcherImpl::ImageFetcherImpl(
16 net::URLRequestContextGetter* url_request_context) 17 net::URLRequestContextGetter* url_request_context)
17 : delegate_(NULL), url_request_context_(url_request_context) {} 18 : delegate_(NULL), url_request_context_(url_request_context) {}
18 19
19 ImageFetcherImpl::~ImageFetcherImpl() {} 20 ImageFetcherImpl::~ImageFetcherImpl() {}
20 21
21 ImageFetcherImpl::ImageRequest::ImageRequest() : fetcher(NULL) {} 22 ImageFetcherImpl::ImageRequest::ImageRequest() : fetcher(NULL) {}
22 23
23 ImageFetcherImpl::ImageRequest::ImageRequest(chrome::BitmapFetcher* f) 24 ImageFetcherImpl::ImageRequest::ImageRequest(chrome::BitmapFetcher* f)
24 : fetcher(f) {} 25 : fetcher(f) {}
25 26
26 ImageFetcherImpl::ImageRequest::ImageRequest(const ImageRequest& other) = 27 ImageFetcherImpl::ImageRequest::ImageRequest(const ImageRequest& other) =
27 default; 28 default;
28 29
29 ImageFetcherImpl::ImageRequest::~ImageRequest() { delete fetcher; } 30 ImageFetcherImpl::ImageRequest::~ImageRequest() { delete fetcher; }
30 31
31 void ImageFetcherImpl::SetImageFetcherDelegate( 32 void ImageFetcherImpl::SetImageFetcherDelegate(
32 image_fetcher::ImageFetcherDelegate* delegate) { 33 image_fetcher::ImageFetcherDelegate* delegate) {
33 DCHECK(delegate); 34 DCHECK(delegate);
34 delegate_ = delegate; 35 delegate_ = delegate;
35 } 36 }
36 37
37 void ImageFetcherImpl::StartOrQueueNetworkRequest( 38 void ImageFetcherImpl::StartOrQueueNetworkRequest(
38 const GURL& url, const GURL& image_url, 39 const GURL& url, const GURL& image_url,
39 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 40 base::Callback<void(const GURL&, const gfx::Image&)> callback) {
40 // Before starting to fetch the image. Look for a request in progress for 41 // Before starting to fetch the image. Look for a request in progress for
41 // |image_url|, and queue if appropriate. 42 // |image_url|, and queue if appropriate.
42 ImageRequestMap::iterator it = pending_net_requests_.find(image_url); 43 ImageRequestMap::iterator it = pending_net_requests_.find(image_url);
43 if (it == pending_net_requests_.end()) { 44 if (it == pending_net_requests_.end()) {
44 // |image_url| is not being fetched, so create a request and initiate 45 // |image_url| is not being fetched, so create a request and initiate
45 // the fetch. 46 // the fetch.
46 ImageRequest request(new chrome::BitmapFetcher(image_url, this)); 47 ImageRequest request(new chrome::BitmapFetcher(image_url, this));
47 request.url = url; 48 request.url = url;
48 request.callbacks.push_back(callback); 49 request.callbacks.push_back(callback);
49 request.fetcher->Init( 50 request.fetcher->Init(
50 url_request_context_, std::string(), 51 url_request_context_, std::string(),
51 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, 52 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
52 net::LOAD_NORMAL); 53 net::LOAD_NORMAL);
53 request.fetcher->Start(); 54 request.fetcher->Start();
54 pending_net_requests_[image_url].swap(&request); 55 pending_net_requests_[image_url].swap(&request);
55 } else { 56 } else {
56 // Request in progress. Register as an interested callback. 57 // Request in progress. Register as an interested callback.
57 it->second.callbacks.push_back(callback); 58 it->second.callbacks.push_back(callback);
58 } 59 }
59 } 60 }
60 61
61 void ImageFetcherImpl::OnFetchComplete(const GURL& image_url, 62 void ImageFetcherImpl::OnFetchComplete(const GURL& image_url,
62 const SkBitmap* bitmap) { 63 const SkBitmap* bitmap) {
63 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url); 64 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url);
64 DCHECK(image_iter != pending_net_requests_.end()); 65 DCHECK(image_iter != pending_net_requests_.end());
65 66
66 ImageRequest* request = &image_iter->second; 67 ImageRequest* request = &image_iter->second;
67 68
68 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the 69 // Here |bitmap| could be NULL. In this case an empty image is passed to the
69 // BitmapFetcher and which ceases to exist after this function. Pass the 70 // callbacks and delegate. The pointer to the bitmap which is owned by the
70 // un-owned pointer to the registered callbacks. 71 // BitmapFetcher ceases to exist after this function. The created gfx::Image
71 for (CallbackVector::iterator callback_iter = request->callbacks.begin(); 72 // shares the pixels with the |bitmap|. The image is passed to the callbacks
72 callback_iter != request->callbacks.end(); ++callback_iter) { 73 // and delegate that are run synchronously.
73 callback_iter->Run(request->url, bitmap); 74 gfx::Image image;
75 if (bitmap != nullptr)
76 image = gfx::Image::CreateFrom1xBitmap(*bitmap);
77
78 for (const auto& callback : request->callbacks) {
79 callback.Run(request->url, image);
74 } 80 }
75 81
76 // Inform the ImageFetcherDelegate. 82 // Inform the ImageFetcherDelegate.
77 if (delegate_) { 83 if (delegate_) {
78 delegate_->OnImageFetched(request->url, bitmap); 84 delegate_->OnImageFetched(request->url, image);
79 } 85 }
80 86
81 // Erase the completed ImageRequest. 87 // Erase the completed ImageRequest.
82 pending_net_requests_.erase(image_iter); 88 pending_net_requests_.erase(image_iter);
83 } 89 }
84 90
85 } // namespace suggestions 91 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698