Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / search / suggestions / image_fetcher_impl.cc
blob87a695077b9ff6459414a1212e16542b495ab609
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.
5 #include "chrome/browser/search/suggestions/image_fetcher_impl.h"
7 #include <string>
9 #include "content/public/browser/browser_thread.h"
10 #include "net/base/load_flags.h"
11 #include "net/url_request/url_request_context_getter.h"
13 namespace suggestions {
15 ImageFetcherImpl::ImageFetcherImpl(
16 net::URLRequestContextGetter* url_request_context)
17 : delegate_(NULL), url_request_context_(url_request_context) {}
19 ImageFetcherImpl::~ImageFetcherImpl() {}
21 ImageFetcherImpl::ImageRequest::ImageRequest() : fetcher(NULL) {}
23 ImageFetcherImpl::ImageRequest::ImageRequest(chrome::BitmapFetcher* f)
24 : fetcher(f) {}
26 ImageFetcherImpl::ImageRequest::~ImageRequest() { delete fetcher; }
28 void ImageFetcherImpl::SetImageFetcherDelegate(ImageFetcherDelegate* delegate) {
29 DCHECK(delegate);
30 delegate_ = delegate;
33 void ImageFetcherImpl::StartOrQueueNetworkRequest(
34 const GURL& url, const GURL& image_url,
35 base::Callback<void(const GURL&, const SkBitmap*)> callback) {
36 // Before starting to fetch the image. Look for a request in progress for
37 // |image_url|, and queue if appropriate.
38 ImageRequestMap::iterator it = pending_net_requests_.find(image_url);
39 if (it == pending_net_requests_.end()) {
40 // |image_url| is not being fetched, so create a request and initiate
41 // the fetch.
42 ImageRequest request(new chrome::BitmapFetcher(image_url, this));
43 request.url = url;
44 request.callbacks.push_back(callback);
45 request.fetcher->Init(
46 url_request_context_, std::string(),
47 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
48 net::LOAD_NORMAL);
49 request.fetcher->Start();
50 pending_net_requests_[image_url].swap(&request);
51 } else {
52 // Request in progress. Register as an interested callback.
53 it->second.callbacks.push_back(callback);
57 void ImageFetcherImpl::OnFetchComplete(const GURL& image_url,
58 const SkBitmap* bitmap) {
59 ImageRequestMap::iterator image_iter = pending_net_requests_.find(image_url);
60 DCHECK(image_iter != pending_net_requests_.end());
62 ImageRequest* request = &image_iter->second;
64 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the
65 // BitmapFetcher and which ceases to exist after this function. Pass the
66 // un-owned pointer to the registered callbacks.
67 for (CallbackVector::iterator callback_iter = request->callbacks.begin();
68 callback_iter != request->callbacks.end(); ++callback_iter) {
69 callback_iter->Run(request->url, bitmap);
72 // Inform the ImageFetcherDelegate.
73 if (delegate_) {
74 delegate_->OnImageFetched(request->url, bitmap);
77 // Erase the completed ImageRequest.
78 pending_net_requests_.erase(image_iter);
81 } // namespace suggestions