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