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_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 // default message center (extension_id.empty()). | 151 // default message center (extension_id.empty()). |
152 | 152 |
153 // If there exist apps/extensions that have notificationProvider permission, | 153 // If there exist apps/extensions that have notificationProvider permission, |
154 // route notifications to one of the apps/extensions. | 154 // route notifications to one of the apps/extensions. |
155 std::string extension_id = GetExtensionTakingOverNotifications(profile); | 155 std::string extension_id = GetExtensionTakingOverNotifications(profile); |
156 if (!extension_id.empty()) | 156 if (!extension_id.empty()) |
157 profile_notification->AddToAlternateProvider(extension_id); | 157 profile_notification->AddToAlternateProvider(extension_id); |
158 | 158 |
159 message_center_->AddNotification(make_scoped_ptr( | 159 message_center_->AddNotification(make_scoped_ptr( |
160 new message_center::Notification(profile_notification->notification()))); | 160 new message_center::Notification(profile_notification->notification()))); |
161 profile_notification->StartDownloads(); | |
162 } | 161 } |
163 | 162 |
164 bool MessageCenterNotificationManager::Update(const Notification& notification, | 163 bool MessageCenterNotificationManager::Update(const Notification& notification, |
165 Profile* profile) { | 164 Profile* profile) { |
166 const base::string16& replace_id = notification.replace_id(); | 165 const base::string16& replace_id = notification.replace_id(); |
167 if (replace_id.empty()) | 166 if (replace_id.empty()) |
168 return false; | 167 return false; |
169 | 168 |
170 const GURL origin_url = notification.origin_url(); | 169 const GURL origin_url = notification.origin_url(); |
171 DCHECK(origin_url.is_valid()); | 170 DCHECK(origin_url.is_valid()); |
(...skipping 24 matching lines...) Expand all Loading... |
196 // providers. | 195 // providers. |
197 | 196 |
198 // WARNING: You MUST use AddProfileNotification or update the message | 197 // WARNING: You MUST use AddProfileNotification or update the message |
199 // center via the notification within a ProfileNotification object or the | 198 // center via the notification within a ProfileNotification object or the |
200 // profile ID will not be correctly set for ChromeOS. | 199 // profile ID will not be correctly set for ChromeOS. |
201 message_center_->UpdateNotification( | 200 message_center_->UpdateNotification( |
202 old_id, | 201 old_id, |
203 make_scoped_ptr(new message_center::Notification( | 202 make_scoped_ptr(new message_center::Notification( |
204 new_notification->notification()))); | 203 new_notification->notification()))); |
205 | 204 |
206 new_notification->StartDownloads(); | |
207 return true; | 205 return true; |
208 } | 206 } |
209 } | 207 } |
210 return false; | 208 return false; |
211 } | 209 } |
212 | 210 |
213 const Notification* MessageCenterNotificationManager::FindById( | 211 const Notification* MessageCenterNotificationManager::FindById( |
214 const std::string& delegate_id, | 212 const std::string& delegate_id, |
215 ProfileID profile_id) const { | 213 ProfileID profile_id) const { |
216 // The profile pointer can be weak, the instance may have been destroyed, so | 214 // The profile pointer can be weak, the instance may have been destroyed, so |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 } | 353 } |
356 | 354 |
357 std::string | 355 std::string |
358 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest( | 356 MessageCenterNotificationManager::GetMessageCenterNotificationIdForTest( |
359 const std::string& delegate_id, | 357 const std::string& delegate_id, |
360 Profile* profile) { | 358 Profile* profile) { |
361 return GetProfileNotificationId(delegate_id, GetProfileID(profile)); | 359 return GetProfileNotificationId(delegate_id, GetProfileID(profile)); |
362 } | 360 } |
363 | 361 |
364 //////////////////////////////////////////////////////////////////////////////// | 362 //////////////////////////////////////////////////////////////////////////////// |
365 // ImageDownloads | |
366 | |
367 MessageCenterNotificationManager::ImageDownloads::ImageDownloads( | |
368 message_center::MessageCenter* message_center, | |
369 ImageDownloadsObserver* observer) | |
370 : message_center_(message_center), | |
371 pending_downloads_(0), | |
372 observer_(observer) { | |
373 } | |
374 | |
375 MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { } | |
376 | |
377 void MessageCenterNotificationManager::ImageDownloads::StartDownloads( | |
378 const Notification& notification) { | |
379 // In case all downloads are synchronous, assume a pending download. | |
380 AddPendingDownload(); | |
381 | |
382 // Notification image. | |
383 StartDownloadWithImage( | |
384 notification, | |
385 NULL, | |
386 notification.image_url(), | |
387 base::Bind(&message_center::MessageCenter::SetNotificationImage, | |
388 base::Unretained(message_center_), | |
389 notification.id())); | |
390 | |
391 // Notification button icons. | |
392 StartDownloadWithImage( | |
393 notification, | |
394 NULL, | |
395 notification.button_one_icon_url(), | |
396 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, | |
397 base::Unretained(message_center_), | |
398 notification.id(), | |
399 0)); | |
400 StartDownloadWithImage( | |
401 notification, | |
402 NULL, | |
403 notification.button_two_icon_url(), | |
404 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, | |
405 base::Unretained(message_center_), | |
406 notification.id(), | |
407 1)); | |
408 | |
409 // This should tell the observer we're done if everything was synchronous. | |
410 PendingDownloadCompleted(); | |
411 } | |
412 | |
413 void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( | |
414 const Notification& notification, | |
415 const gfx::Image* image, | |
416 const GURL& url, | |
417 const SetImageCallback& callback) { | |
418 // Set the image directly if we have it. | |
419 if (image && !image->IsEmpty()) { | |
420 callback.Run(*image); | |
421 return; | |
422 } | |
423 | |
424 // Leave the image null if there's no URL. | |
425 if (url.is_empty()) | |
426 return; | |
427 | |
428 content::WebContents* contents = notification.delegate()->GetWebContents(); | |
429 if (!contents) { | |
430 LOG(WARNING) << "Notification needs an image but has no WebContents"; | |
431 return; | |
432 } | |
433 | |
434 AddPendingDownload(); | |
435 | |
436 contents->DownloadImage( | |
437 url, | |
438 false, // Not a favicon | |
439 0, // No maximum size | |
440 base::Bind( | |
441 &MessageCenterNotificationManager::ImageDownloads::DownloadComplete, | |
442 AsWeakPtr(), | |
443 callback)); | |
444 } | |
445 | |
446 void MessageCenterNotificationManager::ImageDownloads::DownloadComplete( | |
447 const SetImageCallback& callback, | |
448 int download_id, | |
449 int http_status_code, | |
450 const GURL& image_url, | |
451 const std::vector<SkBitmap>& bitmaps, | |
452 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
453 PendingDownloadCompleted(); | |
454 | |
455 if (bitmaps.empty()) | |
456 return; | |
457 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]); | |
458 callback.Run(image); | |
459 } | |
460 | |
461 // Private methods. | |
462 | |
463 void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() { | |
464 ++pending_downloads_; | |
465 } | |
466 | |
467 void | |
468 MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() { | |
469 DCHECK(pending_downloads_ > 0); | |
470 if (--pending_downloads_ == 0 && observer_) | |
471 observer_->OnDownloadsCompleted(); | |
472 } | |
473 | |
474 //////////////////////////////////////////////////////////////////////////////// | |
475 // ProfileNotification | 363 // ProfileNotification |
476 | 364 |
477 MessageCenterNotificationManager::ProfileNotification::ProfileNotification( | 365 MessageCenterNotificationManager::ProfileNotification::ProfileNotification( |
478 Profile* profile, | 366 Profile* profile, |
479 const Notification& notification, | 367 const Notification& notification, |
480 message_center::MessageCenter* message_center) | 368 message_center::MessageCenter* message_center) |
481 : profile_(profile), | 369 : profile_(profile), |
482 notification_( | 370 notification_( |
483 // Uses Notification's copy constructor to assign the message center | 371 // Uses Notification's copy constructor to assign the message center |
484 // id, which should be unique for every profile + Notification pair. | 372 // id, which should be unique for every profile + Notification pair. |
485 GetProfileNotificationId(notification.delegate_id(), | 373 GetProfileNotificationId(notification.delegate_id(), |
486 GetProfileID(profile)), | 374 GetProfileID(profile)), |
487 notification), | 375 notification) { |
488 downloads_(new ImageDownloads(message_center, this)) { | |
489 DCHECK(profile); | 376 DCHECK(profile); |
490 #if defined(OS_CHROMEOS) | 377 #if defined(OS_CHROMEOS) |
491 notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile)); | 378 notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile)); |
492 #endif | 379 #endif |
493 } | 380 } |
494 | 381 |
495 MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() { | 382 MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() { |
496 } | 383 } |
497 | 384 |
498 void MessageCenterNotificationManager::ProfileNotification::StartDownloads() { | |
499 downloads_->StartDownloads(notification_); | |
500 } | |
501 | |
502 void | |
503 MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() { | |
504 notification_.delegate()->ReleaseRenderViewHost(); | |
505 } | |
506 | |
507 void | 385 void |
508 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider( | 386 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider( |
509 const std::string extension_id) { | 387 const std::string extension_id) { |
510 // Convert data from Notification type to NotificationOptions type. | 388 // Convert data from Notification type to NotificationOptions type. |
511 extensions::api::notifications::NotificationOptions options; | 389 extensions::api::notifications::NotificationOptions options; |
512 NotificationConversionHelper::NotificationToNotificationOptions(notification_, | 390 NotificationConversionHelper::NotificationToNotificationOptions(notification_, |
513 &options); | 391 &options); |
514 | 392 |
515 // Send the notification to the alternate provider extension/app. | 393 // Send the notification to the alternate provider extension/app. |
516 extensions::NotificationProviderEventRouter event_router(profile_); | 394 extensions::NotificationProviderEventRouter event_router(profile_); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 it != registry->enabled_extensions().end(); | 441 it != registry->enabled_extensions().end(); |
564 ++it) { | 442 ++it) { |
565 if ((*it->get()).permissions_data()->HasAPIPermission( | 443 if ((*it->get()).permissions_data()->HasAPIPermission( |
566 extensions::APIPermission::ID::kNotificationProvider)) { | 444 extensions::APIPermission::ID::kNotificationProvider)) { |
567 extension_id = (*it->get()).id(); | 445 extension_id = (*it->get()).id(); |
568 return extension_id; | 446 return extension_id; |
569 } | 447 } |
570 } | 448 } |
571 return extension_id; | 449 return extension_id; |
572 } | 450 } |
OLD | NEW |