Commit d1fee60d authored by Josh Karlin's avatar Josh Karlin Committed by Commit Bot

[AdsPageLoadMetrics] Track frames identified as ads by SubresourceFilter

Labels frames identified as ads by the SubresourceFilter as ads. Also adds an
AdType to each frame tracked by AdsPageLoadMetrics. When it's time to record
the metrics, the same logic is applied once to collect stats for each ad type.
There is also an additional ad type, "All", which records stats on all of the
identified ads, regardless of which detector discovered it.

Bug: 708570
Change-Id: Ic0f1f1cfd7176ffdba459b607735a6aa6b4318ff
Reviewed-on: https://chromium-review.googlesource.com/567324Reviewed-by: default avatarRobert Kaplow <rkaplow@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Commit-Queue: Josh Karlin <jkarlin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486797}
parent 2cccbe12
......@@ -5,20 +5,36 @@
#ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSERVER_H_
#define CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_ADS_PAGE_LOAD_METRICS_OBSERVER_H_
#include <bitset>
#include <list>
#include <map>
#include <memory>
#include "base/macros.h"
#include "base/scoped_observer.h"
#include "chrome/browser/page_load_metrics/page_load_metrics_observer.h"
#include "components/subresource_filter/content/browser/subresource_filter_observer.h"
#include "components/subresource_filter/content/browser/subresource_filter_observer_manager.h"
#include "components/subresource_filter/core/common/load_policy.h"
#include "components/ukm/ukm_source.h"
#include "net/http/http_response_info.h"
// This observer labels each sub-frame as an ad or not, and keeps track of
// relevant per-frame and whole-page byte statistics.
class AdsPageLoadMetricsObserver
: public page_load_metrics::PageLoadMetricsObserver {
: public page_load_metrics::PageLoadMetricsObserver,
public subresource_filter::SubresourceFilterObserver {
public:
// The types of ads that one can filter on.
enum AdType {
AD_TYPE_GOOGLE = 0,
AD_TYPE_SUBRESOURCE_FILTER = 1,
AD_TYPE_ALL = 2,
AD_TYPE_MAX = AD_TYPE_ALL
};
using AdTypes = std::bitset<AD_TYPE_MAX>;
// Returns a new AdsPageLoadMetricObserver. If the feature is disabled it
// returns nullptr.
static std::unique_ptr<AdsPageLoadMetricsObserver> CreateIfNeeded();
......@@ -27,6 +43,9 @@ class AdsPageLoadMetricsObserver
~AdsPageLoadMetricsObserver() override;
// page_load_metrics::PageLoadMetricsObserver
ObservePolicy OnStart(content::NavigationHandle* navigation_handle,
const GURL& currently_committed_url,
bool started_in_foreground) override;
ObservePolicy OnCommit(content::NavigationHandle* navigation_handle,
ukm::SourceId source_id) override;
void OnDidFinishSubFrameNavigation(
......@@ -41,15 +60,33 @@ class AdsPageLoadMetricsObserver
private:
struct AdFrameData {
explicit AdFrameData(FrameTreeNodeId frame_tree_node_id);
AdFrameData(FrameTreeNodeId frame_tree_node_id, AdTypes ad_types);
size_t frame_bytes;
size_t frame_bytes_uncached;
const FrameTreeNodeId frame_tree_node_id;
AdTypes ad_types;
};
// subresource_filter::SubresourceFilterObserver:
void OnSubframeNavigationEvaluated(
content::NavigationHandle* navigation_handle,
subresource_filter::LoadPolicy load_policy) override;
void OnSubresourceFilterGoingAway() override;
// Determines if the URL of a frame matches the SubresourceFilter block
// list. Should only be called once per frame navigation.
bool DetectSubresourceFilterAd(FrameTreeNodeId frame_tree_node_id);
// This should only be called once per frame navigation, as the
// SubresourceFilter detector clears its state about detected frames after
// each call in order to free up memory.
AdTypes DetectAds(content::NavigationHandle* navigation_handle);
void ProcessLoadedResource(
const page_load_metrics::ExtraRequestCompleteInfo& extra_request_info);
void RecordHistograms();
void RecordHistogramsForType(int ad_type);
// Checks to see if a resource is waiting for a navigation with the given
// |frame_tree_node_id| to commit before it can be processed. If so, call
......@@ -67,6 +104,11 @@ class AdsPageLoadMetricsObserver
// nullptr.
std::map<FrameTreeNodeId, AdFrameData*> ad_frames_data_;
// The set of frames that have yet to finish but that the SubresourceFilter
// has reported are ads. Once DetectSubresourceFilterAd is called the id is
// removed from the set.
std::set<FrameTreeNodeId> unfinished_subresource_ad_frames_;
// When the observer receives report of a document resource loading for a
// sub-frame before the sub-frame commit occurs, hold onto the resource
// request info (delay it) until the sub-frame commits.
......@@ -77,6 +119,10 @@ class AdsPageLoadMetricsObserver
size_t uncached_page_bytes_ = 0u;
bool committed_ = false;
ScopedObserver<subresource_filter::SubresourceFilterObserverManager,
subresource_filter::SubresourceFilterObserver>
subresource_observer_;
DISALLOW_COPY_AND_ASSIGN(AdsPageLoadMetricsObserver);
};
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment