Index: chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.cc |
diff --git a/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.cc b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..06617029dc52395b88c62f1aac6d677637ed7847 |
--- /dev/null |
+++ b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.cc |
@@ -0,0 +1,135 @@ |
+// Copyright (c) 2013 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/notifications/sync_notifier/notification_bitmap_fetcher.h" |
+ |
+#include "chrome/common/chrome_notification_types.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/notification_details.h" |
+#include "content/public/browser/notification_service.h" |
+#include "net/url_request/url_fetcher.h" |
+ |
+namespace notifier { |
+ |
+NotificationBitmapFetcher::NotificationBitmapFetcher(GURL& url) |
+ : url_(url), image_ready_(false), image_failed_(false) {} |
+ |
+NotificationBitmapFetcher::~NotificationBitmapFetcher() {} |
+ |
+bool NotificationBitmapFetcher::image_ready() { |
+ return image_ready_; |
+} |
+ |
+bool NotificationBitmapFetcher::image_failed() { |
+ return image_failed_; |
+} |
+ |
+SkBitmap* NotificationBitmapFetcher::bitmap() { |
+ return bitmap_.get(); |
+} |
+ |
+void NotificationBitmapFetcher::StartImageFetch() { |
+ if (url_fetcher_ == NULL) |
dcheng
2013/05/23 19:32:25
Why check for NULL here? Is it permissible to call
Pete Williamson
2013/05/24 22:18:06
This is to work with the SetURLFetcherForTest() fu
|
+ url_fetcher_.reset( |
+ net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); |
+ url_fetcher_->Start(); |
+} |
+ |
+void NotificationBitmapFetcher::SetURLFetcherForTest( |
+ scoped_ptr<net::URLFetcher>& url_fetcher) { |
+ url_fetcher_ = url_fetcher.Pass(); |
+} |
+ |
+void NotificationBitmapFetcher::HandleImageDecoded( |
+ scoped_ptr<SkBitmap> bitmap) { |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ bitmap_ = bitmap.Pass(); |
+ image_ready_ = true; |
+ |
+ // Notify observers that the fetch is done. |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, |
+ content::NotificationService::AllSources(), |
+ content::NotificationDetails()); |
+} |
+ |
+void NotificationBitmapFetcher::HandleImageFailed() { |
+ // Mark the image as failed so we don't keep waiting for it. |
+ image_failed_ = true; |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, |
+ content::NotificationService::AllSources(), |
+ content::NotificationDetails()); |
+} |
+ |
+// Methods inherited from URLFetcherDelegate. |
+ |
+void NotificationBitmapFetcher::OnURLFetchComplete( |
+ const net::URLFetcher* source) { |
+ std::string image_data; |
+ |
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ // Copy the data into the string. Keep in mind it may have embedded nulls. |
+ source->GetResponseAsString(&image_data); |
+ |
+ // Handle fetch failure. If it failed, set failed to true, and fire |
+ // notification to listeners. |
+ if (image_data.length() == 0) { |
+ image_failed_ = true; |
+ content::NotificationService::current()->Notify( |
+ chrome::NOTIFICATION_NOTIFY_BITMAP_FETCH_COMPLETE, |
+ content::NotificationService::AllSources(), |
+ content::NotificationDetails()); |
+ } |
+ |
+ // Create an ImageDecoder with the data and assign it to the refptr. |
+ image_decoder_ = new ImageDecoder(this, image_data, |
+ ImageDecoder::DEFAULT_CODEC); |
+ |
+ // Call start to begin decoding. The ImageDecoder will call OnImageDecoded |
+ // with the data when it is done. |
+ // TODO(reviewers): what is the proper thread to use? I picked IO arbitrarily. |
+ scoped_refptr<base::MessageLoopProxy> task_runner = |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::IO); |
+ image_decoder_->Start(task_runner); |
+} |
+ |
+void NotificationBitmapFetcher::OnURLFetchDownloadProgress( |
+ const net::URLFetcher* source, int64 current, int64 total) { |
+ // Do nothing here other than save the values for debugging. |
+ DCHECK(source == url_fetcher_.get()); |
dcheng
2013/05/23 19:32:25
DCHECK_EQ
Pete Williamson
2013/05/24 22:18:06
Removed the check
|
+ progress_total_ = total; |
+ progress_current_ = current; |
dcheng
2013/05/23 19:32:25
It doesn't seem like anything uses these values ou
Pete Williamson
2013/05/24 22:18:06
Done.
|
+} |
+ |
+// Methods inherited from ImageDecoder::Delegate. |
+ |
+// This comes in on another thread, so we send the bitmap back to the UI |
+// thread so that the object stays thread safe. |
+void NotificationBitmapFetcher::OnImageDecoded( |
+ const ImageDecoder* decoder, const SkBitmap& decoded_image) { |
+ // Make a copy of the bitmap which we pass back to the UI thread. |
+ scoped_ptr<SkBitmap> bitmap(new SkBitmap()); |
+ decoded_image.deepCopyTo(bitmap.get(), decoded_image.getConfig()); |
+ |
+ // Post to the UI thread. |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&NotificationBitmapFetcher::HandleImageDecoded, this, |
+ base::Passed(&bitmap))); |
+} |
+ |
+void NotificationBitmapFetcher::OnDecodeImageFailed( |
+ const ImageDecoder* decoder) { |
+ |
+ // Post to the UI thread. |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&NotificationBitmapFetcher::HandleImageFailed, this)); |
+} |
+ |
+} // namespace notifier |