OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/page_load_metrics/observers/ads_page_load_metrics_obser
ver.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/feature_list.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" |
| 12 #include "content/public/browser/navigation_handle.h" |
| 13 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT}; |
| 20 |
| 21 bool FrameIsAd(content::NavigationHandle* navigation_handle) { |
| 22 int frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); |
| 23 content::RenderFrameHost* current_frame_host = |
| 24 navigation_handle->GetWebContents()->FindFrameByFrameTreeNodeId( |
| 25 frame_tree_node_id); |
| 26 const std::string& name = current_frame_host->GetFrameName(); |
| 27 const GURL& url = navigation_handle->GetURL(); |
| 28 |
| 29 // Because sub-resource filtering isn't always enabled, and doesn't work |
| 30 // well in monitoring mode (no CSS enforcement), it's difficult to identify |
| 31 // ads. Google ads are prevalent and easy to track, so we'll start by |
| 32 // tracking those. Note that the frame name can be very large, so be careful |
| 33 // to avoid full string searches if possible. |
| 34 // TODO(jkarlin): Track other ad networks that are easy to identify. |
| 35 return base::StartsWith(name, "google_ads_iframe", |
| 36 base::CompareCase::SENSITIVE) || |
| 37 base::StartsWith(name, "google_ads_frame", |
| 38 base::CompareCase::SENSITIVE) || |
| 39 (url.host_piece() == "tpc.googlesyndication.com" && |
| 40 base::StartsWith(url.path_piece(), "/safeframe", |
| 41 base::CompareCase::SENSITIVE)); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 AdsPageLoadMetricsObserver::AdsPageLoadMetricsObserver() |
| 47 : enabled_(base::FeatureList::IsEnabled(kAdsFeature)) {} |
| 48 |
| 49 AdsPageLoadMetricsObserver::~AdsPageLoadMetricsObserver() = default; |
| 50 |
| 51 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
| 52 AdsPageLoadMetricsObserver::OnCommit( |
| 53 content::NavigationHandle* navigation_handle) { |
| 54 DCHECK(ad_frames_data_.empty()); |
| 55 |
| 56 if (!enabled_) |
| 57 return STOP_OBSERVING; |
| 58 |
| 59 // The main frame is never considered an ad. |
| 60 ad_frames_data_[navigation_handle->GetFrameTreeNodeId()] = nullptr; |
| 61 ProcessDelayedResources(navigation_handle->GetFrameTreeNodeId()); |
| 62 return CONTINUE_OBSERVING; |
| 63 } |
| 64 |
| 65 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
| 66 AdsPageLoadMetricsObserver::OnCommitSubFrame( |
| 67 content::NavigationHandle* navigation_handle) { |
| 68 FrameTreeNodeId frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); |
| 69 content::RenderFrameHost* parent_frame_host = |
| 70 navigation_handle->GetRenderFrameHost()->GetParent(); |
| 71 DCHECK(parent_frame_host); |
| 72 |
| 73 bool top_level_frame = !parent_frame_host->GetParent(); |
| 74 |
| 75 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id); |
| 76 if (id_and_data != ad_frames_data_.end()) { |
| 77 // An existing subframe is navigating again. |
| 78 if (id_and_data->second) { |
| 79 // The subframe was an ad to begin with, keep tracking it as an ad. |
| 80 ProcessDelayedResources(frame_tree_node_id); |
| 81 return CONTINUE_OBSERVING; |
| 82 } |
| 83 // This frame was previously not an ad, process it as usual. If it had |
| 84 // any child frames that were ads, those will still be recorded. |
| 85 } else if (top_level_frame) { |
| 86 top_level_frame_count_ += 1; |
| 87 } |
| 88 |
| 89 // Determine who the parent frame's ad ancestor is. |
| 90 const auto& parent_id_and_data = |
| 91 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId()); |
| 92 DCHECK(parent_id_and_data != ad_frames_data_.end()); |
| 93 AdFrameData* ad_data = parent_id_and_data->second; |
| 94 |
| 95 if (!ad_data && FrameIsAd(navigation_handle)) { |
| 96 // This frame is not nested within an ad frame but is itself an ad. |
| 97 ad_frames_data_storage_.push_back(AdFrameData()); |
| 98 ad_data = &ad_frames_data_storage_.back(); |
| 99 } |
| 100 |
| 101 ad_frames_data_[frame_tree_node_id] = ad_data; |
| 102 |
| 103 if (top_level_frame && ad_data) |
| 104 top_level_ad_frame_count_ += 1; |
| 105 |
| 106 ProcessDelayedResources(frame_tree_node_id); |
| 107 return CONTINUE_OBSERVING; |
| 108 } |
| 109 |
| 110 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
| 111 AdsPageLoadMetricsObserver::FlushMetricsOnAppEnterBackground( |
| 112 const page_load_metrics::PageLoadTiming& timing, |
| 113 const page_load_metrics::PageLoadExtraInfo& extra_info) { |
| 114 // The browser may come back, but there is no guarantee. To be safe, record |
| 115 // what we have now and ignore future changes to this navigation. |
| 116 if (extra_info.did_commit) |
| 117 RecordHistograms(); |
| 118 |
| 119 return STOP_OBSERVING; |
| 120 } |
| 121 |
| 122 void AdsPageLoadMetricsObserver::OnLoadedResource( |
| 123 const page_load_metrics::ExtraRequestInfo& extra_request_info) { |
| 124 const auto& id_and_data = |
| 125 ad_frames_data_.find(extra_request_info.frame_tree_node_id); |
| 126 if (id_and_data == ad_frames_data_.end()) { |
| 127 // This resouce is for a frame that hasn't yet committed. It must be the |
| 128 // main document for the frame. Hold onto it and once it commits we'll run |
| 129 // it in ProcessDelayedResources. |
| 130 // TODO(jkarlin): Plumb the resource type through and DCHECK that the type |
| 131 // is document. |
| 132 delayed_resources_[extra_request_info.frame_tree_node_id] = |
| 133 extra_request_info; |
| 134 return; |
| 135 } |
| 136 |
| 137 page_bytes_ += extra_request_info.raw_body_bytes; |
| 138 if (!extra_request_info.was_cached) |
| 139 uncached_page_bytes_ += extra_request_info.raw_body_bytes; |
| 140 |
| 141 // Determine if the frame (or its ancestor) is an ad, if so attribute the |
| 142 // bytes to the highest ad ancestor. |
| 143 AdFrameData* ancestor_data = id_and_data->second; |
| 144 |
| 145 if (ancestor_data) { |
| 146 ancestor_data->frame_bytes += extra_request_info.raw_body_bytes; |
| 147 if (!extra_request_info.was_cached) { |
| 148 ancestor_data->frame_bytes_uncached += extra_request_info.raw_body_bytes; |
| 149 } |
| 150 } |
| 151 } |
| 152 |
| 153 void AdsPageLoadMetricsObserver::OnComplete( |
| 154 const page_load_metrics::PageLoadTiming& timing, |
| 155 const page_load_metrics::PageLoadExtraInfo& info) { |
| 156 RecordHistograms(); |
| 157 } |
| 158 |
| 159 void AdsPageLoadMetricsObserver::RecordHistograms() { |
| 160 if (page_bytes_ == 0) |
| 161 return; |
| 162 |
| 163 size_t total_ad_frame_bytes = 0; |
| 164 size_t uncached_ad_frame_bytes = 0; |
| 165 |
| 166 for (const AdFrameData& ad_frame_data : ad_frames_data_storage_) { |
| 167 total_ad_frame_bytes += ad_frame_data.frame_bytes; |
| 168 uncached_ad_frame_bytes += ad_frame_data.frame_bytes_uncached; |
| 169 |
| 170 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", |
| 171 ad_frame_data.frame_bytes); |
| 172 PAGE_BYTES_HISTOGRAM( |
| 173 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", |
| 174 ad_frame_data.frame_bytes_uncached); |
| 175 if (ad_frame_data.frame_bytes > 0) { |
| 176 UMA_HISTOGRAM_PERCENTAGE( |
| 177 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", |
| 178 ad_frame_data.frame_bytes_uncached * 100 / ad_frame_data.frame_bytes); |
| 179 } |
| 180 } |
| 181 |
| 182 // Don't post UMA for pages that don't have ads. |
| 183 if (ad_frames_data_storage_.empty()) { |
| 184 UMA_HISTOGRAM_COUNTS("PageLoad.Clients.Ads.Google.PageHasNoAds", 1); |
| 185 return; |
| 186 } |
| 187 |
| 188 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.TopLevelFrameCount", |
| 189 top_level_frame_count_); |
| 190 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", |
| 191 top_level_ad_frame_count_); |
| 192 |
| 193 DCHECK_LT(0, top_level_frame_count_); // Because ad frames isn't empty. |
| 194 UMA_HISTOGRAM_PERCENTAGE( |
| 195 "PageLoad.Clients.Ads.Google.PercentTopLevelFramesAreAdFrames", |
| 196 top_level_ad_frame_count_ * 100 / top_level_frame_count_); |
| 197 |
| 198 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", |
| 199 total_ad_frame_bytes); |
| 200 |
| 201 PAGE_BYTES_HISTOGRAM( |
| 202 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", |
| 203 page_bytes_ - total_ad_frame_bytes); |
| 204 |
| 205 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PageBytes", |
| 206 page_bytes_); |
| 207 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", |
| 208 uncached_page_bytes_); |
| 209 if (page_bytes_) { |
| 210 UMA_HISTOGRAM_PERCENTAGE( |
| 211 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", |
| 212 total_ad_frame_bytes * 100 / page_bytes_); |
| 213 } |
| 214 |
| 215 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.AdFrameCount", |
| 216 ad_frames_data_storage_.size()); |
| 217 |
| 218 PAGE_BYTES_HISTOGRAM( |
| 219 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", |
| 220 uncached_ad_frame_bytes); |
| 221 |
| 222 if (total_ad_frame_bytes) { |
| 223 UMA_HISTOGRAM_PERCENTAGE( |
| 224 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", |
| 225 uncached_ad_frame_bytes * 100 / total_ad_frame_bytes); |
| 226 } |
| 227 if (uncached_page_bytes_ > 0) { |
| 228 UMA_HISTOGRAM_PERCENTAGE( |
| 229 "PageLoad.Clients.Ads.Google.Bytes." |
| 230 "PercentPageNetworkBytesFromAllAdFrames", |
| 231 uncached_ad_frame_bytes * 100 / uncached_page_bytes_); |
| 232 } |
| 233 } |
| 234 |
| 235 void AdsPageLoadMetricsObserver::ProcessDelayedResources( |
| 236 FrameTreeNodeId frame_tree_node_id) { |
| 237 const auto& frame_id_and_request = |
| 238 delayed_resources_.find(frame_tree_node_id); |
| 239 if (frame_id_and_request == delayed_resources_.end()) |
| 240 return; |
| 241 OnLoadedResource(frame_id_and_request->second); |
| 242 delayed_resources_.erase(frame_id_and_request); |
| 243 } |
OLD | NEW |