| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef SYNC_NOTIFIER_ACK_TRACKER_H_ | 5 #ifndef SYNC_NOTIFIER_ACK_TRACKER_H_ |
| 6 #define SYNC_NOTIFIER_ACK_TRACKER_H_ | 6 #define SYNC_NOTIFIER_ACK_TRACKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "net/base/backoff_entry.h" | 16 #include "net/base/backoff_entry.h" |
| 17 #include "sync/notifier/invalidation_util.h" | 17 #include "sync/notifier/invalidation_util.h" |
| 18 | 18 |
| 19 namespace base { |
| 20 class TickClock; |
| 21 } // namespace base |
| 22 |
| 19 namespace syncer { | 23 namespace syncer { |
| 20 | 24 |
| 21 // A simple class that tracks sets of object IDs that have not yet been | 25 // A simple class that tracks sets of object IDs that have not yet been |
| 22 // acknowledged. Internally, it manages timeouts for the tracked object IDs and | 26 // acknowledged. Internally, it manages timeouts for the tracked object IDs and |
| 23 // periodically triggers a callback for each timeout period. The timeout is a | 27 // periodically triggers a callback for each timeout period. The timeout is a |
| 24 // simple exponentially increasing time that starts at 60 seconds and is capped | 28 // simple exponentially increasing time that starts at 60 seconds and is capped |
| 25 // at 600 seconds. | 29 // at 600 seconds. |
| 26 class AckTracker { | 30 class AckTracker { |
| 27 public: | 31 public: |
| 28 class Delegate { | 32 class Delegate { |
| 29 public: | 33 public: |
| 30 virtual ~Delegate(); | 34 virtual ~Delegate(); |
| 31 | 35 |
| 32 // |ids| contains all object IDs that have timed out in this time interval. | 36 // |ids| contains all object IDs that have timed out in this time interval. |
| 33 virtual void OnTimeout(const ObjectIdSet& ids) = 0; | 37 virtual void OnTimeout(const ObjectIdSet& ids) = 0; |
| 34 }; | 38 }; |
| 35 | 39 |
| 36 typedef base::Callback<base::TimeTicks()> NowCallback; | |
| 37 typedef base::Callback<scoped_ptr<net::BackoffEntry>( | 40 typedef base::Callback<scoped_ptr<net::BackoffEntry>( |
| 38 const net::BackoffEntry::Policy* const)> CreateBackoffEntryCallback; | 41 const net::BackoffEntry::Policy* const)> CreateBackoffEntryCallback; |
| 39 | 42 |
| 40 explicit AckTracker(Delegate* delegate); | 43 AckTracker(base::TickClock* tick_clock, Delegate* delegate); |
| 41 ~AckTracker(); | 44 ~AckTracker(); |
| 42 | 45 |
| 43 // Equivalent to calling Ack() on all currently registered object IDs. | 46 // Equivalent to calling Ack() on all currently registered object IDs. |
| 44 void Clear(); | 47 void Clear(); |
| 45 | 48 |
| 46 // Starts tracking timeouts for |ids|. Timeouts will be triggered for each | 49 // Starts tracking timeouts for |ids|. Timeouts will be triggered for each |
| 47 // object ID until it is acknowledged. Note that no de-duplication is | 50 // object ID until it is acknowledged. Note that no de-duplication is |
| 48 // performed; calling Track() twice on the same set of ids will result in two | 51 // performed; calling Track() twice on the same set of ids will result in two |
| 49 // different timeouts being triggered for those ids. | 52 // different timeouts being triggered for those ids. |
| 50 void Track(const ObjectIdSet& ids); | 53 void Track(const ObjectIdSet& ids); |
| 51 // Marks a set of |ids| as acknowledged. | 54 // Marks a set of |ids| as acknowledged. |
| 52 void Ack(const ObjectIdSet& ids); | 55 void Ack(const ObjectIdSet& ids); |
| 53 | 56 |
| 54 // Testing methods. | 57 // Testing methods. |
| 55 void SetNowCallbackForTest(const NowCallback& now_callback); | |
| 56 void SetCreateBackoffEntryCallbackForTest( | 58 void SetCreateBackoffEntryCallbackForTest( |
| 57 const CreateBackoffEntryCallback& create_backoff_entry_callback); | 59 const CreateBackoffEntryCallback& create_backoff_entry_callback); |
| 58 // Returns true iff there are no timeouts scheduled to occur before |now|. | 60 // Returns true iff there are no timeouts scheduled to occur before |now|. |
| 59 // Used in testing to make sure we don't have timeouts set to expire before | 61 // Used in testing to make sure we don't have timeouts set to expire before |
| 60 // when they should. | 62 // when they should. |
| 61 bool TriggerTimeoutAtForTest(base::TimeTicks now); | 63 bool TriggerTimeoutAtForTest(base::TimeTicks now); |
| 62 bool IsQueueEmptyForTest() const; | 64 bool IsQueueEmptyForTest() const; |
| 63 const base::Timer& GetTimerForTest() const; | 65 const base::Timer& GetTimerForTest() const; |
| 64 | 66 |
| 65 private: | 67 private: |
| 66 struct Entry { | 68 struct Entry { |
| 67 Entry(scoped_ptr<net::BackoffEntry> backoff, const ObjectIdSet& ids); | 69 Entry(scoped_ptr<net::BackoffEntry> backoff, const ObjectIdSet& ids); |
| 68 ~Entry(); | 70 ~Entry(); |
| 69 | 71 |
| 70 scoped_ptr<net::BackoffEntry> backoff; | 72 scoped_ptr<net::BackoffEntry> backoff; |
| 71 ObjectIdSet ids; | 73 ObjectIdSet ids; |
| 72 | 74 |
| 73 private: | 75 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(Entry); | 76 DISALLOW_COPY_AND_ASSIGN(Entry); |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 void NudgeTimer(); | 79 void NudgeTimer(); |
| 78 void OnTimeout(); | 80 void OnTimeout(); |
| 79 void OnTimeoutAt(base::TimeTicks now); | 81 void OnTimeoutAt(base::TimeTicks now); |
| 80 | 82 |
| 81 static scoped_ptr<net::BackoffEntry> DefaultCreateBackoffEntryStrategy( | 83 static scoped_ptr<net::BackoffEntry> DefaultCreateBackoffEntryStrategy( |
| 82 const net::BackoffEntry::Policy* const policy); | 84 const net::BackoffEntry::Policy* const policy); |
| 83 | 85 |
| 84 // Used for testing purposes. | 86 // Used for testing purposes. |
| 85 NowCallback now_callback_; | |
| 86 CreateBackoffEntryCallback create_backoff_entry_callback_; | 87 CreateBackoffEntryCallback create_backoff_entry_callback_; |
| 87 | 88 |
| 89 base::TickClock* const tick_clock_; |
| 90 |
| 88 Delegate* const delegate_; | 91 Delegate* const delegate_; |
| 89 | 92 |
| 90 base::OneShotTimer<AckTracker> timer_; | 93 base::OneShotTimer<AckTracker> timer_; |
| 91 // The time that the timer should fire at. We use this to determine if we need | 94 // The time that the timer should fire at. We use this to determine if we need |
| 92 // to start or update |timer_| in NudgeTimer(). We can't simply use | 95 // to start or update |timer_| in NudgeTimer(). We can't simply use |
| 93 // timer_.desired_run_time() for this purpose because it always uses | 96 // timer_.desired_run_time() for this purpose because it always uses |
| 94 // base::TimeTicks::Now() as a reference point when Timer::Start() is called, | 97 // base::TimeTicks::Now() as a reference point when Timer::Start() is called, |
| 95 // while NudgeTimer() needs a fixed reference point to avoid unnecessarily | 98 // while NudgeTimer() needs a fixed reference point to avoid unnecessarily |
| 96 // updating the timer. | 99 // updating the timer. |
| 97 base::TimeTicks desired_run_time_; | 100 base::TimeTicks desired_run_time_; |
| 98 std::multimap<base::TimeTicks, Entry*> queue_; | 101 std::multimap<base::TimeTicks, Entry*> queue_; |
| 99 | 102 |
| 100 base::ThreadChecker thread_checker_; | 103 base::ThreadChecker thread_checker_; |
| 101 | 104 |
| 102 DISALLOW_COPY_AND_ASSIGN(AckTracker); | 105 DISALLOW_COPY_AND_ASSIGN(AckTracker); |
| 103 }; | 106 }; |
| 104 | 107 |
| 105 } // namespace syncer | 108 } // namespace syncer |
| 106 | 109 |
| 107 #endif // SYNC_NOTIFIER_ACK_TRACKER_H_ | 110 #endif // SYNC_NOTIFIER_ACK_TRACKER_H_ |
| OLD | NEW |