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 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 ==
61 ExtensionWelcomeNotification::kChromeNowExtensionID));
63 return isGoogleNowNotification;
66 int GoogleNowNotificationStatsCollector::CountVisibleGoogleNowNotifications() {
67 typedef message_center::NotificationList::Notifications Notifications;
68 const Notifications visible_notifications =
69 message_center_->GetVisibleNotifications();
70 int google_now_notification_count = 0;
71 for (Notifications::iterator iter = visible_notifications.begin();
72 iter != visible_notifications.end();
74 if ((*iter)->notifier_id().id ==
75 ExtensionWelcomeNotification::kChromeNowExtensionID)
76 google_now_notification_count++;
78 return google_now_notification_count;