BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / chrome_bubble_manager.cc
blobb3ed18ba0fe3c3a4145f8a03e1150c8cf29c9e47
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") == 0)
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 // 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);
46 switch (reason) {
47 case BUBBLE_CLOSE_FORCED:
48 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Forced",
49 bubble_id,
50 BUBBLE_TYPE_MAX);
51 return;
52 case BUBBLE_CLOSE_FOCUS_LOST:
53 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.FocusLost",
54 bubble_id,
55 BUBBLE_TYPE_MAX);
56 return;
57 case BUBBLE_CLOSE_TABSWITCHED:
58 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.TabSwitched",
59 bubble_id,
60 BUBBLE_TYPE_MAX);
61 return;
62 case BUBBLE_CLOSE_TABDETACHED:
63 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.TabDetached",
64 bubble_id,
65 BUBBLE_TYPE_MAX);
66 return;
67 case BUBBLE_CLOSE_USER_DISMISSED:
68 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.UserDismissed",
69 bubble_id,
70 BUBBLE_TYPE_MAX);
71 return;
72 case BUBBLE_CLOSE_NAVIGATED:
73 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Navigated",
74 bubble_id,
75 BUBBLE_TYPE_MAX);
76 return;
77 case BUBBLE_CLOSE_FULLSCREEN_TOGGLED:
78 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.FullscreenToggled",
79 bubble_id,
80 BUBBLE_TYPE_MAX);
81 return;
82 case BUBBLE_CLOSE_ACCEPTED:
83 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Accepted",
84 bubble_id,
85 BUBBLE_TYPE_MAX);
86 return;
87 case BUBBLE_CLOSE_CANCELED:
88 UMA_HISTOGRAM_ENUMERATION("Bubbles.Close.Canceled",
89 bubble_id,
90 BUBBLE_TYPE_MAX);
91 return;
94 NOTREACHED();
97 } // namespace
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,
117 int index) {
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,
129 int index,
130 int reason) {
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),
149 BUBBLE_TYPE_MAX);
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);