Chromium Code Reviews| 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) | |
|
gab
2015/05/15 18:21:03
Multi-line conditional, add {}
chrisha
2015/05/15 18:47:12
Done.
| |
| 71 timeout_ms = kInitialDelayTimerMS; | |
| 72 first_timeout_ = base::TimeDelta::FromMilliseconds(timeout_ms); | |
| 73 | |
| 74 timeout = variations::GetVariationParamValue( | |
| 75 kIntelligentSessionRestore, "TabLoadTimeoutMs"); | |
| 76 timeout_ms = 0; | |
| 77 if (timeout.empty() || !base::StringToInt(timeout, &timeout_ms) || | |
| 78 timeout_ms <= 0) | |
|
gab
2015/05/15 18:21:03
{}
chrisha
2015/05/15 18:47:12
Done.
| |
| 79 timeout_ms = kInitialDelayTimerMS; | |
| 80 timeout_ = base::TimeDelta::FromMilliseconds(timeout_ms); | |
| 50 } | 81 } |
| 51 | 82 |
| 52 TabLoaderDelegateImpl::~TabLoaderDelegateImpl() { | 83 TabLoaderDelegateImpl::~TabLoaderDelegateImpl() { |
| 53 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 84 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 54 } | 85 } |
| 55 | 86 |
| 56 void TabLoaderDelegateImpl::OnConnectionTypeChanged( | 87 void TabLoaderDelegateImpl::OnConnectionTypeChanged( |
| 57 net::NetworkChangeNotifier::ConnectionType type) { | 88 net::NetworkChangeNotifier::ConnectionType type) { |
| 58 callback_->SetTabLoadingEnabled( | 89 callback_->SetTabLoadingEnabled( |
| 59 type != net::NetworkChangeNotifier::CONNECTION_NONE); | 90 type != net::NetworkChangeNotifier::CONNECTION_NONE); |
| 60 } | 91 } |
| 61 } // namespace | 92 } // namespace |
| 62 | 93 |
| 63 // static | 94 // static |
| 64 scoped_ptr<TabLoaderDelegate> TabLoaderDelegate::Create( | 95 scoped_ptr<TabLoaderDelegate> TabLoaderDelegate::Create( |
| 65 TabLoaderCallback* callback) { | 96 TabLoaderCallback* callback) { |
| 66 return scoped_ptr<TabLoaderDelegate>( | 97 return scoped_ptr<TabLoaderDelegate>( |
| 67 new TabLoaderDelegateImpl(callback)); | 98 new TabLoaderDelegateImpl(callback)); |
| 68 } | 99 } |
| OLD | NEW |