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 #ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSERVE
R_H_ | 5 #ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSERVE
R_H_ |
6 #define CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSERVE
R_H_ | 6 #define CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSERVE
R_H_ |
7 | 7 |
8 #include <bitset> | |
9 #include <list> | 8 #include <list> |
10 #include <map> | 9 #include <map> |
11 #include <memory> | 10 #include <memory> |
12 | 11 |
13 #include "base/macros.h" | 12 #include "base/macros.h" |
14 #include "base/scoped_observer.h" | 13 #include "base/scoped_observer.h" |
| 14 #include "chrome/browser/page_load_metrics/ads_detection.h" |
15 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h" | 15 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h" |
16 #include "components/subresource_filter/content/browser/subresource_filter_obser
ver.h" | 16 #include "components/subresource_filter/content/browser/subresource_filter_obser
ver.h" |
17 #include "components/subresource_filter/content/browser/subresource_filter_obser
ver_manager.h" | 17 #include "components/subresource_filter/content/browser/subresource_filter_obser
ver_manager.h" |
18 #include "components/subresource_filter/core/common/load_policy.h" | 18 #include "components/subresource_filter/core/common/load_policy.h" |
19 #include "components/ukm/ukm_source.h" | 19 #include "components/ukm/ukm_source.h" |
20 #include "net/http/http_response_info.h" | 20 #include "net/http/http_response_info.h" |
21 | 21 |
22 // This observer labels each sub-frame as an ad or not, and keeps track of | 22 // This observer labels each sub-frame as an ad or not, and keeps track of |
23 // relevant per-frame and whole-page byte statistics. | 23 // relevant per-frame and whole-page byte statistics. |
24 class AdsPageLoadMetricsObserver | 24 class AdsPageLoadMetricsObserver |
25 : public page_load_metrics::PageLoadMetricsObserver, | 25 : public page_load_metrics::PageLoadMetricsObserver, |
26 public subresource_filter::SubresourceFilterObserver { | 26 public subresource_filter::SubresourceFilterObserver { |
27 public: | 27 public: |
28 // The types of ads that one can filter on. | |
29 enum AdType { | |
30 AD_TYPE_GOOGLE = 0, | |
31 AD_TYPE_SUBRESOURCE_FILTER = 1, | |
32 AD_TYPE_ALL = 2, | |
33 AD_TYPE_MAX = AD_TYPE_ALL | |
34 }; | |
35 | |
36 using AdTypes = std::bitset<AD_TYPE_MAX>; | |
37 | |
38 // Returns a new AdsPageLoadMetricObserver. If the feature is disabled it | 28 // Returns a new AdsPageLoadMetricObserver. If the feature is disabled it |
39 // returns nullptr. | 29 // returns nullptr. |
40 static std::unique_ptr<AdsPageLoadMetricsObserver> CreateIfNeeded(); | 30 static std::unique_ptr<AdsPageLoadMetricsObserver> CreateIfNeeded(); |
41 | 31 |
42 AdsPageLoadMetricsObserver(); | 32 AdsPageLoadMetricsObserver(); |
43 ~AdsPageLoadMetricsObserver() override; | 33 ~AdsPageLoadMetricsObserver() override; |
44 | 34 |
45 // page_load_metrics::PageLoadMetricsObserver | 35 // page_load_metrics::PageLoadMetricsObserver |
46 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, | 36 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, |
47 const GURL& currently_committed_url, | 37 const GURL& currently_committed_url, |
48 bool started_in_foreground) override; | 38 bool started_in_foreground) override; |
49 ObservePolicy OnCommit(content::NavigationHandle* navigation_handle, | 39 ObservePolicy OnCommit(content::NavigationHandle* navigation_handle, |
50 ukm::SourceId source_id) override; | 40 ukm::SourceId source_id) override; |
51 void OnDidFinishSubFrameNavigation( | 41 void OnDidFinishSubFrameNavigation( |
52 content::NavigationHandle* navigation_handle) override; | 42 content::NavigationHandle* navigation_handle) override; |
53 ObservePolicy FlushMetricsOnAppEnterBackground( | 43 ObservePolicy FlushMetricsOnAppEnterBackground( |
54 const page_load_metrics::mojom::PageLoadTiming& timing, | 44 const page_load_metrics::mojom::PageLoadTiming& timing, |
55 const page_load_metrics::PageLoadExtraInfo& extra_info) override; | 45 const page_load_metrics::PageLoadExtraInfo& extra_info) override; |
56 void OnLoadedResource(const page_load_metrics::ExtraRequestCompleteInfo& | 46 void OnLoadedResource(const page_load_metrics::ExtraRequestCompleteInfo& |
57 extra_request_info) override; | 47 extra_request_info) override; |
58 void OnComplete(const page_load_metrics::mojom::PageLoadTiming& timing, | 48 void OnComplete(const page_load_metrics::mojom::PageLoadTiming& timing, |
59 const page_load_metrics::PageLoadExtraInfo& info) override; | 49 const page_load_metrics::PageLoadExtraInfo& info) override; |
60 | 50 |
61 private: | 51 private: |
62 struct AdFrameData { | 52 struct AdFrameData { |
63 AdFrameData(FrameTreeNodeId frame_tree_node_id, AdTypes ad_types); | 53 AdFrameData(FrameTreeNodeId frame_tree_node_id, |
| 54 page_load_metrics::AdTypes ad_types); |
64 size_t frame_bytes; | 55 size_t frame_bytes; |
65 size_t frame_bytes_uncached; | 56 size_t frame_bytes_uncached; |
66 const FrameTreeNodeId frame_tree_node_id; | 57 const FrameTreeNodeId frame_tree_node_id; |
67 AdTypes ad_types; | 58 page_load_metrics::AdTypes ad_types; |
68 }; | 59 }; |
69 | 60 |
70 // subresource_filter::SubresourceFilterObserver: | 61 // subresource_filter::SubresourceFilterObserver: |
71 void OnSubframeNavigationEvaluated( | 62 void OnSubframeNavigationEvaluated( |
72 content::NavigationHandle* navigation_handle, | 63 content::NavigationHandle* navigation_handle, |
73 subresource_filter::LoadPolicy load_policy) override; | 64 subresource_filter::LoadPolicy load_policy) override; |
74 void OnSubresourceFilterGoingAway() override; | 65 void OnSubresourceFilterGoingAway() override; |
75 | 66 |
76 // Determines if the URL of a frame matches the SubresourceFilter block | |
77 // list. Should only be called once per frame navigation. | |
78 bool DetectSubresourceFilterAd(FrameTreeNodeId frame_tree_node_id); | |
79 | |
80 // This should only be called once per frame navigation, as the | |
81 // SubresourceFilter detector clears its state about detected frames after | |
82 // each call in order to free up memory. | |
83 AdTypes DetectAds(content::NavigationHandle* navigation_handle); | |
84 | |
85 void ProcessLoadedResource( | 67 void ProcessLoadedResource( |
86 const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info); | 68 const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info); |
87 | 69 |
88 void RecordHistograms(); | 70 void RecordHistograms(); |
89 void RecordHistogramsForType(int ad_type); | 71 void RecordHistogramsForType(int ad_type); |
90 | 72 |
91 // Checks to see if a resource is waiting for a navigation with the given | 73 // Checks to see if a resource is waiting for a navigation with the given |
92 // |frame_tree_node_id| to commit before it can be processed. If so, call | 74 // |frame_tree_node_id| to commit before it can be processed. If so, call |
93 // OnLoadedResource for the delayed resource. | 75 // OnLoadedResource for the delayed resource. |
94 void ProcessOngoingNavigationResource(FrameTreeNodeId frame_tree_node_id); | 76 void ProcessOngoingNavigationResource(FrameTreeNodeId frame_tree_node_id); |
95 | 77 |
96 // Stores the size data of each ad frame. Pointed to by ad_frames_ so use a | 78 // Stores the size data of each ad frame. Pointed to by ad_frames_ so use a |
97 // data structure that won't move the data around. | 79 // data structure that won't move the data around. |
98 std::list<AdFrameData> ad_frames_data_storage_; | 80 std::list<AdFrameData> ad_frames_data_storage_; |
99 | 81 |
100 // Maps a frame (by id) to the AdFrameData responsible for the frame. | 82 // Maps a frame (by id) to the AdFrameData responsible for the frame. |
101 // Multiple frame ids can point to the same AdFrameData. The responsible | 83 // Multiple frame ids can point to the same AdFrameData. The responsible |
102 // frame is the top-most frame labeled as an ad in the frame's ancestry, | 84 // frame is the top-most frame labeled as an ad in the frame's ancestry, |
103 // which may be itself. If no responsible frame is found, the data is | 85 // which may be itself. If no responsible frame is found, the data is |
104 // nullptr. | 86 // nullptr. |
105 std::map<FrameTreeNodeId, AdFrameData*> ad_frames_data_; | 87 std::map<FrameTreeNodeId, AdFrameData*> ad_frames_data_; |
106 | 88 |
107 // The set of frames that have yet to finish but that the SubresourceFilter | |
108 // has reported are ads. Once DetectSubresourceFilterAd is called the id is | |
109 // removed from the set. | |
110 std::set<FrameTreeNodeId> unfinished_subresource_ad_frames_; | |
111 | |
112 // When the observer receives report of a document resource loading for a | 89 // When the observer receives report of a document resource loading for a |
113 // sub-frame before the sub-frame commit occurs, hold onto the resource | 90 // sub-frame before the sub-frame commit occurs, hold onto the resource |
114 // request info (delay it) until the sub-frame commits. | 91 // request info (delay it) until the sub-frame commits. |
115 std::map<FrameTreeNodeId, page_load_metrics::ExtraRequestCompleteInfo> | 92 std::map<FrameTreeNodeId, page_load_metrics::ExtraRequestCompleteInfo> |
116 ongoing_navigation_resources_; | 93 ongoing_navigation_resources_; |
117 | 94 |
118 size_t page_bytes_ = 0u; | 95 size_t page_bytes_ = 0u; |
119 size_t uncached_page_bytes_ = 0u; | 96 size_t uncached_page_bytes_ = 0u; |
120 bool committed_ = false; | 97 bool committed_ = false; |
121 | 98 |
122 ScopedObserver<subresource_filter::SubresourceFilterObserverManager, | 99 ScopedObserver<subresource_filter::SubresourceFilterObserverManager, |
123 subresource_filter::SubresourceFilterObserver> | 100 subresource_filter::SubresourceFilterObserver> |
124 subresource_observer_; | 101 subresource_observer_; |
125 | 102 |
126 DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserver); | 103 DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserver); |
127 }; | 104 }; |
128 | 105 |
129 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSE
RVER_H_ | 106 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSE
RVER_H_ |
OLD | NEW |