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)) {} | |
Charlie Harrison
2017/04/13 19:09:57
Sorry I don't think I was clear. Why do we even ne
jkarlin
2017/04/14 17:50:20
Done.
| |
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(); | |
Charlie Harrison
2017/04/13 19:09:57
optional: top_level_frame (and the associated hist
jkarlin
2017/04/14 17:50:20
Agreed, done.
| |
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 auto it_and_success = delayed_resources_.insert(std::make_pair( | |
Charlie Harrison
2017/04/13 19:09:57
delayed_resources_ is a little generic. What do yo
jkarlin
2017/04/14 17:50:20
Done.
| |
131 extra_request_info.frame_tree_node_id, extra_request_info)); | |
132 DCHECK(it_and_success.second); | |
133 return; | |
134 } | |
135 | |
136 page_bytes_ += extra_request_info.raw_body_bytes; | |
137 if (!extra_request_info.was_cached) | |
138 uncached_page_bytes_ += extra_request_info.raw_body_bytes; | |
139 | |
140 // Determine if the frame (or its ancestor) is an ad, if so attribute the | |
141 // bytes to the highest ad ancestor. | |
142 AdFrameData* ancestor_data = id_and_data->second; | |
143 | |
144 if (ancestor_data) { | |
145 ancestor_data->frame_bytes += extra_request_info.raw_body_bytes; | |
146 if (!extra_request_info.was_cached) { | |
147 ancestor_data->frame_bytes_uncached += extra_request_info.raw_body_bytes; | |
148 } | |
149 } | |
150 } | |
151 | |
152 void AdsPageLoadMetricsObserver::OnComplete( | |
153 const page_load_metrics::PageLoadTiming& timing, | |
154 const page_load_metrics::PageLoadExtraInfo& info) { | |
155 RecordHistograms(); | |
156 } | |
157 | |
158 void AdsPageLoadMetricsObserver::RecordHistograms() { | |
159 if (page_bytes_ == 0) | |
160 return; | |
161 | |
162 size_t total_ad_frame_bytes = 0; | |
163 size_t uncached_ad_frame_bytes = 0; | |
164 | |
165 for (const AdFrameData& ad_frame_data : ad_frames_data_storage_) { | |
166 total_ad_frame_bytes += ad_frame_data.frame_bytes; | |
167 uncached_ad_frame_bytes += ad_frame_data.frame_bytes_uncached; | |
168 | |
169 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AdFrameBytes", | |
170 ad_frame_data.frame_bytes); | |
171 PAGE_BYTES_HISTOGRAM( | |
172 "PageLoad.Clients.Ads.Google.Bytes.AdFrameBytesFromNetwork", | |
173 ad_frame_data.frame_bytes_uncached); | |
174 if (ad_frame_data.frame_bytes > 0) { | |
175 UMA_HISTOGRAM_PERCENTAGE( | |
176 "PageLoad.Clients.Ads.Google.Bytes.PercentAdFrameBytesFromNetwork", | |
177 ad_frame_data.frame_bytes_uncached * 100 / ad_frame_data.frame_bytes); | |
178 } | |
179 } | |
180 | |
181 // Don't post UMA for pages that don't have ads. | |
182 if (ad_frames_data_storage_.empty()) { | |
183 UMA_HISTOGRAM_COUNTS("PageLoad.Clients.Ads.Google.PageHasNoAds", 1); | |
Charlie Harrison
2017/04/13 19:09:57
Use the Boolean specific histogram macros, they're
jkarlin
2017/04/14 17:50:20
Done.
| |
184 return; | |
185 } | |
186 | |
187 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.TopLevelFrameCount", | |
188 top_level_frame_count_); | |
189 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.TopLevelAdFrameCount", | |
190 top_level_ad_frame_count_); | |
191 | |
192 DCHECK_LT(0, top_level_frame_count_); // Because ad frames isn't empty. | |
193 UMA_HISTOGRAM_PERCENTAGE( | |
194 "PageLoad.Clients.Ads.Google.PercentTopLevelFramesAreAdFrames", | |
195 top_level_ad_frame_count_ * 100 / top_level_frame_count_); | |
196 | |
197 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytes", | |
198 total_ad_frame_bytes); | |
199 | |
200 PAGE_BYTES_HISTOGRAM( | |
201 "PageLoad.Clients.Ads.Google.Bytes.PageBytesSansAllAdFrames", | |
202 page_bytes_ - total_ad_frame_bytes); | |
203 | |
204 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PageBytes", | |
205 page_bytes_); | |
206 PAGE_BYTES_HISTOGRAM("PageLoad.Clients.Ads.Google.Bytes.PageBytesFromNetwork", | |
207 uncached_page_bytes_); | |
208 if (page_bytes_) { | |
209 UMA_HISTOGRAM_PERCENTAGE( | |
210 "PageLoad.Clients.Ads.Google.Bytes.PercentPageBytesFromAllAdFrames", | |
211 total_ad_frame_bytes * 100 / page_bytes_); | |
212 } | |
213 | |
214 UMA_HISTOGRAM_COUNTS_1000("PageLoad.Clients.Ads.Google.AdFrameCount", | |
215 ad_frames_data_storage_.size()); | |
216 | |
217 PAGE_BYTES_HISTOGRAM( | |
218 "PageLoad.Clients.Ads.Google.Bytes.AllAdFramesBytesFromNetwork", | |
219 uncached_ad_frame_bytes); | |
220 | |
221 if (total_ad_frame_bytes) { | |
222 UMA_HISTOGRAM_PERCENTAGE( | |
223 "PageLoad.Clients.Ads.Google.Bytes.PercentAllAdFramesBytesFromNetwork", | |
224 uncached_ad_frame_bytes * 100 / total_ad_frame_bytes); | |
225 } | |
226 if (uncached_page_bytes_ > 0) { | |
227 UMA_HISTOGRAM_PERCENTAGE( | |
228 "PageLoad.Clients.Ads.Google.Bytes." | |
229 "PercentPageNetworkBytesFromAllAdFrames", | |
230 uncached_ad_frame_bytes * 100 / uncached_page_bytes_); | |
231 } | |
232 } | |
233 | |
234 void AdsPageLoadMetricsObserver::ProcessDelayedResources( | |
235 FrameTreeNodeId frame_tree_node_id) { | |
236 const auto& frame_id_and_request = | |
237 delayed_resources_.find(frame_tree_node_id); | |
238 if (frame_id_and_request == delayed_resources_.end()) | |
239 return; | |
240 OnLoadedResource(frame_id_and_request->second); | |
241 delayed_resources_.erase(frame_id_and_request); | |
242 } | |
OLD | NEW |