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" |
10 #include "chrome/browser/notifications/notification_display_service.h" | 11 #include "chrome/browser/notifications/notification_display_service.h" |
11 #include "chrome/browser/notifications/notification_display_service_factory.h" | 12 #include "chrome/browser/notifications/notification_display_service_factory.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #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" | |
21 | 14 |
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 | |
31 | 15 |
32 WebNotificationDelegate::WebNotificationDelegate( | 16 WebNotificationDelegate::WebNotificationDelegate( |
33 NotificationCommon::Type notification_type, | 17 NotificationCommon::Type notification_type, |
34 Profile* profile, | 18 Profile* profile, |
35 const std::string& notification_id, | 19 const std::string& notification_id, |
36 const GURL& origin) | 20 const GURL& origin) |
37 : notification_type_(notification_type), | 21 : notification_type_(notification_type), |
38 profile_(profile), | 22 profile_(profile), |
39 notification_id_(notification_id), | 23 notification_id_(notification_id), |
40 origin_(origin) {} | 24 origin_(origin) {} |
41 | 25 |
42 WebNotificationDelegate::~WebNotificationDelegate() {} | 26 WebNotificationDelegate::~WebNotificationDelegate() {} |
43 | 27 |
44 std::string WebNotificationDelegate::id() const { | 28 std::string WebNotificationDelegate::id() const { |
45 return notification_id_; | 29 return notification_id_; |
46 } | 30 } |
47 | 31 |
48 bool WebNotificationDelegate::SettingsClick() { | 32 bool WebNotificationDelegate::SettingsClick() { |
49 #if !defined(OS_CHROMEOS) | 33 #if !defined(OS_CHROMEOS) |
50 NotificationCommon::OpenNotificationSettings(profile_); | 34 NotificationCommon::OpenNotificationSettings(profile_); |
51 return true; | 35 return true; |
52 #else | 36 #else |
53 return false; | 37 return false; |
54 #endif | 38 #endif |
55 } | 39 } |
56 | 40 |
57 bool WebNotificationDelegate::ShouldDisplaySettingsButton() { | 41 bool WebNotificationDelegate::ShouldDisplaySettingsButton() { |
58 return true; | 42 return notification_type_ != NotificationCommon::EXTENSION; |
59 } | 43 } |
60 | 44 |
61 bool WebNotificationDelegate::ShouldDisplayOverFullscreen() const { | 45 bool WebNotificationDelegate::ShouldDisplayOverFullscreen() const { |
62 #if !defined(OS_ANDROID) | 46 NotificationDisplayService* display_service = |
63 // Check to see if this notification comes from a webpage that is displaying | 47 NotificationDisplayServiceFactory::GetForProfile(profile_); |
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; | |
69 | 48 |
70 const content::WebContents* active_contents = | 49 return display_service->ShouldDisplayOverFullscreen(origin_, |
71 browser->tab_strip_model()->GetActiveWebContents(); | 50 notification_type_); |
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; | |
101 } | 51 } |
102 | 52 |
103 void WebNotificationDelegate::Display() {} | |
104 | |
105 void WebNotificationDelegate::Close(bool by_user) { | 53 void WebNotificationDelegate::Close(bool by_user) { |
106 auto* display_service = | 54 auto* display_service = |
107 NotificationDisplayServiceFactory::GetForProfile(profile_); | 55 NotificationDisplayServiceFactory::GetForProfile(profile_); |
108 display_service->ProcessNotificationOperation( | 56 display_service->ProcessNotificationOperation( |
109 NotificationCommon::CLOSE, notification_type_, origin().spec(), | 57 NotificationCommon::CLOSE, notification_type_, origin().spec(), |
110 notification_id_, -1, base::NullableString16()); | 58 notification_id_, -1, base::NullableString16()); |
111 } | 59 } |
112 | 60 |
113 void WebNotificationDelegate::Click() { | 61 void WebNotificationDelegate::Click() { |
114 auto* display_service = | 62 auto* display_service = |
(...skipping 15 matching lines...) Expand all Loading... |
130 void WebNotificationDelegate::ButtonClickWithReply( | 78 void WebNotificationDelegate::ButtonClickWithReply( |
131 int button_index, | 79 int button_index, |
132 const base::string16& reply) { | 80 const base::string16& reply) { |
133 auto* display_service = | 81 auto* display_service = |
134 NotificationDisplayServiceFactory::GetForProfile(profile_); | 82 NotificationDisplayServiceFactory::GetForProfile(profile_); |
135 display_service->ProcessNotificationOperation( | 83 display_service->ProcessNotificationOperation( |
136 NotificationCommon::CLICK, notification_type_, origin().spec(), | 84 NotificationCommon::CLICK, notification_type_, origin().spec(), |
137 notification_id_, button_index, | 85 notification_id_, button_index, |
138 base::NullableString16(reply, false /* is_null */)); | 86 base::NullableString16(reply, false /* is_null */)); |
139 } | 87 } |
OLD | NEW |