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/observers/ads_detection.h" |
14 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" | 15 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" |
15 #include "content/public/browser/navigation_handle.h" | 16 #include "content/public/browser/navigation_handle.h" |
16 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT}; | 23 const base::Feature kAdsFeature{"AdsMetrics", base::FEATURE_ENABLED_BY_DEFAULT}; |
23 | 24 |
24 bool FrameIsAd(content::NavigationHandle* navigation_handle) { | 25 bool IsAdFrame(content::NavigationHandle* navigation_handle) { |
25 // Because sub-resource filtering isn't always enabled, and doesn't work | |
26 // well in monitoring mode (no CSS enforcement), it's difficult to identify | |
27 // ads. Google ads are prevalent and easy to track, so we'll start by | |
28 // tracking those. Note that the frame name can be very large, so be careful | |
29 // to avoid full string searches if possible. | |
30 // TODO(jkarlin): Track other ad networks that are easy to identify. | |
31 | |
32 // In case the navigation aborted, look up the RFH by the Frame Tree Node | 26 // In case the navigation aborted, look up the RFH by the Frame Tree Node |
33 // ID. It returns the committed frame host or the initial frame host for the | 27 // ID. It returns the committed frame host or the initial frame host for the |
34 // frame if no committed host exists. Using a previous host is fine because | 28 // frame if no committed host exists. Using a previous host is fine because |
35 // once a frame has an ad we always consider it to have an ad. | 29 // once a frame has an ad we always consider it to have an ad. |
36 // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not | 30 // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not |
37 // concerned with which process the frame lives on (we're just measuring | 31 // concerned with which process the frame lives on (we're just measuring |
38 // bytes and not granting security priveleges). | 32 // bytes and not granting security priveleges). |
39 content::RenderFrameHost* current_frame_host = | 33 content::RenderFrameHost* current_frame_host = |
40 navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId( | 34 navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId( |
41 navigation_handle->GetFrameTreeNodeId()); | 35 navigation_handle->GetFrameTreeNodeId()); |
42 if (current_frame_host) { | |
43 const std::string& frame_name = current_frame_host->GetFrameName(); | |
44 if (base::StartsWith(frame_name, "google_ads_iframe", | |
45 base::CompareCase::SENSITIVE) || | |
46 base::StartsWith(frame_name, "google_ads_frame", | |
47 base::CompareCase::SENSITIVE)) { | |
48 return true; | |
49 } | |
50 } | |
51 | 36 |
52 const GURL& url = navigation_handle->GetURL(); | 37 return IsAdFrame(current_frame_host ? current_frame_host->GetFrameName() : "", |
53 return url.host_piece() == "tpc.googlesyndication.com" && | 38 navigation_handle->GetURL()); |
54 base::StartsWith(url.path_piece(), "/safeframe", | |
55 base::CompareCase::SENSITIVE); | |
56 } | 39 } |
57 | 40 |
58 void RecordParentExistsForSubFrame(bool parent_exists) { | 41 void RecordParentExistsForSubFrame(bool parent_exists) { |
59 UMA_HISTOGRAM_BOOLEAN("PageLoad.Clients.Ads.Google.ParentExistsForSubFrame", | 42 UMA_HISTOGRAM_BOOLEAN("PageLoad.Clients.Ads.Google.ParentExistsForSubFrame", |
60 parent_exists); | 43 parent_exists); |
61 } | 44 } |
62 | 45 |
63 } // namespace | 46 } // namespace |
64 | 47 |
65 AdsPageLoadMetricsObserver::AdFrameData::AdFrameData( | 48 AdsPageLoadMetricsObserver::AdFrameData::AdFrameData( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 if (id_and_data != ad_frames_data_.end()) { | 90 if (id_and_data != ad_frames_data_.end()) { |
108 // An existing subframe is navigating again. | 91 // An existing subframe is navigating again. |
109 if (id_and_data->second) { | 92 if (id_and_data->second) { |
110 // The subframe was an ad to begin with, keep tracking it as an ad. | 93 // The subframe was an ad to begin with, keep tracking it as an ad. |
111 ProcessOngoingNavigationResource(frame_tree_node_id); | 94 ProcessOngoingNavigationResource(frame_tree_node_id); |
112 | 95 |
113 if (frame_tree_node_id == id_and_data->second->frame_tree_node_id) { | 96 if (frame_tree_node_id == id_and_data->second->frame_tree_node_id) { |
114 // This is the top-most frame in the ad. | 97 // This is the top-most frame in the ad. |
115 UMA_HISTOGRAM_BOOLEAN( | 98 UMA_HISTOGRAM_BOOLEAN( |
116 "PageLoad.Clients.Ads.Google.Navigations.AdFrameRenavigatedToAd", | 99 "PageLoad.Clients.Ads.Google.Navigations.AdFrameRenavigatedToAd", |
117 FrameIsAd(navigation_handle)); | 100 IsAdFrame(navigation_handle)); |
118 } | 101 } |
119 return; | 102 return; |
120 } | 103 } |
121 // This frame was previously not an ad, process it as usual. If it had | 104 // This frame was previously not an ad, process it as usual. If it had |
122 // any child frames that were ads, those will still be recorded. | 105 // any child frames that were ads, those will still be recorded. |
123 UMA_HISTOGRAM_BOOLEAN( | 106 UMA_HISTOGRAM_BOOLEAN( |
124 "PageLoad.Clients.Ads.Google.Navigations.NonAdFrameRenavigatedToAd", | 107 "PageLoad.Clients.Ads.Google.Navigations.NonAdFrameRenavigatedToAd", |
125 FrameIsAd(navigation_handle)); | 108 IsAdFrame(navigation_handle)); |
126 } | 109 } |
127 | 110 |
128 // Determine who the parent frame's ad ancestor is. | 111 // Determine who the parent frame's ad ancestor is. |
129 const auto& parent_id_and_data = | 112 const auto& parent_id_and_data = |
130 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId()); | 113 ad_frames_data_.find(parent_frame_host->GetFrameTreeNodeId()); |
131 if (parent_id_and_data == ad_frames_data_.end()) { | 114 if (parent_id_and_data == ad_frames_data_.end()) { |
132 // We don't know who the parent for this frame is. One possibility is that | 115 // We don't know who the parent for this frame is. One possibility is that |
133 // it's a frame from a previous navigation. | 116 // it's a frame from a previous navigation. |
134 RecordParentExistsForSubFrame(false /* parent_exists */); | 117 RecordParentExistsForSubFrame(false /* parent_exists */); |
135 | 118 |
136 return; | 119 return; |
137 } | 120 } |
138 RecordParentExistsForSubFrame(true /* parent_exists */); | 121 RecordParentExistsForSubFrame(true /* parent_exists */); |
139 | 122 |
140 AdFrameData* ad_data = parent_id_and_data->second; | 123 AdFrameData* ad_data = parent_id_and_data->second; |
141 | 124 |
142 if (!ad_data && FrameIsAd(navigation_handle)) { | 125 if (!ad_data && IsAdFrame(navigation_handle)) { |
143 // This frame is not nested within an ad frame but is itself an ad. | 126 // This frame is not nested within an ad frame but is itself an ad. |
144 ad_frames_data_storage_.emplace_back(frame_tree_node_id); | 127 ad_frames_data_storage_.emplace_back(frame_tree_node_id); |
145 ad_data = &ad_frames_data_storage_.back(); | 128 ad_data = &ad_frames_data_storage_.back(); |
146 } | 129 } |
147 | 130 |
148 ad_frames_data_[frame_tree_node_id] = ad_data; | 131 ad_frames_data_[frame_tree_node_id] = ad_data; |
149 | 132 |
150 ProcessOngoingNavigationResource(frame_tree_node_id); | 133 ProcessOngoingNavigationResource(frame_tree_node_id); |
151 } | 134 } |
152 | 135 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( | 286 void AdsPageLoadMetricsObserver::ProcessOngoingNavigationResource( |
304 FrameTreeNodeId frame_tree_node_id) { | 287 FrameTreeNodeId frame_tree_node_id) { |
305 const auto& frame_id_and_request = | 288 const auto& frame_id_and_request = |
306 ongoing_navigation_resources_.find(frame_tree_node_id); | 289 ongoing_navigation_resources_.find(frame_tree_node_id); |
307 if (frame_id_and_request == ongoing_navigation_resources_.end()) | 290 if (frame_id_and_request == ongoing_navigation_resources_.end()) |
308 return; | 291 return; |
309 | 292 |
310 ProcessLoadedResource(frame_id_and_request->second); | 293 ProcessLoadedResource(frame_id_and_request->second); |
311 ongoing_navigation_resources_.erase(frame_id_and_request); | 294 ongoing_navigation_resources_.erase(frame_id_and_request); |
312 } | 295 } |
OLD | NEW |