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> |
8 #include <list> | 9 #include <list> |
9 #include <map> | 10 #include <map> |
10 #include <memory> | 11 #include <memory> |
11 | 12 |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/scoped_observer.h" | 14 #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 |
28 // Returns a new AdsPageLoadMetricObserver. If the feature is disabled it | 38 // Returns a new AdsPageLoadMetricObserver. If the feature is disabled it |
29 // returns nullptr. | 39 // returns nullptr. |
30 static std::unique_ptr<AdsPageLoadMetricsObserver> CreateIfNeeded(); | 40 static std::unique_ptr<AdsPageLoadMetricsObserver> CreateIfNeeded(); |
31 | 41 |
32 AdsPageLoadMetricsObserver(); | 42 AdsPageLoadMetricsObserver(); |
33 ~AdsPageLoadMetricsObserver() override; | 43 ~AdsPageLoadMetricsObserver() override; |
34 | 44 |
35 // page_load_metrics::PageLoadMetricsObserver | 45 // page_load_metrics::PageLoadMetricsObserver |
36 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, | 46 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, |
37 const GURL& currently_committed_url, | 47 const GURL& currently_committed_url, |
38 bool started_in_foreground) override; | 48 bool started_in_foreground) override; |
39 ObservePolicy OnCommit(content::NavigationHandle* navigation_handle, | 49 ObservePolicy OnCommit(content::NavigationHandle* navigation_handle, |
40 ukm::SourceId source_id) override; | 50 ukm::SourceId source_id) override; |
41 void OnDidFinishSubFrameNavigation( | 51 void OnDidFinishSubFrameNavigation( |
42 content::NavigationHandle* navigation_handle) override; | 52 content::NavigationHandle* navigation_handle) override; |
43 ObservePolicy FlushMetricsOnAppEnterBackground( | 53 ObservePolicy FlushMetricsOnAppEnterBackground( |
44 const page_load_metrics::mojom::PageLoadTiming& timing, | 54 const page_load_metrics::mojom::PageLoadTiming& timing, |
45 const page_load_metrics::PageLoadExtraInfo& extra_info) override; | 55 const page_load_metrics::PageLoadExtraInfo& extra_info) override; |
46 void OnLoadedResource(const page_load_metrics::ExtraRequestCompleteInfo& | 56 void OnLoadedResource(const page_load_metrics::ExtraRequestCompleteInfo& |
47 extra_request_info) override; | 57 extra_request_info) override; |
48 void OnComplete(const page_load_metrics::mojom::PageLoadTiming& timing, | 58 void OnComplete(const page_load_metrics::mojom::PageLoadTiming& timing, |
49 const page_load_metrics::PageLoadExtraInfo& info) override; | 59 const page_load_metrics::PageLoadExtraInfo& info) override; |
50 | 60 |
51 private: | 61 private: |
52 struct AdFrameData { | 62 struct AdFrameData { |
53 AdFrameData(FrameTreeNodeId frame_tree_node_id, | 63 AdFrameData(FrameTreeNodeId frame_tree_node_id, AdTypes ad_types); |
54 page_load_metrics::AdTypes ad_types); | |
55 size_t frame_bytes; | 64 size_t frame_bytes; |
56 size_t frame_bytes_uncached; | 65 size_t frame_bytes_uncached; |
57 const FrameTreeNodeId frame_tree_node_id; | 66 const FrameTreeNodeId frame_tree_node_id; |
58 page_load_metrics::AdTypes ad_types; | 67 AdTypes ad_types; |
59 }; | 68 }; |
60 | 69 |
61 // subresource_filter::SubresourceFilterObserver: | 70 // subresource_filter::SubresourceFilterObserver: |
62 void OnSubframeNavigationEvaluated( | 71 void OnSubframeNavigationEvaluated( |
63 content::NavigationHandle* navigation_handle, | 72 content::NavigationHandle* navigation_handle, |
64 subresource_filter::LoadPolicy load_policy) override; | 73 subresource_filter::LoadPolicy load_policy) override; |
65 void OnSubresourceFilterGoingAway() override; | 74 void OnSubresourceFilterGoingAway() override; |
66 | 75 |
| 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 |
67 void ProcessLoadedResource( | 85 void ProcessLoadedResource( |
68 const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info); | 86 const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info); |
69 | 87 |
70 void RecordHistograms(); | 88 void RecordHistograms(); |
71 void RecordHistogramsForType(int ad_type); | 89 void RecordHistogramsForType(int ad_type); |
72 | 90 |
73 // Checks to see if a resource is waiting for a navigation with the given | 91 // Checks to see if a resource is waiting for a navigation with the given |
74 // |frame_tree_node_id| to commit before it can be processed. If so, call | 92 // |frame_tree_node_id| to commit before it can be processed. If so, call |
75 // OnLoadedResource for the delayed resource. | 93 // OnLoadedResource for the delayed resource. |
76 void ProcessOngoingNavigationResource(FrameTreeNodeId frame_tree_node_id); | 94 void ProcessOngoingNavigationResource(FrameTreeNodeId frame_tree_node_id); |
77 | 95 |
78 // Stores the size data of each ad frame. Pointed to by ad_frames_ so use a | 96 // Stores the size data of each ad frame. Pointed to by ad_frames_ so use a |
79 // data structure that won't move the data around. | 97 // data structure that won't move the data around. |
80 std::list<AdFrameData> ad_frames_data_storage_; | 98 std::list<AdFrameData> ad_frames_data_storage_; |
81 | 99 |
82 // Maps a frame (by id) to the AdFrameData responsible for the frame. | 100 // Maps a frame (by id) to the AdFrameData responsible for the frame. |
83 // Multiple frame ids can point to the same AdFrameData. The responsible | 101 // Multiple frame ids can point to the same AdFrameData. The responsible |
84 // frame is the top-most frame labeled as an ad in the frame's ancestry, | 102 // frame is the top-most frame labeled as an ad in the frame's ancestry, |
85 // which may be itself. If no responsible frame is found, the data is | 103 // which may be itself. If no responsible frame is found, the data is |
86 // nullptr. | 104 // nullptr. |
87 std::map<FrameTreeNodeId, AdFrameData*> ad_frames_data_; | 105 std::map<FrameTreeNodeId, AdFrameData*> ad_frames_data_; |
88 | 106 |
| 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 |
89 // When the observer receives report of a document resource loading for a | 112 // When the observer receives report of a document resource loading for a |
90 // sub-frame before the sub-frame commit occurs, hold onto the resource | 113 // sub-frame before the sub-frame commit occurs, hold onto the resource |
91 // request info (delay it) until the sub-frame commits. | 114 // request info (delay it) until the sub-frame commits. |
92 std::map<FrameTreeNodeId, page_load_metrics::ExtraRequestCompleteInfo> | 115 std::map<FrameTreeNodeId, page_load_metrics::ExtraRequestCompleteInfo> |
93 ongoing_navigation_resources_; | 116 ongoing_navigation_resources_; |
94 | 117 |
95 size_t page_bytes_ = 0u; | 118 size_t page_bytes_ = 0u; |
96 size_t uncached_page_bytes_ = 0u; | 119 size_t uncached_page_bytes_ = 0u; |
97 bool committed_ = false; | 120 bool committed_ = false; |
98 | 121 |
99 ScopedObserver<subresource_filter::SubresourceFilterObserverManager, | 122 ScopedObserver<subresource_filter::SubresourceFilterObserverManager, |
100 subresource_filter::SubresourceFilterObserver> | 123 subresource_filter::SubresourceFilterObserver> |
101 subresource_observer_; | 124 subresource_observer_; |
102 | 125 |
103 DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserver); | 126 DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserver); |
104 }; | 127 }; |
105 | 128 |
106 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSE
RVER_H_ | 129 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSE
RVER_H_ |
OLD | NEW |