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