OLD | NEW |
---|---|
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" | |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 int g_seconds_to_pause_engagement_detection = 10; | 23 int g_seconds_to_pause_engagement_detection = 10; |
20 int g_seconds_delay_after_navigation = 10; | 24 int g_seconds_delay_after_navigation = 10; |
21 int g_seconds_delay_after_media_starts = 10; | 25 int g_seconds_delay_after_media_starts = 10; |
22 int g_seconds_delay_after_show = 5; | 26 int g_seconds_delay_after_show = 5; |
23 | 27 |
24 } // anonymous namespace | 28 } // anonymous namespace |
25 | 29 |
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementService::Helper); | 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementService::Helper); |
27 | 31 |
32 // static | |
33 void SiteEngagementService::Helper::SetSecondsBetweenUserInputCheck( | |
34 int seconds) { | |
35 g_seconds_to_pause_engagement_detection = seconds; | |
36 } | |
37 | |
38 // static | |
39 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation( | |
40 int seconds) { | |
41 g_seconds_delay_after_navigation = seconds; | |
42 } | |
43 | |
44 // static | |
45 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow( | |
46 int seconds) { | |
47 g_seconds_delay_after_show = seconds; | |
48 } | |
49 | |
50 SiteEngagementService::Helper::~Helper() { | |
51 service_->HelperDeleted(this); | |
52 if (web_contents()) { | |
53 input_tracker_.Stop(); | |
54 media_tracker_.Stop(); | |
55 } | |
56 } | |
57 | |
58 void SiteEngagementService::Helper::OnEngagementLevelChanged( | |
59 const GURL& origin, | |
60 blink::mojom::EngagementLevel level) { | |
61 web_contents()->ForEachFrame(base::Bind( | |
62 &SiteEngagementService::Helper::SendEngagementLevelToFramesMatchingOrigin, | |
63 base::Unretained(this), origin, level)); | |
64 } | |
65 | |
28 SiteEngagementService::Helper::PeriodicTracker::PeriodicTracker( | 66 SiteEngagementService::Helper::PeriodicTracker::PeriodicTracker( |
29 SiteEngagementService::Helper* helper) | 67 SiteEngagementService::Helper* helper) |
30 : helper_(helper), pause_timer_(new base::Timer(true, false)) {} | 68 : helper_(helper), pause_timer_(new base::Timer(true, false)) {} |
31 | 69 |
32 SiteEngagementService::Helper::PeriodicTracker::~PeriodicTracker() {} | 70 SiteEngagementService::Helper::PeriodicTracker::~PeriodicTracker() {} |
33 | 71 |
34 void SiteEngagementService::Helper::PeriodicTracker::Start( | 72 void SiteEngagementService::Helper::PeriodicTracker::Start( |
35 base::TimeDelta initial_delay) { | 73 base::TimeDelta initial_delay) { |
36 StartTimer(initial_delay); | 74 StartTimer(initial_delay); |
37 } | 75 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 } | 188 } |
151 | 189 |
152 void SiteEngagementService::Helper::MediaTracker::WasShown() { | 190 void SiteEngagementService::Helper::MediaTracker::WasShown() { |
153 is_hidden_ = false; | 191 is_hidden_ = false; |
154 } | 192 } |
155 | 193 |
156 void SiteEngagementService::Helper::MediaTracker::WasHidden() { | 194 void SiteEngagementService::Helper::MediaTracker::WasHidden() { |
157 is_hidden_ = true; | 195 is_hidden_ = true; |
158 } | 196 } |
159 | 197 |
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) | 198 SiteEngagementService::Helper::Helper(content::WebContents* web_contents) |
168 : content::WebContentsObserver(web_contents), | 199 : content::WebContentsObserver(web_contents), |
169 input_tracker_(this, web_contents), | 200 input_tracker_(this, web_contents), |
170 media_tracker_(this, web_contents), | 201 media_tracker_(this, web_contents), |
171 service_(SiteEngagementService::Get( | 202 service_(SiteEngagementService::Get( |
172 Profile::FromBrowserContext(web_contents->GetBrowserContext()))) {} | 203 Profile::FromBrowserContext(web_contents->GetBrowserContext()))) { |
204 service_->HelperCreated(this); | |
205 } | |
173 | 206 |
174 void SiteEngagementService::Helper::RecordUserInput( | 207 void SiteEngagementService::Helper::RecordUserInput( |
175 SiteEngagementMetrics::EngagementType type) { | 208 SiteEngagementMetrics::EngagementType type) { |
176 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); | 209 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); |
177 content::WebContents* contents = web_contents(); | 210 content::WebContents* contents = web_contents(); |
178 if (contents) | 211 if (contents) |
179 service_->HandleUserInput(contents, type); | 212 service_->HandleUserInput(contents, type); |
180 } | 213 } |
181 | 214 |
182 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) { | 215 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) { |
183 content::WebContents* contents = web_contents(); | 216 content::WebContents* contents = web_contents(); |
184 if (contents) | 217 if (contents) |
185 service_->HandleMediaPlaying(contents, is_hidden); | 218 service_->HandleMediaPlaying(contents, is_hidden); |
186 } | 219 } |
187 | 220 |
221 void SiteEngagementService::Helper::SendEngagementLevelToFramesMatchingOrigin( | |
222 const GURL& origin, | |
223 blink::mojom::EngagementLevel level, | |
224 content::RenderFrameHost* render_frame_host) { | |
225 if (origin == render_frame_host->GetLastCommittedURL()) { | |
nasko
2016/11/28 19:18:08
Why not GetLastCommittedOrigin? Comparing an origi
dominickn
2016/11/29 07:41:22
Done. We have a bunch of type conversion for now b
| |
226 SendEngagementLevelToFrame(level, render_frame_host); | |
227 } | |
228 } | |
229 | |
230 void SiteEngagementService::Helper::SendEngagementLevelToFrame( | |
231 blink::mojom::EngagementLevel level, | |
232 content::RenderFrameHost* render_frame_host) { | |
233 blink::mojom::EngagementClientAssociatedPtr client; | |
234 render_frame_host->GetRemoteAssociatedInterfaces()->GetInterface(&client); | |
235 client->SetEngagementLevel(level); | |
236 } | |
237 | |
188 void SiteEngagementService::Helper::DidFinishNavigation( | 238 void SiteEngagementService::Helper::DidFinishNavigation( |
189 content::NavigationHandle* handle) { | 239 content::NavigationHandle* handle) { |
190 // Ignore uncommitted, non main-frame, same page, or error page navigations. | 240 // Ignore uncommitted, non main-frame, same page, or error page navigations. |
191 if (!handle->HasCommitted() || !handle->IsInMainFrame() || | 241 if (!handle->HasCommitted() || !handle->IsInMainFrame() || |
192 handle->IsSamePage() || handle->IsErrorPage()) { | 242 handle->IsSamePage() || handle->IsErrorPage()) { |
193 return; | 243 return; |
194 } | 244 } |
195 | 245 |
196 input_tracker_.Stop(); | 246 input_tracker_.Stop(); |
197 media_tracker_.Stop(); | 247 media_tracker_.Stop(); |
(...skipping 13 matching lines...) Expand all Loading... | |
211 // will activate even if navigation engagement is not scored. | 261 // will activate even if navigation engagement is not scored. |
212 if (prerender::PrerenderContents::FromWebContents(web_contents()) != nullptr) | 262 if (prerender::PrerenderContents::FromWebContents(web_contents()) != nullptr) |
213 return; | 263 return; |
214 | 264 |
215 service_->HandleNavigation(web_contents(), handle->GetPageTransition()); | 265 service_->HandleNavigation(web_contents(), handle->GetPageTransition()); |
216 | 266 |
217 input_tracker_.Start( | 267 input_tracker_.Start( |
218 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation)); | 268 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation)); |
219 } | 269 } |
220 | 270 |
271 void SiteEngagementService::Helper::ReadyToCommitNavigation( | |
272 content::NavigationHandle* handle) { | |
273 SendEngagementLevelToFrame(service_->GetEngagementLevel(handle->GetURL()), | |
274 handle->GetRenderFrameHost()); | |
275 } | |
276 | |
221 void SiteEngagementService::Helper::WasShown() { | 277 void SiteEngagementService::Helper::WasShown() { |
222 // Ensure that the input callbacks are registered when we come into view. | 278 // Ensure that the input callbacks are registered when we come into view. |
223 input_tracker_.Start( | 279 input_tracker_.Start( |
224 base::TimeDelta::FromSeconds(g_seconds_delay_after_show)); | 280 base::TimeDelta::FromSeconds(g_seconds_delay_after_show)); |
225 } | 281 } |
226 | 282 |
227 void SiteEngagementService::Helper::WasHidden() { | 283 void SiteEngagementService::Helper::WasHidden() { |
228 // Ensure that the input callbacks are not registered when hidden. | 284 // Ensure that the input callbacks are not registered when hidden. |
229 input_tracker_.Stop(); | 285 input_tracker_.Stop(); |
230 } | 286 } |
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 } | |
OLD | NEW |