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 notification.set_context_message( |
| 336 DisplayNameForContextMessage(profile, origin)); |
337 notification.set_vibration_pattern(notification_data.vibration_pattern); | 337 notification.set_vibration_pattern(notification_data.vibration_pattern); |
338 notification.set_silent(notification_data.silent); | 338 notification.set_silent(notification_data.silent); |
339 | 339 |
340 std::vector<message_center::ButtonInfo> buttons; | 340 std::vector<message_center::ButtonInfo> buttons; |
341 for (const auto& action : notification_data.actions) | 341 for (const auto& action : notification_data.actions) |
342 buttons.push_back(message_center::ButtonInfo(action.title)); | 342 buttons.push_back(message_center::ButtonInfo(action.title)); |
343 | 343 |
344 notification.set_buttons(buttons); | 344 notification.set_buttons(buttons); |
345 | 345 |
346 // Web Notifications do not timeout. | 346 // Web Notifications do not timeout. |
347 notification.set_never_timeout(true); | 347 notification.set_never_timeout(true); |
348 | 348 |
349 return notification; | 349 return notification; |
350 } | 350 } |
351 | 351 |
352 NotificationUIManager* | 352 NotificationUIManager* |
353 PlatformNotificationServiceImpl::GetNotificationUIManager() const { | 353 PlatformNotificationServiceImpl::GetNotificationUIManager() const { |
354 if (notification_ui_manager_for_tests_) | 354 if (notification_ui_manager_for_tests_) |
355 return notification_ui_manager_for_tests_; | 355 return notification_ui_manager_for_tests_; |
356 | 356 |
357 return g_browser_process->notification_ui_manager(); | 357 return g_browser_process->notification_ui_manager(); |
358 } | 358 } |
359 | 359 |
360 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( | 360 void PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting( |
361 NotificationUIManager* manager) { | 361 NotificationUIManager* manager) { |
362 notification_ui_manager_for_tests_ = manager; | 362 notification_ui_manager_for_tests_ = manager; |
363 } | 363 } |
364 | 364 |
365 base::string16 PlatformNotificationServiceImpl::DisplayNameForOrigin( | 365 base::string16 PlatformNotificationServiceImpl::DisplayNameForContextMessage( |
366 Profile* profile, | 366 Profile* profile, |
367 const GURL& origin) const { | 367 const GURL& origin) const { |
368 #if defined(ENABLE_EXTENSIONS) | 368 #if defined(ENABLE_EXTENSIONS) |
369 // If the source is an extension, lookup the display name. | 369 // If the source is an extension, lookup the display name. |
370 if (origin.SchemeIs(extensions::kExtensionScheme)) { | 370 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
371 const extensions::Extension* extension = | 371 const extensions::Extension* extension = |
372 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( | 372 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( |
373 origin.host(), extensions::ExtensionRegistry::EVERYTHING); | 373 origin.host(), extensions::ExtensionRegistry::EVERYTHING); |
374 DCHECK(extension); | 374 DCHECK(extension); |
375 | 375 |
376 return base::UTF8ToUTF16(extension->name()); | 376 return base::UTF8ToUTF16(extension->name()); |
377 } | 377 } |
378 #endif | 378 #endif |
379 | 379 |
380 std::string languages = | 380 return base::string16(); |
381 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
382 | |
383 return WebOriginDisplayName(origin, languages); | |
384 } | 381 } |
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 |