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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/notifications/notification.h" | |
10 #include "chrome/browser/notifications/notification_object_proxy.h" | 9 #include "chrome/browser/notifications/notification_object_proxy.h" |
11 #include "chrome/browser/notifications/notification_ui_manager.h" | 10 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 11 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/profiles/profile_io_data.h" | 13 #include "chrome/browser/profiles/profile_io_data.h" |
14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
15 #include "components/content_settings/core/common/content_settings.h" | 15 #include "components/content_settings/core/common/content_settings.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/desktop_notification_delegate.h" | 17 #include "content/public/browser/desktop_notification_delegate.h" |
| 18 #include "content/public/browser/notification_event_dispatcher.h" |
18 #include "content/public/common/platform_notification_data.h" | 19 #include "content/public/common/platform_notification_data.h" |
19 #include "ui/message_center/notifier_settings.h" | 20 #include "ui/message_center/notifier_settings.h" |
20 | 21 |
21 #if defined(ENABLE_EXTENSIONS) | 22 #if defined(ENABLE_EXTENSIONS) |
22 #include "chrome/browser/notifications/desktop_notification_service.h" | 23 #include "chrome/browser/notifications/desktop_notification_service.h" |
23 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 24 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
24 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/info_map.h" | 26 #include "extensions/browser/info_map.h" |
26 #include "extensions/common/constants.h" | 27 #include "extensions/common/constants.h" |
27 #include "extensions/common/extension_set.h" | 28 #include "extensions/common/extension_set.h" |
(...skipping 16 matching lines...) Expand all Loading... |
44 PlatformNotificationServiceImpl* | 45 PlatformNotificationServiceImpl* |
45 PlatformNotificationServiceImpl::GetInstance() { | 46 PlatformNotificationServiceImpl::GetInstance() { |
46 return Singleton<PlatformNotificationServiceImpl>::get(); | 47 return Singleton<PlatformNotificationServiceImpl>::get(); |
47 } | 48 } |
48 | 49 |
49 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() | 50 PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() |
50 : notification_ui_manager_for_tests_(nullptr) {} | 51 : notification_ui_manager_for_tests_(nullptr) {} |
51 | 52 |
52 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} | 53 PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} |
53 | 54 |
| 55 void PlatformNotificationServiceImpl::OnPersistentNotificationClick( |
| 56 content::BrowserContext* browser_context, |
| 57 int64 service_worker_registration_id, |
| 58 const std::string& notification_id, |
| 59 const GURL& origin, |
| 60 const content::PlatformNotificationData& notification_data, |
| 61 const base::Callback<void(content::PersistentNotificationStatus)>& |
| 62 callback) const { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 content::NotificationEventDispatcher::GetInstance() |
| 65 ->DispatchNotificationClickEvent( |
| 66 browser_context, |
| 67 origin, |
| 68 service_worker_registration_id, |
| 69 notification_id, |
| 70 notification_data, |
| 71 callback); |
| 72 } |
| 73 |
54 blink::WebNotificationPermission | 74 blink::WebNotificationPermission |
55 PlatformNotificationServiceImpl::CheckPermission( | 75 PlatformNotificationServiceImpl::CheckPermission( |
56 content::ResourceContext* resource_context, | 76 content::ResourceContext* resource_context, |
57 const GURL& origin, | 77 const GURL& origin, |
58 int render_process_id) { | 78 int render_process_id) { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
60 | 80 |
61 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); | 81 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); |
62 #if defined(ENABLE_EXTENSIONS) | 82 #if defined(ENABLE_EXTENSIONS) |
63 extensions::InfoMap* extension_info_map = io_data->GetExtensionInfoMap(); | 83 extensions::InfoMap* extension_info_map = io_data->GetExtensionInfoMap(); |
64 | 84 |
65 // We want to see if there is an extension that hasn't been manually disabled | 85 // We want to see if there is an extension that hasn't been manually disabled |
66 // that has the notifications permission and applies to this security origin. | 86 // that has the notifications permission and applies to this security origin. |
67 // First, get the list of extensions with permission for the origin. | 87 // First, get the list of extensions with permission for the origin. |
68 extensions::ExtensionSet extensions; | 88 extensions::ExtensionSet extensions; |
69 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | 89 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( |
(...skipping 25 matching lines...) Expand all Loading... |
95 } | 115 } |
96 | 116 |
97 void PlatformNotificationServiceImpl::DisplayNotification( | 117 void PlatformNotificationServiceImpl::DisplayNotification( |
98 content::BrowserContext* browser_context, | 118 content::BrowserContext* browser_context, |
99 const GURL& origin, | 119 const GURL& origin, |
100 const SkBitmap& icon, | 120 const SkBitmap& icon, |
101 const content::PlatformNotificationData& notification_data, | 121 const content::PlatformNotificationData& notification_data, |
102 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 122 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
103 int render_process_id, | 123 int render_process_id, |
104 base::Closure* cancel_callback) { | 124 base::Closure* cancel_callback) { |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
106 | 126 |
107 Profile* profile = Profile::FromBrowserContext(browser_context); | 127 Profile* profile = Profile::FromBrowserContext(browser_context); |
108 DCHECK(profile); | 128 DCHECK(profile); |
109 | 129 |
110 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 130 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
111 base::string16 display_source = DisplayNameForOriginInProcessId( | 131 Notification notification = CreateNotificationFromData( |
112 profile, origin, render_process_id); | 132 profile, origin, icon, notification_data, proxy, render_process_id); |
113 | |
114 // TODO(peter): Icons for Web Notifications are currently always requested for | |
115 // 1x scale, whereas the displays on which they can be displayed can have a | |
116 // different pixel density. Be smarter about this when the API gets updated | |
117 // with a way for developers to specify images of different resolutions. | |
118 Notification notification(origin, notification_data.title, | |
119 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), | |
120 display_source, notification_data.tag, proxy); | |
121 | |
122 // Web Notifications do not timeout. | |
123 notification.set_never_timeout(true); | |
124 | 133 |
125 GetNotificationUIManager()->Add(notification, profile); | 134 GetNotificationUIManager()->Add(notification, profile); |
126 if (cancel_callback) | 135 if (cancel_callback) |
127 *cancel_callback = | 136 *cancel_callback = |
128 base::Bind(&CancelNotification, | 137 base::Bind(&CancelNotification, |
129 proxy->id(), | 138 notification.delegate_id(), |
130 NotificationUIManager::GetProfileID(profile)); | 139 NotificationUIManager::GetProfileID(profile)); |
131 | 140 |
132 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 141 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
133 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 142 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
134 } | 143 } |
135 | 144 |
136 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 145 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
137 content::BrowserContext* browser_context, | 146 content::BrowserContext* browser_context, |
138 int64 service_worker_registration_id, | 147 int64 service_worker_registration_id, |
139 const GURL& origin, | 148 const GURL& origin, |
140 const SkBitmap& icon, | 149 const SkBitmap& icon, |
141 const content::PlatformNotificationData& notification_data, | 150 const content::PlatformNotificationData& notification_data, |
142 int render_process_id) { | 151 int render_process_id) { |
143 NOTIMPLEMENTED(); | 152 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 153 |
| 154 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 155 DCHECK(profile); |
| 156 |
| 157 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
| 158 browser_context, |
| 159 service_worker_registration_id, |
| 160 origin, |
| 161 notification_data); |
| 162 |
| 163 Notification notification = CreateNotificationFromData( |
| 164 profile, origin, icon, notification_data, delegate, render_process_id); |
| 165 |
| 166 GetNotificationUIManager()->Add(notification, profile); |
| 167 |
| 168 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
| 169 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
144 } | 170 } |
145 | 171 |
146 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 172 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
147 content::BrowserContext* browser_context, | 173 content::BrowserContext* browser_context, |
148 const std::string& persistent_notification_id) { | 174 const std::string& persistent_notification_id) { |
149 NOTIMPLEMENTED(); | 175 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 176 |
| 177 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 178 DCHECK(profile); |
| 179 |
| 180 GetNotificationUIManager()->CancelById( |
| 181 persistent_notification_id, NotificationUIManager::GetProfileID(profile)); |
| 182 } |
| 183 |
| 184 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( |
| 185 Profile* profile, |
| 186 const GURL& origin, |
| 187 const SkBitmap& icon, |
| 188 const content::PlatformNotificationData& notification_data, |
| 189 NotificationDelegate* delegate, |
| 190 int render_process_id) const { |
| 191 base::string16 display_source = DisplayNameForOriginInProcessId( |
| 192 profile, origin, render_process_id); |
| 193 |
| 194 // TODO(peter): Icons for Web Notifications are currently always requested for |
| 195 // 1x scale, whereas the displays on which they can be displayed can have a |
| 196 // different pixel density. Be smarter about this when the API gets updated |
| 197 // with a way for developers to specify images of different resolutions. |
| 198 Notification notification(origin, notification_data.title, |
| 199 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), |
| 200 display_source, notification_data.tag, delegate); |
| 201 |
| 202 // Web Notifications do not timeout. |
| 203 notification.set_never_timeout(true); |
| 204 |
| 205 return notification; |
150 } | 206 } |
151 | 207 |
152 NotificationUIManager* | 208 NotificationUIManager* |
153 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 209 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
154 if (notification_ui_manager_for_tests_) | 210 if (notification_ui_manager_for_tests_) |
155 return notification_ui_manager_for_tests_; | 211 return notification_ui_manager_for_tests_; |
156 | 212 |
157 return g_browser_process->notification_ui_manager(); | 213 return g_browser_process->notification_ui_manager(); |
158 } | 214 } |
159 | 215 |
160 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 216 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
161 NotificationUIManager* manager) { | 217 NotificationUIManager* manager) { |
162 notification_ui_manager_for_tests_ = manager; | 218 notification_ui_manager_for_tests_ = manager; |
163 } | 219 } |
164 | 220 |
165 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( | 221 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( |
166 Profile* profile, const GURL& origin, int process_id) { | 222 Profile* profile, const GURL& origin, int process_id) const { |
167 #if defined(ENABLE_EXTENSIONS) | 223 #if defined(ENABLE_EXTENSIONS) |
168 // If the source is an extension, lookup the display name. | 224 // If the source is an extension, lookup the display name. |
169 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 225 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
170 extensions::InfoMap* extension_info_map = | 226 extensions::InfoMap* extension_info_map = |
171 extensions::ExtensionSystem::Get(profile)->info_map(); | 227 extensions::ExtensionSystem::Get(profile)->info_map(); |
172 if (extension_info_map) { | 228 if (extension_info_map) { |
173 extensions::ExtensionSet extensions; | 229 extensions::ExtensionSet extensions; |
174 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | 230 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( |
175 origin, | 231 origin, |
176 process_id, | 232 process_id, |
177 extensions::APIPermission::kNotifications, | 233 extensions::APIPermission::kNotifications, |
178 &extensions); | 234 &extensions); |
179 DesktopNotificationService* desktop_notification_service = | 235 DesktopNotificationService* desktop_notification_service = |
180 DesktopNotificationServiceFactory::GetForProfile(profile); | 236 DesktopNotificationServiceFactory::GetForProfile(profile); |
181 DCHECK(desktop_notification_service); | 237 DCHECK(desktop_notification_service); |
182 | 238 |
183 for (const auto& extension : extensions) { | 239 for (const auto& extension : extensions) { |
184 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | 240 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); |
185 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) | 241 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) |
186 return base::UTF8ToUTF16(extension->name()); | 242 return base::UTF8ToUTF16(extension->name()); |
187 } | 243 } |
188 } | 244 } |
189 } | 245 } |
190 #endif | 246 #endif |
191 | 247 |
192 return base::UTF8ToUTF16(origin.host()); | 248 return base::UTF8ToUTF16(origin.host()); |
193 } | 249 } |
OLD | NEW |