| 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 <utility> |    7 #include <utility> | 
 |    8 #include <vector> | 
|    8  |    9  | 
|    9 #include "base/command_line.h" |   10 #include "base/command_line.h" | 
|   10 #include "base/metrics/histogram_macros.h" |   11 #include "base/metrics/histogram_macros.h" | 
|   11 #include "base/metrics/user_metrics_action.h" |   12 #include "base/metrics/user_metrics_action.h" | 
|   12 #include "base/prefs/pref_service.h" |   13 #include "base/prefs/pref_service.h" | 
|   13 #include "base/strings/string_number_conversions.h" |   14 #include "base/strings/string_number_conversions.h" | 
|   14 #include "base/strings/utf_string_conversions.h" |   15 #include "base/strings/utf_string_conversions.h" | 
|   15 #include "build/build_config.h" |   16 #include "build/build_config.h" | 
|   16 #include "chrome/browser/browser_process.h" |   17 #include "chrome/browser/browser_process.h" | 
|   17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |   18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
|   31 #include "components/content_settings/core/browser/host_content_settings_map.h" |   32 #include "components/content_settings/core/browser/host_content_settings_map.h" | 
|   32 #include "components/content_settings/core/common/content_settings.h" |   33 #include "components/content_settings/core/common/content_settings.h" | 
|   33 #include "components/content_settings/core/common/content_settings_types.h" |   34 #include "components/content_settings/core/common/content_settings_types.h" | 
|   34 #include "components/url_formatter/url_formatter.h" |   35 #include "components/url_formatter/url_formatter.h" | 
|   35 #include "content/public/browser/browser_thread.h" |   36 #include "content/public/browser/browser_thread.h" | 
|   36 #include "content/public/browser/desktop_notification_delegate.h" |   37 #include "content/public/browser/desktop_notification_delegate.h" | 
|   37 #include "content/public/browser/notification_event_dispatcher.h" |   38 #include "content/public/browser/notification_event_dispatcher.h" | 
|   38 #include "content/public/browser/platform_notification_context.h" |   39 #include "content/public/browser/platform_notification_context.h" | 
|   39 #include "content/public/browser/storage_partition.h" |   40 #include "content/public/browser/storage_partition.h" | 
|   40 #include "content/public/browser/user_metrics.h" |   41 #include "content/public/browser/user_metrics.h" | 
 |   42 #include "content/public/common/notification_resources.h" | 
