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 const base::Feature kDelayNavigationFeature{"DelayNavigation", |
| 17 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 18 |
| 19 const char DelayNavigationThrottle::kParamDelayNavigationDurationMillis[] = |
| 20 "delay_millis"; |
| 21 |
| 22 const char DelayNavigationThrottle::kParamDelayNavigationProbability[] = |
| 23 "delay_probability"; |
| 24 |
| 25 // static |
| 26 std::unique_ptr<content::NavigationThrottle> |
| 27 DelayNavigationThrottle::MaybeCreateThrottleFor( |
| 28 content::NavigationHandle* handle) { |
| 29 if (!handle->IsInMainFrame()) |
| 30 return nullptr; |
| 31 |
| 32 if (!base::FeatureList::IsEnabled(kDelayNavigationFeature)) |
| 33 return nullptr; |
| 34 |
| 35 if (!handle->GetURL().SchemeIsHTTPOrHTTPS()) |
| 36 return nullptr; |
| 37 |
| 38 // Do not delay the NTP, which in some cases has an HTTPS URL. |
| 39 if (search::IsNTPURL(handle->GetURL(), |
| 40 Profile::FromBrowserContext( |
| 41 handle->GetWebContents()->GetBrowserContext()))) { |
| 42 return nullptr; |
| 43 } |
| 44 |
| 45 int navigation_delay_millis = base::GetFieldTrialParamByFeatureAsInt( |
| 46 kDelayNavigationFeature, kParamDelayNavigationDurationMillis, |
| 47 -1 /* default value */); |
| 48 |
| 49 if (navigation_delay_millis <= 0) |
| 50 return nullptr; |
| 51 |
| 52 double delay_probability = base::GetFieldTrialParamByFeatureAsDouble( |
| 53 kDelayNavigationFeature, kParamDelayNavigationProbability, |
| 54 0 /* default value */); |
| 55 |
| 56 if (delay_probability == 0 || delay_probability < base::RandDouble()) |
| 57 return nullptr; |
| 58 |
| 59 return base::MakeUnique<DelayNavigationThrottle>( |
| 60 handle, base::ThreadTaskRunnerHandle::Get(), |
| 61 base::TimeDelta::FromMilliseconds(navigation_delay_millis)); |
| 62 } |
| 63 |
| 64 DelayNavigationThrottle::DelayNavigationThrottle( |
| 65 content::NavigationHandle* handle, |
| 66 scoped_refptr<base::TaskRunner> task_runner, |
| 67 base::TimeDelta navigation_delay) |
| 68 : content::NavigationThrottle(handle), |
| 69 task_runner_(task_runner), |
| 70 navigation_delay_(navigation_delay), |
| 71 weak_ptr_factory_(this) {} |
| 72 |
| 73 DelayNavigationThrottle::~DelayNavigationThrottle() {} |
| 74 |
| 75 content::NavigationThrottle::ThrottleCheckResult |
| 76 DelayNavigationThrottle::WillStartRequest() { |
| 77 task_runner_->PostDelayedTask( |
| 78 FROM_HERE, |
| 79 base::Bind(&DelayNavigationThrottle::OnDelayComplete, |
| 80 weak_ptr_factory_.GetWeakPtr()), |
| 81 navigation_delay_); |
| 82 return content::NavigationThrottle::DEFER; |
| 83 } |
| 84 |
| 85 void DelayNavigationThrottle::OnDelayComplete() { |
| 86 navigation_handle()->Resume(); |
| 87 } |
OLD | NEW |