Chromium Code Reviews| Index: chrome/browser/loader/delay_navigation_throttle.cc |
| diff --git a/chrome/browser/loader/delay_navigation_throttle.cc b/chrome/browser/loader/delay_navigation_throttle.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..174c4429d1f638cc70beaa3e33852c7ba6458237 |
| --- /dev/null |
| +++ b/chrome/browser/loader/delay_navigation_throttle.cc |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2017 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 "chrome/browser/loader/delay_navigation_throttle.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/metrics/field_trial_params.h" |
| +#include "base/rand_util.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "chrome/browser/search/search.h" |
| +#include "content/public/browser/browser_thread.h" |
|
Charlie Harrison
2017/03/22 15:30:12
Is it used?
Bryan McQuade
2017/03/22 18:00:01
nope - removed, thanks!
|
| +#include "content/public/browser/navigation_handle.h" |
| + |
| +const base::Feature kDelayNavigationFeature{"DelayNavigation", |
| + base::FEATURE_DISABLED_BY_DEFAULT}; |
| + |
| +const char DelayNavigationThrottle::kParamDelayNavigationDurationMillis[] = |
| + "delay_millis"; |
| + |
| +const char DelayNavigationThrottle::kParamDelayNavigationProbability[] = |
| + "delay_probability"; |
| + |
| +// static |
| +std::unique_ptr<content::NavigationThrottle> |
| +DelayNavigationThrottle::MaybeCreateThrottleFor( |
| + content::NavigationHandle* handle) { |
| + // Never delay: |
| + // * non HTTP/HTTPS URLs |
| + // * the new tab page, which in some cases has an HTTPS URL |
| + if (!handle->GetURL().SchemeIsHTTPOrHTTPS() || |
| + search::IsInstantNTP(handle->GetWebContents())) { |
|
Charlie Harrison
2017/03/22 15:30:12
Is this correct? IsInstantNTP looks at the visible
Bryan McQuade
2017/03/22 18:00:01
interesting, thanks for digging in. it does seem t
|
| + return nullptr; |
| + } |
| + |
| + if (!base::FeatureList::IsEnabled(kDelayNavigationFeature)) |
|
Charlie Harrison
2017/03/22 15:30:12
Let's move this to be the first check. IsInstantNT
Bryan McQuade
2017/03/22 18:00:01
done
|
| + return nullptr; |
| + |
| + int navigation_delay_millis = base::GetFieldTrialParamByFeatureAsInt( |
| + kDelayNavigationFeature, kParamDelayNavigationDurationMillis, |
| + -1 /* default value */); |
| + |
| + if (navigation_delay_millis <= 0) |
| + return nullptr; |
| + |
| + double delay_probability = base::GetFieldTrialParamByFeatureAsDouble( |
| + kDelayNavigationFeature, kParamDelayNavigationProbability, |
| + 0 /* default value */); |
| + |
| + if (delay_probability == 0 || delay_probability < base::RandDouble()) |
| + return nullptr; |
| + |
| + return base::WrapUnique(new DelayNavigationThrottle( |
|
Charlie Harrison
2017/03/22 15:30:12
nit: base::MakeUnique is preferred to avoid bare "
Bryan McQuade
2017/03/22 18:00:01
done
|
| + handle, base::ThreadTaskRunnerHandle::Get(), |
| + base::TimeDelta::FromMilliseconds(navigation_delay_millis))); |
| +} |
| + |
| +DelayNavigationThrottle::DelayNavigationThrottle( |
| + content::NavigationHandle* handle, |
| + scoped_refptr<base::TaskRunner> task_runner, |
| + base::TimeDelta navigation_delay) |
| + : content::NavigationThrottle(handle), |
| + task_runner_(task_runner), |
| + navigation_delay_(navigation_delay), |
| + weak_ptr_factory_(this) {} |
| + |
| +DelayNavigationThrottle::~DelayNavigationThrottle() {} |
| + |
| +content::NavigationThrottle::ThrottleCheckResult |
| +DelayNavigationThrottle::WillStartRequest() { |
| + task_runner_->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&DelayNavigationThrottle::OnDelayComplete, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + navigation_delay_); |
| + return content::NavigationThrottle::DEFER; |
| +} |
| + |
| +void DelayNavigationThrottle::OnDelayComplete() { |
| + navigation_handle()->Resume(); |
| +} |