Home | History | Annotate | Download | only in notifier
      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::IsUnsent(const Invalidation& invalidation) const {
     54   AckHandleMatcher matcher(invalidation.ack_handle());
     55   InvalidationVector::const_iterator it1 = std::find_if(
     56       unsent_invalidations_.begin(),
     57       unsent_invalidations_.end(),
     58       matcher);
     59   return it1 != unsent_invalidations_.end();
     60 }
     61 
     62 void MockAckHandler::Acknowledge(
     63     const invalidation::ObjectId& id,
     64     const AckHandle& handle) {
     65   AckHandleMatcher matcher(handle);
     66   InvalidationVector::iterator it = std::find_if(
     67       unacked_invalidations_.begin(),
     68       unacked_invalidations_.end(),
     69       matcher);
     70   if (it != unacked_invalidations_.end()) {
     71     acked_invalidations_.push_back(*it);
     72     unacked_invalidations_.erase(it);
     73   }
     74 }
     75 
     76 void MockAckHandler::Drop(
     77     const invalidation::ObjectId& id,
     78     const AckHandle& handle) {
     79 }
     80 
     81 WeakHandle<AckHandler> MockAckHandler::WeakHandleThis() {
     82   return WeakHandle<AckHandler>(AsWeakPtr());
     83 }
     84 
     85 }  // namespace syncer
     86