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"
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.
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") == 0)
32 bubble_type
= BUBBLE_TYPE_MOCK
;
34 DCHECK_NE(bubble_type
, BUBBLE_TYPE_UNKNOWN
);
35 DCHECK_NE(bubble_type
, BUBBLE_TYPE_MAX
);
40 // Log the reason for closing this bubble.
41 // Each reason is its own metric. Each histogram call MUST have a runtime
42 // constant value passed in for the title.
43 static void LogBubbleCloseReason(BubbleReference bubble
,
44 BubbleCloseReason reason
) {
45 int bubble_id
= GetBubbleId(bubble
);
47 case BUBBLE_CLOSE_FORCED
:
48 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Forced",
52 case BUBBLE_CLOSE_FOCUS_LOST
:
53 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.FocusLost",
57 case BUBBLE_CLOSE_TABSWITCHED
:
58 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.TabSwitched",
62 case BUBBLE_CLOSE_TABDETACHED
:
63 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.TabDetached",
67 case BUBBLE_CLOSE_USER_DISMISSED
:
68 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.UserDismissed",
72 case BUBBLE_CLOSE_NAVIGATED
:
73 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Navigated",
77 case BUBBLE_CLOSE_FULLSCREEN_TOGGLED
:
78 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.FullscreenToggled",
82 case BUBBLE_CLOSE_ACCEPTED
:
83 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Accepted",
87 case BUBBLE_CLOSE_CANCELED
:
88 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Canceled",
99 ChromeBubbleManager::ChromeBubbleManager(TabStripModel
* tab_strip_model
)
100 : tab_strip_model_(tab_strip_model
) {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
102 DCHECK(tab_strip_model_
);
103 tab_strip_model_
->AddObserver(this);
104 AddBubbleManagerObserver(&chrome_bubble_metrics_
);
107 ChromeBubbleManager::~ChromeBubbleManager() {
108 tab_strip_model_
->RemoveObserver(this);
110 // Finalize requests before removing the BubbleManagerObserver so it can
111 // collect metrics when closing any open bubbles.
112 FinalizePendingRequests();
113 RemoveBubbleManagerObserver(&chrome_bubble_metrics_
);
116 void ChromeBubbleManager::TabDetachedAt(content::WebContents
* contents
,
118 CloseAllBubbles(BUBBLE_CLOSE_TABDETACHED
);
119 // Any bubble that didn't close should update its anchor position.
120 UpdateAllBubbleAnchors();
123 void ChromeBubbleManager::TabDeactivated(content::WebContents
* contents
) {
124 CloseAllBubbles(BUBBLE_CLOSE_TABSWITCHED
);
127 void ChromeBubbleManager::ActiveTabChanged(content::WebContents
* old_contents
,
128 content::WebContents
* new_contents
,
131 Observe(new_contents
);
134 void ChromeBubbleManager::DidToggleFullscreenModeForTab(
135 bool entered_fullscreen
) {
136 CloseAllBubbles(BUBBLE_CLOSE_FULLSCREEN_TOGGLED
);
137 // Any bubble that didn't close should update its anchor position.
138 UpdateAllBubbleAnchors();
141 void ChromeBubbleManager::NavigationEntryCommitted(
142 const content::LoadCommittedDetails
& load_details
) {
143 CloseAllBubbles(BUBBLE_CLOSE_NAVIGATED
);
146 void ChromeBubbleManager::ChromeBubbleMetrics::OnBubbleNeverShown(
147 BubbleReference bubble
) {
148 UMA_HISTOGRAM_ENUMERATION("Bubbles.NeverShown", GetBubbleId(bubble
),
152 void ChromeBubbleManager::ChromeBubbleMetrics::OnBubbleClosed(
153 BubbleReference bubble
, BubbleCloseReason reason
) {
154 // Log the amount of time the bubble was visible.
155 base::TimeDelta visible_time
= bubble
->GetVisibleTime();
156 UMA_HISTOGRAM_LONG_TIMES("Bubbles.DisplayTime.All", visible_time
);
158 LogBubbleCloseReason(bubble
, reason
);