OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/page_load_metrics/experiments/delay_navigation_throttle
.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/field_trial_params.h" |
| 9 #include "base/rand_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search/search.h" |
| 13 #include "content/public/browser/navigation_handle.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 base::TimeDelta GetNavigationDelayFromParams() { |
| 19 double delay_probability = base::GetFieldTrialParamByFeatureAsDouble( |
| 20 kDelayNavigationFeature, |
| 21 DelayNavigationThrottle::kParamDelayNavigationProbability, |
| 22 0 /* default value */); |
| 23 |
| 24 DCHECK_GE(delay_probability, 0.0); |
| 25 DCHECK_LE(delay_probability, 1.0); |
| 26 if (delay_probability == 0 || delay_probability < base::RandDouble()) |
| 27 return base::TimeDelta(); |
| 28 |
| 29 int navigation_delay_millis = base::GetFieldTrialParamByFeatureAsInt( |
| 30 kDelayNavigationFeature, |
| 31 DelayNavigationThrottle::kParamDelayNavigationDurationMillis, |
| 32 -1 /* default value */); |
| 33 |
| 34 if (navigation_delay_millis <= 0) |
| 35 return base::TimeDelta(); |
| 36 |
| 37 bool randomize_delay = base::GetFieldTrialParamByFeatureAsBool( |
| 38 kDelayNavigationFeature, |
| 39 DelayNavigationThrottle::kParamDelayNavigationRandomize, |
| 40 false /* default value */); |
| 41 |
| 42 if (randomize_delay) { |
| 43 // RandGenerator produces a value in [0, navigation_delay_millis). We want |
| 44 // a value in [1, navigation_delay_millis]. |
| 45 navigation_delay_millis = base::RandGenerator(navigation_delay_millis) + 1; |
| 46 } |
| 47 |
| 48 return base::TimeDelta::FromMilliseconds(navigation_delay_millis); |
| 49 } |
| 50 |
| 51 } // namespace |
| 52 |
| 53 const base::Feature kDelayNavigationFeature{"DelayNavigation", |
| 54 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 55 |
| 56 const char DelayNavigationThrottle::kParamDelayNavigationDurationMillis[] = |
| 57 "delay_millis"; |
| 58 |
| 59 const char DelayNavigationThrottle::kParamDelayNavigationRandomize[] = |
| 60 "randomize_delay"; |
| 61 |
| 62 const char DelayNavigationThrottle::kParamDelayNavigationProbability[] = |
| 63 "delay_probability"; |
| 64 |
| 65 // static |
| 66 std::unique_ptr<DelayNavigationThrottle> |
| 67 DelayNavigationThrottle::MaybeCreateThrottleFor( |
| 68 content::NavigationHandle* handle) { |
| 69 if (!handle->IsInMainFrame() || |
| 70 !base::FeatureList::IsEnabled(kDelayNavigationFeature) || |
| 71 !handle->GetURL().SchemeIsHTTPOrHTTPS()) { |
| 72 return nullptr; |
| 73 } |
| 74 |
| 75 // Do not delay the NTP, which in some cases has an HTTPS URL. |
| 76 if (search::IsNTPURL(handle->GetURL(), |
| 77 Profile::FromBrowserContext( |
| 78 handle->GetWebContents()->GetBrowserContext()))) { |
| 79 return nullptr; |
| 80 } |
| 81 |
| 82 base::TimeDelta navigation_delay = GetNavigationDelayFromParams(); |
| 83 if (navigation_delay.is_zero()) |
| 84 return nullptr; |
| 85 |
| 86 return base::MakeUnique<DelayNavigationThrottle>( |
| 87 handle, base::ThreadTaskRunnerHandle::Get(), navigation_delay); |
| 88 } |
| 89 |
| 90 DelayNavigationThrottle::DelayNavigationThrottle( |
| 91 content::NavigationHandle* handle, |
| 92 scoped_refptr<base::TaskRunner> task_runner, |
| 93 base::TimeDelta navigation_delay) |
| 94 : content::NavigationThrottle(handle), |
| 95 task_runner_(task_runner), |
| 96 navigation_delay_(navigation_delay), |
| 97 weak_ptr_factory_(this) {} |
| 98 |
| 99 DelayNavigationThrottle::~DelayNavigationThrottle() {} |
| 100 |
| 101 content::NavigationThrottle::ThrottleCheckResult |
| 102 DelayNavigationThrottle::WillStartRequest() { |
| 103 task_runner_->PostDelayedTask( |
| 104 FROM_HERE, |
| 105 base::Bind(&DelayNavigationThrottle::OnDelayComplete, |
| 106 weak_ptr_factory_.GetWeakPtr()), |
| 107 navigation_delay_); |
| 108 return content::NavigationThrottle::DEFER; |
| 109 } |
| 110 |
| 111 void DelayNavigationThrottle::OnDelayComplete() { |
| 112 navigation_handle()->Resume(); |
| 113 } |
OLD | NEW |