Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: chrome/browser/metrics/tab_reactivation_tracker.cc

Issue 2335203003: Add metrics to keep track of the tab activate/deactivate cycle (Closed)
Patch Set: Tab discarding resistant Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/metrics/tab_reactivation_tracker.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "base/stl_util.h"
9 #include "content/public/browser/web_contents_observer.h"
10
11 namespace metrics {
12
13 // This class is used to track the activation/deactivation cycle per
14 // WebContents.
15 class TabReactivationTracker::WebContentsHelper
16 : public content::WebContentsObserver {
17 public:
18 WebContentsHelper(TabReactivationTracker* tab_reactivation_tracker,
19 content::WebContents* contents);
20 ~WebContentsHelper();
21
22 // content::WebContentsObserver:
23 void WebContentsDestroyed() override;
24
25 void OnTabActivating();
26 void OnTabDeactivating();
27 void OnTabClosing();
28
29 private:
30 // The owning TabReactivationTracker.
31 TabReactivationTracker* tab_reactivation_tracker_;
32
33 // Indicates if the tab has been deactivated before as to only count
34 // reactivations.
35 bool was_deactivated_once_;
36
37 // The deactivation metric is not recorded for closing tabs.
38 bool is_closing_;
39
40 DISALLOW_COPY_AND_ASSIGN(WebContentsHelper);
41 };
42
43 TabReactivationTracker::WebContentsHelper::WebContentsHelper(
44 TabReactivationTracker* tab_reactivation_tracker,
45 content::WebContents* contents)
46 : content::WebContentsObserver(contents),
47 tab_reactivation_tracker_(tab_reactivation_tracker),
48 was_deactivated_once_(false),
49 is_closing_(false) {}
50
51 TabReactivationTracker::WebContentsHelper::~WebContentsHelper() = default;
52
53 void TabReactivationTracker::WebContentsHelper::WebContentsDestroyed() {
54 tab_reactivation_tracker_->OnWebContentsDestroyed(web_contents());
55 }
56
57 void TabReactivationTracker::WebContentsHelper::OnTabActivating() {
58 if (was_deactivated_once_)
59 tab_reactivation_tracker_->NotifyTabReactivating(web_contents());
60 }
61
62 void TabReactivationTracker::WebContentsHelper::OnTabDeactivating() {
63 was_deactivated_once_ = true;
64 if (!is_closing_)
65 tab_reactivation_tracker_->NotifyTabDeactivating(web_contents());
66 }
67
68 void TabReactivationTracker::WebContentsHelper::OnTabClosing() {
69 is_closing_ = true;
70 }
71
72 TabReactivationTracker::TabReactivationTracker(Delegate* delegate)
73 : delegate_(delegate), browser_tab_strip_tracker_(this, nullptr, nullptr) {
74 browser_tab_strip_tracker_.Init(
75 BrowserTabStripTracker::InitWith::ALL_BROWERS);
76 }
77
78 TabReactivationTracker::~TabReactivationTracker() = default;
79
80 void TabReactivationTracker::TabInsertedAt(TabStripModel* tab_strip_model,
81 content::WebContents* contents,
82 int index,
83 bool foreground) {}
84
85 void TabReactivationTracker::TabClosingAt(TabStripModel* tab_strip_model,
86 content::WebContents* contents,
87 int index) {
88 GetHelper(contents)->OnTabClosing();
89 }
90
91 void TabReactivationTracker::ActiveTabChanged(
92 content::WebContents* old_contents,
93 content::WebContents* new_contents,
94 int index,
95 int reason) {
96 if (old_contents) {
Georges Khalil 2016/09/22 18:38:38 nit: no braces
Patrick Monette 2016/09/22 20:18:52 Done.
97 GetHelper(old_contents)->OnTabDeactivating();
98 }
99 GetHelper(new_contents)->OnTabActivating();
100 }
101
102 void TabReactivationTracker::NotifyTabDeactivating(
103 content::WebContents* contents) {
104 delegate_->OnTabDeactivated(contents);
105 }
106
107 void TabReactivationTracker::NotifyTabReactivating(
108 content::WebContents* contents) {
109 delegate_->OnTabReactivated(contents);
110 }
111
112 TabReactivationTracker::WebContentsHelper* TabReactivationTracker::GetHelper(
113 content::WebContents* contents) {
114 // Make sure it exists.
115 if (!base::ContainsKey(helper_map_, contents))
Georges Khalil 2016/09/22 18:38:38 nit: braces
Patrick Monette 2016/09/22 20:18:52 Done.
116 helper_map_.insert(std::make_pair(
117 contents, base::MakeUnique<WebContentsHelper>(this, contents)));
118
119 return helper_map_[contents].get();
120 }
121
122 void TabReactivationTracker::OnWebContentsDestroyed(
123 content::WebContents* contents) {
124 helper_map_.erase(contents);
125 }
126
127 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698