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

Side by Side Diff: chrome/browser/loader/delay_navigation_throttle.cc

Issue 2763613002: Add DelayNavigationThrottle (Closed)
Patch Set: rebase 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 unified diff | Download patch
OLDNEW
(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/loader/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/search/search.h"
12 #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!
13 #include "content/public/browser/navigation_handle.h"
14
15 const base::Feature kDelayNavigationFeature{"DelayNavigation",
16 base::FEATURE_DISABLED_BY_DEFAULT};
17
18 const char DelayNavigationThrottle::kParamDelayNavigationDurationMillis[] =
19 "delay_millis";
20
21 const char DelayNavigationThrottle::kParamDelayNavigationProbability[] =
22 "delay_probability";
23
24 // static
25 std::unique_ptr<content::NavigationThrottle>
26 DelayNavigationThrottle::MaybeCreateThrottleFor(
27 content::NavigationHandle* handle) {
28 // Never delay:
29 // * non HTTP/HTTPS URLs
30 // * the new tab page, which in some cases has an HTTPS URL
31 if (!handle->GetURL().SchemeIsHTTPOrHTTPS() ||
32 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
33 return nullptr;
34 }
35
36 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
37 return nullptr;
38
39 int navigation_delay_millis = base::GetFieldTrialParamByFeatureAsInt(
40 kDelayNavigationFeature, kParamDelayNavigationDurationMillis,
41 -1 /* default value */);
42
43 if (navigation_delay_millis <= 0)
44 return nullptr;
45
46 double delay_probability = base::GetFieldTrialParamByFeatureAsDouble(
47 kDelayNavigationFeature, kParamDelayNavigationProbability,
48 0 /* default value */);
49
50 if (delay_probability == 0 || delay_probability < base::RandDouble())
51 return nullptr;
52
53 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
54 handle, base::ThreadTaskRunnerHandle::Get(),
55 base::TimeDelta::FromMilliseconds(navigation_delay_millis)));
56 }
57
58 DelayNavigationThrottle::DelayNavigationThrottle(
59 content::NavigationHandle* handle,
60 scoped_refptr<base::TaskRunner> task_runner,
61 base::TimeDelta navigation_delay)
62 : content::NavigationThrottle(handle),
63 task_runner_(task_runner),
64 navigation_delay_(navigation_delay),
65 weak_ptr_factory_(this) {}
66
67 DelayNavigationThrottle::~DelayNavigationThrottle() {}
68
69 content::NavigationThrottle::ThrottleCheckResult
70 DelayNavigationThrottle::WillStartRequest() {
71 task_runner_->PostDelayedTask(
72 FROM_HERE,
73 base::Bind(&DelayNavigationThrottle::OnDelayComplete,
74 weak_ptr_factory_.GetWeakPtr()),
75 navigation_delay_);
76 return content::NavigationThrottle::DEFER;
77 }
78
79 void DelayNavigationThrottle::OnDelayComplete() {
80 navigation_handle()->Resume();
81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698