| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/web_notification_delegate.h" | 5 #include "chrome/browser/notifications/web_notification_delegate.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/nullable_string16.h" | 9 #include "base/strings/nullable_string16.h" |
| 10 #include "chrome/browser/notifications/notification_common.h" | |
| 11 #include "chrome/browser/notifications/notification_display_service.h" | 10 #include "chrome/browser/notifications/notification_display_service.h" |
| 12 #include "chrome/browser/notifications/notification_display_service_factory.h" | 11 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/browser_window.h" |
| 16 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h" |
| 17 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "ui/message_center/notifier_settings.h" |
| 14 | 21 |
| 22 using message_center::NotifierId; |
| 23 |
| 24 namespace features { |
| 25 |
| 26 const base::Feature kAllowFullscreenWebNotificationsFeature{ |
| 27 "FSNotificationsWeb", base::FEATURE_ENABLED_BY_DEFAULT |
| 28 }; |
| 29 |
| 30 } // namespace features |
| 15 | 31 |
| 16 WebNotificationDelegate::WebNotificationDelegate( | 32 WebNotificationDelegate::WebNotificationDelegate( |
| 17 NotificationCommon::Type notification_type, | 33 NotificationCommon::Type notification_type, |
| 18 Profile* profile, | 34 Profile* profile, |
| 19 const std::string& notification_id, | 35 const std::string& notification_id, |
| 20 const GURL& origin) | 36 const GURL& origin) |
| 21 : notification_type_(notification_type), | 37 : notification_type_(notification_type), |
| 22 profile_(profile), | 38 profile_(profile), |
| 23 notification_id_(notification_id), | 39 notification_id_(notification_id), |
| 24 origin_(origin) {} | 40 origin_(origin) {} |
| 25 | 41 |
| 26 WebNotificationDelegate::~WebNotificationDelegate() {} | 42 WebNotificationDelegate::~WebNotificationDelegate() {} |
| 27 | 43 |
| 28 std::string WebNotificationDelegate::id() const { | 44 std::string WebNotificationDelegate::id() const { |
| 29 return notification_id_; | 45 return notification_id_; |
| 30 } | 46 } |
| 31 | 47 |
| 32 bool WebNotificationDelegate::SettingsClick() { | 48 bool WebNotificationDelegate::SettingsClick() { |
| 33 #if !defined(OS_CHROMEOS) | 49 #if !defined(OS_CHROMEOS) |
| 34 NotificationCommon::OpenNotificationSettings(profile_); | 50 NotificationCommon::OpenNotificationSettings(profile_); |
| 35 return true; | 51 return true; |
| 36 #else | 52 #else |
| 37 return false; | 53 return false; |
| 38 #endif | 54 #endif |
| 39 } | 55 } |
| 40 | 56 |
| 41 bool WebNotificationDelegate::ShouldDisplaySettingsButton() { | 57 bool WebNotificationDelegate::ShouldDisplaySettingsButton() { |
| 42 return notification_type_ != NotificationCommon::EXTENSION; | 58 return true; |
| 43 } | 59 } |
| 44 | 60 |
| 45 bool WebNotificationDelegate::ShouldDisplayOverFullscreen() const { | 61 bool WebNotificationDelegate::ShouldDisplayOverFullscreen() const { |
| 46 NotificationDisplayService* display_service = | 62 #if !defined(OS_ANDROID) |
| 47 NotificationDisplayServiceFactory::GetForProfile(profile_); | 63 // Check to see if this notification comes from a webpage that is displaying |
| 64 // fullscreen content. |
| 65 for (auto* browser : *BrowserList::GetInstance()) { |
| 66 // Only consider the browsers for the profile that created the notification |
| 67 if (browser->profile() != profile_) |
| 68 continue; |
| 48 | 69 |
| 49 return display_service->ShouldDisplayOverFullscreen(origin_, | 70 const content::WebContents* active_contents = |
| 50 notification_type_); | 71 browser->tab_strip_model()->GetActiveWebContents(); |
| 72 if (!active_contents) |
| 73 continue; |
| 74 |
| 75 // Check to see if |
| 76 // (a) the active tab in the browser shares its origin with the |
| 77 // notification. |
| 78 // (b) the browser is fullscreen |
| 79 // (c) the browser has focus. |
| 80 if (active_contents->GetURL().GetOrigin() == origin_ && |
| 81 browser->exclusive_access_manager()->context()->IsFullscreen() && |
| 82 browser->window()->IsActive()) { |
| 83 bool enabled = base::FeatureList::IsEnabled( |
| 84 features::kAllowFullscreenWebNotificationsFeature); |
| 85 if (enabled) { |
| 86 UMA_HISTOGRAM_ENUMERATION("Notifications.Display_Fullscreen.Shown", |
| 87 NotifierId::WEB_PAGE, |
| 88 NotifierId::SIZE); |
| 89 } else { |
| 90 UMA_HISTOGRAM_ENUMERATION( |
| 91 "Notifications.Display_Fullscreen.Suppressed", |
| 92 NotifierId::WEB_PAGE, |
| 93 NotifierId::SIZE); |
| 94 } |
| 95 return enabled; |
| 96 } |
| 97 } |
| 98 #endif |
| 99 |
| 100 return false; |
| 51 } | 101 } |
| 52 | 102 |
| 103 void WebNotificationDelegate::Display() {} |
| 104 |
| 53 void WebNotificationDelegate::Close(bool by_user) { | 105 void WebNotificationDelegate::Close(bool by_user) { |
| 54 auto* display_service = | 106 auto* display_service = |
| 55 NotificationDisplayServiceFactory::GetForProfile(profile_); | 107 NotificationDisplayServiceFactory::GetForProfile(profile_); |
| 56 display_service->ProcessNotificationOperation( | 108 display_service->ProcessNotificationOperation( |
| 57 NotificationCommon::CLOSE, notification_type_, origin().spec(), | 109 NotificationCommon::CLOSE, notification_type_, origin().spec(), |
| 58 notification_id_, -1, base::NullableString16()); | 110 notification_id_, -1, base::NullableString16()); |
| 59 } | 111 } |
| 60 | 112 |
| 61 void WebNotificationDelegate::Click() { | 113 void WebNotificationDelegate::Click() { |
| 62 auto* display_service = | 114 auto* display_service = |
| (...skipping 15 matching lines...) Expand all Loading... |
| 78 void WebNotificationDelegate::ButtonClickWithReply( | 130 void WebNotificationDelegate::ButtonClickWithReply( |
| 79 int button_index, | 131 int button_index, |
| 80 const base::string16& reply) { | 132 const base::string16& reply) { |
| 81 auto* display_service = | 133 auto* display_service = |
| 82 NotificationDisplayServiceFactory::GetForProfile(profile_); | 134 NotificationDisplayServiceFactory::GetForProfile(profile_); |
| 83 display_service->ProcessNotificationOperation( | 135 display_service->ProcessNotificationOperation( |
| 84 NotificationCommon::CLICK, notification_type_, origin().spec(), | 136 NotificationCommon::CLICK, notification_type_, origin().spec(), |
| 85 notification_id_, button_index, | 137 notification_id_, button_index, |
| 86 base::NullableString16(reply, false /* is_null */)); | 138 base::NullableString16(reply, false /* is_null */)); |
| 87 } | 139 } |
| OLD | NEW |