Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Unified Diff: chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.cc

Issue 2763613002: Add DelayNavigationThrottle (Closed)
Patch Set: fix tests Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.cc
diff --git a/chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.cc b/chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.cc
new file mode 100644
index 0000000000000000000000000000000000000000..191bfd5444c95bb06b0b504ba3547b8179f338a6
--- /dev/null
+++ b/chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.cc
@@ -0,0 +1,87 @@
+// 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/page_load_metrics/experiments/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/profiles/profile.h"
+#include "chrome/browser/search/search.h"
+#include "content/public/browser/navigation_handle.h"
+#include "content/public/browser/web_contents.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) {
+ if (!handle->IsInMainFrame())
+ return nullptr;
+
+ if (!base::FeatureList::IsEnabled(kDelayNavigationFeature))
+ return nullptr;
+
+ if (!handle->GetURL().SchemeIsHTTPOrHTTPS())
+ return nullptr;
+
+ // Do not delay the NTP, which in some cases has an HTTPS URL.
+ if (search::IsNTPURL(handle->GetURL(),
+ Profile::FromBrowserContext(
+ handle->GetWebContents()->GetBrowserContext()))) {
+ 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::MakeUnique<DelayNavigationThrottle>(
+ 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();
+}

Powered by Google App Engine
This is Rietveld 408576698