• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/notifications/google_now_notification_stats_collector.h"
6 
7 #include <string>
8 
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"
15 
16 namespace {
17 const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
18 const int kNotificationsMaxCount = 20;
19 }
20 
GoogleNowNotificationStatsCollector(message_center::MessageCenter * message_center)21 GoogleNowNotificationStatsCollector::GoogleNowNotificationStatsCollector(
22     message_center::MessageCenter* message_center)
23     : message_center_(message_center) {
24   message_center_->AddObserver(this);
25 }
26 
~GoogleNowNotificationStatsCollector()27 GoogleNowNotificationStatsCollector::~GoogleNowNotificationStatsCollector() {
28   message_center_->RemoveObserver(this);
29 }
30 
OnNotificationDisplayed(const std::string & notification_id,const message_center::DisplaySource source)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"));
39   }
40 }
41 
OnCenterVisibilityChanged(message_center::Visibility visibility)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));
48   }
49 }
50 
IsNotificationIdForGoogleNow(const std::string & notification_id)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);
56   if (notification) {
57     isGoogleNowNotification =
58         ((notification->notifier_id().type ==
59               message_center::NotifierId::APPLICATION) &&
60         (notification->notifier_id().id == kChromeNowExtensionID));
61   }
62   return isGoogleNowNotification;
63 }
64 
CountVisibleGoogleNowNotifications()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();
72       ++iter) {
73     if ((*iter)->notifier_id().id == kChromeNowExtensionID)
74       google_now_notification_count++;
75   }
76   return google_now_notification_count;
77 }
78