Chromium Code Reviews| 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/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" | |
| 14 #include "content/public/browser/navigation_handle.h" | |
| 15 #include "content/public/browser/render_frame_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT}; | |
| 22 | |
| 23 bool FrameIsAd(content::NavigationHandle* navigation_handle) { | |
| 24 content::RenderFrameHost* current_frame_host = | |
| 25 navigation_handle->GetRenderFrameHost(); | |
| 26 DCHECK(current_frame_host); | |
| 27 const std::string& name = current_frame_host->GetFrameName(); | |
| 28 const GURL& url = navigation_handle->GetURL(); | |
| 29 | |
| 30 // Because sub-resource filtering isn't always enabled, and doesn't work | |
| 31 // well in monitoring mode (no CSS enforcement), it's difficult to identify | |
| 32 // ads. Google ads are prevalent and easy to track, so we'll start by | |
| 33 // tracking those. Note that the frame name can be very large, so be careful | |
| 34 // to avoid full string searches if possible. | |
| 35 // TODO(jkarlin): Track other ad networks that are easy to identify. | |
| 36 return base::StartsWith(name, "google_ads_iframe", | |
| 37 base::CompareCase::SENSITIVE) || | |
| 38 base::StartsWith(name, "google_ads_frame", | |
| 39 base::CompareCase::SENSITIVE) || | |
| 40 (url.host_piece() == "tpc.googlesyndication.com" && | |
| 41 base::StartsWith(url.path_piece(), "/safeframe", | |
| 42 base::CompareCase::SENSITIVE)); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 // static | |
| 48 std::unique_ptr<AdsPageLoadMetricsObserver> | |
| 49 AdsPageLoadMetricsObserver::CreateIfNeeded() { | |
| 50 if (!base::FeatureList::IsEnabled(kAdsFeature)) | |
| 51 return nullptr; | |
| 52 return base::MakeUnique<AdsPageLoadMetricsObserver>(); | |
| 53 } | |
| 54 | |
| 55 AdsPageLoadMetricsObserver::AdsPageLoadMetricsObserver() = default; | |
| 56 AdsPageLoadMetricsObserver::~AdsPageLoadMetricsObserver() = default; | |
| 57 | |
| 58 page_load_metrics::PageLoadMetricsObserver::ObservePolicy | |
| 59 AdsPageLoadMetricsObserver::OnCommit( | |
| 60 content::NavigationHandle* navigation_handle) { | |
| 61 DCHECK(ad_frames_data_.empty()); | |
| 62 | |
| 63 // The main frame is never considered an ad. | |
| 64 ad_frames_data_[navigation_handle->GetFrameTreeNodeId()] = nullptr; | |
| 65 ProcessOngoingNavigationResource(navigation_handle->GetFrameTreeNodeId()); | |
| 66 return CONTINUE_OBSERVING; | |
| 67 } | |
| 68 | |
| 69 page_load_metrics::PageLoadMetricsObserver::ObservePolicy | |
| 70 AdsPageLoadMetricsObserver::OnDidFinishSubFrameNavigation( | |
| 71 content::NavigationHandle* navigation_handle) { | |
| 72 FrameTreeNodeId frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); | |
| 73 | |
| 74 if (!navigation_handle->HasCommitted() || | |
| 75 navigation_handle->IsSameDocument() || navigation_handle->IsErrorPage()) { | |
| 76 // We're not interested in tracking this navigation. In case we've seen a | |
| 77 // resource for the navigation before this message, clear it from | |
| 78 // ongoing_navigation_resources_. | |
| 79 ongoing_navigation_resources_.erase(frame_tree_node_id); | |
| 80 return CONTINUE_OBSERVING; | |
| 81 } | |
| 82 | |
| 83 content::RenderFrameHost* parent_frame_host = | |
| 84 navigation_handle->GetRenderFrameHost()->GetParent(); | |
| 85 DCHECK(parent_frame_host); | |
| 86 | |
| 87 bool top_level_subframe = !parent_frame_host->GetParent(); | |
| 88 | |
| 89 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id); | |
| 90 if (id_and_data != ad_frames_data_.end()) { | |
| 91 // An existing subframe is navigating again. | |
| 92 if (id_and_data->second) { | |
| 93 // The subframe was an ad to begin with, keep tracking it as an ad. | |
| 94 ProcessOngoingNavigationResource(frame_tree_node_id); | |
| 95 return CONTINUE_OBSERVING; | |
| 96 } | |
| 97 // This frame was previously not an ad, process it as usual. If it had | |
| 98 // any child frames that were ads, those will still be recorded. | |
| 99 } else if (top_level_subframe) { | |
| 100 top_level_subframe_count_ += 1; | |
| 101 } | |
| 102 | |
| 103 // Determine who the parent frame's ad ancestor is. | |
| 104 const auto& parent_id_and_data = | |
| 105 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId()); | |
| 106 DCHECK(parent_id_and_data != ad_frames_data_.end()); | |
| 107 AdFrameData* ad_data = parent_id_and_data->second; | |
| 108 | |
| 109 if (!ad_data && FrameIsAd(navigation_handle)) { | |
| 110 // This frame is not nested within an ad frame but is itself an ad. | |
| 111 ad_frames_data_storage_.emplace_back(); | |
|
Charlie Harrison
2017/04/24 19:18:26
It looks like this call can initialize the storage
jkarlin
2017/04/25 15:56:50
Eek!. I thought it was initialized. Thanks!
| |
| 112 ad_data = &ad_frames_data_storage_.back(); | |
| 113 } | |
| 114 | |
| 115 ad_frames_data_[frame_tree_node_id] = ad_data; | |
| 116 | |
| 117 if (top_level_subframe && ad_data) | |
| 118 top_level_ad_frame_count_ += 1; | |
| 119 | |
| 120 ProcessOngoingNavigationResource(frame_tree_node_id); | |
| 121 return CONTINUE_OBSERVING; | |
| 122 } | |
| 123 | |
| 124 page_load_metrics::PageLoadMetricsObserver::ObservePolicy | |
| 125 AdsPageLoadMetricsObserver::FlushMetricsOnAppEnterBackground( | |
| 126 const page_load_metrics::PageLoadTiming& timing, | |
| 127 const page_load_metrics::PageLoadExtraInfo& extra_info) { | |
| 128 // The browser may come back, but there is no guarantee. To be safe, record | |
| 129 // what we have now and ignore future changes to this navigation. | |
| 130 if (extra_info.did_commit) | |
| 131 RecordHistograms(); | |
| 132 | |
| 133 return STOP_OBSERVING; | |
| 134 } | |
| 135 | |
| 136 void AdsPageLoadMetricsObserver::OnLoadedResource( | |
| 137 const page_load_metrics::ExtraRequestInfo& extra_request_info) { | |
| 138 ProcessLoadedResource(extra_request_info); | |
| 139 } | |
| 140 | |
| 141 void AdsPageLoadMetricsObserver::OnComplete( | |
| 142 const page_load_metrics::PageLoadTiming& timing, | |
| 143 const page_load_metrics::PageLoadExtraInfo& info) { | |
| 144 RecordHistograms(); | |
| 145 } | |
| 146 | |
| 147 void AdsPageLoadMetricsObserver::ProcessLoadedResource( | |
| 148 const page_load_metrics::ExtraRequestInfo& extra_request_info) { | |
| 149 if (!extra_request_info.url.SchemeIsHTTPOrHTTPS()) { | |
| 150 // Data uris should be accounted for in the generating resource, not | |
| 151 // here. Blobs for PLZNavigate shouldn't be counted as the http resource | |
|
Charlie Harrison
2017/04/24 19:18:26
nit: s/PLZ/Plz
jkarlin
2017/04/25 15:56:49
Done.
| |
| 152 // was already counted. Blobs for other things like CacheStorage or | |
| 153 // IndexedDB are also ignored for now, as they're not normal HTTP loads. | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 const auto& id_and_data = | |
| 158 ad_frames_data_.find(extra_request_info.frame_tree_node_id); | |
| 159 if (id_and_data == ad_frames_data_.end()) { | |
| 160 // This resouce is for a frame that hasn't yet committed. It must be the | |
| 161 // main document for the frame. Hold onto it and once it commits we'll run | |
| 162 // it in ProcessOngoingNavigationResource. | |
| 163 // TODO(jkarlin): Plumb the resource type through and DCHECK that the type | |
| 164 // is document. | |
| 165 auto it_and_success = ongoing_navigation_resources_.emplace( | |
|
Charlie Harrison
2017/04/24 19:18:25
I thought map emplace wasn't allowed in Chromium y
jkarlin
2017/04/25 15:56:50
I actually had assumed it was allowed (as it's all
| |
| 166 std::piecewise_construct, | |
|
Charlie Harrison
2017/04/24 19:18:26
#include <utility>
jkarlin
2017/04/25 15:56:50
Done.
| |
| 167 std::forward_as_tuple(extra_request_info.frame_tree_node_id), | |
| 168 std::forward_as_tuple( | |
|
Charlie Harrison
2017/04/24 19:18:26
This seems a bit weird. Are we constructing a new
jkarlin
2017/04/25 15:56:50
We're creating a new one yes, but that's always re
| |
| 169 extra_request_info.url, extra_request_info.frame_tree_node_id, | |
| 170 extra_request_info.was_cached, extra_request_info.raw_body_bytes, | |
| 171 extra_request_info.original_network_content_length, nullptr)); | |
| 172 DCHECK(it_and_success.second); | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 page_bytes_ += extra_request_info.raw_body_bytes; | |
| 177 if (!extra_request_info.was_cached) | |
| 178 uncached_page_bytes_ += extra_request_info.raw_body_bytes; | |
| 179 | |
| 180 // Determine if the frame (or its ancestor) is an ad, if so attribute the | |
| 181 // bytes to the highest ad ancestor. | |
| 182 AdFrameData* ancestor_data = id_and_data->second; | |
| 183 | |
| 184 if (ancestor_data) { | |
| 185 ancestor_data->frame_bytes += extra_request_info.raw_body_bytes; | |
| 186 if (!extra_request_info.was_cached) { | |
| 187 ancestor_data->frame_bytes_uncached += extra_request_info.raw_body_bytes; | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void AdsPageLoadMetricsObserver::RecordHistograms() { | |
| 193 if (page_bytes_ == 0) | |
| 194 return; | |
| 195 | |
| 196 size_t total_ad_frame_bytes = 0; | |
| 197 size_t uncached_ad_frame_bytes = 0; | |
| 198 | |
| 199 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.AdFrameCount", | |
| 200 ad_frames_data_storage_.size()); | |
| 201 | |
| 202 // Don't post UMA for pages that don't have ads. | |
| 203 if (ad_frames_data_storage_.empty()) | |
| 204 return; | |
| 205 | |
| 206 for (const AdFrameData& ad_frame_data : ad_frames_data_storage_) { | |
| 207 total_ad_frame_bytes += ad_frame_data.frame_bytes; | |
| 208 uncached_ad_frame_bytes += ad_frame_data.frame_bytes_uncached; | |
| 209 | |
| 210 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PerAdFrame.Total", | |
| 211 ad_frame_data.frame_bytes); | |
| 212 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PerAdFrame.Network", | |
| 213 ad_frame_data.frame_bytes_uncached); | |
| 214 if (ad_frame_data.frame_bytes > 0) { | |
| 215 UMA_HISTOGRAM_PERCENTAGE( | |
| 216 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrame.Network", | |
| 217 ad_frame_data.frame_bytes_uncached * 100 / ad_frame_data.frame_bytes); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 UMA_HISTOGRAM_COUNTS_1000( | |
| 222 "PageLoad.Clients.Ads.Google.SubFrameCount.TopLevel", | |
| 223 top_level_subframe_count_); | |
| 224 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.AdFrameCount.TopLevel", | |
| 225 top_level_ad_frame_count_); | |
| 226 | |
| 227 DCHECK_LT(0, top_level_subframe_count_); // Because ad frames isn't empty. | |
| 228 UMA_HISTOGRAM_PERCENTAGE( | |
| 229 "PageLoad.Clients.Ads.Google.PercentSubFramesAreAdFrames.TopLevel", | |
| 230 top_level_ad_frame_count_ * 100 / top_level_subframe_count_); | |
| 231 | |
| 232 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.NonAdFrames.Total", | |
| 233 page_bytes_ - total_ad_frame_bytes); | |
| 234 | |
| 235 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.Total", page_bytes_); | |
| 236 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.Network", | |
| 237 uncached_page_bytes_); | |
| 238 if (page_bytes_) { | |
| 239 UMA_HISTOGRAM_PERCENTAGE( | |
| 240 "PageLoad.Clients.Ads.Google.Bytes.PercentPage.AllAdFrames.Total", | |
| 241 total_ad_frame_bytes * 100 / page_bytes_); | |
| 242 } | |
| 243 if (uncached_page_bytes_ > 0) { | |
| 244 UMA_HISTOGRAM_PERCENTAGE( | |
| 245 "PageLoad.Clients.Ads.Google.Bytes." | |
| 246 "PercentPage.AllAdFrames.Network", | |
| 247 uncached_ad_frame_bytes * 100 / uncached_page_bytes_); | |
| 248 } | |
| 249 | |
| 250 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AllAdFrames.Total", | |
| 251 total_ad_frame_bytes); | |
| 252 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AllAdFrames.Network", | |
| 253 uncached_ad_frame_bytes); | |
| 254 | |
| 255 if (total_ad_frame_bytes) { | |
| 256 UMA_HISTOGRAM_PERCENTAGE( | |
| 257 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFrames.Network", | |
| 258 uncached_ad_frame_bytes * 100 / total_ad_frame_bytes); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( | |
| 263 FrameTreeNodeId frame_tree_node_id) { | |
| 264 const auto& frame_id_and_request = | |
| 265 ongoing_navigation_resources_.find(frame_tree_node_id); | |
| 266 if (frame_id_and_request == ongoing_navigation_resources_.end()) | |
| 267 return; | |
| 268 | |
| 269 const page_load_metrics::ExtraRequestInfo& data = | |
| 270 frame_id_and_request->second; | |
| 271 ProcessLoadedResource(data); | |
| 272 ongoing_navigation_resources_.erase(frame_id_and_request); | |
| 273 } | |
| OLD | NEW |