OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sessions/tab_loader_delegate.h" | 5 #include "chrome/browser/sessions/tab_loader_delegate.h" |
6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "components/variations/variations_associated_data.h" |
7 #include "net/base/network_change_notifier.h" | 9 #include "net/base/network_change_notifier.h" |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 // The timeout time after which the next tab gets loaded if the previous tab did | 13 // The timeout time after which the next tab gets loaded if the previous tab did |
12 // not finish loading yet. The used value is half of the median value of all | 14 // not finish loading yet. The used value is half of the median value of all |
13 // ChromeOS devices loading the 25 most common web pages. Half is chosen since | 15 // ChromeOS devices loading the 25 most common web pages. Half is chosen since |
14 // the loading time is a mix of server response and data bandwidth. | 16 // the loading time is a mix of server response and data bandwidth. |
15 static const int kInitialDelayTimerMS = 1500; | 17 static const int kInitialDelayTimerMS = 1500; |
16 | 18 |
17 class TabLoaderDelegateImpl | 19 class TabLoaderDelegateImpl |
18 : public TabLoaderDelegate, | 20 : public TabLoaderDelegate, |
19 public net::NetworkChangeNotifier::ConnectionTypeObserver { | 21 public net::NetworkChangeNotifier::ConnectionTypeObserver { |
20 public: | 22 public: |
21 explicit TabLoaderDelegateImpl(TabLoaderCallback* callback); | 23 explicit TabLoaderDelegateImpl(TabLoaderCallback* callback); |
22 ~TabLoaderDelegateImpl() override; | 24 ~TabLoaderDelegateImpl() override; |
23 | 25 |
24 // TabLoaderDelegate: | 26 // TabLoaderDelegate: |
| 27 base::TimeDelta GetFirstTabLoadingTimeout() const override { |
| 28 return first_timeout_; |
| 29 } |
| 30 |
| 31 // TabLoaderDelegate: |
25 base::TimeDelta GetTimeoutBeforeLoadingNextTab() const override { | 32 base::TimeDelta GetTimeoutBeforeLoadingNextTab() const override { |
26 return base::TimeDelta::FromMilliseconds(kInitialDelayTimerMS); | 33 return timeout_; |
27 } | 34 } |
28 | 35 |
29 // net::NetworkChangeNotifier::ConnectionTypeObserver: | 36 // net::NetworkChangeNotifier::ConnectionTypeObserver: |
30 void OnConnectionTypeChanged( | 37 void OnConnectionTypeChanged( |
31 net::NetworkChangeNotifier::ConnectionType type) override; | 38 net::NetworkChangeNotifier::ConnectionType type) override; |
32 | 39 |
33 private: | 40 private: |
34 // The function to call when the connection type changes. | 41 // The function to call when the connection type changes. |
35 TabLoaderCallback* callback_; | 42 TabLoaderCallback* callback_; |
36 | 43 |
| 44 // The timeouts to use in tab loading. |
| 45 base::TimeDelta first_timeout_; |
| 46 base::TimeDelta timeout_; |
| 47 |
37 DISALLOW_COPY_AND_ASSIGN(TabLoaderDelegateImpl); | 48 DISALLOW_COPY_AND_ASSIGN(TabLoaderDelegateImpl); |
38 }; | 49 }; |
39 | 50 |
40 TabLoaderDelegateImpl::TabLoaderDelegateImpl(TabLoaderCallback* callback) | 51 TabLoaderDelegateImpl::TabLoaderDelegateImpl(TabLoaderCallback* callback) |
41 : callback_(callback) { | 52 : callback_(callback) { |
42 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 53 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
43 if (net::NetworkChangeNotifier::IsOffline()) { | 54 if (net::NetworkChangeNotifier::IsOffline()) { |
44 // When we are off-line we do not allow loading of tabs, since each of | 55 // When we are off-line we do not allow loading of tabs, since each of |
45 // these tabs would start loading simultaneously when going online. | 56 // these tabs would start loading simultaneously when going online. |
46 // TODO(skuhne): Once we get a higher level resource control logic which | 57 // TODO(skuhne): Once we get a higher level resource control logic which |
47 // distributes network access, we can remove this. | 58 // distributes network access, we can remove this. |
48 callback->SetTabLoadingEnabled(false); | 59 callback->SetTabLoadingEnabled(false); |
49 } | 60 } |
| 61 |
| 62 // Initialize the timeouts to use from the session restore field trial. |
| 63 // Default to the usual value if none is specified. |
| 64 |
| 65 static const char kIntelligentSessionRestore[] = "IntelligentSessionRestore"; |
| 66 std::string timeout = variations::GetVariationParamValue( |
| 67 kIntelligentSessionRestore, "FirstTabLoadTimeoutMs"); |
| 68 int timeout_ms = 0; |
| 69 if (timeout.empty() || !base::StringToInt(timeout, &timeout_ms) || |
| 70 timeout_ms <= 0) { |
| 71 timeout_ms = kInitialDelayTimerMS; |
| 72 } |
| 73 first_timeout_ = base::TimeDelta::FromMilliseconds(timeout_ms); |
| 74 |
| 75 timeout = variations::GetVariationParamValue( |
| 76 kIntelligentSessionRestore, "TabLoadTimeoutMs"); |
| 77 timeout_ms = 0; |
| 78 if (timeout.empty() || !base::StringToInt(timeout, &timeout_ms) || |
| 79 timeout_ms <= 0) { |
| 80 timeout_ms = kInitialDelayTimerMS; |
| 81 } |
| 82 timeout_ = base::TimeDelta::FromMilliseconds(timeout_ms); |
50 } | 83 } |
51 | 84 |
52 TabLoaderDelegateImpl::~TabLoaderDelegateImpl() { | 85 TabLoaderDelegateImpl::~TabLoaderDelegateImpl() { |
53 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 86 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
54 } | 87 } |
55 | 88 |
56 void TabLoaderDelegateImpl::OnConnectionTypeChanged( | 89 void TabLoaderDelegateImpl::OnConnectionTypeChanged( |
57 net::NetworkChangeNotifier::ConnectionType type) { | 90 net::NetworkChangeNotifier::ConnectionType type) { |
58 callback_->SetTabLoadingEnabled( | 91 callback_->SetTabLoadingEnabled( |
59 type != net::NetworkChangeNotifier::CONNECTION_NONE); | 92 type != net::NetworkChangeNotifier::CONNECTION_NONE); |
60 } | 93 } |
61 } // namespace | 94 } // namespace |
62 | 95 |
63 // static | 96 // static |
64 scoped_ptr<TabLoaderDelegate> TabLoaderDelegate::Create( | 97 scoped_ptr<TabLoaderDelegate> TabLoaderDelegate::Create( |
65 TabLoaderCallback* callback) { | 98 TabLoaderCallback* callback) { |
66 return scoped_ptr<TabLoaderDelegate>( | 99 return scoped_ptr<TabLoaderDelegate>( |
67 new TabLoaderDelegateImpl(callback)); | 100 new TabLoaderDelegateImpl(callback)); |
68 } | 101 } |
OLD | NEW |