|   41 #include "content/public/common/platform_notification_data.h" |   43 #include "content/public/common/platform_notification_data.h" | 
|   42 #include "ui/base/l10n/l10n_util.h" |   44 #include "ui/base/l10n/l10n_util.h" | 
|   43 #include "ui/base/resource/resource_bundle.h" |   45 #include "ui/base/resource/resource_bundle.h" | 
|   44 #include "ui/message_center/notification_types.h" |   46 #include "ui/message_center/notification_types.h" | 
|   45 #include "ui/message_center/notifier_settings.h" |   47 #include "ui/message_center/notifier_settings.h" | 
|   46 #include "ui/resources/grit/ui_resources.h" |   48 #include "ui/resources/grit/ui_resources.h" | 
|   47 #include "url/url_constants.h" |   49 #include "url/url_constants.h" | 
|   48  |   50  | 
|   49 #if defined(ENABLE_EXTENSIONS) |   51 #if defined(ENABLE_EXTENSIONS) | 
|   50 #include "chrome/browser/notifications/notifier_state_tracker.h" |   52 #include "chrome/browser/notifications/notifier_state_tracker.h" | 
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  331     return blink::WebNotificationPermissionAllowed; |  333     return blink::WebNotificationPermissionAllowed; | 
|  332   if (setting == CONTENT_SETTING_BLOCK) |  334   if (setting == CONTENT_SETTING_BLOCK) | 
|  333     return blink::WebNotificationPermissionDenied; |  335     return blink::WebNotificationPermissionDenied; | 
|  334  |  336  | 
|  335   return blink::WebNotificationPermissionDefault; |  337   return blink::WebNotificationPermissionDefault; | 
|  336 } |  338 } | 
|  337  |  339  | 
|  338 void PlatformNotificationServiceImpl::DisplayNotification( |  340 void PlatformNotificationServiceImpl::DisplayNotification( | 
|  339     BrowserContext* browser_context, |  341     BrowserContext* browser_context, | 
|  340     const GURL& origin, |  342     const GURL& origin, | 
|  341     const SkBitmap& icon, |  | 
|  342     const content::PlatformNotificationData& notification_data, |  343     const content::PlatformNotificationData& notification_data, | 
 |  344     const content::NotificationResources& notification_resources, | 
|  343     scoped_ptr<content::DesktopNotificationDelegate> delegate, |  345     scoped_ptr<content::DesktopNotificationDelegate> delegate, | 
|  344     base::Closure* cancel_callback) { |  346     base::Closure* cancel_callback) { | 
|  345   DCHECK_CURRENTLY_ON(BrowserThread::UI); |  347   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
|  346  |  348  | 
|  347   Profile* profile = Profile::FromBrowserContext(browser_context); |  349   Profile* profile = Profile::FromBrowserContext(browser_context); | 
|  348   DCHECK(profile); |  350   DCHECK(profile); | 
|  349   DCHECK_EQ(0u, notification_data.actions.size()); |  351   DCHECK_EQ(0u, notification_data.actions.size()); | 
|  350  |  352  | 
|  351   NotificationObjectProxy* proxy = |  353   NotificationObjectProxy* proxy = | 
|  352       new NotificationObjectProxy(browser_context, std::move(delegate)); |  354       new NotificationObjectProxy(browser_context, std::move(delegate)); | 
|  353   Notification notification = CreateNotificationFromData( |  355   Notification notification = CreateNotificationFromData( | 
|  354       profile, origin, icon, notification_data, proxy); |  356       profile, origin, notification_data, notification_resources, proxy); | 
|  355  |  357  | 
|  356   GetNotificationUIManager()->Add(notification, profile); |  358   GetNotificationUIManager()->Add(notification, profile); | 
|  357   if (cancel_callback) |  359   if (cancel_callback) | 
|  358     *cancel_callback = |  360     *cancel_callback = | 
|  359         base::Bind(&CancelNotification, |  361         base::Bind(&CancelNotification, | 
|  360                    notification.delegate_id(), |  362                    notification.delegate_id(), | 
|  361                    NotificationUIManager::GetProfileID(profile)); |  363                    NotificationUIManager::GetProfileID(profile)); | 
|  362  |  364  | 
|  363   HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( |  365   HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( | 
|  364       origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |  366       origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 
|  365 } |  367 } | 
|  366  |  368  | 
|  367 void PlatformNotificationServiceImpl::DisplayPersistentNotification( |  369 void PlatformNotificationServiceImpl::DisplayPersistentNotification( | 
|  368     BrowserContext* browser_context, |  370     BrowserContext* browser_context, | 
|  369     int64_t persistent_notification_id, |  371     int64_t persistent_notification_id, | 
|  370     const GURL& origin, |  372     const GURL& origin, | 
|  371     const SkBitmap& icon, |  373     const content::PlatformNotificationData& notification_data, | 
|  372     const content::PlatformNotificationData& notification_data) { |  374     const content::NotificationResources& notification_resources) { | 
|  373   DCHECK_CURRENTLY_ON(BrowserThread::UI); |  375   DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
|  374  |  376  | 
|  375   Profile* profile = Profile::FromBrowserContext(browser_context); |  377   Profile* profile = Profile::FromBrowserContext(browser_context); | 
|  376   DCHECK(profile); |  378   DCHECK(profile); | 
|  377  |  379  | 
|  378   // The notification settings button will be appended after the developer- |  380   // The notification settings button will be appended after the developer- | 
|  379   // supplied buttons, available in |notification_data.actions|. |  381   // supplied buttons, available in |notification_data.actions|. | 
|  380   int settings_button_index = notification_data.actions.size(); |  382   int settings_button_index = notification_data.actions.size(); | 
|  381   PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( |  383   PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( | 
|  382       browser_context, persistent_notification_id, origin, |  384       browser_context, persistent_notification_id, origin, | 
|  383       settings_button_index); |  385       settings_button_index); | 
|  384  |  386  | 
|  385   Notification notification = CreateNotificationFromData( |  387   Notification notification = CreateNotificationFromData( | 
|  386       profile, origin, icon, notification_data, delegate); |  388       profile, origin, notification_data, notification_resources, delegate); | 
|  387  |  389  | 
|  388   // TODO(peter): Remove this mapping when we have reliable id generation for |  390   // TODO(peter): Remove this mapping when we have reliable id generation for | 
|  389   // the message_center::Notification objects. |  391   // the message_center::Notification objects. | 
|  390   persistent_notifications_[persistent_notification_id] = notification.id(); |  392   persistent_notifications_[persistent_notification_id] = notification.id(); | 
|  391  |  393  | 
|  392   GetNotificationUIManager()->Add(notification, profile); |  394   GetNotificationUIManager()->Add(notification, profile); | 
|  393   content::RecordAction( |  395   content::RecordAction( | 
|  394       base::UserMetricsAction("Notifications.Persistent.Shown")); |  396       base::UserMetricsAction("Notifications.Persistent.Shown")); | 
|  395  |  397  | 
|  396   HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( |  398   HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  454 #else |  456 #else | 
|  455   // Android cannot reliably return the notifications that are currently being |  457   // Android cannot reliably return the notifications that are currently being | 
|  456   // displayed on the platform, see the comment in NotificationUIManagerAndroid. |  458   // displayed on the platform, see the comment in NotificationUIManagerAndroid. | 
|  457   return false; |  459   return false; | 
|  458 #endif  // !defined(OS_ANDROID) |  460 #endif  // !defined(OS_ANDROID) | 
|  459 } |  461 } | 
|  460  |  462  | 
|  461 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( |  463 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( | 
|  462     Profile* profile, |  464     Profile* profile, | 
|  463     const GURL& origin, |  465     const GURL& origin, | 
|  464     const SkBitmap& icon, |  | 
|  465     const content::PlatformNotificationData& notification_data, |  466     const content::PlatformNotificationData& notification_data, | 
 |  467     const content::NotificationResources& notification_resources, | 
|  466     NotificationDelegate* delegate) const { |  468     NotificationDelegate* delegate) const { | 
|  467   // TODO(peter): Icons for Web Notifications are currently always requested for |  469   // TODO(peter): Icons for Web Notifications are currently always requested for | 
|  468   // 1x scale, whereas the displays on which they can be displayed can have a |  470   // 1x scale, whereas the displays on which they can be displayed can have a | 
|  469   // different pixel density. Be smarter about this when the API gets updated |  471   // different pixel density. Be smarter about this when the API gets updated | 
|  470   // with a way for developers to specify images of different resolutions. |  472   // with a way for developers to specify images of different resolutions. | 
|  471   Notification notification( |  473   Notification notification( | 
|  472       message_center::NOTIFICATION_TYPE_SIMPLE, notification_data.title, |  474       message_center::NOTIFICATION_TYPE_SIMPLE, notification_data.title, | 
|  473       notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), |  475       notification_data.body, | 
 |  476       gfx::Image::CreateFrom1xBitmap(notification_resources.notification_icon), | 
|  474       message_center::NotifierId(origin), base::UTF8ToUTF16(origin.host()), |  477       message_center::NotifierId(origin), base::UTF8ToUTF16(origin.host()), | 
|  475       origin, notification_data.tag, message_center::RichNotificationData(), |  478       origin, notification_data.tag, message_center::RichNotificationData(), | 
|  476       delegate); |  479       delegate); | 
|  477  |  480  | 
|  478   notification.set_context_message( |  481   notification.set_context_message( | 
|  479       DisplayNameForContextMessage(profile, origin)); |  482       DisplayNameForContextMessage(profile, origin)); | 
|  480   notification.set_vibration_pattern(notification_data.vibration_pattern); |  483   notification.set_vibration_pattern(notification_data.vibration_pattern); | 
|  481   notification.set_silent(notification_data.silent); |  484   notification.set_silent(notification_data.silent); | 
|  482  |  485  | 
|  483   std::vector<message_center::ButtonInfo> buttons; |  486   std::vector<message_center::ButtonInfo> buttons; | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  546         extensions::ExtensionRegistry::Get(profile)->GetExtensionById( |  549         extensions::ExtensionRegistry::Get(profile)->GetExtensionById( | 
|  547             origin.host(), extensions::ExtensionRegistry::EVERYTHING); |  550             origin.host(), extensions::ExtensionRegistry::EVERYTHING); | 
|  548     DCHECK(extension); |  551     DCHECK(extension); | 
|  549  |  552  | 
|  550     return base::UTF8ToUTF16(extension->name()); |  553     return base::UTF8ToUTF16(extension->name()); | 
|  551   } |  554   } | 
|  552 #endif |  555 #endif | 
|  553  |  556  | 
|  554   return base::string16(); |  557   return base::string16(); | 
|  555 } |  558 } | 
| OLD | NEW |