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
3 // found in the LICENSE file.
5 #include "chrome/browser/notifications/google_now_notification_stats_collector.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/notifications/extension_welcome_notification.h"
12 #include "chrome/browser/notifications/notification.h"
13 #include "chrome/browser/notifications/notification_ui_manager.h"
14 #include "content/public/browser/user_metrics.h"
15 #include "ui/message_center/notification.h"
18 const int kNotificationsMaxCount
= 20;
21 // TODO(dewittj) remove the dependency on the MessageCenter because this
23 GoogleNowNotificationStatsCollector::GoogleNowNotificationStatsCollector(
24 message_center::MessageCenter
* message_center
)
25 : message_center_(message_center
) {
26 message_center_
->AddObserver(this);
29 GoogleNowNotificationStatsCollector::~GoogleNowNotificationStatsCollector() {
30 message_center_
->RemoveObserver(this);
33 void GoogleNowNotificationStatsCollector::OnNotificationDisplayed(
34 const std::string
& notification_id
,
35 const message_center::DisplaySource source
) {
36 if ((source
== message_center::DISPLAY_SOURCE_POPUP
) &&
37 IsVisibleNotificationIdForGoogleNow(notification_id
)) {
38 content::RecordAction(
39 base::UserMetricsAction(
40 "GoogleNow.MessageCenter.NotificationPoppedUp"));
44 void GoogleNowNotificationStatsCollector::OnCenterVisibilityChanged(
45 message_center::Visibility visibility
) {
46 if (visibility
== message_center::VISIBILITY_MESSAGE_CENTER
) {
47 UMA_HISTOGRAM_SPARSE_SLOWLY(
48 "GoogleNow.MessageCenter.Displayed.NotificationsVisible",
49 std::min(CountVisibleGoogleNowNotifications(), kNotificationsMaxCount
));
53 bool GoogleNowNotificationStatsCollector::IsVisibleNotificationIdForGoogleNow(
54 const std::string
& notification_id
) {
55 bool isGoogleNowNotification
= false;
56 const message_center::Notification
* const notification
=
57 message_center::MessageCenter::Get()->FindVisibleNotificationById(
60 isGoogleNowNotification
=
61 ((notification
->notifier_id().type
==
62 message_center::NotifierId::APPLICATION
) &&
63 (notification
->notifier_id().id
==
64 ExtensionWelcomeNotification::kChromeNowExtensionID
));
66 return isGoogleNowNotification
;
69 int GoogleNowNotificationStatsCollector::CountVisibleGoogleNowNotifications() {
70 typedef message_center::NotificationList::Notifications Notifications
;
71 const Notifications visible_notifications
=
72 message_center_
->GetVisibleNotifications();
73 int google_now_notification_count
= 0;
74 for (Notifications::iterator iter
= visible_notifications
.begin();
75 iter
!= visible_notifications
.end();
77 if ((*iter
)->notifier_id().id
==
78 ExtensionWelcomeNotification::kChromeNowExtensionID
)
79 google_now_notification_count
++;
81 return google_now_notification_count
;