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

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

Issue 1373363005: Add a delay to Site Engagement tracking after navigation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments, refactor Created 5 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
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 "base/time/time.h" 7 #include "base/time/time.h"
8 #include "base/trace_event/trace_event.h" 8 #include "base/trace_event/trace_event.h"
9 #include "chrome/browser/engagement/site_engagement_service.h" 9 #include "chrome/browser/engagement/site_engagement_service.h"
10 #include "chrome/browser/engagement/site_engagement_service_factory.h" 10 #include "chrome/browser/engagement/site_engagement_service_factory.h"
11 #include "chrome/browser/prerender/prerender_contents.h" 11 #include "chrome/browser/prerender/prerender_contents.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/navigation_entry.h" 13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
15 15
16 namespace { 16 namespace {
17 17
18 double g_seconds_between_user_input_check = 10; 18 int g_seconds_between_user_input_check = 10;
19 int g_seconds_tracking_delay_after_navigation = 10;
20 int g_seconds_tracking_delay_after_show = 5;
19 21
20 } // anonymous namespace 22 } // anonymous namespace
21 23
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper);
23 25
24 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) 26 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper)
25 : helper_(helper), 27 : helper_(helper),
26 pause_timer_(new base::Timer(true, false)), 28 pause_timer_(new base::Timer(true, false)),
29 host_(nullptr),
30 is_tracking_(false),
27 callbacks_added_(false) { 31 callbacks_added_(false) {
28 key_press_event_callback_ = 32 key_press_event_callback_ =
29 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent, 33 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent,
30 base::Unretained(this)); 34 base::Unretained(this));
31 mouse_event_callback_ = 35 mouse_event_callback_ =
32 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent, 36 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent,
33 base::Unretained(this)); 37 base::Unretained(this));
34 } 38 }
35 39
36 SiteEngagementHelper::InputTracker::~InputTracker() { } 40 SiteEngagementHelper::InputTracker::~InputTracker() {}
37 41
38 // Record that there was some user input, and defer handling of the input event. 42 // Record that there was some user input, and defer handling of the input event.
39 // web_contents() will return nullptr if the observed contents have been 43 // web_contents() will return nullptr if the observed contents have been
40 // deleted; if the contents exist, record engagement for the site. Once the 44 // deleted; if the contents exist, record engagement for the site. Once the
41 // timer finishes running, the callbacks detecting user input will be registered 45 // timer finishes running, the callbacks detecting user input will be registered
42 // again. 46 // again.
43 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent( 47 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent(
44 const content::NativeWebKeyboardEvent& event) { 48 const content::NativeWebKeyboardEvent& event) {
45 // Only respond to raw key down to avoid multiple triggering on a single input 49 // Only respond to raw key down to avoid multiple triggering on a single input
46 // (e.g. keypress is a key down then key up). 50 // (e.g. keypress is a key down then key up).
47 if (event.type == blink::WebInputEvent::RawKeyDown) { 51 if (event.type == blink::WebInputEvent::RawKeyDown) {
48 PauseTracking(helper_->web_contents()->GetRenderViewHost()); 52 PauseTracking();
49 helper_->RecordUserInput(); 53 helper_->RecordUserInput();
50 } 54 }
51 return false; 55 return false;
52 } 56 }
53 57
54 bool SiteEngagementHelper::InputTracker::HandleMouseEvent( 58 bool SiteEngagementHelper::InputTracker::HandleMouseEvent(
55 const blink::WebMouseEvent& event) { 59 const blink::WebMouseEvent& event) {
56 // Only respond to mouse down with a button or mouse move events (e.g. a click 60 // Only respond to mouse down with a button or mouse move events (e.g. a click
57 // is a mouse down and mouse up) to avoid cases where multiple events come in 61 // is a mouse down and mouse up) to avoid cases where multiple events come in
58 // before we can pause tracking. 62 // before we can pause tracking.
59 if ((event.button != blink::WebMouseEvent::ButtonNone && 63 if ((event.button != blink::WebMouseEvent::ButtonNone &&
60 event.type == blink::WebInputEvent::MouseDown) || 64 event.type == blink::WebInputEvent::MouseDown) ||
61 event.type == blink::WebInputEvent::MouseWheel) { 65 event.type == blink::WebInputEvent::MouseWheel) {
62 PauseTracking(helper_->web_contents()->GetRenderViewHost()); 66 PauseTracking();
63 helper_->RecordUserInput(); 67 helper_->RecordUserInput();
64 } 68 }
65 return false; 69 return false;
66 } 70 }
67 71
68 void SiteEngagementHelper::InputTracker::StartTracking( 72 void SiteEngagementHelper::InputTracker::StartTracking(
69 content::RenderViewHost* host) { 73 content::RenderViewHost* host,
70 if (!callbacks_added_) { 74 int initial_delay) {
71 host->AddKeyPressEventCallback(key_press_event_callback_); 75 DCHECK(!is_tracking_);
72 host->AddMouseEventCallback(mouse_event_callback_); 76 host_ = host;
73 callbacks_added_ = true; 77 if (!is_tracking_) {
78 StartTimer(initial_delay);
79 is_tracking_ = true;
74 } 80 }
75 } 81 }
76 82
77 void SiteEngagementHelper::InputTracker::PauseTracking( 83 void SiteEngagementHelper::InputTracker::PauseTracking() {
78 content::RenderViewHost* host) { 84 RemoveCallbacks();
79 StopTracking(host); 85 StartTimer(g_seconds_between_user_input_check);
86 }
87
88 void SiteEngagementHelper::InputTracker::StopTracking() {
89 pause_timer_->Stop();
90 is_tracking_ = false;
91 RemoveCallbacks();
92 host_ = nullptr;
93 }
94
95 void SiteEngagementHelper::InputTracker::SetPauseTimerForTesting(
96 scoped_ptr<base::Timer> timer) {
97 pause_timer_ = timer.Pass();
98 }
99
100 void SiteEngagementHelper::InputTracker::StartTimer(int delay) {
80 pause_timer_->Start( 101 pause_timer_->Start(
81 FROM_HERE, 102 FROM_HERE, base::TimeDelta::FromSeconds(delay),
82 base::TimeDelta::FromSeconds(g_seconds_between_user_input_check), 103 base::Bind(&SiteEngagementHelper::InputTracker::AddCallbacks,
83 base::Bind(&SiteEngagementHelper::InputTracker::ResumeTracking,
84 base::Unretained(this))); 104 base::Unretained(this)));
85 } 105 }
86 106
87 void SiteEngagementHelper::InputTracker::ResumeTracking() { 107 void SiteEngagementHelper::InputTracker::AddCallbacks() {
88 content::WebContents* contents = helper_->web_contents(); 108 content::WebContents* contents = helper_->web_contents();
89 if (contents) 109 if (!contents)
90 StartTracking(contents->GetRenderViewHost()); 110 return;
calamity 2015/10/06 03:39:56 Is this check necessary?
dominickn 2015/10/06 04:11:45 web_contents() can return null (e.g. the web conte
calamity 2015/10/07 04:23:50 Acknowledged.
111
112 host_->AddKeyPressEventCallback(key_press_event_callback_);
113 host_->AddMouseEventCallback(mouse_event_callback_);
114 callbacks_added_ = true;
91 } 115 }
92 116
93 void SiteEngagementHelper::InputTracker::StopTracking( 117 void SiteEngagementHelper::InputTracker::RemoveCallbacks() {
94 content::RenderViewHost* host) {
95 pause_timer_->Stop();
96 if (callbacks_added_) { 118 if (callbacks_added_) {
97 host->RemoveKeyPressEventCallback(key_press_event_callback_); 119 host_->RemoveKeyPressEventCallback(key_press_event_callback_);
98 host->RemoveMouseEventCallback(mouse_event_callback_); 120 host_->RemoveMouseEventCallback(mouse_event_callback_);
99 callbacks_added_ = false; 121 callbacks_added_ = false;
100 } 122 }
101 } 123 }
102 124
103 void SiteEngagementHelper::InputTracker::SetTimerForTesting(
104 scoped_ptr<base::Timer> timer) {
105 pause_timer_ = timer.Pass();
106 }
107
108 SiteEngagementHelper::~SiteEngagementHelper() { 125 SiteEngagementHelper::~SiteEngagementHelper() {
109 content::WebContents* contents = web_contents(); 126 content::WebContents* contents = web_contents();
110 if (contents) 127 if (contents)
111 input_tracker_.StopTracking(contents->GetRenderViewHost()); 128 input_tracker_.StopTracking();
112 } 129 }
113 130
114 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) 131 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents)
115 : content::WebContentsObserver(contents), 132 : content::WebContentsObserver(contents),
116 input_tracker_(this), 133 input_tracker_(this),
117 record_engagement_(false) { } 134 record_engagement_(false) {}
118 135
119 void SiteEngagementHelper::RecordUserInput() { 136 void SiteEngagementHelper::RecordUserInput() {
120 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); 137 TRACE_EVENT0("SiteEngagement", "RecordUserInput");
121 content::WebContents* contents = web_contents(); 138 content::WebContents* contents = web_contents();
122 if (contents) { 139 if (contents) {
123 Profile* profile = 140 Profile* profile =
124 Profile::FromBrowserContext(contents->GetBrowserContext()); 141 Profile::FromBrowserContext(contents->GetBrowserContext());
125 SiteEngagementService* service = 142 SiteEngagementService* service =
126 SiteEngagementServiceFactory::GetForProfile(profile); 143 SiteEngagementServiceFactory::GetForProfile(profile);
127 144
128 // Service is null in incognito. 145 // Service is null in incognito.
129 if (service) 146 if (service)
130 service->HandleUserInput(contents->GetVisibleURL()); 147 service->HandleUserInput(contents->GetVisibleURL());
131 } 148 }
132 } 149 }
133 150
134 bool SiteEngagementHelper::ShouldRecordEngagement() {
135 return record_engagement_;
136 }
137
138 void SiteEngagementHelper::DidNavigateMainFrame( 151 void SiteEngagementHelper::DidNavigateMainFrame(
139 const content::LoadCommittedDetails& details, 152 const content::LoadCommittedDetails& details,
140 const content::FrameNavigateParams& params) { 153 const content::FrameNavigateParams& params) {
141 content::WebContents* contents = web_contents(); 154 input_tracker_.StopTracking();
142 input_tracker_.StopTracking(contents->GetRenderViewHost()); 155
156 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS();
143 157
144 // Ignore all schemes except HTTP and HTTPS. 158 // Ignore all schemes except HTTP and HTTPS.
145 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS(); 159 if (!record_engagement_)
146
147 if (!ShouldRecordEngagement())
148 return; 160 return;
149 161
150 Profile* profile = 162 Profile* profile =
151 Profile::FromBrowserContext(contents->GetBrowserContext()); 163 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
152 SiteEngagementService* service = 164 SiteEngagementService* service =
153 SiteEngagementServiceFactory::GetForProfile(profile); 165 SiteEngagementServiceFactory::GetForProfile(profile);
154 166
155 if (service) 167 if (service)
156 service->HandleNavigation(params.url, params.transition); 168 service->HandleNavigation(params.url, params.transition);
157 169
158 input_tracker_.StartTracking(contents->GetRenderViewHost()); 170 input_tracker_.StartTracking(web_contents()->GetRenderViewHost(),
171 g_seconds_tracking_delay_after_navigation);
159 } 172 }
160 173
161 void SiteEngagementHelper::RenderViewHostChanged( 174 void SiteEngagementHelper::RenderViewHostChanged(
162 content::RenderViewHost* old_host, 175 content::RenderViewHost* old_host,
163 content::RenderViewHost* new_host) { 176 content::RenderViewHost* new_host) {
164 // On changing the render view host, we need to re-register the callbacks 177 // On changing the render view host, we need to re-register the callbacks
165 // listening for user input. 178 // listening for user input.
166 if (ShouldRecordEngagement()) { 179 if (input_tracker_.is_tracking()) {
167 if (old_host) 180 input_tracker_.StopTracking();
168 input_tracker_.StopTracking(old_host); 181 input_tracker_.StartTracking(new_host, 0);
169 input_tracker_.StartTracking(new_host);
170 } 182 }
dominickn 2015/10/06 04:11:45 I think this means that the pause timer is reset t
calamity 2015/10/07 04:23:50 I think it's useful to have a straightforward way
dominickn 2015/10/07 05:38:50 I'm mainly concerned that IsTracking() and StartTr
calamity 2015/10/07 07:13:34 Agreed. Done.
171 } 183 }
172 184
173 void SiteEngagementHelper::WasShown() { 185 void SiteEngagementHelper::WasShown() {
174 // Ensure that the input callbacks are registered when we come into view. 186 // Ensure that the input callbacks are registered when we come into view.
175 if (ShouldRecordEngagement()) 187 if (record_engagement_) {
176 input_tracker_.StartTracking(web_contents()->GetRenderViewHost()); 188 input_tracker_.StartTracking(web_contents()->GetRenderViewHost(),
189 g_seconds_tracking_delay_after_show);
190 }
177 } 191 }
178 192
179 void SiteEngagementHelper::WasHidden() { 193 void SiteEngagementHelper::WasHidden() {
180 // Ensure that the input callbacks are not registered when hidden. 194 // Ensure that the input callbacks are not registered when hidden.
181 if (ShouldRecordEngagement()) { 195 input_tracker_.StopTracking();
182 content::WebContents* contents = web_contents();
183 if (contents)
184 input_tracker_.StopTracking(contents->GetRenderViewHost());
185 }
186 } 196 }
187 197
188 // static 198 // static
189 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(double seconds) { 199 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(int seconds) {
190 g_seconds_between_user_input_check = seconds; 200 g_seconds_between_user_input_check = seconds;
191 } 201 }
202
203 // static
204 void SiteEngagementHelper::SetSecondsTrackingDelayAfterNavigation(int seconds) {
205 g_seconds_tracking_delay_after_navigation = seconds;
206 }
207
208 // static
209 void SiteEngagementHelper::SetSecondsTrackingDelayAfterShow(int seconds) {
210 g_seconds_tracking_delay_after_show = seconds;
211 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698