OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/platform_notification_service_impl.h" | 5 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 namespace { | 74 namespace { |
75 | 75 |
76 // Invalid id for a renderer process. Used in cases where we need to check for | 76 // Invalid id for a renderer process. Used in cases where we need to check for |
77 // permission without having an associated renderer process yet. | 77 // permission without having an associated renderer process yet. |
78 const int kInvalidRenderProcessId = -1; | 78 const int kInvalidRenderProcessId = -1; |
79 | 79 |
80 void OnCloseNonPersistentNotificationProfileLoaded( | 80 void OnCloseNonPersistentNotificationProfileLoaded( |
81 const std::string& notification_id, | 81 const std::string& notification_id, |
82 Profile* profile) { | 82 Profile* profile) { |
83 NotificationDisplayServiceFactory::GetForProfile(profile)->Close( | 83 NotificationDisplayServiceFactory::GetForProfile(profile)->Close( |
84 notification_id); | 84 NotificationCommon::NON_PERSISTENT, notification_id); |
85 } | |
86 | |
87 // Callback to run once the profile has been loaded in order to perform a | |
88 // given |operation| in a notification. | |
89 void ProfileLoadedCallback(NotificationCommon::Operation operation, | |
90 const GURL& origin, | |
91 int64_t persistent_notification_id, | |
92 int action_index, | |
93 Profile* profile) { | |
94 if (!profile) { | |
95 // TODO(miguelg): Add UMA for this condition. | |
96 // Perhaps propagate this through PersistentNotificationStatus. | |
97 LOG(WARNING) << "Profile not loaded correctly"; | |
98 return; | |
99 } | |
100 | |
101 switch (operation) { | |
102 case NotificationCommon::CLICK: | |
103 PlatformNotificationServiceImpl::GetInstance() | |
104 ->OnPersistentNotificationClick(profile, persistent_notification_id, | |
105 origin, action_index); | |
106 break; | |
107 case NotificationCommon::CLOSE: | |
108 PlatformNotificationServiceImpl::GetInstance() | |
109 ->OnPersistentNotificationClose(profile, persistent_notification_id, | |
110 origin, true); | |
111 break; | |
112 case NotificationCommon::SETTINGS: | |
113 NotificationCommon::OpenNotificationSettings(profile); | |
114 break; | |
115 } | |
116 } | 85 } |
117 | 86 |
118 // Callback used to close an non-persistent notification from blink. | 87 // Callback used to close an non-persistent notification from blink. |
119 void CancelNotification(const std::string& notification_id, | 88 void CancelNotification(const std::string& notification_id, |
120 std::string profile_id, | 89 std::string profile_id, |
121 bool incognito) { | 90 bool incognito) { |
122 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 91 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
123 DCHECK(profile_manager); | 92 DCHECK(profile_manager); |
124 profile_manager->LoadProfile( | 93 profile_manager->LoadProfile( |
125 profile_id, incognito, | 94 profile_id, incognito, |
(...skipping 11 matching lines...) Expand all Loading... |
137 | 106 |
138 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() | 107 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
139 : test_display_service_(nullptr) { | 108 : test_display_service_(nullptr) { |
140 #if BUILDFLAG(ENABLE_BACKGROUND) | 109 #if BUILDFLAG(ENABLE_BACKGROUND) |
141 pending_click_dispatch_events_ = 0; | 110 pending_click_dispatch_events_ = 0; |
142 #endif | 111 #endif |
143 } | 112 } |
144 | 113 |
145 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 114 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
146 | 115 |
147 void PlatformNotificationServiceImpl::ProcessPersistentNotificationOperation( | |
148 NotificationCommon::Operation operation, | |
149 const std::string& profile_id, | |
150 bool incognito, | |
151 const GURL& origin, | |
152 int64_t persistent_notification_id, | |
153 int action_index) { | |
154 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
155 DCHECK(profile_manager); | |
156 | |
157 profile_manager->LoadProfile( | |
158 profile_id, incognito, | |
159 base::Bind(&ProfileLoadedCallback, operation, origin, | |
160 persistent_notification_id, action_index)); | |
161 } | |
162 | |
163 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( | 116 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( |
164 BrowserContext* browser_context, | 117 BrowserContext* browser_context, |
165 int64_t persistent_notification_id, | 118 int64_t persistent_notification_id, |
166 const GURL& origin, | 119 const GURL& origin, |
167 int action_index) { | 120 int action_index) { |
168 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
169 blink::mojom::PermissionStatus permission_status = | 122 blink::mojom::PermissionStatus permission_status = |
170 CheckPermissionOnUIThread(browser_context, origin, | 123 CheckPermissionOnUIThread(browser_context, origin, |
171 kInvalidRenderProcessId); | 124 kInvalidRenderProcessId); |
172 | 125 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 | 283 |
331 Profile* profile = Profile::FromBrowserContext(browser_context); | 284 Profile* profile = Profile::FromBrowserContext(browser_context); |
332 DCHECK(profile); | 285 DCHECK(profile); |
333 DCHECK_EQ(0u, notification_data.actions.size()); | 286 DCHECK_EQ(0u, notification_data.actions.size()); |
334 DCHECK_EQ(0u, notification_resources.action_icons.size()); | 287 DCHECK_EQ(0u, notification_resources.action_icons.size()); |
335 | 288 |
336 NotificationObjectProxy* proxy = | 289 NotificationObjectProxy* proxy = |
337 new NotificationObjectProxy(browser_context, std::move(delegate)); | 290 new NotificationObjectProxy(browser_context, std::move(delegate)); |
338 Notification notification = CreateNotificationFromData( | 291 Notification notification = CreateNotificationFromData( |
339 profile, origin, notification_data, notification_resources, proxy); | 292 profile, origin, notification_data, notification_resources, proxy); |
340 GetNotificationDisplayService(profile)->Display(notification.delegate_id(), | 293 |
341 notification); | 294 GetNotificationDisplayService(profile)->Display( |
| 295 NotificationCommon::NON_PERSISTENT, notification.delegate_id(), |
| 296 notification); |
342 if (cancel_callback) { | 297 if (cancel_callback) { |
343 #if defined(OS_WIN) | 298 #if defined(OS_WIN) |
344 std::string profile_id = | 299 std::string profile_id = |
345 base::WideToUTF8(profile->GetPath().BaseName().value()); | 300 base::WideToUTF8(profile->GetPath().BaseName().value()); |
346 #elif defined(OS_POSIX) | 301 #elif defined(OS_POSIX) |
347 std::string profile_id = profile->GetPath().BaseName().value(); | 302 std::string profile_id = profile->GetPath().BaseName().value(); |
348 #endif | 303 #endif |
349 *cancel_callback = | 304 *cancel_callback = |
350 base::Bind(&CancelNotification, notification.delegate_id(), profile_id, | 305 base::Bind(&CancelNotification, notification.delegate_id(), profile_id, |
351 profile->IsOffTheRecord()); | 306 profile->IsOffTheRecord()); |
(...skipping 22 matching lines...) Expand all Loading... |
374 settings_button_index); | 329 settings_button_index); |
375 | 330 |
376 Notification notification = CreateNotificationFromData( | 331 Notification notification = CreateNotificationFromData( |
377 profile, origin, notification_data, notification_resources, delegate); | 332 profile, origin, notification_data, notification_resources, delegate); |
378 | 333 |
379 // TODO(peter): Remove this mapping when we have reliable id generation for | 334 // TODO(peter): Remove this mapping when we have reliable id generation for |
380 // the message_center::Notification objects. | 335 // the message_center::Notification objects. |
381 persistent_notifications_[persistent_notification_id] = notification.id(); | 336 persistent_notifications_[persistent_notification_id] = notification.id(); |
382 | 337 |
383 GetNotificationDisplayService(profile)->Display( | 338 GetNotificationDisplayService(profile)->Display( |
| 339 NotificationCommon::PERSISTENT, |
384 base::Int64ToString(delegate->persistent_notification_id()), | 340 base::Int64ToString(delegate->persistent_notification_id()), |
385 notification); | 341 notification); |
386 content::RecordAction( | 342 content::RecordAction( |
387 base::UserMetricsAction("Notifications.Persistent.Shown")); | 343 base::UserMetricsAction("Notifications.Persistent.Shown")); |
388 | 344 |
389 HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( | 345 HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( |
390 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 346 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
391 } | 347 } |
392 | 348 |
393 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 349 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
(...skipping 10 matching lines...) Expand all Loading... |
404 bool cancel_by_persistent_id = true; | 360 bool cancel_by_persistent_id = true; |
405 #else | 361 #else |
406 bool cancel_by_persistent_id = | 362 bool cancel_by_persistent_id = |
407 GetNotificationDisplayService(profile)->SupportsNotificationCenter(); | 363 GetNotificationDisplayService(profile)->SupportsNotificationCenter(); |
408 #endif | 364 #endif |
409 | 365 |
410 if (cancel_by_persistent_id) { | 366 if (cancel_by_persistent_id) { |
411 // TODO(peter): Remove this conversion when the notification ids are being | 367 // TODO(peter): Remove this conversion when the notification ids are being |
412 // generated by the caller of this method. | 368 // generated by the caller of this method. |
413 GetNotificationDisplayService(profile)->Close( | 369 GetNotificationDisplayService(profile)->Close( |
| 370 NotificationCommon::PERSISTENT, |
414 base::Int64ToString(persistent_notification_id)); | 371 base::Int64ToString(persistent_notification_id)); |
415 } else { | 372 } else { |
416 auto iter = persistent_notifications_.find(persistent_notification_id); | 373 auto iter = persistent_notifications_.find(persistent_notification_id); |
417 if (iter == persistent_notifications_.end()) | 374 if (iter == persistent_notifications_.end()) |
418 return; | 375 return; |
419 GetNotificationDisplayService(profile)->Close(iter->second); | 376 GetNotificationDisplayService(profile)->Close( |
| 377 NotificationCommon::PERSISTENT, iter->second); |
420 } | 378 } |
421 | 379 |
422 persistent_notifications_.erase(persistent_notification_id); | 380 persistent_notifications_.erase(persistent_notification_id); |
423 } | 381 } |
424 | 382 |
425 bool PlatformNotificationServiceImpl::GetDisplayedPersistentNotifications( | 383 bool PlatformNotificationServiceImpl::GetDisplayedPersistentNotifications( |
426 BrowserContext* browser_context, | 384 BrowserContext* browser_context, |
427 std::set<std::string>* displayed_notifications) { | 385 std::set<std::string>* displayed_notifications) { |
428 DCHECK(displayed_notifications); | 386 DCHECK(displayed_notifications); |
429 | 387 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 } | 496 } |
539 #endif | 497 #endif |
540 | 498 |
541 return base::string16(); | 499 return base::string16(); |
542 } | 500 } |
543 | 501 |
544 void PlatformNotificationServiceImpl::SetNotificationDisplayServiceForTesting( | 502 void PlatformNotificationServiceImpl::SetNotificationDisplayServiceForTesting( |
545 NotificationDisplayService* display_service) { | 503 NotificationDisplayService* display_service) { |
546 test_display_service_ = display_service; | 504 test_display_service_ = display_service; |
547 } | 505 } |
OLD | NEW |