1 // Copyright 2014 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.
5 #include "chrome/browser/sessions/tab_loader_delegate.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "components/variations/variations_associated_data.h"
9 #include "net/base/network_change_notifier.h"
13 // The timeout time after which the next tab gets loaded if the previous tab did
14 // not finish loading yet. The used value is half of the median value of all
15 // ChromeOS devices loading the 25 most common web pages. Half is chosen since
16 // the loading time is a mix of server response and data bandwidth.
17 static const int kInitialDelayTimerMS
= 1500;
19 class TabLoaderDelegateImpl
20 : public TabLoaderDelegate
,
21 public net::NetworkChangeNotifier::ConnectionTypeObserver
{
23 explicit TabLoaderDelegateImpl(TabLoaderCallback
* callback
);
24 ~TabLoaderDelegateImpl() override
;
27 base::TimeDelta
GetFirstTabLoadingTimeout() const override
{
28 return first_timeout_
;
32 base::TimeDelta
GetTimeoutBeforeLoadingNextTab() const override
{
36 // net::NetworkChangeNotifier::ConnectionTypeObserver:
37 void OnConnectionTypeChanged(
38 net::NetworkChangeNotifier::ConnectionType type
) override
;
41 // The function to call when the connection type changes.
42 TabLoaderCallback
* callback_
;
44 // The timeouts to use in tab loading.
45 base::TimeDelta first_timeout_
;
46 base::TimeDelta timeout_
;
48 DISALLOW_COPY_AND_ASSIGN(TabLoaderDelegateImpl
);
51 TabLoaderDelegateImpl::TabLoaderDelegateImpl(TabLoaderCallback
* callback
)
52 : callback_(callback
) {
53 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
54 if (net::NetworkChangeNotifier::IsOffline()) {
55 // When we are off-line we do not allow loading of tabs, since each of
56 // these tabs would start loading simultaneously when going online.
57 // TODO(skuhne): Once we get a higher level resource control logic which
58 // distributes network access, we can remove this.
59 callback
->SetTabLoadingEnabled(false);
62 // Initialize the timeouts to use from the session restore field trial.
63 // Default to the usual value if none is specified.
65 static const char kIntelligentSessionRestore
[] = "IntelligentSessionRestore";
66 std::string timeout
= variations::GetVariationParamValue(
67 kIntelligentSessionRestore
, "FirstTabLoadTimeoutMs");
69 if (timeout
.empty() || !base::StringToInt(timeout
, &timeout_ms
) ||
71 timeout_ms
= kInitialDelayTimerMS
;
73 first_timeout_
= base::TimeDelta::FromMilliseconds(timeout_ms
);
75 timeout
= variations::GetVariationParamValue(
76 kIntelligentSessionRestore
, "TabLoadTimeoutMs");
78 if (timeout
.empty() || !base::StringToInt(timeout
, &timeout_ms
) ||
80 timeout_ms
= kInitialDelayTimerMS
;
82 timeout_
= base::TimeDelta::FromMilliseconds(timeout_ms
);
85 TabLoaderDelegateImpl::~TabLoaderDelegateImpl() {
86 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
89 void TabLoaderDelegateImpl::OnConnectionTypeChanged(
90 net::NetworkChangeNotifier::ConnectionType type
) {
91 callback_
->SetTabLoadingEnabled(
92 type
!= net::NetworkChangeNotifier::CONNECTION_NONE
);
97 scoped_ptr
<TabLoaderDelegate
> TabLoaderDelegate::Create(
98 TabLoaderCallback
* callback
) {
99 return scoped_ptr
<TabLoaderDelegate
>(
100 new TabLoaderDelegateImpl(callback
));