| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sync/notifier/mock_ack_handler.h" | |
| 6 | |
| 7 #include "sync/internal_api/public/base/ack_handle.h" | |
| 8 #include "sync/internal_api/public/base/invalidation.h" | |
| 9 | |
| 10 namespace syncer { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 struct AckHandleMatcher { | |
| 15 AckHandleMatcher(const AckHandle& handle); | |
| 16 bool operator()(const syncer::Invalidation& invalidation) const; | |
| 17 | |
| 18 syncer::AckHandle handle_; | |
| 19 }; | |
| 20 | |
| 21 AckHandleMatcher::AckHandleMatcher(const AckHandle& handle) | |
| 22 : handle_(handle) {} | |
| 23 | |
| 24 bool AckHandleMatcher::operator()( | |
| 25 const syncer::Invalidation& invalidation) const { | |
| 26 return handle_.Equals(invalidation.ack_handle()); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 MockAckHandler::MockAckHandler() {} | |
| 32 | |
| 33 MockAckHandler::~MockAckHandler() {} | |
| 34 | |
| 35 void MockAckHandler::RegisterInvalidation(Invalidation* invalidation) { | |
| 36 unacked_invalidations_.push_back(*invalidation); | |
| 37 invalidation->set_ack_handler(WeakHandleThis()); | |
| 38 } | |
| 39 | |
| 40 void MockAckHandler::RegisterUnsentInvalidation(Invalidation* invalidation) { | |
| 41 unsent_invalidations_.push_back(*invalidation); | |
| 42 } | |
| 43 | |
| 44 bool MockAckHandler::IsUnacked(const Invalidation& invalidation) const { | |
| 45 AckHandleMatcher matcher(invalidation.ack_handle()); | |
| 46 InvalidationVector::const_iterator it = std::find_if( | |
| 47 unacked_invalidations_.begin(), | |
| 48 unacked_invalidations_.end(), | |
| 49 matcher); | |
| 50 return it != unacked_invalidations_.end(); | |
| 51 } | |
| 52 | |
| 53 bool MockAckHandler::IsAcknowledged(const Invalidation& invalidation) const { | |
| 54 AckHandleMatcher matcher(invalidation.ack_handle()); | |
| 55 InvalidationVector::const_iterator it = std::find_if( | |
| 56 acked_invalidations_.begin(), | |
| 57 acked_invalidations_.end(), | |
| 58 matcher); | |
| 59 return it != acked_invalidations_.end(); | |
| 60 } | |
| 61 | |
| 62 bool MockAckHandler::IsDropped(const Invalidation& invalidation) const { | |
| 63 AckHandleMatcher matcher(invalidation.ack_handle()); | |
| 64 InvalidationVector::const_iterator it = std::find_if( | |
| 65 dropped_invalidations_.begin(), | |
| 66 dropped_invalidations_.end(), | |
| 67 matcher); | |
| 68 return it != dropped_invalidations_.end(); | |
| 69 } | |
| 70 | |
| 71 bool MockAckHandler::IsUnsent(const Invalidation& invalidation) const { | |
| 72 AckHandleMatcher matcher(invalidation.ack_handle()); | |
| 73 InvalidationVector::const_iterator it1 = std::find_if( | |
| 74 unsent_invalidations_.begin(), | |
| 75 unsent_invalidations_.end(), | |
| 76 matcher); | |
| 77 return it1 != unsent_invalidations_.end(); | |
| 78 } | |
| 79 | |
| 80 bool MockAckHandler::AllInvalidationsAccountedFor() const { | |
| 81 return unacked_invalidations_.empty() && unrecovered_drop_events_.empty(); | |
| 82 } | |
| 83 | |
| 84 void MockAckHandler::Acknowledge( | |
| 85 const invalidation::ObjectId& id, | |
| 86 const AckHandle& handle) { | |
| 87 AckHandleMatcher matcher(handle); | |
| 88 InvalidationVector::iterator it = std::find_if( | |
| 89 unacked_invalidations_.begin(), | |
| 90 unacked_invalidations_.end(), | |
| 91 matcher); | |
| 92 if (it != unacked_invalidations_.end()) { | |
| 93 acked_invalidations_.push_back(*it); | |
| 94 unacked_invalidations_.erase(it); | |
| 95 } | |
| 96 | |
| 97 IdHandleMap::iterator it2 = unrecovered_drop_events_.find(id); | |
| 98 if (it2 != unrecovered_drop_events_.end() && it2->second.Equals(handle)) { | |
| 99 unrecovered_drop_events_.erase(it2); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void MockAckHandler::Drop( | |
| 104 const invalidation::ObjectId& id, | |
| 105 const AckHandle& handle) { | |
| 106 AckHandleMatcher matcher(handle); | |
| 107 InvalidationVector::iterator it = std::find_if( | |
| 108 unacked_invalidations_.begin(), | |
| 109 unacked_invalidations_.end(), | |
| 110 matcher); | |
| 111 if (it != unacked_invalidations_.end()) { | |
| 112 dropped_invalidations_.push_back(*it); | |
| 113 unacked_invalidations_.erase(it); | |
| 114 } | |
| 115 unrecovered_drop_events_.erase(id); | |
| 116 unrecovered_drop_events_.insert(std::make_pair(id, handle)); | |
| 117 } | |
| 118 | |
| 119 WeakHandle<AckHandler> MockAckHandler::WeakHandleThis() { | |
| 120 return WeakHandle<AckHandler>(AsWeakPtr()); | |
| 121 } | |
| 122 | |
| 123 } // namespace syncer | |
| OLD | NEW |