| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/notifications/message_center_notification_manager.h" | 5 #include "chrome/browser/notifications/message_center_notification_manager.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "chrome/browser/extensions/extension_info_map.h" | 10 #include "chrome/browser/extensions/extension_info_map.h" |
| 11 #include "chrome/browser/extensions/extension_system.h" | 11 #include "chrome/browser/extensions/extension_system.h" |
| 12 #include "chrome/browser/notifications/desktop_notification_service.h" | 12 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 13 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 13 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 14 #include "chrome/browser/notifications/message_center_settings_controller.h" | 14 #include "chrome/browser/notifications/message_center_settings_controller.h" |
| 15 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 18 #include "chrome/browser/ui/chrome_pages.h" | 18 #include "chrome/browser/ui/chrome_pages.h" |
| 19 #include "chrome/browser/ui/host_desktop.h" | 19 #include "chrome/browser/ui/host_desktop.h" |
| 20 #include "chrome/common/extensions/extension_set.h" | 20 #include "chrome/common/extensions/extension_set.h" |
| 21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/common/url_constants.h" | 23 #include "content/public/common/url_constants.h" |
| 24 #include "ui/message_center/message_center_style.h" | 24 #include "ui/message_center/message_center_style.h" |
| 25 #include "ui/message_center/message_center_tray.h" | 25 #include "ui/message_center/message_center_tray.h" |
| 26 #include "ui/message_center/notifier_settings.h" | 26 #include "ui/message_center/notifier_settings.h" |
| 27 | 27 |
| 28 MessageCenterNotificationManager::MessageCenterNotificationManager( | 28 MessageCenterNotificationManager::MessageCenterNotificationManager( |
| 29 message_center::MessageCenter* message_center) | 29 message_center::MessageCenter* message_center) |
| 30 : message_center_(message_center), | 30 : message_center_(message_center), |
| 31 settings_controller_(new MessageCenterSettingsController) { | 31 settings_controller_(new MessageCenterSettingsController) { |
| 32 message_center_->SetDelegate(this); | 32 message_center_->SetDelegate(this); |
| 33 message_center_->AddObserver(this); | 33 message_center_->AddObserver(this); |
| 34 | 34 |
| 35 #if defined(OS_WIN) || defined(OS_MACOSX) | 35 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 36 // On Windows and Mac, the notification manager owns the tray icon and views. | 36 // On Windows and Mac, the notification manager owns the tray icon and views. |
| 37 // Other platforms have global ownership and Create will return NULL. | 37 // Other platforms have global ownership and Create will return NULL. |
| 38 tray_.reset(message_center::CreateMessageCenterTray()); | 38 tray_.reset(message_center::CreateMessageCenterTray()); |
| 39 #endif | 39 #endif |
| 40 } | 40 } |
| 41 | 41 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 DCHECK(message_center_->HasNotification(old_id)); | 159 DCHECK(message_center_->HasNotification(old_id)); |
| 160 | 160 |
| 161 // Add/remove notification in the local list but just update the same | 161 // Add/remove notification in the local list but just update the same |
| 162 // one in MessageCenter. | 162 // one in MessageCenter. |
| 163 old_notification->notification().Close(false); // Not by user. | 163 old_notification->notification().Close(false); // Not by user. |
| 164 delete old_notification; | 164 delete old_notification; |
| 165 profile_notifications_.erase(old_id); | 165 profile_notifications_.erase(old_id); |
| 166 ProfileNotification* new_notification = | 166 ProfileNotification* new_notification = |
| 167 new ProfileNotification(profile, notification, message_center_); | 167 new ProfileNotification(profile, notification, message_center_); |
| 168 profile_notifications_[notification.notification_id()] = new_notification; | 168 profile_notifications_[notification.notification_id()] = new_notification; |
| 169 |
| 170 // Now pass a copy to message center. |
| 171 scoped_ptr<message_center::Notification> message_center_notification( |
| 172 make_scoped_ptr(new message_center::Notification(notification))); |
| 173 message_center_notification->set_extension_id( |
| 174 new_notification->GetExtensionId()); |
| 169 message_center_->UpdateNotification(old_id, | 175 message_center_->UpdateNotification(old_id, |
| 170 notification.notification_id(), | 176 message_center_notification.Pass()); |
| 171 notification.title(), | 177 |
| 172 notification.body(), | |
| 173 notification.optional_fields(), | |
| 174 notification.delegate()); | |
| 175 new_notification->StartDownloads(); | 178 new_notification->StartDownloads(); |
| 176 return true; | 179 return true; |
| 177 } | 180 } |
| 178 } | 181 } |
| 179 return false; | 182 return false; |
| 180 } | 183 } |
| 181 | 184 |
| 182 //////////////////////////////////////////////////////////////////////////////// | 185 //////////////////////////////////////////////////////////////////////////////// |
| 183 // MessageCenter::Delegate | 186 // MessageCenter::Delegate |
| 184 | 187 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 StartDownloadWithImage( | 287 StartDownloadWithImage( |
| 285 notification, | 288 notification, |
| 286 ¬ification.icon(), | 289 ¬ification.icon(), |
| 287 notification.icon_url(), | 290 notification.icon_url(), |
| 288 message_center::kNotificationIconSize, | 291 message_center::kNotificationIconSize, |
| 289 base::Bind(&message_center::MessageCenter::SetNotificationIcon, | 292 base::Bind(&message_center::MessageCenter::SetNotificationIcon, |
| 290 base::Unretained(message_center_), | 293 base::Unretained(message_center_), |
| 291 notification.notification_id())); | 294 notification.notification_id())); |
| 292 | 295 |
| 293 // Notification image. | 296 // Notification image. |
| 294 StartDownloadByKey( | 297 StartDownloadWithImage( |
| 295 notification, | 298 notification, |
| 296 message_center::kImageUrlKey, | 299 NULL, |
| 300 notification.image_url(), |
| 297 message_center::kNotificationPreferredImageSize, | 301 message_center::kNotificationPreferredImageSize, |
| 298 base::Bind(&message_center::MessageCenter::SetNotificationImage, | 302 base::Bind(&message_center::MessageCenter::SetNotificationImage, |
| 299 base::Unretained(message_center_), | 303 base::Unretained(message_center_), |
| 300 notification.notification_id())); | 304 notification.notification_id())); |
| 301 | 305 |
| 302 // Notification button icons. | 306 // Notification button icons. |
| 303 StartDownloadByKey( | 307 StartDownloadWithImage( |
| 304 notification, | 308 notification, |
| 305 message_center::kButtonOneIconUrlKey, | 309 NULL, |
| 310 notification.button_one_icon_url(), |
| 306 message_center::kNotificationButtonIconSize, | 311 message_center::kNotificationButtonIconSize, |
| 307 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, | 312 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, |
| 308 base::Unretained(message_center_), | 313 base::Unretained(message_center_), |
| 309 notification.notification_id(), 0)); | 314 notification.notification_id(), |
| 310 StartDownloadByKey( | 315 0)); |
| 311 notification, message_center::kButtonTwoIconUrlKey, | 316 StartDownloadWithImage( |
| 317 notification, |
| 318 NULL, |
| 319 notification.button_two_icon_url(), |
| 312 message_center::kNotificationButtonIconSize, | 320 message_center::kNotificationButtonIconSize, |
| 313 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, | 321 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, |
| 314 base::Unretained(message_center_), | 322 base::Unretained(message_center_), |
| 315 notification.notification_id(), 1)); | 323 notification.notification_id(), |
| 324 1)); |
| 316 | 325 |
| 317 // This should tell the observer we're done if everything was synchronous. | 326 // This should tell the observer we're done if everything was synchronous. |
| 318 PendingDownloadCompleted(); | 327 PendingDownloadCompleted(); |
| 319 } | 328 } |
| 320 | 329 |
| 321 void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( | 330 void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( |
| 322 const Notification& notification, | 331 const Notification& notification, |
| 323 const gfx::Image* image, | 332 const gfx::Image* image, |
| 324 const GURL& url, | 333 const GURL& url, |
| 325 int size, | 334 int size, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 352 contents->DownloadImage( | 361 contents->DownloadImage( |
| 353 url, | 362 url, |
| 354 false, | 363 false, |
| 355 size, | 364 size, |
| 356 base::Bind( | 365 base::Bind( |
| 357 &MessageCenterNotificationManager::ImageDownloads::DownloadComplete, | 366 &MessageCenterNotificationManager::ImageDownloads::DownloadComplete, |
| 358 AsWeakPtr(), | 367 AsWeakPtr(), |
| 359 callback)); | 368 callback)); |
| 360 } | 369 } |
| 361 | 370 |
| 362 void MessageCenterNotificationManager::ImageDownloads::StartDownloadByKey( | |
| 363 const Notification& notification, | |
| 364 const char* key, | |
| 365 int size, | |
| 366 const SetImageCallback& callback) { | |
| 367 const base::DictionaryValue* optional_fields = notification.optional_fields(); | |
| 368 if (optional_fields && optional_fields->HasKey(key)) { | |
| 369 string16 url; | |
| 370 optional_fields->GetString(key, &url); | |
| 371 StartDownloadWithImage(notification, NULL, GURL(url), size, callback); | |
| 372 } | |
| 373 } | |
| 374 | |
| 375 void MessageCenterNotificationManager::ImageDownloads::DownloadComplete( | 371 void MessageCenterNotificationManager::ImageDownloads::DownloadComplete( |
| 376 const SetImageCallback& callback, | 372 const SetImageCallback& callback, |
| 377 int download_id, | 373 int download_id, |
| 378 int http_status_code, | 374 int http_status_code, |
| 379 const GURL& image_url, | 375 const GURL& image_url, |
| 380 int requested_size, | 376 int requested_size, |
| 381 const std::vector<SkBitmap>& bitmaps) { | 377 const std::vector<SkBitmap>& bitmaps) { |
| 382 PendingDownloadCompleted(); | 378 PendingDownloadCompleted(); |
| 383 | 379 |
| 384 if (bitmaps.empty()) | 380 if (bitmaps.empty()) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 // private | 444 // private |
| 449 | 445 |
| 450 void MessageCenterNotificationManager::AddProfileNotification( | 446 void MessageCenterNotificationManager::AddProfileNotification( |
| 451 ProfileNotification* profile_notification) { | 447 ProfileNotification* profile_notification) { |
| 452 const Notification& notification = profile_notification->notification(); | 448 const Notification& notification = profile_notification->notification(); |
| 453 std::string id = notification.notification_id(); | 449 std::string id = notification.notification_id(); |
| 454 // Notification ids should be unique. | 450 // Notification ids should be unique. |
| 455 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); | 451 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
| 456 profile_notifications_[id] = profile_notification; | 452 profile_notifications_[id] = profile_notification; |
| 457 | 453 |
| 458 message_center_->AddNotification(notification.type(), | 454 // Create the copy for message center, and ensure the extension ID is correct. |
| 459 notification.notification_id(), | 455 scoped_ptr<message_center::Notification> message_center_notification( |
| 460 notification.title(), | 456 new message_center::Notification(notification)); |
| 461 notification.body(), | 457 message_center_notification->set_extension_id( |
| 462 notification.display_source(), | 458 profile_notification->GetExtensionId()); |
| 463 profile_notification->GetExtensionId(), | 459 message_center_->AddNotification(message_center_notification.Pass()); |
| 464 notification.optional_fields(), | 460 |
| 465 notification.delegate()); | |
| 466 profile_notification->StartDownloads(); | 461 profile_notification->StartDownloads(); |
| 467 } | 462 } |
| 468 | 463 |
| 469 void MessageCenterNotificationManager::RemoveProfileNotification( | 464 void MessageCenterNotificationManager::RemoveProfileNotification( |
| 470 ProfileNotification* profile_notification, | 465 ProfileNotification* profile_notification, |
| 471 bool by_user) { | 466 bool by_user) { |
| 472 profile_notification->notification().Close(by_user); | 467 profile_notification->notification().Close(by_user); |
| 473 std::string id = profile_notification->notification().notification_id(); | 468 std::string id = profile_notification->notification().notification_id(); |
| 474 profile_notifications_.erase(id); | 469 profile_notifications_.erase(id); |
| 475 delete profile_notification; | 470 delete profile_notification; |
| 476 } | 471 } |
| 477 | 472 |
| 478 MessageCenterNotificationManager::ProfileNotification* | 473 MessageCenterNotificationManager::ProfileNotification* |
| 479 MessageCenterNotificationManager::FindProfileNotification( | 474 MessageCenterNotificationManager::FindProfileNotification( |
| 480 const std::string& id) const { | 475 const std::string& id) const { |
| 481 NotificationMap::const_iterator iter = profile_notifications_.find(id); | 476 NotificationMap::const_iterator iter = profile_notifications_.find(id); |
| 482 if (iter == profile_notifications_.end()) | 477 if (iter == profile_notifications_.end()) |
| 483 return NULL; | 478 return NULL; |
| 484 | 479 |
| 485 return (*iter).second; | 480 return (*iter).second; |
| 486 } | 481 } |
| OLD | NEW |