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/notification.h"
12 #include "chrome/browser/notifications/notification_ui_manager.h"
13 #include "content/public/browser/user_metrics.h"
14 #include "ui/message_center/notification.h"
17 const char kChromeNowExtensionID
[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
18 const int kNotificationsMaxCount
= 20;
21 GoogleNowNotificationStatsCollector::GoogleNowNotificationStatsCollector(
22 message_center::MessageCenter
* message_center
)
23 : message_center_(message_center
) {
24 message_center_
->AddObserver(this);
27 GoogleNowNotificationStatsCollector::~GoogleNowNotificationStatsCollector() {
28 message_center_
->RemoveObserver(this);
31 void GoogleNowNotificationStatsCollector::OnNotificationDisplayed(
32 const std::string
& notification_id
,
33 const message_center::DisplaySource source
) {
34 if ((source
== message_center::DISPLAY_SOURCE_POPUP
) &&
35 IsNotificationIdForGoogleNow(notification_id
)) {
36 content::RecordAction(
37 base::UserMetricsAction(
38 "GoogleNow.MessageCenter.NotificationPoppedUp"));
42 void GoogleNowNotificationStatsCollector::OnCenterVisibilityChanged(
43 message_center::Visibility visibility
) {
44 if (visibility
== message_center::VISIBILITY_MESSAGE_CENTER
) {
45 UMA_HISTOGRAM_SPARSE_SLOWLY(
46 "GoogleNow.MessageCenter.Displayed.NotificationsVisible",
47 std::min(CountVisibleGoogleNowNotifications(), kNotificationsMaxCount
));
51 bool GoogleNowNotificationStatsCollector::IsNotificationIdForGoogleNow(
52 const std::string
& notification_id
) {
53 bool isGoogleNowNotification
= false;
54 const Notification
* const notification
=
55 g_browser_process
->notification_ui_manager()->FindById(notification_id
);
57 isGoogleNowNotification
=
58 ((notification
->notifier_id().type
==
59 message_center::NotifierId::APPLICATION
) &&
60 (notification
->notifier_id().id
== kChromeNowExtensionID
));
62 return isGoogleNowNotification
;
65 int GoogleNowNotificationStatsCollector::CountVisibleGoogleNowNotifications() {
66 typedef message_center::NotificationList::Notifications Notifications
;
67 const Notifications visible_notifications
=
68 message_center_
->GetVisibleNotifications();
69 int google_now_notification_count
= 0;
70 for (Notifications::iterator iter
= visible_notifications
.begin();
71 iter
!= visible_notifications
.end();
73 if ((*iter
)->notifier_id().id
== kChromeNowExtensionID
)
74 google_now_notification_count
++;
76 return google_now_notification_count
;