Index: chrome/browser/notifications/message_center_notification_manager.cc |
diff --git a/chrome/browser/notifications/message_center_notification_manager.cc b/chrome/browser/notifications/message_center_notification_manager.cc |
index d2cc43f32347774ff8952ee70219432f3be48614..b3d1aeba0e580ab921773c5654e4daa73b966031 100644 |
--- a/chrome/browser/notifications/message_center_notification_manager.cc |
+++ b/chrome/browser/notifications/message_center_notification_manager.cc |
@@ -163,16 +163,9 @@ bool MessageCenterNotificationManager::UpdateNotification( |
old_notification->notification().Close(false); // Not by user. |
delete old_notification; |
profile_notifications_.erase(old_id); |
- ProfileNotification* new_notification = |
- new ProfileNotification(profile, notification, message_center_); |
+ ProfileNotification* new_notification = new ProfileNotification( |
+ profile, notification, message_center_, old_id); |
profile_notifications_[notification.notification_id()] = new_notification; |
- message_center_->UpdateNotification(old_id, |
- notification.notification_id(), |
- notification.title(), |
- notification.body(), |
- notification.optional_fields(), |
- notification.delegate()); |
- new_notification->StartDownloads(); |
return true; |
} |
} |
@@ -266,77 +259,104 @@ void MessageCenterNotificationManager::OnNotificationRemoved( |
// ImageDownloads |
MessageCenterNotificationManager::ImageDownloads::ImageDownloads( |
- message_center::MessageCenter* message_center, |
+ message_center::Notification* notification, |
ImageDownloadsObserver* observer) |
- : message_center_(message_center), |
+ : message_center_notification_(notification), |
pending_downloads_(0), |
- observer_(observer) { |
-} |
+ observer_(observer) {} |
MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { } |
void MessageCenterNotificationManager::ImageDownloads::StartDownloads( |
const Notification& notification) { |
+ |
// In case all downloads are synchronous, assume a pending download. |
AddPendingDownload(); |
- // Notification primary icon. |
- StartDownloadWithImage( |
- notification, |
- ¬ification.icon(), |
- notification.icon_url(), |
- message_center::kNotificationIconSize, |
- base::Bind(&message_center::MessageCenter::SetNotificationIcon, |
- base::Unretained(message_center_), |
- notification.notification_id())); |
+ content::RenderViewHost* host = notification.GetRenderViewHost(); |
+ if (!host) { |
+ DVLOG(1) << "Unable to download images due to a missing RenderViewHost."; |
+ PendingDownloadCompleted(); |
+ return; |
+ } |
+ |
+ if (notification.icon().IsEmpty()) { |
+ // Notification primary icon. |
+ StartDownloadByURL(host, |
+ notification.icon_url(), |
+ message_center::kNotificationIconSize, |
+ base::Bind(&ImageDownloads::SetIcon, AsWeakPtr())); |
+ } else { |
+ SetIcon(notification.icon()); |
+ } |
+ |
+ const base::DictionaryValue* optional_fields = notification.optional_fields(); |
+ if (!optional_fields) { |
+ // Unable to download anything else because no URLs were specified in |
+ // optional_fields. |
+ PendingDownloadCompleted(); |
+ return; |
+ } |
// Notification image. |
- StartDownloadByKey( |
- notification, |
- message_center::kImageUrlKey, |
- message_center::kNotificationPreferredImageSize, |
- base::Bind(&message_center::MessageCenter::SetNotificationImage, |
- base::Unretained(message_center_), |
- notification.notification_id())); |
+ StartDownloadByKey(host, |
+ optional_fields, |
+ message_center::kImageUrlKey, |
+ message_center::kNotificationPreferredImageSize, |
+ base::Bind(&ImageDownloads::SetImage, AsWeakPtr())); |
// Notification button icons. |
StartDownloadByKey( |
- notification, |
+ host, |
+ optional_fields, |
message_center::kButtonOneIconUrlKey, |
message_center::kNotificationButtonIconSize, |
- base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, |
- base::Unretained(message_center_), |
- notification.notification_id(), 0)); |
+ base::Bind(&ImageDownloads::SetButtonIcon, AsWeakPtr(), 0)); |
StartDownloadByKey( |
- notification, message_center::kButtonTwoIconUrlKey, |
+ host, |
+ optional_fields, |
+ message_center::kButtonTwoIconUrlKey, |
message_center::kNotificationButtonIconSize, |
- base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, |
- base::Unretained(message_center_), |
- notification.notification_id(), 1)); |
+ base::Bind(&ImageDownloads::SetButtonIcon, AsWeakPtr(), 1)); |
// This should tell the observer we're done if everything was synchronous. |
PendingDownloadCompleted(); |
} |
-void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( |
- const Notification& notification, |
- const gfx::Image* image, |
- const GURL& url, |
+void MessageCenterNotificationManager::ImageDownloads::StartDownloadByKey( |
+ content::RenderViewHost* host, |
+ const base::DictionaryValue* optional_fields, |
+ const char* key, |
int size, |
const SetImageCallback& callback) { |
- // Set the image directly if we have it. |
- if (image && !image->IsEmpty()) { |
- callback.Run(*image); |
+ DCHECK(optional_fields); |
+ DCHECK(key); |
+ |
+ if (!optional_fields->HasKey(key)) { |
return; |
} |
+ string16 url_str; |
+ optional_fields->GetString(key, &url_str); |
+ |
+ GURL url(url_str); |
+ |
// Leave the image null if there's no URL. |
- if (url.is_empty()) |
+ if (!url.is_valid()) |
return; |
- content::RenderViewHost* host = notification.GetRenderViewHost(); |
+ StartDownloadByURL(host, url, size, callback); |
+} |
+ |
+void MessageCenterNotificationManager::ImageDownloads::StartDownloadByURL( |
+ content::RenderViewHost* host, |
+ const GURL& url, |
+ int size, |
+ const SetImageCallback& callback) { |
+ |
if (!host) { |
LOG(WARNING) << "Notification needs an image but has no RenderViewHost"; |
+ callback.Run(gfx::Image()); |
return; |
} |
@@ -344,6 +364,7 @@ void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( |
content::WebContents::FromRenderViewHost(host); |
if (!contents) { |
LOG(WARNING) << "Notification needs an image but has no WebContents"; |
+ callback.Run(gfx::Image()); |
return; |
} |
@@ -359,19 +380,6 @@ void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( |
callback)); |
} |
-void MessageCenterNotificationManager::ImageDownloads::StartDownloadByKey( |
- const Notification& notification, |
- const char* key, |
- int size, |
- const SetImageCallback& callback) { |
- const base::DictionaryValue* optional_fields = notification.optional_fields(); |
- if (optional_fields && optional_fields->HasKey(key)) { |
- string16 url; |
- optional_fields->GetString(key, &url); |
- StartDownloadWithImage(notification, NULL, GURL(url), size, callback); |
- } |
-} |
- |
void MessageCenterNotificationManager::ImageDownloads::DownloadComplete( |
const SetImageCallback& callback, |
int download_id, |
@@ -379,12 +387,12 @@ void MessageCenterNotificationManager::ImageDownloads::DownloadComplete( |
const GURL& image_url, |
int requested_size, |
const std::vector<SkBitmap>& bitmaps) { |
- PendingDownloadCompleted(); |
+ if (!bitmaps.empty()) { |
+ gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]); |
+ callback.Run(image); |
+ } |
- if (bitmaps.empty()) |
- return; |
- gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]); |
- callback.Run(image); |
+ PendingDownloadCompleted(); |
} |
// Private methods. |
@@ -400,6 +408,20 @@ MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() { |
observer_->OnDownloadsCompleted(); |
} |
+void MessageCenterNotificationManager::ImageDownloads::SetIcon( |
+ const gfx::Image& image) { |
+ message_center_notification_->set_icon(image); |
+} |
+void MessageCenterNotificationManager::ImageDownloads::SetImage( |
+ const gfx::Image& image) { |
+ message_center_notification_->set_image(image); |
+} |
+void MessageCenterNotificationManager::ImageDownloads::SetButtonIcon( |
+ size_t button_number, |
+ const gfx::Image& image) { |
+ message_center_notification_->SetButtonIcon(button_number, image); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// ProfileNotification |
@@ -409,20 +431,78 @@ MessageCenterNotificationManager::ProfileNotification::ProfileNotification( |
message_center::MessageCenter* message_center) |
: profile_(profile), |
notification_(notification), |
- downloads_(new ImageDownloads(message_center, this)) { |
- DCHECK(profile); |
+ message_center_(message_center), |
+ update_(false), |
+ updated_id_(std::string()), |
+ downloads_(NULL) { |
+ Init(); |
+} |
+ |
+MessageCenterNotificationManager::ProfileNotification::ProfileNotification( |
+ Profile* profile, |
+ const Notification& notification, |
+ message_center::MessageCenter* message_center, |
+ const std::string& updated_id) |
+ : profile_(profile), |
+ notification_(notification), |
+ message_center_(message_center), |
+ update_(true), |
+ updated_id_(updated_id), |
+ downloads_(NULL) { |
+ Init(); |
+} |
+ |
+void MessageCenterNotificationManager::ProfileNotification::Init() { |
+ DCHECK(profile_); |
+ |
+ bool has_rich_notification = notification_.has_rich_notification(); |
+ if (has_rich_notification) { |
+ const message_center::RichNotificationData& data = |
+ notification_.rich_notification_data(); |
+ message_center_notification_.reset( |
+ new message_center::Notification(notification_.type(), |
+ notification_.notification_id(), |
+ notification_.title(), |
+ notification_.body(), |
+ notification_.display_source(), |
+ GetExtensionId(), |
+ data, |
+ notification_.delegate())); |
+ OnDownloadsCompleted(); |
+ } else { |
+ message_center_notification_.reset( |
+ new message_center::Notification(notification_.type(), |
+ notification_.notification_id(), |
+ notification_.title(), |
+ notification_.body(), |
+ notification_.icon(), |
+ notification_.display_source(), |
+ GetExtensionId(), |
+ notification_.optional_fields(), |
+ notification_.delegate())); |
+ StartDownloads(); |
+ } |
} |
MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() { |
} |
void MessageCenterNotificationManager::ProfileNotification::StartDownloads() { |
+ DCHECK(message_center_notification_.get()); |
+ downloads_.reset( |
+ new ImageDownloads(message_center_notification_.get(), this)); |
downloads_->StartDownloads(notification_); |
} |
void |
MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() { |
notification_.DoneRendering(); |
+ if (update_) { |
+ message_center_->UpdateNotification(updated_id_, |
Jun Mukai
2013/05/31 17:08:10
It seems that UpdateNotification will still be cal
|
+ message_center_notification_.Pass()); |
+ } else { |
+ message_center_->AddNotification(message_center_notification_.Pass()); |
+ } |
} |
std::string |
@@ -455,15 +535,6 @@ void MessageCenterNotificationManager::AddProfileNotification( |
DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
profile_notifications_[id] = profile_notification; |
- message_center_->AddNotification(notification.type(), |
- notification.notification_id(), |
- notification.title(), |
- notification.body(), |
- notification.display_source(), |
- profile_notification->GetExtensionId(), |
- notification.optional_fields(), |
- notification.delegate()); |
- profile_notification->StartDownloads(); |
} |
void MessageCenterNotificationManager::RemoveProfileNotification( |