| 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" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 110 } |
| 111 | 111 |
| 112 //////////////////////////////////////////////////////////////////////////////// | 112 //////////////////////////////////////////////////////////////////////////////// |
| 113 // NotificationUIManager | 113 // NotificationUIManager |
| 114 | 114 |
| 115 void MessageCenterNotificationManager::Add(const Notification& notification, | 115 void MessageCenterNotificationManager::Add(const Notification& notification, |
| 116 Profile* profile) { | 116 Profile* profile) { |
| 117 if (Update(notification, profile)) | 117 if (Update(notification, profile)) |
| 118 return; | 118 return; |
| 119 | 119 |
| 120 ProfileNotification* profile_notification = | 120 ProfileNotification* profile_notification = new ProfileNotification( |
| 121 new ProfileNotification(profile, notification); | 121 NotificationUIManager::GetProfileID(profile), notification); |
| 122 | 122 |
| 123 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)-> | 123 ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)-> |
| 124 ShowWelcomeNotificationIfNecessary(profile_notification->notification()); | 124 ShowWelcomeNotificationIfNecessary(profile_notification->notification()); |
| 125 | 125 |
| 126 // WARNING: You MUST use AddProfileNotification or update the message center | 126 // WARNING: You MUST use AddProfileNotification or update the message center |
| 127 // via the notification within a ProfileNotification object or the profile ID | 127 // via the notification within a ProfileNotification object or the profile ID |
| 128 // will not be correctly set for ChromeOS. | 128 // will not be correctly set for ChromeOS. |
| 129 // Takes ownership of profile_notification. | 129 // Takes ownership of profile_notification. |
| 130 AddProfileNotification(profile_notification); | 130 AddProfileNotification(profile_notification); |
| 131 | 131 |
| 132 // TODO(liyanhou): Change the logic to only send notifications to one party. | 132 // TODO(liyanhou): Change the logic to only send notifications to one party. |
| 133 // Currently, if there is an app with notificationProvider permission, | 133 // Currently, if there is an app with notificationProvider permission, |
| 134 // notifications will go to both message center and the app. | 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 | 135 // Change it to send notifications to message center only when the user chose |
| 136 // default message center (extension_id.empty()). | 136 // default message center (extension_id.empty()). |
| 137 | 137 |
| 138 // If there exist apps/extensions that have notificationProvider permission, | 138 // If there exist apps/extensions that have notificationProvider permission, |
| 139 // route notifications to one of the apps/extensions. | 139 // route notifications to one of the apps/extensions. |
| 140 std::string extension_id = GetExtensionTakingOverNotifications(profile); | 140 std::string extension_id = GetExtensionTakingOverNotifications(profile); |
| 141 if (!extension_id.empty()) | 141 if (!extension_id.empty()) |
| 142 AddNotificationToAlternateProvider(profile_notification, extension_id); | 142 AddNotificationToAlternateProvider(profile_notification->notification(), |
| 143 profile, extension_id); |
| 143 | 144 |
| 144 message_center_->AddNotification(make_scoped_ptr( | 145 message_center_->AddNotification(make_scoped_ptr( |
| 145 new message_center::Notification(profile_notification->notification()))); | 146 new message_center::Notification(profile_notification->notification()))); |
| 146 } | 147 } |
| 147 | 148 |
| 148 bool MessageCenterNotificationManager::Update(const Notification& notification, | 149 bool MessageCenterNotificationManager::Update(const Notification& notification, |
| 149 Profile* profile) { | 150 Profile* profile) { |
| 150 const std::string& tag = notification.tag(); | 151 const std::string& tag = notification.tag(); |
| 151 if (tag.empty()) | 152 if (tag.empty()) |
| 152 return false; | 153 return false; |
| 153 | 154 |
| 154 const GURL origin_url = notification.origin_url(); | 155 const GURL origin_url = notification.origin_url(); |
| 155 DCHECK(origin_url.is_valid()); | 156 DCHECK(origin_url.is_valid()); |
| 156 | 157 |
| 157 // Since tag is provided by arbitrary JS, we need to use origin_url | 158 // Since tag is provided by arbitrary JS, we need to use origin_url |
| 158 // (which is an app url in case of app/extension) to scope the tags | 159 // (which is an app url in case of app/extension) to scope the tags |
| 159 // in the given profile. | 160 // in the given profile. |
| 160 for (NotificationMap::iterator iter = profile_notifications_.begin(); | 161 for (NotificationMap::iterator iter = profile_notifications_.begin(); |
| 161 iter != profile_notifications_.end(); ++iter) { | 162 iter != profile_notifications_.end(); ++iter) { |
| 162 ProfileNotification* old_notification = (*iter).second; | 163 ProfileNotification* old_notification = (*iter).second; |
| 163 if (old_notification->notification().tag() == tag && | 164 if (old_notification->notification().tag() == tag && |
| 164 old_notification->notification().origin_url() == origin_url && | 165 old_notification->notification().origin_url() == origin_url && |
| 165 old_notification->profile() == profile) { | 166 old_notification->profile_id() == |
| 167 NotificationUIManager::GetProfileID(profile)) { |
| 166 // Changing the type from non-progress to progress does not count towards | 168 // Changing the type from non-progress to progress does not count towards |
| 167 // the immediate update allowed in the message center. | 169 // the immediate update allowed in the message center. |
| 168 std::string old_id = old_notification->notification().id(); | 170 std::string old_id = old_notification->notification().id(); |
| 169 | 171 |
| 170 // Add/remove notification in the local list but just update the same | 172 // Add/remove notification in the local list but just update the same |
| 171 // one in MessageCenter. | 173 // one in MessageCenter. |
| 172 delete old_notification; | 174 delete old_notification; |
| 173 profile_notifications_.erase(old_id); | 175 profile_notifications_.erase(old_id); |
| 174 ProfileNotification* new_notification = | 176 ProfileNotification* new_notification = new ProfileNotification( |
| 175 new ProfileNotification(profile, notification); | 177 NotificationUIManager::GetProfileID(profile), notification); |
| 176 profile_notifications_[new_notification->notification().id()] = | 178 profile_notifications_[new_notification->notification().id()] = |
| 177 new_notification; | 179 new_notification; |
| 178 | 180 |
| 179 // TODO(liyanhou): Add routing updated notifications to alternative | 181 // TODO(liyanhou): Add routing updated notifications to alternative |
| 180 // providers. | 182 // providers. |
| 181 | 183 |
| 182 // WARNING: You MUST use AddProfileNotification or update the message | 184 // WARNING: You MUST use AddProfileNotification or update the message |
| 183 // center via the notification within a ProfileNotification object or the | 185 // center via the notification within a ProfileNotification object or the |
| 184 // profile ID will not be correctly set for ChromeOS. | 186 // profile ID will not be correctly set for ChromeOS. |
| 185 message_center_->UpdateNotification( | 187 message_center_->UpdateNotification( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 return false; | 226 return false; |
| 225 | 227 |
| 226 RemoveProfileNotification(iter->second); | 228 RemoveProfileNotification(iter->second); |
| 227 message_center_->RemoveNotification(profile_notification_id, | 229 message_center_->RemoveNotification(profile_notification_id, |
| 228 /* by_user */ false); | 230 /* by_user */ false); |
| 229 return true; | 231 return true; |
| 230 } | 232 } |
| 231 | 233 |
| 232 std::set<std::string> | 234 std::set<std::string> |
| 233 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin( | 235 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin( |
| 234 Profile* profile, | 236 ProfileID profile_id, |
| 235 const GURL& source) { | 237 const GURL& source) { |
| 236 // The profile pointer can be weak, the instance may have been destroyed, so | |
| 237 // no profile method should be called inside this function. | |
| 238 | |
| 239 std::set<std::string> delegate_ids; | 238 std::set<std::string> delegate_ids; |
| 240 for (const auto& pair : profile_notifications_) { | 239 for (const auto& pair : profile_notifications_) { |
| 241 const Notification& notification = pair.second->notification(); | 240 const Notification& notification = pair.second->notification(); |
| 242 if (pair.second->profile() == profile && | 241 if (pair.second->profile_id() == profile_id && |
| 243 notification.origin_url() == source) { | 242 notification.origin_url() == source) { |
| 244 delegate_ids.insert(notification.delegate_id()); | 243 delegate_ids.insert(notification.delegate_id()); |
| 245 } | 244 } |
| 246 } | 245 } |
| 247 | 246 |
| 248 return delegate_ids; | 247 return delegate_ids; |
| 249 } | 248 } |
| 250 | 249 |
| 251 std::set<std::string> MessageCenterNotificationManager::GetAllIdsByProfile( | 250 std::set<std::string> MessageCenterNotificationManager::GetAllIdsByProfile( |
| 252 Profile* profile) { | 251 ProfileID profile_id) { |
| 253 // The profile pointer can be weak, the instance may have been destroyed, so | |
| 254 // no profile method should be called inside this function. | |
| 255 | |
| 256 std::set<std::string> delegate_ids; | 252 std::set<std::string> delegate_ids; |
| 257 for (const auto& pair : profile_notifications_) { | 253 for (const auto& pair : profile_notifications_) { |
| 258 if (pair.second->profile() == profile) | 254 if (pair.second->profile_id() == profile_id) |
| 259 delegate_ids.insert(pair.second->notification().delegate_id()); | 255 delegate_ids.insert(pair.second->notification().delegate_id()); |
| 260 } | 256 } |
| 261 | 257 |
| 262 return delegate_ids; | 258 return delegate_ids; |
| 263 } | 259 } |
| 264 | 260 |
| 265 bool MessageCenterNotificationManager::CancelAllBySourceOrigin( | 261 bool MessageCenterNotificationManager::CancelAllBySourceOrigin( |
| 266 const GURL& source) { | 262 const GURL& source) { |
| 267 // Same pattern as CancelById, but more complicated than the above | 263 // Same pattern as CancelById, but more complicated than the above |
| 268 // because there may be multiple notifications from the same source. | 264 // because there may be multiple notifications from the same source. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 282 } | 278 } |
| 283 | 279 |
| 284 bool MessageCenterNotificationManager::CancelAllByProfile( | 280 bool MessageCenterNotificationManager::CancelAllByProfile( |
| 285 ProfileID profile_id) { | 281 ProfileID profile_id) { |
| 286 // Same pattern as CancelAllBySourceOrigin. | 282 // Same pattern as CancelAllBySourceOrigin. |
| 287 bool removed = false; | 283 bool removed = false; |
| 288 | 284 |
| 289 for (NotificationMap::iterator loopiter = profile_notifications_.begin(); | 285 for (NotificationMap::iterator loopiter = profile_notifications_.begin(); |
| 290 loopiter != profile_notifications_.end(); ) { | 286 loopiter != profile_notifications_.end(); ) { |
| 291 NotificationMap::iterator curiter = loopiter++; | 287 NotificationMap::iterator curiter = loopiter++; |
| 292 if (profile_id == NotificationUIManager::GetProfileID( | 288 if (profile_id == (*curiter).second->profile_id()) { |
| 293 (*curiter).second->profile())) { | |
| 294 const std::string id = curiter->first; | 289 const std::string id = curiter->first; |
| 295 RemoveProfileNotification(curiter->second); | 290 RemoveProfileNotification(curiter->second); |
| 296 message_center_->RemoveNotification(id, /* by_user */ false); | 291 message_center_->RemoveNotification(id, /* by_user */ false); |
| 297 removed = true; | 292 removed = true; |
| 298 } | 293 } |
| 299 } | 294 } |
| 300 return removed; | 295 return removed; |
| 301 } | 296 } |
| 302 | 297 |
| 303 void MessageCenterNotificationManager::CancelAll() { | 298 void MessageCenterNotificationManager::CancelAll() { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 const std::string& delegate_id, | 352 const std::string& delegate_id, |
| 358 Profile* profile) { | 353 Profile* profile) { |
| 359 return ProfileNotification::GetProfileNotificationId(delegate_id, | 354 return ProfileNotification::GetProfileNotificationId(delegate_id, |
| 360 GetProfileID(profile)); | 355 GetProfileID(profile)); |
| 361 } | 356 } |
| 362 | 357 |
| 363 //////////////////////////////////////////////////////////////////////////////// | 358 //////////////////////////////////////////////////////////////////////////////// |
| 364 // private | 359 // private |
| 365 | 360 |
| 366 void MessageCenterNotificationManager::AddNotificationToAlternateProvider( | 361 void MessageCenterNotificationManager::AddNotificationToAlternateProvider( |
| 367 ProfileNotification* profile_notification, | 362 const Notification& notification, |
| 363 Profile* profile, |
| 368 const std::string& extension_id) const { | 364 const std::string& extension_id) const { |
| 369 const Notification& notification = profile_notification->notification(); | |
| 370 | |
| 371 // Convert data from Notification type to NotificationOptions type. | 365 // Convert data from Notification type to NotificationOptions type. |
| 372 extensions::api::notifications::NotificationOptions options; | 366 extensions::api::notifications::NotificationOptions options; |
| 373 NotificationConversionHelper::NotificationToNotificationOptions(notification, | 367 NotificationConversionHelper::NotificationToNotificationOptions(notification, |
| 374 &options); | 368 &options); |
| 375 | 369 |
| 376 // Send the notification to the alternate provider extension/app. | 370 // Send the notification to the alternate provider extension/app. |
| 377 extensions::NotificationProviderEventRouter event_router( | 371 extensions::NotificationProviderEventRouter event_router(profile); |
| 378 profile_notification->profile()); | |
| 379 event_router.CreateNotification(extension_id, | 372 event_router.CreateNotification(extension_id, |
| 380 notification.notifier_id().id, | 373 notification.notifier_id().id, |
| 381 notification.delegate_id(), | 374 notification.delegate_id(), |
| 382 options); | 375 options); |
| 383 } | 376 } |
| 384 | 377 |
| 385 void MessageCenterNotificationManager::AddProfileNotification( | 378 void MessageCenterNotificationManager::AddProfileNotification( |
| 386 ProfileNotification* profile_notification) { | 379 ProfileNotification* profile_notification) { |
| 387 const Notification& notification = profile_notification->notification(); | 380 const Notification& notification = profile_notification->notification(); |
| 388 std::string id = notification.id(); | 381 std::string id = notification.id(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 it != registry->enabled_extensions().end(); | 414 it != registry->enabled_extensions().end(); |
| 422 ++it) { | 415 ++it) { |
| 423 if ((*it->get()).permissions_data()->HasAPIPermission( | 416 if ((*it->get()).permissions_data()->HasAPIPermission( |
| 424 extensions::APIPermission::ID::kNotificationProvider)) { | 417 extensions::APIPermission::ID::kNotificationProvider)) { |
| 425 extension_id = (*it->get()).id(); | 418 extension_id = (*it->get()).id(); |
| 426 return extension_id; | 419 return extension_id; |
| 427 } | 420 } |
| 428 } | 421 } |
| 429 return extension_id; | 422 return extension_id; |
| 430 } | 423 } |
| OLD | NEW |