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

Side by Side Diff: chrome/browser/engagement/site_engagement_helper.cc

Issue 1975723002: Reduce the site engagement service public interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix friends Created 4 years, 7 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/engagement/site_engagement_helper.h" 5 #include "chrome/browser/engagement/site_engagement_helper.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
11 #include "chrome/browser/engagement/site_engagement_service.h" 11 #include "chrome/browser/engagement/site_engagement_service.h"
12 #include "chrome/browser/engagement/site_engagement_service_factory.h" 12 #include "chrome/browser/engagement/site_engagement_service_factory.h"
13 #include "chrome/browser/prerender/prerender_contents.h" 13 #include "chrome/browser/prerender/prerender_contents.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/navigation_handle.h" 15 #include "content/public/browser/navigation_handle.h"
16 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
17 17
18 namespace { 18 namespace {
19 19
20 int g_seconds_to_pause_engagement_detection = 10; 20 int g_seconds_to_pause_engagement_detection = 10;
21 int g_seconds_delay_after_navigation = 10; 21 int g_seconds_delay_after_navigation = 10;
22 int g_seconds_delay_after_media_starts = 10; 22 int g_seconds_delay_after_media_starts = 10;
23 int g_seconds_delay_after_show = 5; 23 int g_seconds_delay_after_show = 5;
24 24
25 } // anonymous namespace 25 } // anonymous namespace
26 26
27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); 27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementService::Helper);
28 28
29 SiteEngagementHelper::PeriodicTracker::PeriodicTracker( 29 SiteEngagementService::Helper::PeriodicTracker::PeriodicTracker(
30 SiteEngagementHelper* helper) 30 SiteEngagementService::Helper* helper)
31 : helper_(helper), pause_timer_(new base::Timer(true, false)) {} 31 : helper_(helper), pause_timer_(new base::Timer(true, false)) {}
32 32
33 SiteEngagementHelper::PeriodicTracker::~PeriodicTracker() {} 33 SiteEngagementService::Helper::PeriodicTracker::~PeriodicTracker() {}
34 34
35 void SiteEngagementHelper::PeriodicTracker::Start( 35 void SiteEngagementService::Helper::PeriodicTracker::Start(
36 base::TimeDelta initial_delay) { 36 base::TimeDelta initial_delay) {
37 StartTimer(initial_delay); 37 StartTimer(initial_delay);
38 } 38 }
39 39
40 void SiteEngagementHelper::PeriodicTracker::Pause() { 40 void SiteEngagementService::Helper::PeriodicTracker::Pause() {
41 TrackingStopped(); 41 TrackingStopped();
42 StartTimer( 42 StartTimer(
43 base::TimeDelta::FromSeconds(g_seconds_to_pause_engagement_detection)); 43 base::TimeDelta::FromSeconds(g_seconds_to_pause_engagement_detection));
44 } 44 }
45 45
46 void SiteEngagementHelper::PeriodicTracker::Stop() { 46 void SiteEngagementService::Helper::PeriodicTracker::Stop() {
47 TrackingStopped(); 47 TrackingStopped();
48 pause_timer_->Stop(); 48 pause_timer_->Stop();
49 } 49 }
50 50
51 bool SiteEngagementHelper::PeriodicTracker::IsTimerRunning() { 51 bool SiteEngagementService::Helper::PeriodicTracker::IsTimerRunning() {
52 return pause_timer_->IsRunning(); 52 return pause_timer_->IsRunning();
53 } 53 }
54 54
55 void SiteEngagementHelper::PeriodicTracker::SetPauseTimerForTesting( 55 void SiteEngagementService::Helper::PeriodicTracker::SetPauseTimerForTesting(
56 std::unique_ptr<base::Timer> timer) { 56 std::unique_ptr<base::Timer> timer) {
57 pause_timer_ = std::move(timer); 57 pause_timer_ = std::move(timer);
58 } 58 }
59 59
60 void SiteEngagementHelper::PeriodicTracker::StartTimer( 60 void SiteEngagementService::Helper::PeriodicTracker::StartTimer(
61 base::TimeDelta delay) { 61 base::TimeDelta delay) {
62 pause_timer_->Start( 62 pause_timer_->Start(
63 FROM_HERE, delay, 63 FROM_HERE, delay,
64 base::Bind(&SiteEngagementHelper::PeriodicTracker::TrackingStarted, 64 base::Bind(
65 base::Unretained(this))); 65 &SiteEngagementService::Helper::PeriodicTracker::TrackingStarted,
66 base::Unretained(this)));
66 } 67 }
67 68
68 SiteEngagementHelper::InputTracker::InputTracker( 69 SiteEngagementService::Helper::InputTracker::InputTracker(
69 SiteEngagementHelper* helper, 70 SiteEngagementService::Helper* helper,
70 content::WebContents* web_contents) 71 content::WebContents* web_contents)
71 : PeriodicTracker(helper), 72 : PeriodicTracker(helper),
72 content::WebContentsObserver(web_contents), 73 content::WebContentsObserver(web_contents),
73 is_tracking_(false) {} 74 is_tracking_(false) {}
74 75
75 void SiteEngagementHelper::InputTracker::TrackingStarted() { 76 void SiteEngagementService::Helper::InputTracker::TrackingStarted() {
76 is_tracking_ = true; 77 is_tracking_ = true;
77 } 78 }
78 79
79 void SiteEngagementHelper::InputTracker::TrackingStopped() { 80 void SiteEngagementService::Helper::InputTracker::TrackingStopped() {
80 is_tracking_ = false; 81 is_tracking_ = false;
81 } 82 }
82 83
83 // Record that there was some user input, and defer handling of the input event. 84 // Record that there was some user input, and defer handling of the input event.
84 // Once the timer finishes running, the callbacks detecting user input will be 85 // Once the timer finishes running, the callbacks detecting user input will be
85 // registered again. 86 // registered again.
86 void SiteEngagementHelper::InputTracker::DidGetUserInteraction( 87 void SiteEngagementService::Helper::InputTracker::DidGetUserInteraction(
87 const blink::WebInputEvent::Type type) { 88 const blink::WebInputEvent::Type type) {
88 // Only respond to raw key down to avoid multiple triggering on a single input 89 // Only respond to raw key down to avoid multiple triggering on a single input
89 // (e.g. keypress is a key down then key up). 90 // (e.g. keypress is a key down then key up).
90 if (!is_tracking_) 91 if (!is_tracking_)
91 return; 92 return;
92 93
93 // This switch has a default NOTREACHED case because it will not test all 94 // This switch has a default NOTREACHED case because it will not test all
94 // of the values of the WebInputEvent::Type enum (hence it won't require the 95 // of the values of the WebInputEvent::Type enum (hence it won't require the
95 // compiler verifying that all cases are covered). 96 // compiler verifying that all cases are covered).
96 switch (type) { 97 switch (type) {
(...skipping 12 matching lines...) Expand all
109 break; 110 break;
110 case blink::WebInputEvent::Undefined: 111 case blink::WebInputEvent::Undefined:
111 // Explicitly ignore browser-initiated navigation input. 112 // Explicitly ignore browser-initiated navigation input.
112 break; 113 break;
113 default: 114 default:
114 NOTREACHED(); 115 NOTREACHED();
115 } 116 }
116 Pause(); 117 Pause();
117 } 118 }
118 119
119 SiteEngagementHelper::MediaTracker::MediaTracker( 120 SiteEngagementService::Helper::MediaTracker::MediaTracker(
120 SiteEngagementHelper* helper, 121 SiteEngagementService::Helper* helper,
121 content::WebContents* web_contents) 122 content::WebContents* web_contents)
122 : PeriodicTracker(helper), 123 : PeriodicTracker(helper),
123 content::WebContentsObserver(web_contents), 124 content::WebContentsObserver(web_contents),
124 is_hidden_(false) {} 125 is_hidden_(false) {}
125 126
126 SiteEngagementHelper::MediaTracker::~MediaTracker() {} 127 SiteEngagementService::Helper::MediaTracker::~MediaTracker() {}
127 128
128 void SiteEngagementHelper::MediaTracker::TrackingStarted() { 129 void SiteEngagementService::Helper::MediaTracker::TrackingStarted() {
129 if (!active_media_players_.empty()) 130 if (!active_media_players_.empty())
130 helper()->RecordMediaPlaying(is_hidden_); 131 helper()->RecordMediaPlaying(is_hidden_);
131 132
132 Pause(); 133 Pause();
133 } 134 }
134 135
135 void SiteEngagementHelper::MediaTracker::MediaStartedPlaying( 136 void SiteEngagementService::Helper::MediaTracker::MediaStartedPlaying(
136 const MediaPlayerId& id) { 137 const MediaPlayerId& id) {
137 // Only begin engagement detection when media actually starts playing. 138 // Only begin engagement detection when media actually starts playing.
138 active_media_players_.push_back(id); 139 active_media_players_.push_back(id);
139 if (!IsTimerRunning()) 140 if (!IsTimerRunning())
140 Start(base::TimeDelta::FromSeconds(g_seconds_delay_after_media_starts)); 141 Start(base::TimeDelta::FromSeconds(g_seconds_delay_after_media_starts));
141 } 142 }
142 143
143 void SiteEngagementHelper::MediaTracker::MediaStoppedPlaying( 144 void SiteEngagementService::Helper::MediaTracker::MediaStoppedPlaying(
144 const MediaPlayerId& id) { 145 const MediaPlayerId& id) {
145 active_media_players_.erase(std::remove(active_media_players_.begin(), 146 active_media_players_.erase(std::remove(active_media_players_.begin(),
146 active_media_players_.end(), id), 147 active_media_players_.end(), id),
147 active_media_players_.end()); 148 active_media_players_.end());
148 } 149 }
149 150
150 void SiteEngagementHelper::MediaTracker::WasShown() { 151 void SiteEngagementService::Helper::MediaTracker::WasShown() {
151 is_hidden_ = false; 152 is_hidden_ = false;
152 } 153 }
153 154
154 void SiteEngagementHelper::MediaTracker::WasHidden() { 155 void SiteEngagementService::Helper::MediaTracker::WasHidden() {
155 is_hidden_ = true; 156 is_hidden_ = true;
156 } 157 }
157 158
158 SiteEngagementHelper::~SiteEngagementHelper() { 159 SiteEngagementService::Helper::~Helper() {
159 if (web_contents()) { 160 if (web_contents()) {
160 input_tracker_.Stop(); 161 input_tracker_.Stop();
161 media_tracker_.Stop(); 162 media_tracker_.Stop();
162 } 163 }
163 } 164 }
164 165
165 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* web_contents) 166 SiteEngagementService::Helper::Helper(content::WebContents* web_contents)
166 : content::WebContentsObserver(web_contents), 167 : content::WebContentsObserver(web_contents),
167 input_tracker_(this, web_contents), 168 input_tracker_(this, web_contents),
168 media_tracker_(this, web_contents), 169 media_tracker_(this, web_contents),
169 record_engagement_(false) {} 170 record_engagement_(false) {}
170 171
171 void SiteEngagementHelper::RecordUserInput( 172 void SiteEngagementService::Helper::RecordUserInput(
172 SiteEngagementMetrics::EngagementType type) { 173 SiteEngagementMetrics::EngagementType type) {
173 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); 174 TRACE_EVENT0("SiteEngagement", "RecordUserInput");
174 content::WebContents* contents = web_contents(); 175 content::WebContents* contents = web_contents();
175 if (contents) { 176 if (contents) {
176 Profile* profile = 177 Profile* profile =
177 Profile::FromBrowserContext(contents->GetBrowserContext()); 178 Profile::FromBrowserContext(contents->GetBrowserContext());
178 SiteEngagementService* service = 179 SiteEngagementService* service =
179 SiteEngagementServiceFactory::GetForProfile(profile); 180 SiteEngagementServiceFactory::GetForProfile(profile);
180 181
181 // Service is null in incognito. 182 // Service is null in incognito.
182 if (service) 183 if (service)
183 service->HandleUserInput(contents->GetVisibleURL(), type); 184 service->HandleUserInput(contents->GetVisibleURL(), type);
184 } 185 }
185 } 186 }
186 187
187 void SiteEngagementHelper::RecordMediaPlaying(bool is_hidden) { 188 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) {
188 content::WebContents* contents = web_contents(); 189 content::WebContents* contents = web_contents();
189 if (contents) { 190 if (contents) {
190 Profile* profile = 191 Profile* profile =
191 Profile::FromBrowserContext(contents->GetBrowserContext()); 192 Profile::FromBrowserContext(contents->GetBrowserContext());
192 SiteEngagementService* service = 193 SiteEngagementService* service =
193 SiteEngagementServiceFactory::GetForProfile(profile); 194 SiteEngagementServiceFactory::GetForProfile(profile);
194 195
195 if (service) 196 if (service)
196 service->HandleMediaPlaying(contents->GetVisibleURL(), is_hidden); 197 service->HandleMediaPlaying(contents->GetVisibleURL(), is_hidden);
197 } 198 }
198 } 199 }
199 200
200 void SiteEngagementHelper::DidFinishNavigation( 201 void SiteEngagementService::Helper::DidFinishNavigation(
201 content::NavigationHandle* handle) { 202 content::NavigationHandle* handle) {
202 input_tracker_.Stop(); 203 input_tracker_.Stop();
203 media_tracker_.Stop(); 204 media_tracker_.Stop();
204 205
205 // Ignore all schemes except HTTP and HTTPS, as well as uncommitted, non 206 // Ignore all schemes except HTTP and HTTPS, as well as uncommitted, non
206 // main-frame, same page, or error page navigations. 207 // main-frame, same page, or error page navigations.
207 record_engagement_ = handle->GetURL().SchemeIsHTTPOrHTTPS(); 208 record_engagement_ = handle->GetURL().SchemeIsHTTPOrHTTPS();
208 if (!handle->HasCommitted() || !handle->IsInMainFrame() || 209 if (!handle->HasCommitted() || !handle->IsInMainFrame() ||
209 handle->IsSamePage() || handle->IsErrorPage() || !record_engagement_) { 210 handle->IsSamePage() || handle->IsErrorPage() || !record_engagement_) {
210 return; 211 return;
(...skipping 20 matching lines...) Expand all
231 SiteEngagementService* service = 232 SiteEngagementService* service =
232 SiteEngagementServiceFactory::GetForProfile(profile); 233 SiteEngagementServiceFactory::GetForProfile(profile);
233 234
234 if (service) 235 if (service)
235 service->HandleNavigation(handle->GetURL(), handle->GetPageTransition()); 236 service->HandleNavigation(handle->GetURL(), handle->GetPageTransition());
236 237
237 input_tracker_.Start( 238 input_tracker_.Start(
238 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation)); 239 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation));
239 } 240 }
240 241
241 void SiteEngagementHelper::WasShown() { 242 void SiteEngagementService::Helper::WasShown() {
242 // Ensure that the input callbacks are registered when we come into view. This 243 // Ensure that the input callbacks are registered when we come into view.
243 // occurs when switching tabs, or when prerendered pages are swapped in.
244 if (record_engagement_) { 244 if (record_engagement_) {
245 input_tracker_.Start( 245 input_tracker_.Start(
246 base::TimeDelta::FromSeconds(g_seconds_delay_after_show)); 246 base::TimeDelta::FromSeconds(g_seconds_delay_after_show));
247 } 247 }
248 } 248 }
249 249
250 void SiteEngagementHelper::WasHidden() { 250 void SiteEngagementService::Helper::WasHidden() {
251 // Ensure that the input callbacks are not registered when hidden. 251 // Ensure that the input callbacks are not registered when hidden.
252 input_tracker_.Stop(); 252 input_tracker_.Stop();
253 } 253 }
254 254
255 // static 255 // static
256 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(int seconds) { 256 void SiteEngagementService::Helper::SetSecondsBetweenUserInputCheck(
257 int seconds) {
257 g_seconds_to_pause_engagement_detection = seconds; 258 g_seconds_to_pause_engagement_detection = seconds;
258 } 259 }
259 260
260 // static 261 // static
261 void SiteEngagementHelper::SetSecondsTrackingDelayAfterNavigation(int seconds) { 262 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation(
263 int seconds) {
262 g_seconds_delay_after_navigation = seconds; 264 g_seconds_delay_after_navigation = seconds;
263 } 265 }
264 266
265 // static 267 // static
266 void SiteEngagementHelper::SetSecondsTrackingDelayAfterShow(int seconds) { 268 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow(
269 int seconds) {
267 g_seconds_delay_after_show = seconds; 270 g_seconds_delay_after_show = seconds;
268 } 271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698