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