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