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