Index: chrome/browser/bitmap_fetcher/bitmap_batch_fetcher.cc |
diff --git a/chrome/browser/bitmap_fetcher/bitmap_batch_fetcher.cc b/chrome/browser/bitmap_fetcher/bitmap_batch_fetcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..891754c193bcf0f8634eca11feb3ebf73e7be972 |
--- /dev/null |
+++ b/chrome/browser/bitmap_fetcher/bitmap_batch_fetcher.cc |
@@ -0,0 +1,88 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/bitmap_fetcher/bitmap_batch_fetcher.h" |
+ |
+#include "content/public/browser/browser_thread.h" |
+#include "net/url_request/url_fetcher.h" |
+#include "net/url_request/url_request_context_getter.h" |
+#include "net/url_request/url_request_status.h" |
+ |
+namespace chrome { |
+ |
+BitmapBatchFetcher::BitmapBatchFetcher() { |
+ image_batch_decoder_ = |
+ new ImageBatchDecoder( |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::UI)); |
+} |
+ |
+BitmapBatchFetcher::~BitmapBatchFetcher() { |
+} |
+ |
+void BitmapBatchFetcher::Start(net::URLRequestContextGetter* request_context, |
+ const std::string& referrer, |
+ net::URLRequest::ReferrerPolicy referrer_policy, |
+ int load_flags, |
+ const GURL& url, |
+ BitmapFetcherDelegate* delegate) { |
+ net::URLFetcher* url_fetcher = |
+ net::URLFetcher::Create(url, net::URLFetcher::GET, this); |
+ url_fetcher_map_[url_fetcher] = |
+ std::pair<GURL, BitmapFetcherDelegate*>(url, delegate); |
+ |
+ url_fetcher->SetRequestContext(request_context); |
+ url_fetcher->SetReferrer(referrer); |
+ url_fetcher->SetReferrerPolicy(referrer_policy); |
+ url_fetcher->SetLoadFlags(load_flags); |
+ url_fetcher->Start(); |
+} |
+ |
+// Methods inherited from URLFetcherDelegate. |
+ |
+void BitmapBatchFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) { |
+ url_fetcher_map_[source].second->OnFetchComplete( |
+ url_fetcher_map_[source].first, NULL); |
+ return; |
+ } |
+ |
+ std::string image_data; |
+ source->GetResponseAsString(&image_data); |
+ // Call start to begin decoding. The ImageBatchDecoder will call |
+ // OnImageDecoded with the data when it is done. |
+ scoped_refptr<base::MessageLoopProxy> task_runner = |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::UI); |
+ image_batch_decoder_->Start( |
+ new BitmapBatchFetcher::Delegate(url_fetcher_map_[source].first, |
+ url_fetcher_map_[source].second), |
+ image_data, |
+ ImageBatchDecoder::DEFAULT_CODEC); |
+} |
+ |
+void BitmapBatchFetcher::OnURLFetchDownloadProgress( |
+ const net::URLFetcher* source, |
+ int64 current, |
+ int64 total) { |
+ // Do nothing here. |
+} |
+ |
+// Methods inherited from ImageBatchDecoder::Delegate. |
+ |
+void BitmapBatchFetcher::Delegate::OnImageDecoded( |
+ const ImageBatchDecoder* decoder, |
+ const SkBitmap& decoded_image) { |
+ // Report success. |
+ delegate_->OnFetchComplete(url_, &decoded_image); |
+} |
+ |
+void BitmapBatchFetcher::Delegate::OnDecodeImageFailed( |
+ const ImageBatchDecoder* decoder) { |
+ delegate_->OnFetchComplete(url_, NULL); |
+} |
+ |
+} // namespace chrome |