Chromium Code Reviews| 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..08abca26d3463029ea5c8ffa3972debff9685393 |
| --- /dev/null |
| +++ b/chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.cc |
| @@ -0,0 +1,132 @@ |
| +// 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, |
|
dcheng
2013/05/22 19:38:35
url doesn't seem to be used.
Pete Williamson
2013/05/23 16:47:29
url is assigned to url_, which we fetch. url_, ho
|
| + scoped_ptr<net::URLFetcher>& url_fetcher, |
| + scoped_refptr<base::MessageLoopProxy> task_runner) : |
|
dcheng
2013/05/22 19:38:35
task_runner doesn't seem to be used.
Pete Williamson
2013/05/23 16:47:29
removed.
|
| + url_(url), image_ready_(false), image_failed_(false), |
| + url_fetcher_(url_fetcher.Pass()), task_runner_(task_runner), |
| + bitmap_(NULL) {} |
|
dcheng
2013/05/22 19:38:35
Explicit initialization of scoped_ptr with NULL wo
Pete Williamson
2013/05/23 16:47:29
Done.
|
| + |
| +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() { |
| + url_fetcher_->Start(); |
| +} |
| + |
| +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. |
|
dcheng
2013/05/22 19:38:35
When in doubt, look at how other code does it =)
Pete Williamson
2013/05/23 16:47:29
The whole point of this is to get off the Browser
dcheng
2013/05/23 19:32:25
The security that this provides comes from the fac
Pete Williamson
2013/05/24 22:18:06
Oh, I thought this task runner was telling the Ima
|
| + 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()); |
| + progress_total_ = total; |
| + progress_current_ = current; |
| +} |
| + |
| +// 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 |