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