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" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/extensions/api/notification_provider/notification_provi
der_api.h" | |
14 #include "chrome/browser/notifications/desktop_notification_service.h" | 13 #include "chrome/browser/notifications/desktop_notification_service.h" |
15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 14 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
16 #include "chrome/browser/notifications/fullscreen_notification_blocker.h" | 15 #include "chrome/browser/notifications/fullscreen_notification_blocker.h" |
17 #include "chrome/browser/notifications/message_center_settings_controller.h" | 16 #include "chrome/browser/notifications/message_center_settings_controller.h" |
18 #include "chrome/browser/notifications/notification.h" | 17 #include "chrome/browser/notifications/notification.h" |
19 #include "chrome/browser/notifications/notification_conversion_helper.h" | |
20 #include "chrome/browser/notifications/screen_lock_notification_blocker.h" | 18 #include "chrome/browser/notifications/screen_lock_notification_blocker.h" |
21 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/ui/browser_finder.h" | 20 #include "chrome/browser/ui/browser_finder.h" |
23 #include "chrome/browser/ui/chrome_pages.h" | 21 #include "chrome/browser/ui/chrome_pages.h" |
24 #include "chrome/browser/ui/host_desktop.h" | 22 #include "chrome/browser/ui/host_desktop.h" |
25 #include "chrome/common/extensions/api/notification_provider.h" | |
26 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
27 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
28 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
29 #include "content/public/common/url_constants.h" | 26 #include "content/public/common/url_constants.h" |
30 #include "extensions/browser/extension_registry.h" | |
31 #include "extensions/browser/extension_system.h" | 27 #include "extensions/browser/extension_system.h" |
32 #include "extensions/browser/info_map.h" | 28 #include "extensions/browser/info_map.h" |
33 #include "extensions/common/extension_set.h" | 29 #include "extensions/common/extension_set.h" |
34 #include "extensions/common/permissions/permissions_data.h" | |
35 #include "ui/gfx/image/image_skia.h" | 30 #include "ui/gfx/image/image_skia.h" |
36 #include "ui/message_center/message_center_style.h" | 31 #include "ui/message_center/message_center_style.h" |
37 #include "ui/message_center/message_center_tray.h" | 32 #include "ui/message_center/message_center_tray.h" |
38 #include "ui/message_center/message_center_types.h" | 33 #include "ui/message_center/message_center_types.h" |
39 #include "ui/message_center/notifier_settings.h" | 34 #include "ui/message_center/notifier_settings.h" |
40 | 35 |
41 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
42 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos
.h" | 37 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos
.h" |
43 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 38 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
44 #endif | 39 #endif |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // NotificationUIManager | 110 // NotificationUIManager |
116 | 111 |
117 void MessageCenterNotificationManager::Add(const Notification& notification, | 112 void MessageCenterNotificationManager::Add(const Notification& notification, |
118 Profile* profile) { | 113 Profile* profile) { |
119 if (Update(notification, profile)) | 114 if (Update(notification, profile)) |
120 return; | 115 return; |
121 | 116 |
122 DesktopNotificationServiceFactory::GetForProfile(profile)-> | 117 DesktopNotificationServiceFactory::GetForProfile(profile)-> |
123 ShowWelcomeNotificationIfNecessary(notification); | 118 ShowWelcomeNotificationIfNecessary(notification); |
124 | 119 |
125 // WARNING: You MUST update the message center via the notification within a | 120 // WARNING: You MUST use AddProfileNotification or update the message center |
126 // ProfileNotification object or the profile ID will not be correctly set for | 121 // via the notification within a ProfileNotification object or the profile ID |
127 // ChromeOS. | 122 // will not be correctly set for ChromeOS. |
128 ProfileNotification* profile_notification( | 123 AddProfileNotification( |
129 new ProfileNotification(profile, notification, message_center_)); | 124 new ProfileNotification(profile, notification, message_center_)); |
130 AddProfileNotification(profile_notification); | |
131 | |
132 // TODO(liyanhou): Change the logic to only send notifications to one party. | |
133 // Currently, if there is an app with notificationProvider permission, | |
134 // notifications will go to both message center and the app. | |
135 // Change it to send notifications to message center only when the user chose | |
136 // default message center (extension_id.empty()). | |
137 | |
138 // If there exist apps/extensions that have notificationProvider permission, | |
139 // route notifications to one of the apps/extensions. | |
140 std::string extension_id = GetExtensionTakingOverNotifications(profile); | |
141 if (!extension_id.empty()) | |
142 profile_notification->AddToAlternateProvider(extension_id); | |
143 | |
144 message_center_->AddNotification(make_scoped_ptr( | |
145 new message_center::Notification(profile_notification->notification()))); | |
146 profile_notification->StartDownloads(); | |
147 } | 125 } |
148 | 126 |
149 bool MessageCenterNotificationManager::Update(const Notification& notification, | 127 bool MessageCenterNotificationManager::Update(const Notification& notification, |
150 Profile* profile) { | 128 Profile* profile) { |
151 const base::string16& replace_id = notification.replace_id(); | 129 const base::string16& replace_id = notification.replace_id(); |
152 if (replace_id.empty()) | 130 if (replace_id.empty()) |
153 return false; | 131 return false; |
154 | 132 |
155 const GURL origin_url = notification.origin_url(); | 133 const GURL origin_url = notification.origin_url(); |
156 DCHECK(origin_url.is_valid()); | 134 DCHECK(origin_url.is_valid()); |
(...skipping 13 matching lines...) Expand all Loading... |
170 old_notification->notification().delegate_id(); | 148 old_notification->notification().delegate_id(); |
171 | 149 |
172 // Add/remove notification in the local list but just update the same | 150 // Add/remove notification in the local list but just update the same |
173 // one in MessageCenter. | 151 // one in MessageCenter. |
174 delete old_notification; | 152 delete old_notification; |
175 profile_notifications_.erase(old_id); | 153 profile_notifications_.erase(old_id); |
176 ProfileNotification* new_notification = | 154 ProfileNotification* new_notification = |
177 new ProfileNotification(profile, notification, message_center_); | 155 new ProfileNotification(profile, notification, message_center_); |
178 profile_notifications_[notification.delegate_id()] = new_notification; | 156 profile_notifications_[notification.delegate_id()] = new_notification; |
179 | 157 |
180 // TODO(liyanhou): Add routing updated notifications to alternative | |
181 // providers. | |
182 | |
183 // WARNING: You MUST use AddProfileNotification or update the message | 158 // WARNING: You MUST use AddProfileNotification or update the message |
184 // center via the notification within a ProfileNotification object or the | 159 // center via the notification within a ProfileNotification object or the |
185 // profile ID will not be correctly set for ChromeOS. | 160 // profile ID will not be correctly set for ChromeOS. |
186 message_center_->UpdateNotification( | 161 message_center_->UpdateNotification( |
187 old_id, | 162 old_id, |
188 make_scoped_ptr(new message_center::Notification( | 163 make_scoped_ptr(new message_center::Notification( |
189 new_notification->notification()))); | 164 new_notification->notification()))); |
190 | 165 |
191 new_notification->StartDownloads(); | 166 new_notification->StartDownloads(); |
192 return true; | 167 return true; |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | 470 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); |
496 iter != extensions.end(); ++iter) { | 471 iter != extensions.end(); ++iter) { |
497 if (desktop_service->IsNotifierEnabled(message_center::NotifierId( | 472 if (desktop_service->IsNotifierEnabled(message_center::NotifierId( |
498 message_center::NotifierId::APPLICATION, (*iter)->id()))) { | 473 message_center::NotifierId::APPLICATION, (*iter)->id()))) { |
499 return (*iter)->id(); | 474 return (*iter)->id(); |
500 } | 475 } |
501 } | 476 } |
502 return std::string(); | 477 return std::string(); |
503 } | 478 } |
504 | 479 |
505 void | |
506 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider( | |
507 const std::string extension_id) { | |
508 // Convert data from Notification type to NotificationOptions type. | |
509 extensions::api::notifications::NotificationOptions options; | |
510 NotificationConversionHelper::NotificationToNotificationOptions(notification_, | |
511 &options); | |
512 | |
513 // Send the notification to the alternate provider extension/app. | |
514 extensions::NotificationProviderEventRouter event_router(profile_); | |
515 event_router.CreateNotification(extension_id, | |
516 notification_.notifier_id().id, | |
517 notification_.delegate_id(), | |
518 options); | |
519 } | |
520 | |
521 //////////////////////////////////////////////////////////////////////////////// | 480 //////////////////////////////////////////////////////////////////////////////// |
522 // private | 481 // private |
523 | 482 |
524 void MessageCenterNotificationManager::AddProfileNotification( | 483 void MessageCenterNotificationManager::AddProfileNotification( |
525 ProfileNotification* profile_notification) { | 484 ProfileNotification* profile_notification) { |
526 std::string id = profile_notification->notification().delegate_id(); | 485 const Notification& notification = profile_notification->notification(); |
| 486 std::string id = notification.delegate_id(); |
527 // Notification ids should be unique. | 487 // Notification ids should be unique. |
528 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); | 488 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
529 profile_notifications_[id] = profile_notification; | 489 profile_notifications_[id] = profile_notification; |
| 490 |
| 491 // Create the copy for message center, and ensure the extension ID is correct. |
| 492 scoped_ptr<message_center::Notification> message_center_notification( |
| 493 new message_center::Notification(notification)); |
| 494 message_center_->AddNotification(message_center_notification.Pass()); |
| 495 |
| 496 profile_notification->StartDownloads(); |
530 } | 497 } |
531 | 498 |
532 void MessageCenterNotificationManager::RemoveProfileNotification( | 499 void MessageCenterNotificationManager::RemoveProfileNotification( |
533 ProfileNotification* profile_notification) { | 500 ProfileNotification* profile_notification) { |
534 std::string id = profile_notification->notification().delegate_id(); | 501 std::string id = profile_notification->notification().delegate_id(); |
535 profile_notifications_.erase(id); | 502 profile_notifications_.erase(id); |
536 delete profile_notification; | 503 delete profile_notification; |
537 } | 504 } |
538 | 505 |
539 MessageCenterNotificationManager::ProfileNotification* | 506 MessageCenterNotificationManager::ProfileNotification* |
540 MessageCenterNotificationManager::FindProfileNotification( | 507 MessageCenterNotificationManager::FindProfileNotification( |
541 const std::string& id) const { | 508 const std::string& id) const { |
542 NotificationMap::const_iterator iter = profile_notifications_.find(id); | 509 NotificationMap::const_iterator iter = profile_notifications_.find(id); |
543 if (iter == profile_notifications_.end()) | 510 if (iter == profile_notifications_.end()) |
544 return NULL; | 511 return NULL; |
545 | 512 |
546 return (*iter).second; | 513 return (*iter).second; |
547 } | 514 } |
548 | |
549 std::string | |
550 MessageCenterNotificationManager::GetExtensionTakingOverNotifications( | |
551 Profile* profile) { | |
552 // TODO(liyanhou): When additional settings in Chrome Settings is implemented, | |
553 // change choosing the last app with permission to a user selected app. | |
554 extensions::ExtensionRegistry* registry = | |
555 extensions::ExtensionRegistry::Get(profile); | |
556 DCHECK(registry); | |
557 std::string extension_id; | |
558 for (extensions::ExtensionSet::const_iterator it = | |
559 registry->enabled_extensions().begin(); | |
560 it != registry->enabled_extensions().end(); | |
561 ++it) { | |
562 if ((*it->get()).permissions_data()->HasAPIPermission( | |
563 extensions::APIPermission::ID::kNotificationProvider)) { | |
564 extension_id = (*it->get()).id(); | |
565 return extension_id; | |
566 } | |
567 } | |
568 return extension_id; | |
569 } | |
OLD | NEW |