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/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/notifications/notification_object_proxy.h" | 10 #include "chrome/browser/notifications/notification_object_proxy.h" |
10 #include "chrome/browser/notifications/notification_ui_manager.h" | 11 #include "chrome/browser/notifications/notification_ui_manager.h" |
11 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 12 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/profiles/profile_io_data.h" | 14 #include "chrome/browser/profiles/profile_io_data.h" |
| 15 #include "chrome/common/pref_names.h" |
14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
15 #include "components/content_settings/core/common/content_settings.h" | 17 #include "components/content_settings/core/common/content_settings.h" |
16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/desktop_notification_delegate.h" | 19 #include "content/public/browser/desktop_notification_delegate.h" |
18 #include "content/public/browser/notification_event_dispatcher.h" | 20 #include "content/public/browser/notification_event_dispatcher.h" |
19 #include "content/public/common/platform_notification_data.h" | 21 #include "content/public/common/platform_notification_data.h" |
| 22 #include "net/base/net_util.h" |
20 #include "ui/message_center/notifier_settings.h" | 23 #include "ui/message_center/notifier_settings.h" |
21 | 24 |
22 #if defined(ENABLE_EXTENSIONS) | 25 #if defined(ENABLE_EXTENSIONS) |
23 #include "chrome/browser/notifications/desktop_notification_service.h" | 26 #include "chrome/browser/notifications/desktop_notification_service.h" |
24 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 27 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 28 #include "extensions/browser/extension_registry.h" |
25 #include "extensions/browser/extension_system.h" | 29 #include "extensions/browser/extension_system.h" |
26 #include "extensions/browser/info_map.h" | 30 #include "extensions/browser/info_map.h" |
27 #include "extensions/common/constants.h" | 31 #include "extensions/common/constants.h" |
28 #include "extensions/common/extension_set.h" | 32 #include "extensions/common/extension_set.h" |
29 #include "extensions/common/permissions/api_permission.h" | 33 #include "extensions/common/permissions/api_permission.h" |
30 #endif | 34 #endif |
31 | 35 |
32 using content::BrowserThread; | 36 using content::BrowserThread; |
33 using message_center::NotifierId; | 37 using message_center::NotifierId; |
34 | 38 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 117 |
114 return blink::WebNotificationPermissionDefault; | 118 return blink::WebNotificationPermissionDefault; |
115 } | 119 } |
116 | 120 |
117 void PlatformNotificationServiceImpl::DisplayNotification( | 121 void PlatformNotificationServiceImpl::DisplayNotification( |
118 content::BrowserContext* browser_context, | 122 content::BrowserContext* browser_context, |
119 const GURL& origin, | 123 const GURL& origin, |
120 const SkBitmap& icon, | 124 const SkBitmap& icon, |
121 const content::PlatformNotificationData& notification_data, | 125 const content::PlatformNotificationData& notification_data, |
122 scoped_ptr<content::DesktopNotificationDelegate> delegate, | 126 scoped_ptr<content::DesktopNotificationDelegate> delegate, |
123 int render_process_id, | |
124 base::Closure* cancel_callback) { | 127 base::Closure* cancel_callback) { |
125 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
126 | 129 |
127 Profile* profile = Profile::FromBrowserContext(browser_context); | 130 Profile* profile = Profile::FromBrowserContext(browser_context); |
128 DCHECK(profile); | 131 DCHECK(profile); |
129 | 132 |
130 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); | 133 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); |
131 Notification notification = CreateNotificationFromData( | 134 Notification notification = CreateNotificationFromData( |
132 profile, origin, icon, notification_data, proxy, render_process_id); | 135 profile, origin, icon, notification_data, proxy); |
133 | 136 |
134 GetNotificationUIManager()->Add(notification, profile); | 137 GetNotificationUIManager()->Add(notification, profile); |
135 if (cancel_callback) | 138 if (cancel_callback) |
136 *cancel_callback = | 139 *cancel_callback = |
137 base::Bind(&CancelNotification, | 140 base::Bind(&CancelNotification, |
138 notification.delegate_id(), | 141 notification.delegate_id(), |
139 NotificationUIManager::GetProfileID(profile)); | 142 NotificationUIManager::GetProfileID(profile)); |
140 | 143 |
141 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 144 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
142 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 145 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
143 } | 146 } |
144 | 147 |
145 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 148 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |
146 content::BrowserContext* browser_context, | 149 content::BrowserContext* browser_context, |
147 int64 service_worker_registration_id, | 150 int64 service_worker_registration_id, |
148 const GURL& origin, | 151 const GURL& origin, |
149 const SkBitmap& icon, | 152 const SkBitmap& icon, |
150 const content::PlatformNotificationData& notification_data, | 153 const content::PlatformNotificationData& notification_data) { |
151 int render_process_id) { | |
152 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 154 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
153 | 155 |
154 Profile* profile = Profile::FromBrowserContext(browser_context); | 156 Profile* profile = Profile::FromBrowserContext(browser_context); |
155 DCHECK(profile); | 157 DCHECK(profile); |
156 | 158 |
157 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( | 159 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |
158 browser_context, | 160 browser_context, |
159 service_worker_registration_id, | 161 service_worker_registration_id, |
160 origin, | 162 origin, |
161 notification_data); | 163 notification_data); |
162 | 164 |
163 Notification notification = CreateNotificationFromData( | 165 Notification notification = CreateNotificationFromData( |
164 profile, origin, icon, notification_data, delegate, render_process_id); | 166 profile, origin, icon, notification_data, delegate); |
165 | 167 |
166 GetNotificationUIManager()->Add(notification, profile); | 168 GetNotificationUIManager()->Add(notification, profile); |
167 | 169 |
168 profile->GetHostContentSettingsMap()->UpdateLastUsage( | 170 profile->GetHostContentSettingsMap()->UpdateLastUsage( |
169 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 171 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
170 } | 172 } |
171 | 173 |
172 void PlatformNotificationServiceImpl::ClosePersistentNotification( | 174 void PlatformNotificationServiceImpl::ClosePersistentNotification( |
173 content::BrowserContext* browser_context, | 175 content::BrowserContext* browser_context, |
174 const std::string& persistent_notification_id) { | 176 const std::string& persistent_notification_id) { |
175 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
176 | 178 |
177 Profile* profile = Profile::FromBrowserContext(browser_context); | 179 Profile* profile = Profile::FromBrowserContext(browser_context); |
178 DCHECK(profile); | 180 DCHECK(profile); |
179 | 181 |
180 GetNotificationUIManager()->CancelById( | 182 GetNotificationUIManager()->CancelById( |
181 persistent_notification_id, NotificationUIManager::GetProfileID(profile)); | 183 persistent_notification_id, NotificationUIManager::GetProfileID(profile)); |
182 } | 184 } |
183 | 185 |
184 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( | 186 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( |
185 Profile* profile, | 187 Profile* profile, |
186 const GURL& origin, | 188 const GURL& origin, |
187 const SkBitmap& icon, | 189 const SkBitmap& icon, |
188 const content::PlatformNotificationData& notification_data, | 190 const content::PlatformNotificationData& notification_data, |
189 NotificationDelegate* delegate, | 191 NotificationDelegate* delegate) const { |
190 int render_process_id) const { | 192 base::string16 display_source = DisplayNameForOrigin(profile, origin); |
191 base::string16 display_source = DisplayNameForOriginInProcessId( | |
192 profile, origin, render_process_id); | |
193 | 193 |
194 // TODO(peter): Icons for Web Notifications are currently always requested for | 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 | 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 | 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. | 197 // with a way for developers to specify images of different resolutions. |
198 Notification notification(origin, notification_data.title, | 198 Notification notification(origin, notification_data.title, |
199 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), | 199 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), |
200 display_source, notification_data.tag, delegate); | 200 display_source, notification_data.tag, delegate); |
201 | 201 |
| 202 notification.set_context_message(display_source); |
| 203 |
202 // Web Notifications do not timeout. | 204 // Web Notifications do not timeout. |
203 notification.set_never_timeout(true); | 205 notification.set_never_timeout(true); |
204 | 206 |
205 return notification; | 207 return notification; |
206 } | 208 } |
207 | 209 |
208 NotificationUIManager* | 210 NotificationUIManager* |
209 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 211 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
210 if (notification_ui_manager_for_tests_) | 212 if (notification_ui_manager_for_tests_) |
211 return notification_ui_manager_for_tests_; | 213 return notification_ui_manager_for_tests_; |
212 | 214 |
213 return g_browser_process->notification_ui_manager(); | 215 return g_browser_process->notification_ui_manager(); |
214 } | 216 } |
215 | 217 |
216 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 218 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
217 NotificationUIManager* manager) { | 219 NotificationUIManager* manager) { |
218 notification_ui_manager_for_tests_ = manager; | 220 notification_ui_manager_for_tests_ = manager; |
219 } | 221 } |
220 | 222 |
221 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( | 223 base::string16 PlatformNotificationServiceImpl::DisplayNameForOrigin( |
222 Profile* profile, const GURL& origin, int process_id) const { | 224 Profile* profile, |
| 225 const GURL& origin) const { |
223 #if defined(ENABLE_EXTENSIONS) | 226 #if defined(ENABLE_EXTENSIONS) |
224 // If the source is an extension, lookup the display name. | 227 // If the source is an extension, lookup the display name. |
225 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 228 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
226 extensions::InfoMap* extension_info_map = | 229 const extensions::Extension* extension = |
227 extensions::ExtensionSystem::Get(profile)->info_map(); | 230 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( |
228 if (extension_info_map) { | 231 origin.host(), extensions::ExtensionRegistry::EVERYTHING); |
229 extensions::ExtensionSet extensions; | 232 DCHECK(extension); |
230 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | |
231 origin, | |
232 process_id, | |
233 extensions::APIPermission::kNotifications, | |
234 &extensions); | |
235 DesktopNotificationService* desktop_notification_service = | |
236 DesktopNotificationServiceFactory::GetForProfile(profile); | |
237 DCHECK(desktop_notification_service); | |
238 | 233 |
239 for (const auto& extension : extensions) { | 234 return base::UTF8ToUTF16(extension->name()); |
240 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | |
241 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) | |
242 return base::UTF8ToUTF16(extension->name()); | |
243 } | |
244 } | |
245 } | 235 } |
246 #endif | 236 #endif |
247 | 237 |
248 return base::UTF8ToUTF16(origin.host()); | 238 std::string languages = |
| 239 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 240 |
| 241 return WebOriginDisplayName(origin, languages); |
249 } | 242 } |
| 243 |
| 244 // static |
| 245 base::string16 PlatformNotificationServiceImpl::WebOriginDisplayName( |
| 246 const GURL& origin, |
| 247 const std::string& languages) { |
| 248 if (origin.SchemeIsHTTPOrHTTPS()) { |
| 249 base::string16 formatted_origin = |
| 250 net::IDNToUnicode(origin.host(), languages); |
| 251 if (origin.has_port()) { |
| 252 formatted_origin.push_back(':'); |
| 253 formatted_origin.append(base::UTF8ToUTF16(origin.port())); |
| 254 } |
| 255 return formatted_origin; |
| 256 } |
| 257 |
| 258 // TODO(dewittj): Once file:// URLs are passed in to the origin |
| 259 // GURL here, begin returning the path as the display name. |
| 260 return net::FormatUrl(origin, languages); |
| 261 } |
OLD | NEW |