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 "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/notifications/notification.h" | 10 #include "chrome/browser/notifications/notification.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/common/extensions/extension_set.h" |
11 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
12 | 15 |
| 16 #if defined(USE_ASH) |
| 17 #include "ash/shell.h" |
| 18 #else |
| 19 #include "chrome/browser/browser_process.h" |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 message_center::MessageCenter* GetMessageCenterInstance() { |
| 24 #if defined (USE_ASH) |
| 25 return ash::Shell::GetInstance()->message_center(); |
| 26 #else |
| 27 return g_browser_process->message_center(); |
| 28 #endif |
| 29 } |
| 30 } |
| 31 |
13 MessageCenterNotificationManager::MessageCenterNotificationManager() { | 32 MessageCenterNotificationManager::MessageCenterNotificationManager() { |
| 33 message_center_ = GetMessageCenterInstance(); |
| 34 message_center_->SetDelegate(this); |
14 } | 35 } |
15 | 36 |
16 MessageCenterNotificationManager::~MessageCenterNotificationManager() { | 37 MessageCenterNotificationManager::~MessageCenterNotificationManager() { |
17 } | 38 } |
18 | 39 |
| 40 //////////////////////////////////////////////////////////////////////////////// |
| 41 // NotificationUIManager |
| 42 |
19 bool MessageCenterNotificationManager::CancelById(const std::string& id) { | 43 bool MessageCenterNotificationManager::CancelById(const std::string& id) { |
20 // See if this ID hasn't been shown yet. | 44 // See if this ID hasn't been shown yet. |
21 if (NotificationUIManagerImpl::CancelById(id)) | 45 if (NotificationUIManagerImpl::CancelById(id)) |
22 return true; | 46 return true; |
23 | 47 |
24 // If it has been shown, remove it. | 48 // If it has been shown, remove it. |
25 NOTIMPLEMENTED(); | 49 NotificationMap::iterator iter = profile_notifications_.find(id); |
26 return false; | 50 if (iter == profile_notifications_.end()) |
| 51 return false; |
| 52 |
| 53 RemoveProfileNotification((*iter).second); |
| 54 return true; |
27 } | 55 } |
28 | 56 |
29 bool MessageCenterNotificationManager::CancelAllBySourceOrigin( | 57 bool MessageCenterNotificationManager::CancelAllBySourceOrigin( |
30 const GURL& source) { | 58 const GURL& source) { |
31 // Same pattern as CancelById, but more complicated than the above | 59 // Same pattern as CancelById, but more complicated than the above |
32 // because there may be multiple notifications from the same source. | 60 // because there may be multiple notifications from the same source. |
33 bool removed = NotificationUIManagerImpl::CancelAllBySourceOrigin(source); | 61 bool removed = NotificationUIManagerImpl::CancelAllBySourceOrigin(source); |
34 | 62 |
35 NOTIMPLEMENTED(); | 63 for (NotificationMap::iterator loopiter = profile_notifications_.begin(); |
36 return false || removed; | 64 loopiter != profile_notifications_.end(); ) { |
| 65 NotificationMap::iterator curiter = loopiter++; |
| 66 if ((*curiter).second->notification().origin_url() == source) { |
| 67 RemoveProfileNotification((*curiter).second); |
| 68 removed = true; |
| 69 } |
| 70 } |
| 71 return removed; |
37 } | 72 } |
38 | 73 |
39 bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) { | 74 bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) { |
40 // Same pattern as CancelAllBySourceOrigin. | 75 // Same pattern as CancelAllBySourceOrigin. |
41 bool removed = NotificationUIManagerImpl::CancelAllByProfile(profile); | 76 bool removed = NotificationUIManagerImpl::CancelAllByProfile(profile); |
42 | 77 |
43 NOTIMPLEMENTED(); | 78 for (NotificationMap::iterator loopiter = profile_notifications_.begin(); |
44 return false || removed; | 79 loopiter != profile_notifications_.end(); ) { |
| 80 NotificationMap::iterator curiter = loopiter++; |
| 81 if ((*curiter).second->profile()->IsSameProfile(profile)) { |
| 82 RemoveProfileNotification((*curiter).second); |
| 83 removed = true; |
| 84 } |
| 85 } |
| 86 return removed; |
45 } | 87 } |
46 | 88 |
47 void MessageCenterNotificationManager::CancelAll() { | 89 void MessageCenterNotificationManager::CancelAll() { |
48 NotificationUIManagerImpl::CancelAll(); | 90 NotificationUIManagerImpl::CancelAll(); |
49 NOTIMPLEMENTED(); | 91 |
| 92 for (NotificationMap::iterator loopiter = profile_notifications_.begin(); |
| 93 loopiter != profile_notifications_.end(); ) { |
| 94 RemoveProfileNotification((*loopiter++).second); |
| 95 } |
50 } | 96 } |
51 | 97 |
| 98 //////////////////////////////////////////////////////////////////////////////// |
| 99 // NotificationUIManagerImpl |
| 100 |
52 bool MessageCenterNotificationManager::ShowNotification( | 101 bool MessageCenterNotificationManager::ShowNotification( |
53 const Notification& notification, Profile* profile) { | 102 const Notification& notification, Profile* profile) { |
54 NOTIMPLEMENTED(); | 103 // There is always space in MessageCenter, it never rejects Notifications. |
| 104 AddProfileNotification(new ProfileNotification(profile, notification)); |
55 return true; | 105 return true; |
56 } | 106 } |
57 | 107 |
58 bool MessageCenterNotificationManager::UpdateNotification( | 108 bool MessageCenterNotificationManager::UpdateNotification( |
59 const Notification& notification) { | 109 const Notification& notification, Profile* profile) { |
60 const string16& replace_id = notification.replace_id(); | 110 const string16& replace_id = notification.replace_id(); |
61 DCHECK(!replace_id.empty()); | 111 DCHECK(!replace_id.empty()); |
| 112 const GURL origin_url = notification.origin_url(); |
| 113 DCHECK(origin_url.is_valid()); |
62 | 114 |
63 NOTIMPLEMENTED(); | 115 // Since replace_id is provided by arbitrary JS, we need to use origin_url |
| 116 // (which is an app url in case of app/extension) to scope the replace ids |
| 117 // in the given profile. |
| 118 for (NotificationMap::iterator iter = profile_notifications_.begin(); |
| 119 iter != profile_notifications_.end(); ++iter) { |
| 120 ProfileNotification* old_notification = (*iter).second; |
| 121 if (old_notification->notification().replace_id() == replace_id && |
| 122 old_notification->notification().origin_url() == origin_url && |
| 123 old_notification->profile()->IsSameProfile(profile)) { |
| 124 std::string old_id = |
| 125 old_notification->notification().notification_id(); |
| 126 DCHECK(message_center_->GetNotificationList()->HasNotification(old_id)); |
| 127 |
| 128 // Add/remove notification in the local list but just update the same |
| 129 // one in MessageCenter. |
| 130 old_notification->notification().Close(false); // Not by user. |
| 131 delete old_notification; |
| 132 profile_notifications_.erase(old_id); |
| 133 profile_notifications_[notification.notification_id()] = |
| 134 new ProfileNotification(profile, notification); |
| 135 |
| 136 message_center_->UpdateNotification(old_id, |
| 137 notification.notification_id(), |
| 138 notification.title(), |
| 139 notification.body(), |
| 140 notification.optional_fields()); |
| 141 notification.Display(); |
| 142 return true; |
| 143 } |
| 144 } |
64 return false; | 145 return false; |
65 } | 146 } |
| 147 |
| 148 //////////////////////////////////////////////////////////////////////////////// |
| 149 // MessageCenter::Delegate |
| 150 |
| 151 void MessageCenterNotificationManager::DisableExtension( |
| 152 const std::string& notification_id) { |
| 153 NOTIMPLEMENTED(); |
| 154 } |
| 155 |
| 156 void MessageCenterNotificationManager::DisableNotificationsFromSource( |
| 157 const std::string& notification_id) { |
| 158 NOTIMPLEMENTED(); |
| 159 } |
| 160 |
| 161 void MessageCenterNotificationManager::NotificationRemoved( |
| 162 const std::string& notification_id) { |
| 163 NOTIMPLEMENTED(); |
| 164 } |
| 165 |
| 166 void MessageCenterNotificationManager::ShowSettings( |
| 167 const std::string& notification_id) { |
| 168 NOTIMPLEMENTED(); |
| 169 } |
| 170 |
| 171 void MessageCenterNotificationManager::OnClicked( |
| 172 const std::string& notification_id) { |
| 173 NOTIMPLEMENTED(); |
| 174 } |
| 175 |
| 176 void MessageCenterNotificationManager::OnButtonClicked( |
| 177 const std::string& notification_id, int button_index) { |
| 178 NOTIMPLEMENTED(); |
| 179 } |
| 180 |
| 181 //////////////////////////////////////////////////////////////////////////////// |
| 182 // ProfileNotification |
| 183 |
| 184 MessageCenterNotificationManager::ProfileNotification::ProfileNotification( |
| 185 Profile* profile, const Notification& notification) |
| 186 : profile_(profile), |
| 187 notification_(notification) { |
| 188 DCHECK(profile); |
| 189 } |
| 190 |
| 191 std::string |
| 192 MessageCenterNotificationManager::ProfileNotification::GetExtensionId() { |
| 193 const ExtensionURLInfo url(notification().origin_url()); |
| 194 const ExtensionService* service = profile()->GetExtensionService(); |
| 195 const extensions::Extension* extension = |
| 196 service->extensions()->GetExtensionOrAppByURL(url); |
| 197 return extension ? extension->id() : std::string(); |
| 198 } |
| 199 |
| 200 //////////////////////////////////////////////////////////////////////////////// |
| 201 // private |
| 202 |
| 203 void MessageCenterNotificationManager::AddProfileNotification( |
| 204 ProfileNotification* profile_notification) { |
| 205 const Notification& notification = profile_notification->notification(); |
| 206 std::string id = notification.notification_id(); |
| 207 // Notification ids should be unique. |
| 208 DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); |
| 209 profile_notifications_[id] = profile_notification; |
| 210 |
| 211 message_center_->AddNotification(notification.type(), |
| 212 notification.notification_id(), |
| 213 notification.title(), |
| 214 notification.body(), |
| 215 notification.display_source(), |
| 216 profile_notification->GetExtensionId(), |
| 217 notification.optional_fields()); |
| 218 notification.Display(); |
| 219 } |
| 220 |
| 221 void MessageCenterNotificationManager::RemoveProfileNotification( |
| 222 ProfileNotification* profile_notification) { |
| 223 profile_notification->notification().Close(false); // Not by user. |
| 224 std::string id = profile_notification->notification().notification_id(); |
| 225 message_center_->RemoveNotification(id); |
| 226 profile_notifications_.erase(id); |
| 227 delete profile_notification; |
| 228 } |
OLD | NEW |