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 if (delay_probability == 0 || delay_probability < base::RandDouble()) | |
Charlie Harrison
2017/03/24 17:06:58
Can we DCHECK that delay_probability is <= 1?
Bryan McQuade
2017/03/24 17:13:57
sure, good idea. done, thanks!
| |
25 return base::TimeDelta(); | |
26 | |
27 int navigation_delay_millis = base::GetFieldTrialParamByFeatureAsInt( | |
28 kDelayNavigationFeature, | |
29 DelayNavigationThrottle::kParamDelayNavigationDurationMillis, | |
30 -1 /* default value */); | |
31 | |
32 if (navigation_delay_millis <= 0) | |
33 return base::TimeDelta(); | |
34 | |
35 bool randomize_delay = base::GetFieldTrialParamByFeatureAsBool( | |
36 kDelayNavigationFeature, | |
37 DelayNavigationThrottle::kParamDelayNavigationRandomize, | |
38 false /* default value */); | |
39 | |
40 if (randomize_delay) { | |
41 // RandGenerator produces a value in [0, navigation_delay_millis). We want | |
42 // a value in [1, navigation_delay_millis]. | |
43 navigation_delay_millis = base::RandGenerator(navigation_delay_millis) + 1; | |
44 } | |
45 | |
46 return base::TimeDelta::FromMilliseconds(navigation_delay_millis); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 const base::Feature kDelayNavigationFeature{"DelayNavigation", | |
52 base::FEATURE_DISABLED_BY_DEFAULT}; | |
53 | |
54 const char DelayNavigationThrottle::kParamDelayNavigationDurationMillis[] = | |
55 "delay_millis"; | |
56 | |
57 const char DelayNavigationThrottle::kParamDelayNavigationRandomize[] = | |
58 "randomize_delay"; | |
59 | |
60 const char DelayNavigationThrottle::kParamDelayNavigationProbability[] = | |
61 "delay_probability"; | |
62 | |
63 // static | |
64 std::unique_ptr<DelayNavigationThrottle> | |
65 DelayNavigationThrottle::MaybeCreateThrottleFor( | |
66 content::NavigationHandle* handle) { | |
67 if (!handle->IsInMainFrame() || | |
68 !base::FeatureList::IsEnabled(kDelayNavigationFeature) || | |
69 !handle->GetURL().SchemeIsHTTPOrHTTPS()) { | |
70 return nullptr; | |
71 } | |
72 | |
73 // Do not delay the NTP, which in some cases has an HTTPS URL. | |
74 if (search::IsNTPURL(handle->GetURL(), | |
75 Profile::FromBrowserContext( | |
76 handle->GetWebContents()->GetBrowserContext()))) { | |
77 return nullptr; | |
78 } | |
79 | |
80 base::TimeDelta navigation_delay = GetNavigationDelayFromParams(); | |
81 if (navigation_delay.is_zero()) | |
82 return nullptr; | |
83 | |
84 return base::MakeUnique<DelayNavigationThrottle>( | |
85 handle, base::ThreadTaskRunnerHandle::Get(), navigation_delay); | |
86 } | |
87 | |
88 DelayNavigationThrottle::DelayNavigationThrottle( | |
89 content::NavigationHandle* handle, | |
90 scoped_refptr<base::TaskRunner> task_runner, | |
91 base::TimeDelta navigation_delay) | |
92 : content::NavigationThrottle(handle), | |
93 task_runner_(task_runner), | |
94 navigation_delay_(navigation_delay), | |
95 weak_ptr_factory_(this) {} | |
96 | |
97 DelayNavigationThrottle::~DelayNavigationThrottle() {} | |
98 | |
99 content::NavigationThrottle::ThrottleCheckResult | |
100 DelayNavigationThrottle::WillStartRequest() { | |
101 task_runner_->PostDelayedTask( | |
102 FROM_HERE, | |
103 base::Bind(&DelayNavigationThrottle::OnDelayComplete, | |
104 weak_ptr_factory_.GetWeakPtr()), | |
105 navigation_delay_); | |
106 return content::NavigationThrottle::DEFER; | |
107 } | |
108 | |
109 void DelayNavigationThrottle::OnDelayComplete() { | |
110 navigation_handle()->Resume(); | |
111 } | |
OLD | NEW |