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/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"
16
17namespace {
18const int kNotificationsMaxCount = 20;
19}
20
21GoogleNowNotificationStatsCollector::GoogleNowNotificationStatsCollector(
22    message_center::MessageCenter* message_center)
23    : message_center_(message_center) {
24  message_center_->AddObserver(this);
25}
26
27GoogleNowNotificationStatsCollector::~GoogleNowNotificationStatsCollector() {
28  message_center_->RemoveObserver(this);
29}
30
31void 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
42void 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
51bool 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 ==
61              ExtensionWelcomeNotification::kChromeNowExtensionID));
62  }
63  return isGoogleNowNotification;
64}
65
66int 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();
73      ++iter) {
74    if ((*iter)->notifier_id().id ==
75            ExtensionWelcomeNotification::kChromeNowExtensionID)
76      google_now_notification_count++;
77  }
78  return google_now_notification_count;
79}
80