Chromium Code Reviews| 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 "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 kSecondsBetweenUserInputCheck = 10; |
| 19 | 19 |
| 20 } // anonymous namespace | 20 } // anonymous namespace |
| 21 | 21 |
| 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); | 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); |
| 23 | 23 |
| 24 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) | 24 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) |
| 25 : helper_(helper), | 25 : helper_(helper), |
| 26 pause_timer_(new base::Timer(true, false)), | 26 pause_timer_(new base::Timer(true, false)), |
| 27 callbacks_added_(false) { | 27 callbacks_added_(false) { |
| 28 key_press_event_callback_ = | 28 key_press_event_callback_ = |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 host->AddMouseEventCallback(mouse_event_callback_); | 72 host->AddMouseEventCallback(mouse_event_callback_); |
| 73 callbacks_added_ = true; | 73 callbacks_added_ = true; |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 void SiteEngagementHelper::InputTracker::PauseTracking( | 77 void SiteEngagementHelper::InputTracker::PauseTracking( |
| 78 content::RenderViewHost* host) { | 78 content::RenderViewHost* host) { |
| 79 StopTracking(host); | 79 StopTracking(host); |
| 80 pause_timer_->Start( | 80 pause_timer_->Start( |
| 81 FROM_HERE, | 81 FROM_HERE, |
| 82 base::TimeDelta::FromSeconds(g_seconds_between_user_input_check), | 82 base::TimeDelta::FromSeconds(kSecondsBetweenUserInputCheck), |
| 83 base::Bind(&SiteEngagementHelper::InputTracker::ResumeTracking, | 83 base::Bind(&SiteEngagementHelper::InputTracker::ResumeTracking, |
| 84 base::Unretained(this))); | 84 base::Unretained(this))); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void SiteEngagementHelper::InputTracker::ResumeTracking() { | 87 void SiteEngagementHelper::InputTracker::ResumeTracking() { |
| 88 content::WebContents* contents = helper_->web_contents(); | 88 content::WebContents* contents = helper_->web_contents(); |
| 89 if (contents) | 89 if (contents) |
| 90 StartTracking(contents->GetRenderViewHost()); | 90 StartTracking(contents->GetRenderViewHost()); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void SiteEngagementHelper::InputTracker::StopTracking( | 93 void SiteEngagementHelper::InputTracker::StopTracking( |
| 94 content::RenderViewHost* host) { | 94 content::RenderViewHost* host) { |
| 95 pause_timer_->Stop(); | 95 pause_timer_->Stop(); |
| 96 if (callbacks_added_) { | 96 if (callbacks_added_) { |
| 97 host->RemoveKeyPressEventCallback(key_press_event_callback_); | 97 host->RemoveKeyPressEventCallback(key_press_event_callback_); |
| 98 host->RemoveMouseEventCallback(mouse_event_callback_); | 98 host->RemoveMouseEventCallback(mouse_event_callback_); |
| 99 callbacks_added_ = false; | 99 callbacks_added_ = false; |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 | 102 |
| 103 void SiteEngagementHelper::InputTracker::SetTimerForTesting( | 103 bool SiteEngagementHelper::InputTracker::IsTracking() { |
| 104 return callbacks_added_; | |
| 105 } | |
| 106 | |
| 107 void SiteEngagementHelper::InputTracker::SetPauseTimerForTesting( | |
| 104 scoped_ptr<base::Timer> timer) { | 108 scoped_ptr<base::Timer> timer) { |
| 105 pause_timer_ = timer.Pass(); | 109 pause_timer_ = timer.Pass(); |
| 106 } | 110 } |
| 107 | 111 |
| 108 SiteEngagementHelper::~SiteEngagementHelper() { | 112 SiteEngagementHelper::~SiteEngagementHelper() { |
| 109 content::WebContents* contents = web_contents(); | 113 content::WebContents* contents = web_contents(); |
| 110 if (contents) | 114 if (contents) |
| 111 input_tracker_.StopTracking(contents->GetRenderViewHost()); | 115 input_tracker_.StopTracking(contents->GetRenderViewHost()); |
| 112 } | 116 } |
| 113 | 117 |
| 114 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) | 118 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) |
| 115 : content::WebContentsObserver(contents), | 119 : content::WebContentsObserver(contents), |
| 120 navigation_timer_(new base::Timer(true, false)), | |
| 116 input_tracker_(this), | 121 input_tracker_(this), |
| 117 record_engagement_(false) { } | 122 record_engagement_(false) {} |
| 118 | 123 |
| 119 void SiteEngagementHelper::RecordUserInput() { | 124 void SiteEngagementHelper::RecordUserInput() { |
| 120 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); | 125 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); |
| 121 content::WebContents* contents = web_contents(); | 126 content::WebContents* contents = web_contents(); |
| 122 if (contents) { | 127 if (contents) { |
| 123 Profile* profile = | 128 Profile* profile = |
| 124 Profile::FromBrowserContext(contents->GetBrowserContext()); | 129 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 125 SiteEngagementService* service = | 130 SiteEngagementService* service = |
| 126 SiteEngagementServiceFactory::GetForProfile(profile); | 131 SiteEngagementServiceFactory::GetForProfile(profile); |
| 127 | 132 |
| 128 // Service is null in incognito. | 133 // Service is null in incognito. |
| 129 if (service) | 134 if (service) |
| 130 service->HandleUserInput(contents->GetVisibleURL()); | 135 service->HandleUserInput(contents->GetVisibleURL()); |
| 131 } | 136 } |
| 132 } | 137 } |
| 133 | 138 |
| 134 bool SiteEngagementHelper::ShouldRecordEngagement() { | |
| 135 return record_engagement_; | |
| 136 } | |
| 137 | |
| 138 void SiteEngagementHelper::DidNavigateMainFrame( | 139 void SiteEngagementHelper::DidNavigateMainFrame( |
| 139 const content::LoadCommittedDetails& details, | 140 const content::LoadCommittedDetails& details, |
| 140 const content::FrameNavigateParams& params) { | 141 const content::FrameNavigateParams& params) { |
| 141 content::WebContents* contents = web_contents(); | 142 content::WebContents* contents = web_contents(); |
| 142 input_tracker_.StopTracking(contents->GetRenderViewHost()); | 143 input_tracker_.StopTracking(contents->GetRenderViewHost()); |
| 144 navigation_timer_->Stop(); | |
| 145 | |
| 146 record_engagement_ = false; | |
| 143 | 147 |
| 144 // Ignore all schemes except HTTP and HTTPS. | 148 // Ignore all schemes except HTTP and HTTPS. |
| 145 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS(); | 149 if (!params.url.SchemeIsHTTPOrHTTPS()) |
| 146 | |
| 147 if (!ShouldRecordEngagement()) | |
| 148 return; | 150 return; |
| 149 | 151 |
| 150 Profile* profile = | 152 Profile* profile = |
| 151 Profile::FromBrowserContext(contents->GetBrowserContext()); | 153 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 152 SiteEngagementService* service = | 154 SiteEngagementService* service = |
| 153 SiteEngagementServiceFactory::GetForProfile(profile); | 155 SiteEngagementServiceFactory::GetForProfile(profile); |
| 154 | 156 |
| 155 if (service) | 157 if (service) |
| 156 service->HandleNavigation(params.url, params.transition); | 158 service->HandleNavigation(params.url, params.transition); |
| 157 | 159 |
| 158 input_tracker_.StartTracking(contents->GetRenderViewHost()); | 160 // Input tracking commences after a delay. |
| 161 navigation_timer_->Start( | |
| 162 FROM_HERE, base::TimeDelta::FromSeconds(kSecondsBetweenUserInputCheck), | |
| 163 base::Bind(&SiteEngagementHelper::OnNavigationTimerFired, | |
| 164 base::Unretained(this))); | |
| 159 } | 165 } |
| 160 | 166 |
| 161 void SiteEngagementHelper::RenderViewHostChanged( | 167 void SiteEngagementHelper::RenderViewHostChanged( |
| 162 content::RenderViewHost* old_host, | 168 content::RenderViewHost* old_host, |
| 163 content::RenderViewHost* new_host) { | 169 content::RenderViewHost* new_host) { |
| 164 // On changing the render view host, we need to re-register the callbacks | 170 // On changing the render view host, we need to re-register the callbacks |
| 165 // listening for user input. | 171 // listening for user input. |
| 166 if (ShouldRecordEngagement()) { | 172 if (input_tracker_.IsTracking()) { |
| 167 if (old_host) | 173 if (old_host) |
| 168 input_tracker_.StopTracking(old_host); | 174 input_tracker_.StopTracking(old_host); |
| 175 | |
| 169 input_tracker_.StartTracking(new_host); | 176 input_tracker_.StartTracking(new_host); |
| 170 } | 177 } |
| 171 } | 178 } |
| 172 | 179 |
| 180 void SiteEngagementHelper::SetNavigationTimerForTesting( | |
| 181 scoped_ptr<base::Timer> timer) { | |
| 182 navigation_timer_ = timer.Pass(); | |
| 183 } | |
| 184 | |
| 185 void SiteEngagementHelper::OnNavigationTimerFired() { | |
| 186 record_engagement_ = true; | |
| 187 if (visible_) | |
| 188 input_tracker_.StartTracking(web_contents()->GetRenderViewHost()); | |
| 189 } | |
| 190 | |
| 173 void SiteEngagementHelper::WasShown() { | 191 void SiteEngagementHelper::WasShown() { |
| 192 visible_ = true; | |
| 174 // Ensure that the input callbacks are registered when we come into view. | 193 // Ensure that the input callbacks are registered when we come into view. |
| 175 if (ShouldRecordEngagement()) | 194 if (record_engagement_) |
| 176 input_tracker_.StartTracking(web_contents()->GetRenderViewHost()); | 195 input_tracker_.StartTracking(web_contents()->GetRenderViewHost()); |
|
dominickn
2015/10/06 00:04:41
Is it correct that WasShown() should immediately t
calamity
2015/10/06 03:39:55
Agreed.
| |
| 177 } | 196 } |
| 178 | 197 |
| 179 void SiteEngagementHelper::WasHidden() { | 198 void SiteEngagementHelper::WasHidden() { |
| 199 visible_ = false; | |
| 180 // Ensure that the input callbacks are not registered when hidden. | 200 // Ensure that the input callbacks are not registered when hidden. |
| 181 if (ShouldRecordEngagement()) { | 201 if (input_tracker_.IsTracking()) { |
| 182 content::WebContents* contents = web_contents(); | 202 content::WebContents* contents = web_contents(); |
| 183 if (contents) | 203 if (contents) |
| 184 input_tracker_.StopTracking(contents->GetRenderViewHost()); | 204 input_tracker_.StopTracking(contents->GetRenderViewHost()); |
| 185 } | 205 } |
| 186 } | 206 } |
| 187 | |
| 188 // static | |
|
dominickn
2015/10/06 00:04:41
Is there a particular reason for removing this fun
calamity
2015/10/06 03:39:55
Yeah, ok.
| |
| 189 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(double seconds) { | |
| 190 g_seconds_between_user_input_check = seconds; | |
| 191 } | |
| OLD | NEW |