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

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

Issue 2535483002: Plumb site engagement to the renderer process. (Closed)
Patch Set: Rebase Created 4 years 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/bind.h"
10 #include "base/callback.h"
9 #include "base/time/time.h" 11 #include "base/time/time.h"
10 #include "base/trace_event/trace_event.h" 12 #include "base/trace_event/trace_event.h"
11 #include "chrome/browser/engagement/site_engagement_service.h" 13 #include "chrome/browser/engagement/site_engagement_service.h"
12 #include "chrome/browser/prerender/prerender_contents.h" 14 #include "chrome/browser/prerender/prerender_contents.h"
13 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
14 #include "content/public/browser/navigation_handle.h" 16 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/associated_interface_provider.h"
20 #include "url/gurl.h"
21 #include "url/origin.h"
16 22
17 namespace { 23 namespace {
18 24
19 int g_seconds_to_pause_engagement_detection = 10; 25 int g_seconds_to_pause_engagement_detection = 10;
20 int g_seconds_delay_after_navigation = 10; 26 int g_seconds_delay_after_navigation = 10;
21 int g_seconds_delay_after_media_starts = 10; 27 int g_seconds_delay_after_media_starts = 10;
22 int g_seconds_delay_after_show = 5; 28 int g_seconds_delay_after_show = 5;
23 29
24 } // anonymous namespace 30 } // anonymous namespace
25 31
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementService::Helper); 32 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementService::Helper);
27 33
34 // static
35 void SiteEngagementService::Helper::SetSecondsBetweenUserInputCheck(
36 int seconds) {
37 g_seconds_to_pause_engagement_detection = seconds;
38 }
39
40 // static
41 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation(
42 int seconds) {
43 g_seconds_delay_after_navigation = seconds;
44 }
45
46 // static
47 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow(
48 int seconds) {
49 g_seconds_delay_after_show = seconds;
50 }
51
52 SiteEngagementService::Helper::~Helper() {
53 service_->HelperDeleted(this);
54 if (web_contents()) {
55 input_tracker_.Stop();
56 media_tracker_.Stop();
57 }
58 }
59
60 void SiteEngagementService::Helper::OnEngagementLevelChanged(
61 const GURL& url,
62 blink::mojom::EngagementLevel level) {
63 web_contents()->ForEachFrame(base::Bind(
64 &SiteEngagementService::Helper::SendEngagementLevelToFramesMatchingOrigin,
65 base::Unretained(this), url::Origin(url), level));
66 }
67
28 SiteEngagementService::Helper::PeriodicTracker::PeriodicTracker( 68 SiteEngagementService::Helper::PeriodicTracker::PeriodicTracker(
29 SiteEngagementService::Helper* helper) 69 SiteEngagementService::Helper* helper)
30 : helper_(helper), pause_timer_(new base::Timer(true, false)) {} 70 : helper_(helper), pause_timer_(new base::Timer(true, false)) {}
31 71
32 SiteEngagementService::Helper::PeriodicTracker::~PeriodicTracker() {} 72 SiteEngagementService::Helper::PeriodicTracker::~PeriodicTracker() {}
33 73
34 void SiteEngagementService::Helper::PeriodicTracker::Start( 74 void SiteEngagementService::Helper::PeriodicTracker::Start(
35 base::TimeDelta initial_delay) { 75 base::TimeDelta initial_delay) {
36 StartTimer(initial_delay); 76 StartTimer(initial_delay);
37 } 77 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 190 }
151 191
152 void SiteEngagementService::Helper::MediaTracker::WasShown() { 192 void SiteEngagementService::Helper::MediaTracker::WasShown() {
153 is_hidden_ = false; 193 is_hidden_ = false;
154 } 194 }
155 195
156 void SiteEngagementService::Helper::MediaTracker::WasHidden() { 196 void SiteEngagementService::Helper::MediaTracker::WasHidden() {
157 is_hidden_ = true; 197 is_hidden_ = true;
158 } 198 }
159 199
160 SiteEngagementService::Helper::~Helper() {
161 if (web_contents()) {
162 input_tracker_.Stop();
163 media_tracker_.Stop();
164 }
165 }
166
167 SiteEngagementService::Helper::Helper(content::WebContents* web_contents) 200 SiteEngagementService::Helper::Helper(content::WebContents* web_contents)
168 : content::WebContentsObserver(web_contents), 201 : content::WebContentsObserver(web_contents),
169 input_tracker_(this, web_contents), 202 input_tracker_(this, web_contents),
170 media_tracker_(this, web_contents), 203 media_tracker_(this, web_contents),
171 service_(SiteEngagementService::Get( 204 service_(SiteEngagementService::Get(
172 Profile::FromBrowserContext(web_contents->GetBrowserContext()))) {} 205 Profile::FromBrowserContext(web_contents->GetBrowserContext()))) {
206 service_->HelperCreated(this);
207 }
173 208
174 void SiteEngagementService::Helper::RecordUserInput( 209 void SiteEngagementService::Helper::RecordUserInput(
175 SiteEngagementMetrics::EngagementType type) { 210 SiteEngagementMetrics::EngagementType type) {
176 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); 211 TRACE_EVENT0("SiteEngagement", "RecordUserInput");
177 content::WebContents* contents = web_contents(); 212 content::WebContents* contents = web_contents();
178 if (contents) 213 if (contents)
179 service_->HandleUserInput(contents, type); 214 service_->HandleUserInput(contents, type);
180 } 215 }
181 216
182 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) { 217 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) {
183 content::WebContents* contents = web_contents(); 218 content::WebContents* contents = web_contents();
184 if (contents) 219 if (contents)
185 service_->HandleMediaPlaying(contents, is_hidden); 220 service_->HandleMediaPlaying(contents, is_hidden);
186 } 221 }
187 222
223 void SiteEngagementService::Helper::SendEngagementLevelToFramesMatchingOrigin(
224 const url::Origin& origin,
225 blink::mojom::EngagementLevel level,
226 content::RenderFrameHost* render_frame_host) {
227 if (origin == render_frame_host->GetLastCommittedOrigin()) {
228 SendEngagementLevelToFrame(origin, level, render_frame_host);
229 }
230 }
231
232 void SiteEngagementService::Helper::SendEngagementLevelToFrame(
233 const url::Origin& origin,
234 blink::mojom::EngagementLevel level,
235 content::RenderFrameHost* render_frame_host) {
236 blink::mojom::EngagementClientAssociatedPtr client;
237 render_frame_host->GetRemoteAssociatedInterfaces()->GetInterface(&client);
238 client->SetEngagementLevel(origin, level);
239 }
240
188 void SiteEngagementService::Helper::DidFinishNavigation( 241 void SiteEngagementService::Helper::DidFinishNavigation(
189 content::NavigationHandle* handle) { 242 content::NavigationHandle* handle) {
190 // Ignore uncommitted, non main-frame, same page, or error page navigations. 243 // Ignore uncommitted, non main-frame, same page, or error page navigations.
191 if (!handle->HasCommitted() || !handle->IsInMainFrame() || 244 if (!handle->HasCommitted() || !handle->IsInMainFrame() ||
192 handle->IsSamePage() || handle->IsErrorPage()) { 245 handle->IsSamePage() || handle->IsErrorPage()) {
193 return; 246 return;
194 } 247 }
195 248
196 input_tracker_.Stop(); 249 input_tracker_.Stop();
197 media_tracker_.Stop(); 250 media_tracker_.Stop();
(...skipping 13 matching lines...) Expand all
211 // will activate even if navigation engagement is not scored. 264 // will activate even if navigation engagement is not scored.
212 if (prerender::PrerenderContents::FromWebContents(web_contents()) != nullptr) 265 if (prerender::PrerenderContents::FromWebContents(web_contents()) != nullptr)
213 return; 266 return;
214 267
215 service_->HandleNavigation(web_contents(), handle->GetPageTransition()); 268 service_->HandleNavigation(web_contents(), handle->GetPageTransition());
216 269
217 input_tracker_.Start( 270 input_tracker_.Start(
218 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation)); 271 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation));
219 } 272 }
220 273
274 void SiteEngagementService::Helper::ReadyToCommitNavigation(
275 content::NavigationHandle* handle) {
276 if (service_->ShouldRecordEngagement(handle->GetURL())) {
277 // Don't bother sending the engagement if we wouldn't have recorded any for
278 // the URL. These will have NONE engagement by default.
279 SendEngagementLevelToFrame(url::Origin(handle->GetURL()),
280 service_->GetEngagementLevel(handle->GetURL()),
281 handle->GetRenderFrameHost());
282 }
283 }
284
221 void SiteEngagementService::Helper::WasShown() { 285 void SiteEngagementService::Helper::WasShown() {
222 // Ensure that the input callbacks are registered when we come into view. 286 // Ensure that the input callbacks are registered when we come into view.
223 input_tracker_.Start( 287 input_tracker_.Start(
224 base::TimeDelta::FromSeconds(g_seconds_delay_after_show)); 288 base::TimeDelta::FromSeconds(g_seconds_delay_after_show));
225 } 289 }
226 290
227 void SiteEngagementService::Helper::WasHidden() { 291 void SiteEngagementService::Helper::WasHidden() {
228 // Ensure that the input callbacks are not registered when hidden. 292 // Ensure that the input callbacks are not registered when hidden.
229 input_tracker_.Stop(); 293 input_tracker_.Stop();
230 } 294 }
231
232 // static
233 void SiteEngagementService::Helper::SetSecondsBetweenUserInputCheck(
234 int seconds) {
235 g_seconds_to_pause_engagement_detection = seconds;
236 }
237
238 // static
239 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation(
240 int seconds) {
241 g_seconds_delay_after_navigation = seconds;
242 }
243
244 // static
245 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow(
246 int seconds) {
247 g_seconds_delay_after_show = seconds;
248 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_helper.h ('k') | chrome/browser/engagement/site_engagement_score.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698