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 "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
11 #include "chrome/browser/notifications/notification_object_proxy.h" | 11 #include "chrome/browser/notifications/notification_object_proxy.h" |
12 #include "chrome/browser/notifications/notification_ui_manager.h" | 12 #include "chrome/browser/notifications/notification_ui_manager.h" |
13 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 13 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/profiles/profile_io_data.h" | 15 #include "chrome/browser/profiles/profile_io_data.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
18 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/desktop_notification_delegate.h" | 20 #include "content/public/browser/desktop_notification_delegate.h" |
21 #include "content/public/browser/notification_event_dispatcher.h" | 21 #include "content/public/browser/notification_event_dispatcher.h" |
22 #include "content/public/browser/platform_notification_context.h" | |
23 #include "content/public/browser/storage_partition.h" | |
22 #include "content/public/common/platform_notification_data.h" | 24 #include "content/public/common/platform_notification_data.h" |
23 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
24 #include "ui/message_center/notifier_settings.h" | 26 #include "ui/message_center/notifier_settings.h" |
25 #include "url/url_constants.h" | 27 #include "url/url_constants.h" |
26 | 28 |
27 #if defined(ENABLE_EXTENSIONS) | 29 #if defined(ENABLE_EXTENSIONS) |
28 #include "chrome/browser/notifications/desktop_notification_service.h" | 30 #include "chrome/browser/notifications/desktop_notification_service.h" |
29 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 31 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
30 #include "extensions/browser/extension_registry.h" | 32 #include "extensions/browser/extension_registry.h" |
31 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
32 #include "extensions/browser/info_map.h" | 34 #include "extensions/browser/info_map.h" |
33 #include "extensions/common/constants.h" | 35 #include "extensions/common/constants.h" |
34 #include "extensions/common/extension_set.h" | 36 #include "extensions/common/extension_set.h" |
35 #include "extensions/common/permissions/api_permission.h" | 37 #include "extensions/common/permissions/api_permission.h" |
36 #include "extensions/common/permissions/permissions_data.h" | 38 #include "extensions/common/permissions/permissions_data.h" |
37 #endif | 39 #endif |
38 | 40 |
39 #if defined(OS_ANDROID) | 41 #if defined(OS_ANDROID) |
40 #include "base/strings/string_number_conversions.h" | 42 #include "base/strings/string_number_conversions.h" |
41 #endif | 43 #endif |
42 | 44 |
45 using content::BrowserContext; | |
43 using content::BrowserThread; | 46 using content::BrowserThread; |
47 using content::PlatformNotificationContext; | |
44 using message_center::NotifierId; | 48 using message_center::NotifierId; |
45 | 49 |
46 namespace { | 50 namespace { |
47 | 51 |
52 // Callback to provide when deleting the data associated with persistent Web | |
53 // Notifications from the notification database. | |
54 void OnPersistentNotificationDataDeleted(bool success) { | |
55 // TODO(peter): Record UMA for notification deletion requests created by the | |
56 // PlatformNotificationService. | |
57 } | |
58 | |
48 void CancelNotification(const std::string& id, ProfileID profile_id) { | 59 void CancelNotification(const std::string& id, ProfileID profile_id) { |
49 PlatformNotificationServiceImpl::GetInstance() | 60 PlatformNotificationServiceImpl::GetInstance() |
50 ->GetNotificationUIManager()->CancelById(id, profile_id); | 61 ->GetNotificationUIManager()->CancelById(id, profile_id); |
51 } | 62 } |
52 | 63 |
53 } // namespace | 64 } // namespace |
54 | 65 |
55 // static | 66 // static |
56 PlatformNotificationServiceImpl* | 67 PlatformNotificationServiceImpl* |
57 PlatformNotificationServiceImpl::GetInstance() { | 68 PlatformNotificationServiceImpl::GetInstance() { |
58 return Singleton<PlatformNotificationServiceImpl>::get(); | 69 return Singleton<PlatformNotificationServiceImpl>::get(); |
59 } | 70 } |
60 | 71 |
61 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() | 72 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
62 : notification_ui_manager_for_tests_(nullptr) {} | 73 : notification_ui_manager_for_tests_(nullptr) {} |
63 | 74 |
64 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 75 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
65 | 76 |
66 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( | 77 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( |
67 content::BrowserContext* browser_context, | 78 BrowserContext* browser_context, |
68 int64_t persistent_notification_id, | 79 int64_t persistent_notification_id, |
69 const GURL& origin, | 80 const GURL& origin, |
70 const base::Callback<void(content::PersistentNotificationStatus)>& | 81 const base::Callback<void(content::PersistentNotificationStatus)>& |
71 callback) const { | 82 callback) const { |
72 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
73 content::NotificationEventDispatcher::GetInstance() | 84 content::NotificationEventDispatcher::GetInstance() |
74 ->DispatchNotificationClickEvent( | 85 ->DispatchNotificationClickEvent( |
75 browser_context, | 86 browser_context, |
76 persistent_notification_id, | 87 persistent_notification_id, |
77 origin, | 88 origin, |
78 callback); | 89 callback); |
79 } | 90 } |
80 | 91 |
92 void PlatformNotificationServiceImpl::OnPersistentNotificationClose( | |
93 BrowserContext* browser_context, | |
94 int64_t persistent_notification_id, | |
95 const GURL& origin) const { | |
96 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
97 PlatformNotificationContext* context = | |
98 BrowserContext::GetStoragePartitionForSite(browser_context, origin) | |
99 ->GetPlatformNotificationContext(); | |
100 | |
101 BrowserThread::PostTask( | |
102 BrowserThread::IO, | |
103 FROM_HERE, | |
104 base::Bind(&PlatformNotificationContext::DeleteNotificationData, | |
105 base::Unretained(context), | |
johnme
2015/04/10 11:54:39
This doesn't look safe. Possible solution: move th
dewittj
2015/04/10 17:18:30
Other classes hanging off of StoragePartition use
Peter Beverloo
2015/04/20 16:47:20
Done.
| |
106 persistent_notification_id, | |
107 origin, | |
108 base::Bind(&OnPersistentNotificationDataDeleted))); | |
109 } | |
110 | |
81 blink::WebNotificationPermission | 111 blink::WebNotificationPermission |
82 PlatformNotificationServiceImpl::CheckPermissionOnUIThread( | 112 PlatformNotificationServiceImpl::CheckPermissionOnUIThread( |
83 content::BrowserContext* browser_context, | 113 BrowserContext* browser_context, |
84 const GURL& origin, | 114 const GURL& origin, |
85 int render_process_id) { | 115 int render_process_id) { |
86 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
87 | 117 |
88 Profile* profile = Profile::FromBrowserContext(browser_context); | 118 Profile* profile = Profile::FromBrowserContext(browser_context); |
89 DCHECK(profile); | 119 DCHECK(profile); |
90 | 120 |
91 #if defined(ENABLE_EXTENSIONS) | 121 #if defined(ENABLE_EXTENSIONS) |
92 // Extensions support an API permission named "notification". This will grant | 122 // Extensions support an API permission named "notification". This will grant |
93 // not only grant permission for using the Chrome App extension API, but also | 123 // not only grant permission for using the Chrome App extension API, but also |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 | 213 |
184 if (setting == CONTENT_SETTING_ALLOW) | 214 if (setting == CONTENT_SETTING_ALLOW) |
185 return blink::WebNotificationPermissionAllowed; | 215 return blink::WebNotificationPermissionAllowed; |
186 if (setting == CONTENT_SETTING_BLOCK) | 216 if (setting == CONTENT_SETTING_BLOCK) |
187 return blink::WebNotificationPermissionDenied; | 217 return blink::WebNotificationPermissionDenied; |
188 | 218 |
189 return blink::WebNotificationPermissionDefault; | 219 return blink::WebNotificationPermissionDefault; |
190 } | 220 } |
191 | 221 |
192 void PlatformNotificationServiceImpl::DisplayNotification( | 222 void PlatformNotificationServiceImpl::DisplayNotification( |
193 content::BrowserContext* browser_context, | 223 BrowserContext* browser_context, |
194 const GURL& origin, | 224 const GURL& origin, |
195 const SkBitmap& icon, | 225 const SkBitmap& icon, |
196 const content::PlatformNotificationData& notification_data, | 226 const content::PlatformNotificationData& notification_data, |
197 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 227 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
198 base::Closure* cancel_callback) { | 228 base::Closure* cancel_callback) { |
199 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 229 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
200 | 230 |
201 Profile* profile = Profile::FromBrowserContext(browser_context); | 231 Profile* profile = Profile::FromBrowserContext(browser_context); |
202 DCHECK(profile); | 232 DCHECK(profile); |
203 | 233 |
204 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 234 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
205 Notification notification = CreateNotificationFromData( | 235 Notification notification = CreateNotificationFromData( |
206 profile, origin, icon, notification_data, proxy); | 236 profile, origin, icon, notification_data, proxy); |
207 | 237 |
208 GetNotificationUIManager()->Add(notification, profile); | 238 GetNotificationUIManager()->Add(notification, profile); |
209 if (cancel_callback) | 239 if (cancel_callback) |
210 *cancel_callback = | 240 *cancel_callback = |
211 base::Bind(&CancelNotification, | 241 base::Bind(&CancelNotification, |
212 notification.delegate_id(), | 242 notification.delegate_id(), |
213 NotificationUIManager::GetProfileID(profile)); | 243 NotificationUIManager::GetProfileID(profile)); |
214 | 244 |
215 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 245 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
216 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 246 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
217 } | 247 } |
218 | 248 |
219 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 249 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
220 content::BrowserContext* browser_context, | 250 BrowserContext* browser_context, |
221 int64_t persistent_notification_id, | 251 int64_t persistent_notification_id, |
222 const GURL& origin, | 252 const GURL& origin, |
223 const SkBitmap& icon, | 253 const SkBitmap& icon, |
224 const content::PlatformNotificationData& notification_data) { | 254 const content::PlatformNotificationData& notification_data) { |
225 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
226 | 256 |
227 Profile* profile = Profile::FromBrowserContext(browser_context); | 257 Profile* profile = Profile::FromBrowserContext(browser_context); |
228 DCHECK(profile); | 258 DCHECK(profile); |
229 | 259 |
230 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( | 260 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
231 browser_context, persistent_notification_id, origin); | 261 browser_context, persistent_notification_id, origin); |
232 | 262 |
233 Notification notification = CreateNotificationFromData( | 263 Notification notification = CreateNotificationFromData( |
234 profile, origin, icon, notification_data, delegate); | 264 profile, origin, icon, notification_data, delegate); |
235 | 265 |
236 // TODO(peter): Remove this mapping when we have reliable id generation for | 266 // TODO(peter): Remove this mapping when we have reliable id generation for |
237 // the message_center::Notification objects. | 267 // the message_center::Notification objects. |
238 persistent_notifications_[persistent_notification_id] = notification.id(); | 268 persistent_notifications_[persistent_notification_id] = notification.id(); |
239 | 269 |
240 GetNotificationUIManager()->Add(notification, profile); | 270 GetNotificationUIManager()->Add(notification, profile); |
241 | 271 |
242 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 272 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
243 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 273 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
244 } | 274 } |
245 | 275 |
246 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 276 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
247 content::BrowserContext* browser_context, | 277 BrowserContext* browser_context, |
248 int64_t persistent_notification_id) { | 278 int64_t persistent_notification_id) { |
249 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 279 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
250 | 280 |
251 Profile* profile = Profile::FromBrowserContext(browser_context); | 281 Profile* profile = Profile::FromBrowserContext(browser_context); |
252 DCHECK(profile); | 282 DCHECK(profile); |
253 | 283 |
254 auto iter = persistent_notifications_.find(persistent_notification_id); | 284 auto iter = persistent_notifications_.find(persistent_notification_id); |
255 if (iter == persistent_notifications_.end()) { | 285 if (iter == persistent_notifications_.end()) { |
256 #if defined(OS_ANDROID) | 286 #if defined(OS_ANDROID) |
257 // TODO(peter): Remove this conversion when the notification ids are being | 287 // TODO(peter): Remove this conversion when the notification ids are being |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 formatted_origin.push_back(':'); | 379 formatted_origin.push_back(':'); |
350 formatted_origin.append(base::UTF8ToUTF16(origin.port())); | 380 formatted_origin.append(base::UTF8ToUTF16(origin.port())); |
351 } | 381 } |
352 return formatted_origin; | 382 return formatted_origin; |
353 } | 383 } |
354 | 384 |
355 // TODO(dewittj): Once file:// URLs are passed in to the origin | 385 // TODO(dewittj): Once file:// URLs are passed in to the origin |
356 // GURL here, begin returning the path as the display name. | 386 // GURL here, begin returning the path as the display name. |
357 return net::FormatUrl(origin, languages); | 387 return net::FormatUrl(origin, languages); |
358 } | 388 } |
OLD | NEW |