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 "chrome/browser/profiles/profile.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "net/url_request/url_fetcher.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(Profile
* profile
) {
23 DCHECK(url_fetcher_
== NULL
);
24 url_fetcher_
.reset(net::URLFetcher::Create(url_
, net::URLFetcher::GET
, this));
25 url_fetcher_
->SetRequestContext(profile
->GetRequestContext());
26 url_fetcher_
->Start();
29 // Methods inherited from URLFetcherDelegate.
31 void BitmapFetcher::OnURLFetchComplete(const net::URLFetcher
* source
) {
32 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
34 if (source
->GetStatus().status() != net::URLRequestStatus::SUCCESS
) {
39 std::string image_data
;
40 source
->GetResponseAsString(&image_data
);
42 new ImageDecoder(this, image_data
, ImageDecoder::DEFAULT_CODEC
);
44 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded
45 // with the data when it is done.
46 scoped_refptr
<base::MessageLoopProxy
> task_runner
=
47 content::BrowserThread::GetMessageLoopProxyForThread(
48 content::BrowserThread::UI
);
49 image_decoder_
->Start(task_runner
);
52 void BitmapFetcher::OnURLFetchDownloadProgress(const net::URLFetcher
* source
,
58 // Methods inherited from ImageDecoder::Delegate.
60 void BitmapFetcher::OnImageDecoded(const ImageDecoder
* decoder
,
61 const SkBitmap
& decoded_image
) {
62 // Make a copy of the bitmap which we pass back to the UI thread.
63 scoped_ptr
<SkBitmap
> bitmap(new SkBitmap());
64 decoded_image
.deepCopyTo(bitmap
.get(), decoded_image
.getConfig());
67 delegate_
->OnFetchComplete(url_
, bitmap
.get());
70 void BitmapFetcher::OnDecodeImageFailed(const ImageDecoder
* decoder
) {
74 void BitmapFetcher::ReportFailure() {
75 delegate_
->OnFetchComplete(url_
, NULL
);