Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / chrome_bubble_manager.cc
blobd7f3febaeb53952226b5a289147dd5b07a4b5485
1 // Copyright 2015 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/ui/chrome_bubble_manager.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "components/bubble/bubble_controller.h"
10 #include "components/bubble/bubble_delegate.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents.h"
14 namespace {
16 // Add any new enum before BUBBLE_TYPE_MAX and update BubbleType in
17 // tools/metrics/histograms/histograms.xml.
18 // Do not re-order these because they are used for collecting metrics.
19 enum BubbleType {
20 BUBBLE_TYPE_UNKNOWN, // Used for unrecognized bubble names.
21 BUBBLE_TYPE_MOCK, // Used for testing.
22 BUBBLE_TYPE_MAX, // Number of bubble types; used for metrics.
25 // Convert from bubble name to ID. The bubble ID will allow collecting the
26 // close reason for each bubble type.
27 static int GetBubbleId(BubbleReference bubble) {
28 BubbleType bubble_type = BUBBLE_TYPE_UNKNOWN;
30 // Translate from bubble name to enum.
31 if (bubble->GetName().compare("MockBubble"))
32 bubble_type = BUBBLE_TYPE_MOCK;
34 DCHECK_NE(bubble_type, BUBBLE_TYPE_UNKNOWN);
35 DCHECK_NE(bubble_type, BUBBLE_TYPE_MAX);
37 return bubble_type;
40 // Get the UMA title for this close reason.
41 static std::string GetBubbleCloseReasonName(BubbleCloseReason reason) {
42 switch (reason) {
43 case BUBBLE_CLOSE_FORCED: return "Bubbles.Close.Forced";
44 case BUBBLE_CLOSE_FOCUS_LOST: return "Bubbles.Close.FocusLost";
45 case BUBBLE_CLOSE_TABSWITCHED: return "Bubbles.Close.TabSwitched";
46 case BUBBLE_CLOSE_TABDETACHED: return "Bubbles.Close.TabDetached";
47 case BUBBLE_CLOSE_USER_DISMISSED: return "Bubbles.Close.UserDismissed";
48 case BUBBLE_CLOSE_NAVIGATED: return "Bubbles.Close.Navigated";
49 case BUBBLE_CLOSE_FULLSCREEN_TOGGLED:
50 return "Bubbles.Close.FullscreenToggled";
51 case BUBBLE_CLOSE_ACCEPTED: return "Bubbles.Close.Accepted";
52 case BUBBLE_CLOSE_CANCELED: return "Bubbles.Close.Canceled";
55 NOTREACHED();
56 return "Bubbles.Close.Unknown";
59 } // namespace
61 ChromeBubbleManager::ChromeBubbleManager(TabStripModel* tab_strip_model)
62 : tab_strip_model_(tab_strip_model) {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
64 DCHECK(tab_strip_model_);
65 tab_strip_model_->AddObserver(this);
66 AddBubbleManagerObserver(&chrome_bubble_metrics_);
69 ChromeBubbleManager::~ChromeBubbleManager() {
70 tab_strip_model_->RemoveObserver(this);
72 // Finalize requests before removing the BubbleManagerObserver so it can
73 // collect metrics when closing any open bubbles.
74 FinalizePendingRequests();
75 RemoveBubbleManagerObserver(&chrome_bubble_metrics_);
78 void ChromeBubbleManager::TabDetachedAt(content::WebContents* contents,
79 int index) {
80 CloseAllBubbles(BUBBLE_CLOSE_TABDETACHED);
81 // Any bubble that didn't close should update its anchor position.
82 UpdateAllBubbleAnchors();
85 void ChromeBubbleManager::TabDeactivated(content::WebContents* contents) {
86 CloseAllBubbles(BUBBLE_CLOSE_TABSWITCHED);
89 void ChromeBubbleManager::ActiveTabChanged(content::WebContents* old_contents,
90 content::WebContents* new_contents,
91 int index,
92 int reason) {
93 Observe(new_contents);
96 void ChromeBubbleManager::DidToggleFullscreenModeForTab(
97 bool entered_fullscreen) {
98 CloseAllBubbles(BUBBLE_CLOSE_FULLSCREEN_TOGGLED);
99 // Any bubble that didn't close should update its anchor position.
100 UpdateAllBubbleAnchors();
103 void ChromeBubbleManager::NavigationEntryCommitted(
104 const content::LoadCommittedDetails& load_details) {
105 CloseAllBubbles(BUBBLE_CLOSE_NAVIGATED);
108 void ChromeBubbleManager::ChromeBubbleMetrics::OnBubbleNeverShown(
109 BubbleReference bubble) {
110 UMA_HISTOGRAM_ENUMERATION("Bubbles.NeverShown", GetBubbleId(bubble),
111 BUBBLE_TYPE_MAX);
114 void ChromeBubbleManager::ChromeBubbleMetrics::OnBubbleClosed(
115 BubbleReference bubble, BubbleCloseReason reason) {
116 // Log the amount of time the bubble was visible.
117 base::TimeDelta visible_time = bubble->GetVisibleTime();
118 UMA_HISTOGRAM_LONG_TIMES("Bubbles.DisplayTime.All", visible_time);
120 // Log the reason the bubble was closed.
121 UMA_HISTOGRAM_ENUMERATION(GetBubbleCloseReasonName(reason),
122 GetBubbleId(bubble),
123 BUBBLE_TYPE_MAX);