summaryrefslogtreecommitdiff
path: root/chromium/components/subresource_filter/content/common/ad_delay_throttle.cc
blob: be5c04e4048b549038948a5b722adf9852dc3659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/subresource_filter/content/common/ad_delay_throttle.h"

#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "base/sequenced_task_runner.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "components/subresource_filter/core/common/common_features.h"
#include "url/gurl.h"
#include "url/url_constants.h"

namespace subresource_filter {

namespace {

void LogSecureInfo(AdDelayThrottle::SecureInfo info) {
  UMA_HISTOGRAM_ENUMERATION("SubresourceFilter.AdDelay.SecureInfo", info);
}

class InsecureCondition : public AdDelayThrottle::DeferCondition {
 public:
  InsecureCondition(base::TimeDelta delay,
                    AdDelayThrottle::MetadataProvider* provider)
      : DeferCondition(delay, provider) {}
  ~InsecureCondition() override {
    if (provider()->IsAdRequest()) {
      LogSecureInfo(was_condition_ever_satisfied()
                        ? AdDelayThrottle::SecureInfo::kInsecureAd
                        : AdDelayThrottle::SecureInfo::kSecureAd);
    } else {
      LogSecureInfo(was_condition_ever_satisfied()
                        ? AdDelayThrottle::SecureInfo::kInsecureNonAd
                        : AdDelayThrottle::SecureInfo::kSecureNonAd);
    }
  }

 private:
  // DeferCondition:
  bool IsConditionSatisfied(const GURL& url) override {
    // Note: this should probably be using content::IsOriginSecure which
    // accounts for things like whitelisted origins, localhost, etc. This isn't
    // used here because that function is quite expensive for insecure schemes,
    // involving many allocations and string scans.
    return url.SchemeIs(url::kHttpScheme);
  }
};

class NonIsolatedCondition : public AdDelayThrottle::DeferCondition {
 public:
  NonIsolatedCondition(base::TimeDelta delay,
                       AdDelayThrottle::MetadataProvider* provider)
      : DeferCondition(delay, provider) {}
  ~NonIsolatedCondition() override {
    if (provider()->IsAdRequest()) {
      UMA_HISTOGRAM_ENUMERATION(
          "SubresourceFilter.AdDelay.IsolatedInfo",
          was_condition_ever_satisfied()
              ? AdDelayThrottle::IsolatedInfo::kNonIsolatedAd
              : AdDelayThrottle::IsolatedInfo::kIsolatedAd);
    }
  }

