Chromium Code Reviews| 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" |
| 25 #include "extensions/browser/extension_system.h" | 28 #include "extensions/browser/extension_system.h" |
| 26 #include "extensions/browser/info_map.h" | 29 #include "extensions/browser/info_map.h" |
| 27 #include "extensions/common/constants.h" | 30 #include "extensions/common/constants.h" |
| 28 #include "extensions/common/extension_set.h" | 31 #include "extensions/common/extension_set.h" |
| 29 #include "extensions/common/permissions/api_permission.h" | 32 #include "extensions/common/permissions/api_permission.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 profile, origin, render_process_id); | 195 profile, origin, render_process_id); |
| 193 | 196 |
| 194 // TODO(peter): Icons for Web Notifications are currently always requested for | 197 // 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 | 198 // 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 | 199 // different pixel density. Be smarter about this when the API gets updated |
| 197 // with a way for developers to specify images of different resolutions. | 200 // with a way for developers to specify images of different resolutions. |
| 198 Notification notification(origin, notification_data.title, | 201 Notification notification(origin, notification_data.title, |
| 199 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), | 202 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), |
| 200 display_source, notification_data.tag, delegate); | 203 display_source, notification_data.tag, delegate); |
| 201 | 204 |
| 205 notification.set_context_message(display_source); | |
| 206 | |
| 202 // Web Notifications do not timeout. | 207 // Web Notifications do not timeout. |
| 203 notification.set_never_timeout(true); | 208 notification.set_never_timeout(true); |
| 204 | 209 |
| 205 return notification; | 210 return notification; |
| 206 } | 211 } |
| 207 | 212 |
| 208 NotificationUIManager* | 213 NotificationUIManager* |
| 209 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 214 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
| 210 if (notification_ui_manager_for_tests_) | 215 if (notification_ui_manager_for_tests_) |
| 211 return notification_ui_manager_for_tests_; | 216 return notification_ui_manager_for_tests_; |
| 212 | 217 |
| 213 return g_browser_process->notification_ui_manager(); | 218 return g_browser_process->notification_ui_manager(); |
| 214 } | 219 } |
| 215 | 220 |
| 216 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 221 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
| 217 NotificationUIManager* manager) { | 222 NotificationUIManager* manager) { |
| 218 notification_ui_manager_for_tests_ = manager; | 223 notification_ui_manager_for_tests_ = manager; |
| 219 } | 224 } |
| 220 | 225 |
| 221 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( | 226 base::string16 PlatformNotificationServiceImpl::DisplayNameForOriginInProcessId( |
| 222 Profile* profile, const GURL& origin, int process_id) const { | 227 Profile* profile, |
| 228 const GURL& origin, | |
| 229 int process_id) const { | |
| 223 #if defined(ENABLE_EXTENSIONS) | 230 #if defined(ENABLE_EXTENSIONS) |
| 224 // If the source is an extension, lookup the display name. | 231 // If the source is an extension, lookup the display name. |
| 225 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 232 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
| 226 extensions::InfoMap* extension_info_map = | 233 base::string16 extension_display_name; |
| 227 extensions::ExtensionSystem::Get(profile)->info_map(); | 234 if (ExtensionDisplayName(profile, origin, process_id, |
| 228 if (extension_info_map) { | 235 &extension_display_name)) { |
| 229 extensions::ExtensionSet extensions; | 236 return extension_display_name; |
| 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 | |
| 239 for (const auto& extension : extensions) { | |
| 240 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | |
| 241 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) | |
| 242 return base::UTF8ToUTF16(extension->name()); | |
| 243 } | |
| 244 } | 237 } |
| 245 } | 238 } |
| 246 #endif | 239 #endif |
| 247 | 240 |
| 248 return base::UTF8ToUTF16(origin.host()); | 241 std::string languages = |
| 242 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
| 243 | |
| 244 return OriginDisplayName(origin, languages); | |
| 249 } | 245 } |
| 246 | |
| 247 #if defined(ENABLE_EXTENSIONS) | |
| 248 bool PlatformNotificationServiceImpl::ExtensionDisplayName( | |
| 249 Profile* profile, | |
| 250 const GURL& origin, | |
| 251 int process_id, | |
| 252 base::string16* out) const { | |
| 253 DCHECK(origin.SchemeIs(extensions::kExtensionScheme)); | |
| 254 DCHECK(out); | |
| 255 | |
| 256 extensions::InfoMap* extension_info_map = | |
| 257 extensions::ExtensionSystem::Get(profile)->info_map(); | |
|
Peter Beverloo
2015/01/10 00:03:33
This is the code that's violating threading constr
dewittj
2015/01/13 22:18:01
Yes, I know this is the threading problem; just mo
Peter Beverloo
2015/01/14 19:18:14
That seems good to me. If this does ever happen in
| |
| 258 if (extension_info_map) { | |
| 259 extensions::ExtensionSet extensions; | |
| 260 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( | |
| 261 origin, process_id, extensions::APIPermission::kNotifications, | |
| 262 &extensions); | |
| 263 | |
| 264 DesktopNotificationService* desktop_notification_service = | |
| 265 DesktopNotificationServiceFactory::GetForProfile(profile); | |
| 266 DCHECK(desktop_notification_service); | |
| 267 | |
| 268 for (auto& extension : extensions) { | |
| 269 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | |
| 270 if (desktop_notification_service->IsNotifierEnabled(notifier_id)) { | |
| 271 *out = base::UTF8ToUTF16(extension->name()); | |
| 272 return true; | |
| 273 } | |
| 274 } | |
| 275 } | |
| 276 return false; | |
| 277 } | |
| 278 #endif // defined(ENABLE_EXTENSIONS) | |
| 279 | |
| 280 // static | |
| 281 base::string16 PlatformNotificationServiceImpl::OriginDisplayName( | |
| 282 const GURL& origin, | |
| 283 const std::string& languages) { | |
| 284 if (origin.SchemeIsHTTPOrHTTPS()) { | |
| 285 base::string16 formatted_origin = | |
| 286 net::IDNToUnicode(origin.host(), languages); | |
| 287 if (origin.has_port()) { | |
| 288 formatted_origin.push_back(':'); | |
| 289 formatted_origin.append(base::UTF8ToUTF16(origin.port())); | |
| 290 } | |
| 291 return formatted_origin; | |
| 292 } | |
| 293 | |
| 294 // TODO(dewittj): Once file:// URLs are passed in to the origin | |
| 295 // GURL here, begin returning the path as the display name. | |
| 296 return net::FormatUrl(origin, languages); | |
| 297 } | |
| 298 | |
| 299 | |
| OLD | NEW |