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

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

Issue 1338603002: Implement a site engagement score based on time-on-site. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing reviewer feedback Created 5 years, 3 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 "chrome/browser/engagement/site_engagement_service.h" 8 #include "chrome/browser/engagement/site_engagement_service.h"
8 #include "chrome/browser/engagement/site_engagement_service_factory.h" 9 #include "chrome/browser/engagement/site_engagement_service_factory.h"
9 #include "chrome/browser/prerender/prerender_contents.h" 10 #include "chrome/browser/prerender/prerender_contents.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/navigation_entry.h" 12 #include "content/public/browser/navigation_entry.h"
12 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
13 14
15 namespace {
16
17 double g_seconds_between_user_input_check = 10;
18 bool g_disable_callback_registration_for_testing = false;
19
20 } // anonymous namespace
21
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper);
15 23
16 SiteEngagementHelper::~SiteEngagementHelper() { 24 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper)
25 : helper_(helper),
26 pause_timer_(new base::Timer(true, false)),
27 callbacks_added_(false) {
28 key_press_event_callback_ =
29 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent,
30 base::Unretained(this));
31 mouse_event_callback_ =
32 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent,
33 base::Unretained(this));
17 } 34 }
18 35
19 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* web_contents) 36 SiteEngagementHelper::InputTracker::~InputTracker() { }
20 : content::WebContentsObserver(web_contents) { 37
38 // 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
40 // deleted; if the contents exist, record engagement for the site. Once the
41 // timer finishes running, the callbacks detecting user input will be registered
42 // again.
43 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent(
44 const content::NativeWebKeyboardEvent& event) {
45 // 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).
47 if (event.type == blink::WebInputEvent::RawKeyDown) {
48 PauseTracking(helper_->web_contents()->GetRenderViewHost());
49 helper_->RecordUserInput();
50 }
51 return false;
21 } 52 }
22 53
23 void SiteEngagementHelper::DidStartNavigationToPendingEntry( 54 bool SiteEngagementHelper::InputTracker::HandleMouseEvent(
24 const GURL& url, 55 const blink::WebMouseEvent& event) {
25 content::NavigationController::ReloadType reload_type) { 56 // Only respond to mouse down with a button or mouse move events (e.g. a click
26 prerender::PrerenderContents* prerender_contents = 57 // is a mouse down and mouse up) to avoid cases where multiple events come in
27 prerender::PrerenderContents::FromWebContents(web_contents()); 58 // before we can pause tracking.
59 if ((event.button != blink::WebMouseEvent::ButtonNone &&
60 event.type == blink::WebInputEvent::MouseDown) ||
61 event.type == blink::WebInputEvent::MouseMove ||
jdduke (slow) 2015/09/24 15:13:25 I'm not sure MouseMove's without an active button
dominickn 2015/09/25 00:08:56 Done, though I think it's also important to listen
62 event.type == blink::WebInputEvent::MouseWheel) {
63 PauseTracking(helper_->web_contents()->GetRenderViewHost());
64 helper_->RecordUserInput();
65 }
66 return false;
67 }
28 68
29 // Ignore pre-render loads. 69 void SiteEngagementHelper::InputTracker::StartTracking(
30 if (prerender_contents != NULL) 70 content::RenderViewHost* host) {
31 return; 71 if (!callbacks_added_ && !g_disable_callback_registration_for_testing) {
72 host->AddKeyPressEventCallback(key_press_event_callback_);
73 host->AddMouseEventCallback(mouse_event_callback_);
74 callbacks_added_ = true;
75 }
76 }
77
78 void SiteEngagementHelper::InputTracker::PauseTracking(
79 content::RenderViewHost* host) {
80 StopTracking(host);
81 pause_timer_->Start(FROM_HERE, base::TimeDelta::FromSeconds(
82 g_seconds_between_user_input_check),
83 base::Bind(&SiteEngagementHelper::InputTracker::ResumeTracking,
84 base::Unretained(this)));
85 }
86
87 void SiteEngagementHelper::InputTracker::ResumeTracking() {
88 content::WebContents* contents = helper_->web_contents();
89 if (contents)
90 StartTracking(contents->GetRenderViewHost());
91 }
92
93 void SiteEngagementHelper::InputTracker::StopTracking(
94 content::RenderViewHost* host) {
95 pause_timer_->Stop();
96 if (callbacks_added_) {
97 host->RemoveKeyPressEventCallback(key_press_event_callback_);
98 host->RemoveMouseEventCallback(mouse_event_callback_);
99 callbacks_added_ = false;
100 }
101 }
102
103 void SiteEngagementHelper::InputTracker::SetTimerForTesting(
104 scoped_ptr<base::Timer> timer) {
105 pause_timer_ = timer.Pass();
106 }
107
108 SiteEngagementHelper::~SiteEngagementHelper() {
109 content::WebContents* contents = web_contents();
110 if (contents)
111 input_tracker_->StopTracking(contents->GetRenderViewHost());
112 }
113
114 void SiteEngagementHelper::AddObserverForTesting(Observer* observer) {
115 observer_list_.AddObserver(observer);
116 }
117
118 void SiteEngagementHelper::RemoveObserverForTesting(Observer* observer) {
119 observer_list_.RemoveObserver(observer);
120 }
121
122 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents)
123 : content::WebContentsObserver(contents),
124 input_tracker_(new InputTracker(this)),
125 record_engagement_(false) { }
126
127 void SiteEngagementHelper::RecordUserInput() {
128 content::WebContents* contents = web_contents();
129 if (contents) {
130 Profile* profile =
131 Profile::FromBrowserContext(contents->GetBrowserContext());
132 SiteEngagementService* service =
133 SiteEngagementServiceFactory::GetForProfile(profile);
134
135 // Service is null in incognito.
136 if (service) {
137 service->HandleUserInput(contents->GetVisibleURL());
138 FOR_EACH_OBSERVER(Observer, observer_list_, OnInputRecorded(this));
jdduke (slow) 2015/09/24 15:17:35 Any idea what the potential costs are here? Is it
dominickn 2015/09/25 00:08:56 Do you mean the observer or the cost of recording
139 }
140 }
141 }
142
143 void SiteEngagementHelper::DidNavigateMainFrame(
144 const content::LoadCommittedDetails& details,
145 const content::FrameNavigateParams& params) {
146 content::WebContents* contents = web_contents();
147 input_tracker_->StopTracking(contents->GetRenderViewHost());
32 148
33 // Ignore all schemes except HTTP and HTTPS. 149 // Ignore all schemes except HTTP and HTTPS.
34 if (!url.SchemeIsHTTPOrHTTPS()) 150 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS();
151
152 if (!record_engagement_)
35 return; 153 return;
36 154
37 Profile* profile = 155 Profile* profile =
38 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 156 Profile::FromBrowserContext(contents->GetBrowserContext());
39 SiteEngagementService* service = 157 SiteEngagementService* service =
40 SiteEngagementServiceFactory::GetForProfile(profile); 158 SiteEngagementServiceFactory::GetForProfile(profile);
41 // Service is null in incognito.
42 if (!service)
43 return;
44 159
45 service->HandleNavigation(url); 160 if (service)
161 service->HandleNavigation(params.url, params.transition);
162
163 input_tracker_->StartTracking(contents->GetRenderViewHost());
46 } 164 }
165
166 void SiteEngagementHelper::RenderViewHostChanged(
167 content::RenderViewHost* old_host,
168 content::RenderViewHost* new_host) {
169 // On changing the render view host, we need to re-register the callbacks
170 // listening for user input.
171 if (record_engagement_) {
172 if (old_host)
173 input_tracker_->StopTracking(old_host);
174 input_tracker_->StartTracking(new_host);
175 }
176 }
177
178 void SiteEngagementHelper::WasShown() {
179 // Ensure that the input callbacks are registered when we come into view.
180 if (record_engagement_)
181 input_tracker_->StartTracking(web_contents()->GetRenderViewHost());
182 }
183
184 void SiteEngagementHelper::WasHidden() {
185 // Ensure that the input callbacks are not registered when hidden.
186 if (record_engagement_) {
187 content::WebContents* contents = web_contents();
188 if (contents)
189 input_tracker_->StopTracking(contents->GetRenderViewHost());
190 }
191 }
192
193 // static
194 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(double seconds) {
195 g_seconds_between_user_input_check = seconds;
196 }
197
198 // static
199 void SiteEngagementHelper::EnableCallbackRegistrationForTesting() {
200 g_disable_callback_registration_for_testing = false;
201 }
202
203 // static
204 void SiteEngagementHelper::DisableCallbackRegistrationForTesting() {
205 g_disable_callback_registration_for_testing = true;
206 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_helper.h ('k') | chrome/browser/engagement/site_engagement_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698