 private:
  // DeferCondition:
  bool IsConditionSatisfied(const GURL& url) override {
    return provider()->RequestIsInNonIsolatedSubframe();
  }
};

};  // namespace

AdDelayThrottle::DeferCondition::DeferCondition(
    base::TimeDelta delay,
    AdDelayThrottle::MetadataProvider* provider)
    : delay_(delay), provider_(provider) {
  DCHECK(provider);
}
AdDelayThrottle::DeferCondition::~DeferCondition() = default;

bool AdDelayThrottle::DeferCondition::ShouldDefer(const GURL& url) {
  if (was_condition_applied_) {
    DCHECK(was_condition_ever_satisfied_);
    return false;
  }
  was_condition_ever_satisfied_ |= IsConditionSatisfied(url);
  return was_condition_ever_satisfied_;
}

// The request will be deferred. Returns the amount of time to defer.
base::TimeDelta AdDelayThrottle::DeferCondition::OnReadyToDefer() {
  DCHECK(!was_condition_applied_);
  DCHECK(was_condition_ever_satisfied_);
  was_condition_applied_ = true;
  return delay_;
}

constexpr base::TimeDelta AdDelayThrottle::kDefaultDelay;

AdDelayThrottle::Factory::Factory()
    : insecure_delay_(base::TimeDelta::FromMilliseconds(
          base::GetFieldTrialParamByFeatureAsInt(
              kDelayUnsafeAds,
              kInsecureDelayParam,
              kDefaultDelay.InMilliseconds()))),
      non_isolated_delay_(base::TimeDelta::FromMilliseconds(
          base::GetFieldTrialParamByFeatureAsInt(
              kDelayUnsafeAds,
              kNonIsolatedDelayParam,
              kDefaultDelay.InMilliseconds()))),
      delay_enabled_(base::FeatureList::IsEnabled(kAdTagging) &&
                     base::FeatureList::IsEnabled(kDelayUnsafeAds)) {}

AdDelayThrottle::Factory::~Factory() = default;

std::unique_ptr<AdDelayThrottle> AdDelayThrottle::Factory::MaybeCreate(
    std::unique_ptr<AdDelayThrottle::MetadataProvider> provider) const {
  DCHECK(provider);
  return base::WrapUnique(new AdDelayThrottle(std::move(provider), this));
}

AdDelayThrottle::~AdDelayThrottle() {
  if (!expected_delay_.is_zero()) {
    UMA_HISTOGRAM_TIMES("SubresourceFilter.AdDelay.Delay", actual_delay_);
    UMA_HISTOGRAM_TIMES("SubresourceFilter.AdDelay.Delay.Expected",
                        expected_delay_);
    UMA_HISTOGRAM_TIMES("SubresourceFilter.AdDelay.Delay.Queuing",
                        actual_delay_ - expected_delay_);
  }
}

void AdDelayThrottle::DetachFromCurrentSequence() {
  // The throttle is moving to another thread. Ensure this is done before any
  // weak pointers are created.
  DCHECK(!weak_factory_.HasWeakPtrs());
}

void AdDelayThrottle::WillStartRequest(network::ResourceRequest* request,
                                       bool* defer) {
  *defer = MaybeDefer(request->url);
}

void AdDelayThrottle::WillRedirectRequest(
    const net::RedirectInfo& redirect_info,
    const network::ResourceResponseHead& response_head,
    bool* defer) {
  // Note: some MetadataProviders may not be able to distinguish requests that
  // are only tagged as ads after a redirect.
  *defer = MaybeDefer(redirect_info.new_url);
}

bool AdDelayThrottle::MaybeDefer(const GURL& url) {
  // Check for condition matching before checking if the feature is enabled, to
  // ensure metrics can be reported.
  std::vector<DeferCondition*> matched_conditions;
  for (auto& condition : defer_conditions_) {
    if (condition->ShouldDefer(url))
      matched_conditions.push_back(condition.get());
  }

  if (!delay_enabled_ || matched_conditions.empty() ||
      !provider_->IsAdRequest()) {
    return false;
  }

  base::TimeDelta delay;
  for (DeferCondition* condition : matched_conditions) {
    delay += condition->OnReadyToDefer();
  }
  // TODO(csharrison): Consider logging to the console here that Chrome
  // delayed this request.
  expected_delay_ += delay;
  base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&AdDelayThrottle::Resume, weak_factory_.GetWeakPtr(),
                     tick_clock_->NowTicks()),
      delay);
  return true;
}

void AdDelayThrottle::Resume(base::TimeTicks defer_start) {
  actual_delay_ += tick_clock_->NowTicks() - defer_start;
  delegate_->Resume();
}

AdDelayThrottle::AdDelayThrottle(std::unique_ptr<MetadataProvider> provider,
                                 const AdDelayThrottle::Factory* factory)
    : provider_(std::move(provider)),
      tick_clock_(base::DefaultTickClock::GetInstance()),
      delay_enabled_(factory->delay_enabled()),
      weak_factory_(this) {
  defer_conditions_.emplace_back(std::make_unique<InsecureCondition>(
      factory->insecure_delay(), provider_.get()));
  defer_conditions_.emplace_back(std::make_unique<NonIsolatedCondition>(
      factory->non_isolated_delay(), provider_.get()));
}

}  // namespace subresource_filter