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/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 return false; | 316 return false; |
317 #endif // !defined(OS_ANDROID) | 317 #endif // !defined(OS_ANDROID) |
318 } | 318 } |
319 | 319 |
320 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( | 320 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( |
321 Profile* profile, | 321 Profile* profile, |
322 const GURL& origin, | 322 const GURL& origin, |
323 const SkBitmap& icon, | 323 const SkBitmap& icon, |
324 const content::PlatformNotificationData& notification_data, | 324 const content::PlatformNotificationData& notification_data, |
325 NotificationDelegate* delegate) const { | 325 NotificationDelegate* delegate) const { |
326 base::string16 display_source = DisplayNameForOrigin(profile, origin); | |
327 | |
328 // TODO(peter): Icons for Web Notifications are currently always requested for | 326 // TODO(peter): Icons for Web Notifications are currently always requested for |
329 // 1x scale, whereas the displays on which they can be displayed can have a | 327 // 1x scale, whereas the displays on which they can be displayed can have a |
330 // different pixel density. Be smarter about this when the API gets updated | 328 // different pixel density. Be smarter about this when the API gets updated |
331 // with a way for developers to specify images of different resolutions. | 329 // with a way for developers to specify images of different resolutions. |
332 Notification notification(origin, notification_data.title, | 330 Notification notification( |
333 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), | 331 origin, notification_data.title, notification_data.body, |
334 display_source, notification_data.tag, delegate); | 332 gfx::Image::CreateFrom1xBitmap(icon), base::UTF8ToUTF16(origin.host()), |
| 333 notification_data.tag, delegate); |
335 | 334 |
336 notification.set_context_message(display_source); | 335 std::string display_source = DisplayNameForOrigin(profile, origin); |
| 336 #if defined(ENABLE_EXTENSIONS) |
| 337 notification.set_context_message( |
| 338 display_source, !origin.SchemeIs(extensions::kExtensionScheme)); |
| 339 #else |
| 340 notification.set_context_message(display_source, true); |
| 341 #endif |
337 notification.set_vibration_pattern(notification_data.vibration_pattern); | 342 notification.set_vibration_pattern(notification_data.vibration_pattern); |
338 notification.set_silent(notification_data.silent); | 343 notification.set_silent(notification_data.silent); |
339 | 344 |
340 std::vector<message_center::ButtonInfo> buttons; | 345 std::vector<message_center::ButtonInfo> buttons; |
341 for (const auto& action : notification_data.actions) | 346 for (const auto& action : notification_data.actions) |
342 buttons.push_back(message_center::ButtonInfo(action.title)); | 347 buttons.push_back(message_center::ButtonInfo(action.title)); |
343 | 348 |
344 notification.set_buttons(buttons); | 349 notification.set_buttons(buttons); |
345 | 350 |
346 // Web Notifications do not timeout. | 351 // Web Notifications do not timeout. |
347 notification.set_never_timeout(true); | 352 notification.set_never_timeout(true); |
348 | 353 |
349 return notification; | 354 return notification; |
350 } | 355 } |
351 | 356 |
352 NotificationUIManager* | 357 NotificationUIManager* |
353 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 358 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
354 if (notification_ui_manager_for_tests_) | 359 if (notification_ui_manager_for_tests_) |
355 return notification_ui_manager_for_tests_; | 360 return notification_ui_manager_for_tests_; |
356 | 361 |
357 return g_browser_process->notification_ui_manager(); | 362 return g_browser_process->notification_ui_manager(); |
358 } | 363 } |
359 | 364 |
360 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 365 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
361 NotificationUIManager* manager) { | 366 NotificationUIManager* manager) { |
362 notification_ui_manager_for_tests_ = manager; | 367 notification_ui_manager_for_tests_ = manager; |
363 } | 368 } |
364 | 369 |
365 base::string16 PlatformNotificationServiceImpl::DisplayNameForOrigin( | 370 std::string PlatformNotificationServiceImpl::DisplayNameForOrigin( |
366 Profile* profile, | 371 Profile* profile, |
367 const GURL& origin) const { | 372 const GURL& origin) const { |
368 #if defined(ENABLE_EXTENSIONS) | 373 #if defined(ENABLE_EXTENSIONS) |
369 // If the source is an extension, lookup the display name. | 374 // If the source is an extension, lookup the display name. |
370 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 375 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
371 const extensions::Extension* extension = | 376 const extensions::Extension* extension = |
372 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( | 377 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( |
373 origin.host(), extensions::ExtensionRegistry::EVERYTHING); | 378 origin.host(), extensions::ExtensionRegistry::EVERYTHING); |
374 DCHECK(extension); | 379 DCHECK(extension); |
375 | 380 |
376 return base::UTF8ToUTF16(extension->name()); | 381 return extension->name(); |
377 } | 382 } |
378 #endif | 383 #endif |
379 | 384 |
380 std::string languages = | 385 return origin.spec(); |
381 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
382 | |
383 return WebOriginDisplayName(origin, languages); | |
384 } | 386 } |
385 | |
386 // static | |
387 // TODO(palmer): It might be good to replace this with a call to | |
388 // |FormatUrlForSecurityDisplay|. crbug.com/496965 | |
389 base::string16 PlatformNotificationServiceImpl::WebOriginDisplayName( | |
390 const GURL& origin, | |
391 const std::string& languages) { | |
392 if (origin.SchemeIsHTTPOrHTTPS()) { | |
393 base::string16 formatted_origin; | |
394 if (origin.SchemeIs(url::kHttpScheme)) { | |
395 const url::Parsed& parsed = origin.parsed_for_possibly_invalid_spec(); | |
396 const std::string& spec = origin.possibly_invalid_spec(); | |
397 formatted_origin.append( | |
398 spec.begin(), | |
399 spec.begin() + | |
400 parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); | |
401 } | |
402 formatted_origin.append( | |
403 url_formatter::IDNToUnicode(origin.host(), languages)); | |
404 if (origin.has_port()) { | |
405 formatted_origin.push_back(':'); | |
406 formatted_origin.append(base::UTF8ToUTF16(origin.port())); | |
407 } | |
408 return formatted_origin; | |
409 } | |
410 | |
411 // TODO(dewittj): Once file:// URLs are passed in to the origin | |
412 // GURL here, begin returning the path as the display name. | |
413 return url_formatter::FormatUrl(origin, languages); | |
414 } | |
OLD | NEW |