Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: chrome/browser/notifications/message_center_notification_manager.cc

Issue 1155453002: Passing ProfileID instead of Profile* to clarify that profile should not be used for making any cal… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes as per review comments. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
(...skipping 23 matching lines...) Expand all
155 DCHECK(origin_url.is_valid()); 155 DCHECK(origin_url.is_valid());
156 156
157 // Since tag is provided by arbitrary JS, we need to use origin_url 157 // 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 158 // (which is an app url in case of app/extension) to scope the tags
159 // in the given profile. 159 // in the given profile.
160 for (NotificationMap::iterator iter = profile_notifications_.begin(); 160 for (NotificationMap::iterator iter = profile_notifications_.begin();
161 iter != profile_notifications_.end(); ++iter) { 161 iter != profile_notifications_.end(); ++iter) {
162 ProfileNotification* old_notification = (*iter).second; 162 ProfileNotification* old_notification = (*iter).second;
163 if (old_notification->notification().tag() == tag && 163 if (old_notification->notification().tag() == tag &&
164 old_notification->notification().origin_url() == origin_url && 164 old_notification->notification().origin_url() == origin_url &&
165 old_notification->profile() == profile) { 165 old_notification->profile() ==
166 NotificationUIManager::GetProfileID(profile)) {
166 // Changing the type from non-progress to progress does not count towards 167 // Changing the type from non-progress to progress does not count towards
167 // the immediate update allowed in the message center. 168 // the immediate update allowed in the message center.
168 std::string old_id = old_notification->notification().id(); 169 std::string old_id = old_notification->notification().id();
169 170
170 // Add/remove notification in the local list but just update the same 171 // Add/remove notification in the local list but just update the same
171 // one in MessageCenter. 172 // one in MessageCenter.
172 delete old_notification; 173 delete old_notification;
173 profile_notifications_.erase(old_id); 174 profile_notifications_.erase(old_id);
174 ProfileNotification* new_notification = 175 ProfileNotification* new_notification = new ProfileNotification(
175 new ProfileNotification(profile, notification); 176 NotificationUIManager::GetProfileID(profile), notification);
176 profile_notifications_[new_notification->notification().id()] = 177 profile_notifications_[new_notification->notification().id()] =
177 new_notification; 178 new_notification;
178 179
179 // TODO(liyanhou): Add routing updated notifications to alternative 180 // TODO(liyanhou): Add routing updated notifications to alternative
180 // providers. 181 // providers.
181 182
182 // WARNING: You MUST use AddProfileNotification or update the message 183 // WARNING: You MUST use AddProfileNotification or update the message
183 // center via the notification within a ProfileNotification object or the 184 // center via the notification within a ProfileNotification object or the
184 // profile ID will not be correctly set for ChromeOS. 185 // profile ID will not be correctly set for ChromeOS.
185 message_center_->UpdateNotification( 186 message_center_->UpdateNotification(
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 return false; 225 return false;
225 226
226 RemoveProfileNotification(iter->second); 227 RemoveProfileNotification(iter->second);
227 message_center_->RemoveNotification(profile_notification_id, 228 message_center_->RemoveNotification(profile_notification_id,
228 /* by_user */ false); 229 /* by_user */ false);
229 return true; 230 return true;
230 } 231 }
231 232
232 std::set<std::string> 233 std::set<std::string>
233 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin( 234 MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
234 Profile* profile, 235 ProfileID profile_id,
235 const GURL& source) { 236 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; 237 std::set<std::string> delegate_ids;
240 for (const auto& pair : profile_notifications_) { 238 for (const auto& pair : profile_notifications_) {
241 const Notification& notification = pair.second->notification(); 239 const Notification& notification = pair.second->notification();
242 if (pair.second->profile() == profile && 240 if (pair.second->profile() == profile_id &&
243 notification.origin_url() == source) { 241 notification.origin_url() == source) {
244 delegate_ids.insert(notification.delegate_id()); 242 delegate_ids.insert(notification.delegate_id());
245 } 243 }
246 } 244 }
247 245
248 return delegate_ids; 246 return delegate_ids;
249 } 247 }
250 248
251 std::set<std::string> MessageCenterNotificationManager::GetAllIdsByProfile( 249 std::set<std::string> MessageCenterNotificationManager::GetAllIdsByProfile(
252 Profile* profile) { 250 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; 251 std::set<std::string> delegate_ids;
257 for (const auto& pair : profile_notifications_) { 252 for (const auto& pair : profile_notifications_) {
258 if (pair.second->profile() == profile) 253 if (pair.second->profile() == profile_id)
259 delegate_ids.insert(pair.second->notification().delegate_id()); 254 delegate_ids.insert(pair.second->notification().delegate_id());
260 } 255 }
261 256
262 return delegate_ids; 257 return delegate_ids;
263 } 258 }
264 259
265 bool MessageCenterNotificationManager::CancelAllBySourceOrigin( 260 bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
266 const GURL& source) { 261 const GURL& source) {
267 // Same pattern as CancelById, but more complicated than the above 262 // Same pattern as CancelById, but more complicated than the above
268 // because there may be multiple notifications from the same source. 263 // because there may be multiple notifications from the same source.
(...skipping 13 matching lines...) Expand all
282 } 277 }
283 278
284 bool MessageCenterNotificationManager::CancelAllByProfile( 279 bool MessageCenterNotificationManager::CancelAllByProfile(
285 ProfileID profile_id) { 280 ProfileID profile_id) {
286 // Same pattern as CancelAllBySourceOrigin. 281 // Same pattern as CancelAllBySourceOrigin.
287 bool removed = false; 282 bool removed = false;
288 283
289 for (NotificationMap::iterator loopiter = profile_notifications_.begin(); 284 for (NotificationMap::iterator loopiter = profile_notifications_.begin();
290 loopiter != profile_notifications_.end(); ) { 285 loopiter != profile_notifications_.end(); ) {
291 NotificationMap::iterator curiter = loopiter++; 286 NotificationMap::iterator curiter = loopiter++;
292 if (profile_id == NotificationUIManager::GetProfileID( 287 if (profile_id == (*curiter).second->profile()) {
293 (*curiter).second->profile())) {
294 const std::string id = curiter->first; 288 const std::string id = curiter->first;
295 RemoveProfileNotification(curiter->second); 289 RemoveProfileNotification(curiter->second);
296 message_center_->RemoveNotification(id, /* by_user */ false); 290 message_center_->RemoveNotification(id, /* by_user */ false);
297 removed = true; 291 removed = true;
298 } 292 }
299 } 293 }
300 return removed; 294 return removed;
301 } 295 }
302 296
303 void MessageCenterNotificationManager::CancelAll() { 297 void MessageCenterNotificationManager::CancelAll() {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 const std::string& extension_id) const { 362 const std::string& extension_id) const {
369 const Notification& notification = profile_notification->notification(); 363 const Notification& notification = profile_notification->notification();
370 364
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(
378 profile_notification->profile()); 372 static_cast<Profile*>(profile_notification->profile()));
dewittj 2015/05/22 16:54:37 We shouldn't cast ProfileId to Profile. Instead,
Deepak 2015/05/23 06:19:11 Done.
379 event_router.CreateNotification(extension_id, 373 event_router.CreateNotification(extension_id,
380 notification.notifier_id().id, 374 notification.notifier_id().id,
381 notification.delegate_id(), 375 notification.delegate_id(),
382 options); 376 options);
383 } 377 }
384 378
385 void MessageCenterNotificationManager::AddProfileNotification( 379 void MessageCenterNotificationManager::AddProfileNotification(
386 ProfileNotification* profile_notification) { 380 ProfileNotification* profile_notification) {
387 const Notification& notification = profile_notification->notification(); 381 const Notification& notification = profile_notification->notification();
388 std::string id = notification.id(); 382 std::string id = notification.id();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 it != registry->enabled_extensions().end(); 415 it != registry->enabled_extensions().end();
422 ++it) { 416 ++it) {
423 if ((*it->get()).permissions_data()->HasAPIPermission( 417 if ((*it->get()).permissions_data()->HasAPIPermission(
424 extensions::APIPermission::ID::kNotificationProvider)) { 418 extensions::APIPermission::ID::kNotificationProvider)) {
425 extension_id = (*it->get()).id(); 419 extension_id = (*it->get()).id();
426 return extension_id; 420 return extension_id;
427 } 421 }
428 } 422 }
429 return extension_id; 423 return extension_id;
430 } 424 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698