1 // Copyright 2013 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/notifications/sync_notifier/notification_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 NotificationBitmapFetcher::NotificationBitmapFetcher(
16 NotificationBitmapFetcherDelegate
* delegate
)
17 : url_(url
), delegate_(delegate
) {}
19 NotificationBitmapFetcher::~NotificationBitmapFetcher() {}
21 void NotificationBitmapFetcher::Start(Profile
* profile
) {
23 net::URLFetcher::Create(url_
, net::URLFetcher::GET
, this));
24 // The RequestContext is coming from the current profile.
25 // TODO(petewil): Make sure this is the right profile to use.
26 // It seems to work, but we might prefer to use a blank profile with
28 url_fetcher_
->SetRequestContext(profile
->GetRequestContext());
29 url_fetcher_
->Start();
32 // Methods inherited from URLFetcherDelegate.
34 void NotificationBitmapFetcher::OnURLFetchComplete(
35 const net::URLFetcher
* source
) {
36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
38 if (source
->GetStatus().status() != net::URLRequestStatus::SUCCESS
) {
39 OnDecodeImageFailed(NULL
);
43 std::string image_data
;
44 source
->GetResponseAsString(&image_data
);
45 // Create an ImageDecoder with the data and assign it to the refptr.
46 image_decoder_
= new ImageDecoder(this, image_data
,
47 ImageDecoder::DEFAULT_CODEC
);
49 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded
50 // with the data when it is done.
51 scoped_refptr
<base::MessageLoopProxy
> task_runner
=
52 content::BrowserThread::GetMessageLoopProxyForThread(
53 content::BrowserThread::UI
);
54 image_decoder_
->Start(task_runner
);
57 void NotificationBitmapFetcher::OnURLFetchDownloadProgress(
58 const net::URLFetcher
* source
, int64 current
, int64 total
) {
62 // Methods inherited from ImageDecoder::Delegate.
64 void NotificationBitmapFetcher::OnImageDecoded(
65 const ImageDecoder
* decoder
, const SkBitmap
& decoded_image
) {
66 // Make a copy of the bitmap which we pass back to the UI thread.
67 bitmap_
.reset(new SkBitmap());
68 decoded_image
.deepCopyTo(bitmap_
.get(), decoded_image
.getConfig());
71 delegate_
->OnFetchComplete(url_
, bitmap_
.get());
74 void NotificationBitmapFetcher::OnDecodeImageFailed(
75 const ImageDecoder
* decoder
) {
78 delegate_
->OnFetchComplete(url_
, NULL
);
81 } // namespace notifier