OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/page_load_metrics/observers/ads_page_load_metrics_obser ver.h" | 5 #include "chrome/browser/page_load_metrics/observers/ads_page_load_metrics_obser ver.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "chrome/browser/page_load_metrics/ads_detection.h" | |
14 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" | 15 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" |
15 #include "content/public/browser/navigation_handle.h" | 16 #include "content/public/browser/navigation_handle.h" |
16 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT}; | 23 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT}; |
23 | 24 |
24 #define ADS_HISTOGRAM(suffix, hist_macro, ad_type, value) \ | 25 #define ADS_HISTOGRAM(suffix, hist_macro, ad_type, value) \ |
25 switch (ad_type) { \ | 26 switch (ad_type) { \ |
26 case AdsPageLoadMetricsObserver::AD_TYPE_GOOGLE: \ | 27 case AD_TYPE_GOOGLE: \ |
27 hist_macro("PageLoad.Clients.Ads.Google." suffix, value); \ | 28 hist_macro("PageLoad.Clients.Ads.Google." suffix, value); \ |
28 break; \ | 29 break; \ |
29 case AdsPageLoadMetricsObserver::AD_TYPE_SUBRESOURCE_FILTER: \ | 30 case AD_TYPE_SUBRESOURCE_FILTER: \ |
30 hist_macro("PageLoad.Clients.Ads.SubresourceFilter." suffix, value); \ | 31 hist_macro("PageLoad.Clients.Ads.SubresourceFilter." suffix, value); \ |
31 break; \ | 32 break; \ |
32 case AdsPageLoadMetricsObserver::AD_TYPE_ALL: \ | 33 case AD_TYPE_ALL: \ |
33 hist_macro("PageLoad.Clients.Ads.All." suffix, value); \ | 34 hist_macro("PageLoad.Clients.Ads.All." suffix, value); \ |
34 break; \ | 35 break; \ |
35 } | 36 } |
36 | 37 |
37 bool DetectGoogleAd(content::NavigationHandle* navigation_handle) { | 38 void RecordParentExistsForSubFrame(bool parent_exists, |
38 // Because sub-resource filtering isn't always enabled, and doesn't work | 39 const AdTypes& ad_types) { |
39 // well in monitoring mode (no CSS enforcement), it's difficult to identify | 40 ADS_HISTOGRAM("ParentExistsForSubFrame", UMA_HISTOGRAM_BOOLEAN, AD_TYPE_ALL, |
40 // ads. Google ads are prevalent and easy to track, so we'll start by | 41 parent_exists); |
41 // tracking those. Note that the frame name can be very large, so be careful | |
42 // to avoid full string searches if possible. | |
43 // TODO(jkarlin): Track other ad networks that are easy to identify. | |
44 | |
45 // In case the navigation aborted, look up the RFH by the Frame Tree Node | |
46 // ID. It returns the committed frame host or the initial frame host for the | |
47 // frame if no committed host exists. Using a previous host is fine because | |
48 // once a frame has an ad we always consider it to have an ad. | |
49 // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not | |
50 // concerned with which process the frame lives on (we're just measuring | |
51 // bytes and not granting security priveleges). | |
52 content::RenderFrameHost* current_frame_host = | |
53 navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId( | |
54 navigation_handle->GetFrameTreeNodeId()); | |
55 if (current_frame_host) { | |
56 const std::string& frame_name = current_frame_host->GetFrameName(); | |
57 if (base::StartsWith(frame_name, "google_ads_iframe", | |
58 base::CompareCase::SENSITIVE) || | |
59 base::StartsWith(frame_name, "google_ads_frame", | |
60 base::CompareCase::SENSITIVE)) { | |
61 return true; | |
62 } | |
63 } | |
64 | |
65 const GURL& url = navigation_handle->GetURL(); | |
66 return url.host_piece() == "tpc.googlesyndication.com" && | |
67 base::StartsWith(url.path_piece(), "/safeframe", | |
68 base::CompareCase::SENSITIVE); | |
69 } | |
70 | |
71 void RecordParentExistsForSubFrame( | |
72 bool parent_exists, | |
73 const AdsPageLoadMetricsObserver::AdTypes& ad_types) { | |
74 ADS_HISTOGRAM("ParentExistsForSubFrame", UMA_HISTOGRAM_BOOLEAN, | |
75 AdsPageLoadMetricsObserver::AD_TYPE_ALL, parent_exists); | |
76 } | 42 } |
77 | 43 |
78 } // namespace | 44 } // namespace |
79 | 45 |
80 AdsPageLoadMetricsObserver::AdFrameData::AdFrameData( | 46 AdsPageLoadMetricsObserver::AdFrameData::AdFrameData( |
81 FrameTreeNodeId frame_tree_node_id, | 47 FrameTreeNodeId frame_tree_node_id, |
82 AdTypes ad_types) | 48 AdTypes ad_types) |
83 : frame_bytes(0u), | 49 : frame_bytes(0u), |
84 frame_bytes_uncached(0u), | 50 frame_bytes_uncached(0u), |
85 frame_tree_node_id(frame_tree_node_id), | 51 frame_tree_node_id(frame_tree_node_id), |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 void AdsPageLoadMetricsObserver::OnDidFinishSubFrameNavigation( | 96 void AdsPageLoadMetricsObserver::OnDidFinishSubFrameNavigation( |
131 content::NavigationHandle* navigation_handle) { | 97 content::NavigationHandle* navigation_handle) { |
132 // Determine if the frame is part of an existing ad, the root of a new ad, | 98 // Determine if the frame is part of an existing ad, the root of a new ad, |
133 // or a non-ad frame. Once a frame is labled as an ad, it is always | 99 // or a non-ad frame. Once a frame is labled as an ad, it is always |
134 // considered an ad, even if it navigates to a non-ad page. This function | 100 // considered an ad, even if it navigates to a non-ad page. This function |
135 // labels all of a page's frames, even those that fail to commit. | 101 // labels all of a page's frames, even those that fail to commit. |
136 FrameTreeNodeId frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); | 102 FrameTreeNodeId frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); |
137 content::RenderFrameHost* parent_frame_host = | 103 content::RenderFrameHost* parent_frame_host = |
138 navigation_handle->GetParentFrame(); | 104 navigation_handle->GetParentFrame(); |
139 | 105 |
140 AdTypes ad_types = DetectAds(navigation_handle); | 106 AdTypes ad_types = GetDetectedAdTypes(navigation_handle); |
141 | 107 |
142 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id); | 108 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id); |
143 if (id_and_data != ad_frames_data_.end()) { | 109 if (id_and_data != ad_frames_data_.end()) { |
144 // An existing subframe is navigating again. | 110 // An existing subframe is navigating again. |
145 if (id_and_data->second) { | 111 if (id_and_data->second) { |
146 // The subframe was an ad to begin with, keep tracking it as an ad. | 112 // The subframe was an ad to begin with, keep tracking it as an ad. |
147 ProcessOngoingNavigationResource(frame_tree_node_id); | 113 ProcessOngoingNavigationResource(frame_tree_node_id); |
148 | 114 |
149 if (frame_tree_node_id == id_and_data->second->frame_tree_node_id) { | 115 if (frame_tree_node_id == id_and_data->second->frame_tree_node_id) { |
150 // This is the top-most frame in the ad. | 116 // This is the top-most frame in the ad. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 RecordHistograms(); | 172 RecordHistograms(); |
207 } | 173 } |
208 | 174 |
209 void AdsPageLoadMetricsObserver::OnSubframeNavigationEvaluated( | 175 void AdsPageLoadMetricsObserver::OnSubframeNavigationEvaluated( |
210 content::NavigationHandle* navigation_handle, | 176 content::NavigationHandle* navigation_handle, |
211 subresource_filter::LoadPolicy load_policy) { | 177 subresource_filter::LoadPolicy load_policy) { |
212 // We don't track DISALLOW frames because their resources won't be loaded | 178 // We don't track DISALLOW frames because their resources won't be loaded |
213 // and therefore would provide bad histogram data. Note that WOULD_DISALLOW | 179 // and therefore would provide bad histogram data. Note that WOULD_DISALLOW |
214 // is only seen in dry runs. | 180 // is only seen in dry runs. |
215 if (load_policy == subresource_filter::LoadPolicy::WOULD_DISALLOW) { | 181 if (load_policy == subresource_filter::LoadPolicy::WOULD_DISALLOW) { |
216 unfinished_subresource_ad_frames_.insert( | 182 SetDetectedAdTypes(navigation_handle, AD_TYPE_SUBRESOURCE_FILTER); |
jkarlin
2017/07/19 13:32:36
Yay for this change. This is much nicer than unfin
| |
217 navigation_handle->GetFrameTreeNodeId()); | |
218 } | 183 } |
219 } | 184 } |
220 | 185 |
221 void AdsPageLoadMetricsObserver::OnSubresourceFilterGoingAway() { | 186 void AdsPageLoadMetricsObserver::OnSubresourceFilterGoingAway() { |
222 subresource_observer_.RemoveAll(); | 187 subresource_observer_.RemoveAll(); |
223 } | 188 } |
224 | 189 |
225 bool AdsPageLoadMetricsObserver::DetectSubresourceFilterAd( | |
226 FrameTreeNodeId frame_tree_node_id) { | |
227 return unfinished_subresource_ad_frames_.erase(frame_tree_node_id); | |
228 } | |
229 | |
230 AdsPageLoadMetricsObserver::AdTypes AdsPageLoadMetricsObserver::DetectAds( | |
231 content::NavigationHandle* navigation_handle) { | |
232 AdTypes ad_types; | |
233 | |
234 if (DetectGoogleAd(navigation_handle)) | |
235 ad_types.set(AD_TYPE_GOOGLE); | |
236 | |
237 if (DetectSubresourceFilterAd(navigation_handle->GetFrameTreeNodeId())) | |
238 ad_types.set(AD_TYPE_SUBRESOURCE_FILTER); | |
239 | |
240 return ad_types; | |
241 } | |
242 | |
243 void AdsPageLoadMetricsObserver::ProcessLoadedResource( | 190 void AdsPageLoadMetricsObserver::ProcessLoadedResource( |
244 const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info) { | 191 const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info) { |
245 const auto& id_and_data = | 192 const auto& id_and_data = |
246 ad_frames_data_.find(extra_request_info.frame_tree_node_id); | 193 ad_frames_data_.find(extra_request_info.frame_tree_node_id); |
247 if (id_and_data == ad_frames_data_.end()) { | 194 if (id_and_data == ad_frames_data_.end()) { |
248 if (extra_request_info.resource_type == content::RESOURCE_TYPE_MAIN_FRAME || | 195 if (extra_request_info.resource_type == content::RESOURCE_TYPE_MAIN_FRAME || |
249 extra_request_info.resource_type == content::RESOURCE_TYPE_SUB_FRAME) { | 196 extra_request_info.resource_type == content::RESOURCE_TYPE_SUB_FRAME) { |
250 // This resource request is the primary resource load for a frame that | 197 // This resource request is the primary resource load for a frame that |
251 // hasn't yet finished navigating. Hang onto the request info and replay | 198 // hasn't yet finished navigating. Hang onto the request info and replay |
252 // it once the frame finishes navigating. | 199 // it once the frame finishes navigating. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( | 314 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( |
368 FrameTreeNodeId frame_tree_node_id) { | 315 FrameTreeNodeId frame_tree_node_id) { |
369 const auto& frame_id_and_request = | 316 const auto& frame_id_and_request = |
370 ongoing_navigation_resources_.find(frame_tree_node_id); | 317 ongoing_navigation_resources_.find(frame_tree_node_id); |
371 if (frame_id_and_request == ongoing_navigation_resources_.end()) | 318 if (frame_id_and_request == ongoing_navigation_resources_.end()) |
372 return; | 319 return; |
373 | 320 |
374 ProcessLoadedResource(frame_id_and_request->second); | 321 ProcessLoadedResource(frame_id_and_request->second); |
375 ongoing_navigation_resources_.erase(frame_id_and_request); | 322 ongoing_navigation_resources_.erase(frame_id_and_request); |
376 } | 323 } |
OLD | NEW |