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/page_load_metrics_util.h" | 14 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" |
15 #include "content/public/browser/navigation_handle.h" | 15 #include "content/public/browser/navigation_handle.h" |
16 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 const base::Feature kAdsFeature{"AdsMetrics", | 22 const base::Feature kAdsFeature{"AdsMetrics", |
23 base::FEATURE_DISABLED_BY_DEFAULT}; | 23 base::FEATURE_DISABLED_BY_DEFAULT}; |
24 | 24 |
25 bool FrameIsAd(content::NavigationHandle* navigation_handle) { | 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 | 26 // 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 | 27 // 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 | 28 // 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 | 29 // tracking those. Note that the frame name can be very large, so be careful |
36 // to avoid full string searches if possible. | 30 // to avoid full string searches if possible. |
37 // TODO(jkarlin): Track other ad networks that are easy to identify. | 31 // TODO(jkarlin): Track other ad networks that are easy to identify. |
38 return base::StartsWith(name, "google_ads_iframe", | 32 |
39 base::CompareCase::SENSITIVE) || | 33 // In case the navigation aborted, look up the RFH by the Frame Tree Node |
40 base::StartsWith(name, "google_ads_frame", | 34 // ID. It returns the committed frame host or the initial frame host for the |
41 base::CompareCase::SENSITIVE) || | 35 // frame if no committed host exists. Using a previous host is fine because |
42 (url.host_piece() == "tpc.googlesyndication.com" && | 36 // once a frame has an ad we always consider it to have an ad. |
43 base::StartsWith(url.path_piece(), "/safeframe", | 37 // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not |
44 base::CompareCase::SENSITIVE)); | 38 // concerned with which process the frame lives on (we're just measuring |
| 39 // bytes and not granting security priveleges). |
| 40 content::RenderFrameHost* current_frame_host = |
| 41 navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId( |
| 42 navigation_handle->GetFrameTreeNodeId()); |
| 43 if (current_frame_host) { |
| 44 const std::string& frame_name = current_frame_host->GetFrameName(); |
| 45 if (base::StartsWith(frame_name, "google_ads_iframe", |
| 46 base::CompareCase::SENSITIVE) || |
| 47 base::StartsWith(frame_name, "google_ads_frame", |
| 48 base::CompareCase::SENSITIVE)) { |
| 49 return true; |
| 50 } |
| 51 } |
| 52 |
| 53 const GURL& url = navigation_handle->GetURL(); |
| 54 return url.host_piece() == "tpc.googlesyndication.com" && |
| 55 base::StartsWith(url.path_piece(), "/safeframe", |
| 56 base::CompareCase::SENSITIVE); |
45 } | 57 } |
46 | 58 |
47 } // namespace | 59 } // namespace |
48 | 60 |
49 AdsPageLoadMetricsObserver::AdFrameData::AdFrameData( | 61 AdsPageLoadMetricsObserver::AdFrameData::AdFrameData( |
50 FrameTreeNodeId frame_tree_node_id) | 62 FrameTreeNodeId frame_tree_node_id) |
51 : frame_bytes(0u), | 63 : frame_bytes(0u), |
52 frame_bytes_uncached(0u), | 64 frame_bytes_uncached(0u), |
53 frame_tree_node_id(frame_tree_node_id) {} | 65 frame_tree_node_id(frame_tree_node_id) {} |
54 | 66 |
(...skipping 15 matching lines...) Expand all Loading... |
70 | 82 |
71 // The main frame is never considered an ad. | 83 // The main frame is never considered an ad. |
72 ad_frames_data_[navigation_handle->GetFrameTreeNodeId()] = nullptr; | 84 ad_frames_data_[navigation_handle->GetFrameTreeNodeId()] = nullptr; |
73 ProcessOngoingNavigationResource(navigation_handle->GetFrameTreeNodeId()); | 85 ProcessOngoingNavigationResource(navigation_handle->GetFrameTreeNodeId()); |
74 return CONTINUE_OBSERVING; | 86 return CONTINUE_OBSERVING; |
75 } | 87 } |
76 | 88 |
77 page_load_metrics::PageLoadMetricsObserver::ObservePolicy | 89 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
78 AdsPageLoadMetricsObserver::OnDidFinishSubFrameNavigation( | 90 AdsPageLoadMetricsObserver::OnDidFinishSubFrameNavigation( |
79 content::NavigationHandle* navigation_handle) { | 91 content::NavigationHandle* navigation_handle) { |
| 92 // Determine if the frame is part of an existing ad, the root of a new ad, |
| 93 // or a non-ad frame. Once a frame is labled as an ad, it is always |
| 94 // considered an ad, even if it navigates to a non-ad page. This function |
| 95 // labels all of a page's frames, even those that fail to commit. |
80 FrameTreeNodeId frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); | 96 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 = | 97 content::RenderFrameHost* parent_frame_host = |
92 navigation_handle->GetRenderFrameHost()->GetParent(); | 98 navigation_handle->GetParentFrame(); |
93 DCHECK(parent_frame_host); | |
94 | |
95 bool top_level_subframe = !parent_frame_host->GetParent(); | |
96 | 99 |
97 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id); | 100 const auto& id_and_data = ad_frames_data_.find(frame_tree_node_id); |
98 if (id_and_data != ad_frames_data_.end()) { | 101 if (id_and_data != ad_frames_data_.end()) { |
99 // An existing subframe is navigating again. | 102 // An existing subframe is navigating again. |
100 if (id_and_data->second) { | 103 if (id_and_data->second) { |
101 // The subframe was an ad to begin with, keep tracking it as an ad. | 104 // The subframe was an ad to begin with, keep tracking it as an ad. |
102 ProcessOngoingNavigationResource(frame_tree_node_id); | 105 ProcessOngoingNavigationResource(frame_tree_node_id); |
103 | 106 |
104 if (frame_tree_node_id == id_and_data->second->frame_tree_node_id) { | 107 if (frame_tree_node_id == id_and_data->second->frame_tree_node_id) { |
105 // This is the top-most frame in the ad. | 108 // This is the top-most frame in the ad. |
106 UMA_HISTOGRAM_BOOLEAN( | 109 UMA_HISTOGRAM_BOOLEAN( |
107 "PageLoad.Clients.Ads.Google.Navigations.AdFrameRenavigatedToAd", | 110 "PageLoad.Clients.Ads.Google.Navigations.AdFrameRenavigatedToAd", |
108 FrameIsAd(navigation_handle)); | 111 FrameIsAd(navigation_handle)); |
109 } | 112 } |
110 | |
111 return CONTINUE_OBSERVING; | 113 return CONTINUE_OBSERVING; |
112 } | 114 } |
113 // This frame was previously not an ad, process it as usual. If it had | 115 // 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. | 116 // any child frames that were ads, those will still be recorded. |
115 UMA_HISTOGRAM_BOOLEAN( | 117 UMA_HISTOGRAM_BOOLEAN( |
116 "PageLoad.Clients.Ads.Google.Navigations.NonAdFrameRenavigatedToAd", | 118 "PageLoad.Clients.Ads.Google.Navigations.NonAdFrameRenavigatedToAd", |
117 FrameIsAd(navigation_handle)); | 119 FrameIsAd(navigation_handle)); |
118 } else if (top_level_subframe) { | 120 } else if (navigation_handle->IsParentMainFrame()) { |
119 top_level_subframe_count_ += 1; | 121 top_level_subframe_count_ += 1; |
120 } | 122 } |
121 | 123 |
122 // Determine who the parent frame's ad ancestor is. | 124 // Determine who the parent frame's ad ancestor is. |
123 const auto& parent_id_and_data = | 125 const auto& parent_id_and_data = |
124 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId()); | 126 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId()); |
125 DCHECK(parent_id_and_data != ad_frames_data_.end()); | 127 DCHECK(parent_id_and_data != ad_frames_data_.end()); |
126 AdFrameData* ad_data = parent_id_and_data->second; | 128 AdFrameData* ad_data = parent_id_and_data->second; |
127 | 129 |
128 if (!ad_data && FrameIsAd(navigation_handle)) { | 130 if (!ad_data && FrameIsAd(navigation_handle)) { |
129 // This frame is not nested within an ad frame but is itself an ad. | 131 // 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); | 132 ad_frames_data_storage_.emplace_back(frame_tree_node_id); |
131 ad_data = &ad_frames_data_storage_.back(); | 133 ad_data = &ad_frames_data_storage_.back(); |
132 } | 134 } |
133 | 135 |
134 ad_frames_data_[frame_tree_node_id] = ad_data; | 136 ad_frames_data_[frame_tree_node_id] = ad_data; |
135 | 137 |
136 if (top_level_subframe && ad_data) | 138 if (navigation_handle->IsParentMainFrame() && ad_data) |
137 top_level_ad_frame_count_ += 1; | 139 top_level_ad_frame_count_ += 1; |
138 | 140 |
139 ProcessOngoingNavigationResource(frame_tree_node_id); | 141 ProcessOngoingNavigationResource(frame_tree_node_id); |
140 return CONTINUE_OBSERVING; | 142 return CONTINUE_OBSERVING; |
141 } | 143 } |
142 | 144 |
143 page_load_metrics::PageLoadMetricsObserver::ObservePolicy | 145 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
144 AdsPageLoadMetricsObserver::FlushMetricsOnAppEnterBackground( | 146 AdsPageLoadMetricsObserver::FlushMetricsOnAppEnterBackground( |
145 const page_load_metrics::PageLoadTiming& timing, | 147 const page_load_metrics::PageLoadTiming& timing, |
146 const page_load_metrics::PageLoadExtraInfo& extra_info) { | 148 const page_load_metrics::PageLoadExtraInfo& extra_info) { |
(...skipping 22 matching lines...) Expand all Loading... |
169 // Data uris should be accounted for in the generating resource, not | 171 // Data uris should be accounted for in the generating resource, not |
170 // here. Blobs for PlzNavigate shouldn't be counted as the http resource | 172 // here. Blobs for PlzNavigate shouldn't be counted as the http resource |
171 // was already counted. Blobs for other things like CacheStorage or | 173 // was already counted. Blobs for other things like CacheStorage or |
172 // IndexedDB are also ignored for now, as they're not normal HTTP loads. | 174 // IndexedDB are also ignored for now, as they're not normal HTTP loads. |
173 return; | 175 return; |
174 } | 176 } |
175 | 177 |
176 const auto& id_and_data = | 178 const auto& id_and_data = |
177 ad_frames_data_.find(extra_request_info.frame_tree_node_id); | 179 ad_frames_data_.find(extra_request_info.frame_tree_node_id); |
178 if (id_and_data == ad_frames_data_.end()) { | 180 if (id_and_data == ad_frames_data_.end()) { |
179 // This resouce is for a frame that hasn't yet committed. It must be the | 181 // This resource is for a frame that hasn't ever finished a navigation. We |
180 // main document for the frame. Hold onto it and once it commits we'll run | 182 // expect it to be the frame's main resource but don't have enough |
181 // it in ProcessOngoingNavigationResource. | 183 // confidence in that to dcheck it. For example, there might be races |
182 // TODO(jkarlin): Plumb the resource type through and DCHECK that the type | 184 // between doc.written resources and navigation failure. |
183 // is document. | 185 // TODO(jkarlin): Add UMA to measure how often we see multiple resources |
184 auto it_and_success = ongoing_navigation_resources_.emplace( | 186 // for a frame before navigation finishes. |
| 187 ongoing_navigation_resources_.emplace( |
185 std::piecewise_construct, | 188 std::piecewise_construct, |
186 std::forward_as_tuple(extra_request_info.frame_tree_node_id), | 189 std::forward_as_tuple(extra_request_info.frame_tree_node_id), |
187 std::forward_as_tuple( | 190 std::forward_as_tuple( |
188 extra_request_info.url, extra_request_info.frame_tree_node_id, | 191 extra_request_info.url, extra_request_info.frame_tree_node_id, |
189 extra_request_info.was_cached, extra_request_info.raw_body_bytes, | 192 extra_request_info.was_cached, extra_request_info.raw_body_bytes, |
190 extra_request_info.original_network_content_length, nullptr, | 193 extra_request_info.original_network_content_length, nullptr, |
191 extra_request_info.resource_type)); | 194 extra_request_info.resource_type)); |
192 DCHECK(it_and_success.second); | |
193 return; | 195 return; |
194 } | 196 } |
195 | 197 |
196 page_bytes_ += extra_request_info.raw_body_bytes; | 198 page_bytes_ += extra_request_info.raw_body_bytes; |
197 if (!extra_request_info.was_cached) | 199 if (!extra_request_info.was_cached) |
198 uncached_page_bytes_ += extra_request_info.raw_body_bytes; | 200 uncached_page_bytes_ += extra_request_info.raw_body_bytes; |
199 | 201 |
200 // Determine if the frame (or its ancestor) is an ad, if so attribute the | 202 // Determine if the frame (or its ancestor) is an ad, if so attribute the |
201 // bytes to the highest ad ancestor. | 203 // bytes to the highest ad ancestor. |
202 AdFrameData* ancestor_data = id_and_data->second; | 204 AdFrameData* ancestor_data = id_and_data->second; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( | 291 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( |
290 FrameTreeNodeId frame_tree_node_id) { | 292 FrameTreeNodeId frame_tree_node_id) { |
291 const auto& frame_id_and_request = | 293 const auto& frame_id_and_request = |
292 ongoing_navigation_resources_.find(frame_tree_node_id); | 294 ongoing_navigation_resources_.find(frame_tree_node_id); |
293 if (frame_id_and_request == ongoing_navigation_resources_.end()) | 295 if (frame_id_and_request == ongoing_navigation_resources_.end()) |
294 return; | 296 return; |
295 | 297 |
296 ProcessLoadedResource(frame_id_and_request->second); | 298 ProcessLoadedResource(frame_id_and_request->second); |
297 ongoing_navigation_resources_.erase(frame_id_and_request); | 299 ongoing_navigation_resources_.erase(frame_id_and_request); |
298 } | 300 } |
OLD | NEW |