OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher
.h" |
| 6 |
| 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "net/url_request/url_fetcher.h" |
| 9 |
| 10 namespace notifier { |
| 11 |
| 12 NotificationBitmapFetcher::NotificationBitmapFetcher( |
| 13 const GURL& url, |
| 14 NotificationBitmapFetcherDelegate* delegate) |
| 15 : url_(url), delegate_(delegate) {} |
| 16 |
| 17 NotificationBitmapFetcher::~NotificationBitmapFetcher() {} |
| 18 |
| 19 void NotificationBitmapFetcher::Start() { |
| 20 if (url_fetcher_ == NULL) { |
| 21 url_fetcher_.reset( |
| 22 net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); |
| 23 } |
| 24 url_fetcher_->Start(); |
| 25 } |
| 26 |
| 27 void NotificationBitmapFetcher::SetURLFetcherForTest( |
| 28 scoped_ptr<net::URLFetcher> url_fetcher) { |
| 29 url_fetcher_ = url_fetcher.Pass(); |
| 30 } |
| 31 |
| 32 // Methods inherited from URLFetcherDelegate. |
| 33 |
| 34 void NotificationBitmapFetcher::OnURLFetchComplete( |
| 35 const net::URLFetcher* source) { |
| 36 std::string image_data; |
| 37 |
| 38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 39 |
| 40 // Copy the data into the string. Keep in mind it may have embedded nulls. |
| 41 source->GetResponseAsString(&image_data); |
| 42 |
| 43 // Handle fetch failure. If it failed, set failed to true, and fire |
| 44 // notification to listeners. |
| 45 if (image_data.length() == 0) { |
| 46 OnDecodeImageFailed(NULL); |
| 47 return; |
| 48 } |
| 49 |
| 50 // Create an ImageDecoder with the data and assign it to the refptr. |
| 51 image_decoder_ = new ImageDecoder(this, image_data, |
| 52 ImageDecoder::DEFAULT_CODEC); |
| 53 |
| 54 // Call start to begin decoding. The ImageDecoder will call OnImageDecoded |
| 55 // with the data when it is done. |
| 56 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 57 content::BrowserThread::GetMessageLoopProxyForThread( |
| 58 content::BrowserThread::UI); |
| 59 image_decoder_->Start(task_runner); |
| 60 } |
| 61 |
| 62 void NotificationBitmapFetcher::OnURLFetchDownloadProgress( |
| 63 const net::URLFetcher* source, int64 current, int64 total) { |
| 64 // Do nothing here. |
| 65 } |
| 66 |
| 67 // Methods inherited from ImageDecoder::Delegate. |
| 68 |
| 69 void NotificationBitmapFetcher::OnImageDecoded( |
| 70 const ImageDecoder* decoder, const SkBitmap& decoded_image) { |
| 71 // Make a copy of the bitmap which we pass back to the UI thread. |
| 72 bitmap_.reset(new SkBitmap()); |
| 73 decoded_image.deepCopyTo(bitmap_.get(), decoded_image.getConfig()); |
| 74 |
| 75 // Report success. |
| 76 delegate_->OnFetchComplete(bitmap_.get()); |
| 77 } |
| 78 |
| 79 void NotificationBitmapFetcher::OnDecodeImageFailed( |
| 80 const ImageDecoder* decoder) { |
| 81 |
| 82 // Report failure. |
| 83 delegate_->OnFetchComplete(NULL); |
| 84 } |
| 85 |
| 86 } // namespace notifier |
OLD | NEW |