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/bitmap_fetcher.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "net/url_request/url_fetcher.h"
9 #include "net/url_request/url_request_context_getter.h"
10 #include "net/url_request/url_request_status.h"
14 BitmapFetcher::BitmapFetcher(const GURL
& url
,
15 BitmapFetcherDelegate
* delegate
)
20 BitmapFetcher::~BitmapFetcher() {}
22 void BitmapFetcher::Start(net::URLRequestContextGetter
* request_context
) {
23 if (url_fetcher_
!= NULL
)
26 url_fetcher_
.reset(net::URLFetcher::Create(url_
, net::URLFetcher::GET
, this));
27 url_fetcher_
->SetRequestContext(request_context
);
28 url_fetcher_
->Start();
31 // Methods inherited from URLFetcherDelegate.
33 void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher
* source
) {
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
36 if (source
->GetStatus().status() != net::URLRequestStatus::SUCCESS
) {
41 std::string image_data
;
42 source
->GetResponseAsString(&image_data
);
44 new ImageDecoder(this, image_data
, ImageDecoder::DEFAULT_CODEC
);
46 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded
47 // with the data when it is done.
48 scoped_refptr
<base::MessageLoopProxy
> task_runner
=
49 content::BrowserThread::GetMessageLoopProxyForThread(
50 content::BrowserThread::UI
);
51 image_decoder_
->Start(task_runner
);
54 void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher
* source
,
60 // Methods inherited from ImageDecoder::Delegate.
62 void BitmapFetcher::OnImageDecoded(const ImageDecoder
* decoder
,
63 const SkBitmap
& decoded_image
) {
64 // Make a copy of the bitmap which we pass back to the UI thread.
65 scoped_ptr
<SkBitmap
> bitmap(new SkBitmap());
66 decoded_image
.deepCopyTo(bitmap
.get());
69 delegate_
->OnFetchComplete(url_
, bitmap
.get());
72 void BitmapFetcher::OnDecodeImageFailed(const ImageDecoder
* decoder
) {
76 void BitmapFetcher::ReportFailure() {
77 delegate_
->OnFetchComplete(url_
, NULL
);