Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/page_load_metrics/observers/ads_page_load_metrics_observer.cc

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

Powered by Google App Engine
This is Rietveld 408576698