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 #include "sync/notifier/fake_invalidation_state_tracker.h" | 5 #include "sync/notifier/fake_invalidation_state_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace syncer { | 13 namespace syncer { |
14 | 14 |
15 const int64 FakeInvalidationStateTracker::kMinVersion = kint64min; | 15 const int64 FakeInvalidationStateTracker::kMinVersion = kint64min; |
16 | 16 |
17 FakeInvalidationStateTracker::FakeInvalidationStateTracker() {} | 17 FakeInvalidationStateTracker::FakeInvalidationStateTracker() {} |
18 | 18 |
19 FakeInvalidationStateTracker::~FakeInvalidationStateTracker() {} | 19 FakeInvalidationStateTracker::~FakeInvalidationStateTracker() {} |
20 | 20 |
21 int64 FakeInvalidationStateTracker::GetMaxVersion( | |
22 const invalidation::ObjectId& id) const { | |
23 InvalidationStateMap::const_iterator it = state_map_.find(id); | |
24 return (it == state_map_.end()) ? kMinVersion : it->second.version; | |
25 } | |
26 | |
27 InvalidationStateMap | |
28 FakeInvalidationStateTracker::GetAllInvalidationStates() const { | |
29 return state_map_; | |
30 } | |
31 | |
32 void FakeInvalidationStateTracker::SetMaxVersionAndPayload( | |
33 const invalidation::ObjectId& id, | |
34 int64 max_version, | |
35 const std::string& payload) { | |
36 InvalidationStateMap::const_iterator it = state_map_.find(id); | |
37 if ((it != state_map_.end()) && (max_version <= it->second.version)) { | |
38 ADD_FAILURE(); | |
39 return; | |
40 } | |
41 state_map_[id].version = max_version; | |
42 } | |
43 | |
44 void FakeInvalidationStateTracker::Forget(const ObjectIdSet& ids) { | |
45 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
46 state_map_.erase(*it); | |
47 } | |
48 } | |
49 | |
50 void FakeInvalidationStateTracker::SetInvalidatorClientId( | 21 void FakeInvalidationStateTracker::SetInvalidatorClientId( |
51 const std::string& client_id) { | 22 const std::string& client_id) { |
52 Clear(); | 23 Clear(); |
53 invalidator_client_id_ = client_id; | 24 invalidator_client_id_ = client_id; |
54 } | 25 } |
55 | 26 |
56 std::string FakeInvalidationStateTracker::GetInvalidatorClientId() const { | 27 std::string FakeInvalidationStateTracker::GetInvalidatorClientId() const { |
57 return invalidator_client_id_; | 28 return invalidator_client_id_; |
58 } | 29 } |
59 | 30 |
60 void FakeInvalidationStateTracker::SetBootstrapData( | 31 void FakeInvalidationStateTracker::SetBootstrapData( |
61 const std::string& data) { | 32 const std::string& data) { |
62 bootstrap_data_ = data; | 33 bootstrap_data_ = data; |
63 } | 34 } |
64 | 35 |
65 std::string FakeInvalidationStateTracker::GetBootstrapData() const { | 36 std::string FakeInvalidationStateTracker::GetBootstrapData() const { |
66 return bootstrap_data_; | 37 return bootstrap_data_; |
67 } | 38 } |
68 | 39 |
69 void FakeInvalidationStateTracker::Clear() { | 40 void FakeInvalidationStateTracker::SetSavedInvalidations( |
70 invalidator_client_id_ = ""; | 41 const UnackedInvalidationsMap& states) { |
71 state_map_ = InvalidationStateMap(); | 42 unacked_invalidations_map_ = states; |
72 bootstrap_data_ = ""; | |
73 } | 43 } |
74 | 44 |
75 void FakeInvalidationStateTracker::GenerateAckHandles( | 45 UnackedInvalidationsMap |
76 const ObjectIdSet& ids, | 46 FakeInvalidationStateTracker::GetSavedInvalidations() const { |
77 const scoped_refptr<base::TaskRunner>& task_runner, | 47 return unacked_invalidations_map_; |
78 base::Callback<void(const AckHandleMap&)> callback) { | |
79 AckHandleMap ack_handles; | |
80 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
81 state_map_[*it].expected = AckHandle::CreateUnique(); | |
82 ack_handles.insert(std::make_pair(*it, state_map_[*it].expected)); | |
83 } | |
84 if (!task_runner->PostTask(FROM_HERE, base::Bind(callback, ack_handles))) | |
85 ADD_FAILURE(); | |
86 } | 48 } |
87 | 49 |
88 void FakeInvalidationStateTracker::Acknowledge(const invalidation::ObjectId& id, | 50 void FakeInvalidationStateTracker::Clear() { |
89 const AckHandle& ack_handle) { | 51 invalidator_client_id_.clear(); |
90 InvalidationStateMap::iterator it = state_map_.find(id); | 52 bootstrap_data_.clear(); |
91 if (it == state_map_.end()) | |
92 ADD_FAILURE(); | |
93 it->second.current = ack_handle; | |
94 } | 53 } |
95 | 54 |
96 } // namespace syncer | 55 } // namespace syncer |
OLD | NEW |