| 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 #import "ios/chrome/browser/web/auto_reload_controller.h" | 5 #import "ios/chrome/browser/web/auto_reload_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/ios/weak_nsobject.h" | |
| 10 #include "base/mac/bind_objc_block.h" | 9 #include "base/mac/bind_objc_block.h" |
| 11 #include "base/mac/scoped_nsobject.h" | |
| 12 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
| 13 | 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 base::TimeDelta GetAutoReloadTime(size_t reload_count) { | 18 base::TimeDelta GetAutoReloadTime(size_t reload_count) { |
| 17 static const int kDelaysMs[] = { | 19 static const int kDelaysMs[] = { |
| 18 0, 5000, 30000, 60000, 300000, 600000, 1800000, | 20 0, 5000, 30000, 60000, 300000, 600000, 1800000, |
| 19 }; | 21 }; |
| 20 if (reload_count >= arraysize(kDelaysMs)) | 22 if (reload_count >= arraysize(kDelaysMs)) |
| 21 reload_count = arraysize(kDelaysMs) - 1; | 23 reload_count = arraysize(kDelaysMs) - 1; |
| 22 return base::TimeDelta::FromMilliseconds(kDelaysMs[reload_count]); | 24 return base::TimeDelta::FromMilliseconds(kDelaysMs[reload_count]); |
| 23 } | 25 } |
| 24 | 26 |
| 25 } // namespace | 27 } // namespace |
| 26 | 28 |
| 27 @interface AutoReloadController () { | 29 @interface AutoReloadController () { |
| 28 GURL _failedUrl; | 30 GURL _failedUrl; |
| 29 int _reloadCount; | 31 int _reloadCount; |
| 30 BOOL _shouldReload; | 32 BOOL _shouldReload; |
| 31 BOOL _online; | 33 BOOL _online; |
| 32 id<AutoReloadDelegate> _delegate; | 34 __weak id<AutoReloadDelegate> _delegate; |
| 33 std::unique_ptr<base::Timer> _timer; | 35 std::unique_ptr<base::Timer> _timer; |
| 34 } | 36 } |
| 35 | 37 |
| 36 // This method is called when the system transitions to offline from online, | 38 // This method is called when the system transitions to offline from online, |
| 37 // possibly causing a paused reload timer to start. | 39 // possibly causing a paused reload timer to start. |
| 38 - (void)transitionedToOffline; | 40 - (void)transitionedToOffline; |
| 39 | 41 |
| 40 // This method is called when the system transitions from online to offline, | 42 // This method is called when the system transitions from online to offline, |
| 41 // possibly pausing the reload timer. | 43 // possibly pausing the reload timer. |
| 42 - (void)transitionedToOnline; | 44 - (void)transitionedToOnline; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 else if (online && !_online) | 97 else if (online && !_online) |
| 96 [self transitionedToOnline]; | 98 [self transitionedToOnline]; |
| 97 _online = online; | 99 _online = online; |
| 98 } | 100 } |
| 99 | 101 |
| 100 - (void)setTimerForTesting:(std::unique_ptr<base::Timer>)timer { | 102 - (void)setTimerForTesting:(std::unique_ptr<base::Timer>)timer { |
| 101 _timer.reset(timer.release()); | 103 _timer.reset(timer.release()); |
| 102 } | 104 } |
| 103 | 105 |
| 104 - (void)startTimer { | 106 - (void)startTimer { |
| 105 base::WeakNSObject<AutoReloadController> weakSelf(self); | 107 __weak AutoReloadController* weakSelf = self; |
| 106 base::TimeDelta delay = GetAutoReloadTime(_reloadCount); | 108 base::TimeDelta delay = GetAutoReloadTime(_reloadCount); |
| 107 _timer->Start(FROM_HERE, delay, base::BindBlock(^{ | 109 _timer->Start(FROM_HERE, delay, base::BindBlockArc(^{ |
| 108 base::scoped_nsobject<AutoReloadController> strongSelf( | 110 AutoReloadController* strongSelf = weakSelf; |
| 109 [weakSelf retain]); | |
| 110 // self owns the timer owns this closure, so self must outlive | 111 // self owns the timer owns this closure, so self must outlive |
| 111 // this closure. | 112 // this closure. |
| 112 DCHECK(strongSelf); | 113 DCHECK(strongSelf); |
| 113 [strongSelf timerFired]; | 114 [strongSelf timerFired]; |
| 114 })); | 115 })); |
| 115 } | 116 } |
| 116 | 117 |
| 117 - (void)transitionedToOffline { | 118 - (void)transitionedToOffline { |
| 118 DCHECK(!_shouldReload); | 119 DCHECK(!_shouldReload); |
| 119 if (_timer->IsRunning()) { | 120 if (_timer->IsRunning()) { |
| 120 _shouldReload = true; | 121 _shouldReload = true; |
| 121 _timer->Stop(); | 122 _timer->Stop(); |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 | 125 |
| 125 - (void)transitionedToOnline { | 126 - (void)transitionedToOnline { |
| 126 if (_shouldReload) { | 127 if (_shouldReload) { |
| 127 _shouldReload = false; | 128 _shouldReload = false; |
| 128 [self startTimer]; | 129 [self startTimer]; |
| 129 } | 130 } |
| 130 } | 131 } |
| 131 | 132 |
| 132 - (void)timerFired { | 133 - (void)timerFired { |
| 133 _reloadCount++; | 134 _reloadCount++; |
| 134 [_delegate reload]; | 135 [_delegate reload]; |
| 135 } | 136 } |
| 136 | 137 |
| 137 @end | 138 @end |
| OLD | NEW |