• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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/message_center_stats_collector.h"
6 
7 #include <string>
8 
9 #include "base/metrics/histogram.h"
10 #include "content/public/browser/user_metrics.h"
11 #include "ui/message_center/message_center.h"
12 
NotificationStats()13 MessageCenterStatsCollector::NotificationStats::NotificationStats() {}
14 
NotificationStats(const std::string & id)15 MessageCenterStatsCollector::NotificationStats::NotificationStats(
16     const std::string& id) : id_(id) {
17   for (size_t i = 0; i < NOTIFICATION_ACTION_COUNT; i++) {
18     actions_[i] = false;
19   }
20 }
21 
~NotificationStats()22 MessageCenterStatsCollector::NotificationStats::~NotificationStats() {}
23 
CollectAction(NotificationActionType type)24 void MessageCenterStatsCollector::NotificationStats::CollectAction(
25     NotificationActionType type) {
26   DCHECK(!id_.empty());
27 
28   UMA_HISTOGRAM_ENUMERATION("Notifications.Actions",
29                             type,
30                             NOTIFICATION_ACTION_COUNT);
31   actions_[type] = true;
32 }
33 
RecordAggregateStats()34 void MessageCenterStatsCollector::NotificationStats::RecordAggregateStats() {
35   DCHECK(!id_.empty());
36 
37   for (size_t i = 0; i < NOTIFICATION_ACTION_COUNT; i++) {
38     if (!actions_[i])
39       continue;
40     UMA_HISTOGRAM_ENUMERATION("Notifications.PerNotificationActions",
41                               i,
42                               NOTIFICATION_ACTION_COUNT);
43   }
44 }
45 
MessageCenterStatsCollector(message_center::MessageCenter * message_center)46 MessageCenterStatsCollector::MessageCenterStatsCollector(
47     message_center::MessageCenter* message_center)
48     : message_center_(message_center) {
49   message_center_->AddObserver(this);
50 }
51 
~MessageCenterStatsCollector()52 MessageCenterStatsCollector::~MessageCenterStatsCollector() {
53   message_center_->RemoveObserver(this);
54 }
55 
OnNotificationAdded(const std::string & notification_id)56 void MessageCenterStatsCollector::OnNotificationAdded(
57     const std::string& notification_id) {
58   stats_[notification_id] = NotificationStats(notification_id);
59 
60   StatsCollection::iterator iter = stats_.find(notification_id);
61   DCHECK(iter != stats_.end());
62 
63   stats_[notification_id].CollectAction(NOTIFICATION_ACTION_ADD);
64 }
65 
OnNotificationRemoved(const std::string & notification_id,bool by_user)66 void MessageCenterStatsCollector::OnNotificationRemoved(
67     const std::string& notification_id, bool by_user) {
68   StatsCollection::iterator iter = stats_.find(notification_id);
69   if (iter == stats_.end())
70     return;
71   NotificationStats& notification_stat = iter->second;
72   notification_stat.CollectAction(by_user ?
73       NOTIFICATION_ACTION_CLOSE_BY_USER :
74       NOTIFICATION_ACTION_CLOSE_BY_SYSTEM);
75   notification_stat.RecordAggregateStats();
76   stats_.erase(notification_id);
77 }
78 
OnNotificationUpdated(const std::string & notification_id)79 void MessageCenterStatsCollector::OnNotificationUpdated(
80     const std::string& notification_id) {
81   StatsCollection::iterator iter = stats_.find(notification_id);
82   if (iter == stats_.end())
83     return;
84   NotificationStats& notification_stat = iter->second;
85 
86   notification_stat.CollectAction(NOTIFICATION_ACTION_UPDATE);
87 }
88 
OnNotificationClicked(const std::string & notification_id)89 void MessageCenterStatsCollector::OnNotificationClicked(
90     const std::string& notification_id) {
91   StatsCollection::iterator iter = stats_.find(notification_id);
92   if (iter == stats_.end())
93     return;
94   NotificationStats& notification_stat = iter->second;
95 
96   notification_stat.CollectAction(NOTIFICATION_ACTION_CLICK);
97 }
98 
OnNotificationButtonClicked(const std::string & notification_id,int button_index)99 void MessageCenterStatsCollector::OnNotificationButtonClicked(
100     const std::string& notification_id,
101                                            int button_index) {
102   StatsCollection::iterator iter = stats_.find(notification_id);
103   if (iter == stats_.end())
104     return;
105   NotificationStats& notification_stat = iter->second;
106 
107   notification_stat.CollectAction(NOTIFICATION_ACTION_BUTTON_CLICK);
108 }
109 
OnNotificationDisplayed(const std::string & notification_id,const message_center::DisplaySource source)110 void MessageCenterStatsCollector::OnNotificationDisplayed(
111     const std::string& notification_id,
112     const message_center::DisplaySource source) {
113   StatsCollection::iterator iter = stats_.find(notification_id);
114   if (iter == stats_.end())
115     return;
116   NotificationStats& notification_stat = iter->second;
117 
118   notification_stat.CollectAction(NOTIFICATION_ACTION_DISPLAY);
119 }
120 
OnCenterVisibilityChanged(message_center::Visibility visibility)121 void MessageCenterStatsCollector::OnCenterVisibilityChanged(
122     message_center::Visibility visibility) {
123   switch (visibility) {
124     case message_center::VISIBILITY_TRANSIENT:
125       break;
126     case message_center::VISIBILITY_MESSAGE_CENTER:
127       content::RecordAction(
128           base::UserMetricsAction("Notifications.ShowMessageCenter"));
129       break;
130     case message_center::VISIBILITY_SETTINGS:
131       content::RecordAction(
132           base::UserMetricsAction("Notifications.ShowSettings"));
133       break;
134   }
135 }
136 
OnQuietModeChanged(bool in_quiet_mode)137 void MessageCenterStatsCollector::OnQuietModeChanged(bool in_quiet_mode) {
138   if (in_quiet_mode) {
139     content::RecordAction(
140         base::UserMetricsAction("Notifications.Mute"));
141   } else {
142     content::RecordAction(
143         base::UserMetricsAction("Notifications.Unmute"));
144   }
145 }
146 
147