OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/sessions/data_type_tracker.h" | 5 #include "sync/sessions/data_type_tracker.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "sync/internal_api/public/base/invalidation_interface.h" | 8 #include "sync/internal_api/public/base/invalidation_interface.h" |
9 #include "sync/sessions/nudge_tracker.h" | 9 #include "sync/sessions/nudge_tracker.h" |
10 | 10 |
11 namespace syncer { | 11 namespace syncer { |
12 namespace sessions { | 12 namespace sessions { |
13 | 13 |
14 DataTypeTracker::DataTypeTracker() | 14 DataTypeTracker::DataTypeTracker() |
15 : local_nudge_count_(0), | 15 : local_nudge_count_(0), |
16 local_refresh_request_count_(0), | 16 local_refresh_request_count_(0), |
17 payload_buffer_size_(NudgeTracker::kDefaultMaxPayloadsPerType) { | 17 payload_buffer_size_(NudgeTracker::kDefaultMaxPayloadsPerType), |
| 18 initial_sync_required_(false) { |
18 } | 19 } |
19 | 20 |
20 DataTypeTracker::~DataTypeTracker() { } | 21 DataTypeTracker::~DataTypeTracker() { } |
21 | 22 |
22 void DataTypeTracker::RecordLocalChange() { | 23 void DataTypeTracker::RecordLocalChange() { |
23 local_nudge_count_++; | 24 local_nudge_count_++; |
24 } | 25 } |
25 | 26 |
26 void DataTypeTracker::RecordLocalRefreshRequest() { | 27 void DataTypeTracker::RecordLocalRefreshRequest() { |
27 local_refresh_request_count_++; | 28 local_refresh_request_count_++; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 80 |
80 // The incoming invalidation may have caused us to exceed our buffer size. | 81 // The incoming invalidation may have caused us to exceed our buffer size. |
81 // Trim some items from our list, if necessary. | 82 // Trim some items from our list, if necessary. |
82 while (pending_invalidations_.size() > payload_buffer_size_) { | 83 while (pending_invalidations_.size() > payload_buffer_size_) { |
83 last_dropped_invalidation_.reset(pending_invalidations_.front()); | 84 last_dropped_invalidation_.reset(pending_invalidations_.front()); |
84 last_dropped_invalidation_->Drop(); | 85 last_dropped_invalidation_->Drop(); |
85 pending_invalidations_.weak_erase(pending_invalidations_.begin()); | 86 pending_invalidations_.weak_erase(pending_invalidations_.begin()); |
86 } | 87 } |
87 } | 88 } |
88 | 89 |
| 90 void DataTypeTracker::RecordInitialSyncRequired() { |
| 91 initial_sync_required_ = true; |
| 92 } |
| 93 |
89 void DataTypeTracker::RecordSuccessfulSyncCycle() { | 94 void DataTypeTracker::RecordSuccessfulSyncCycle() { |
90 // If we were throttled, then we would have been excluded from this cycle's | 95 // If we were throttled, then we would have been excluded from this cycle's |
91 // GetUpdates and Commit actions. Our state remains unchanged. | 96 // GetUpdates and Commit actions. Our state remains unchanged. |
92 if (IsThrottled()) | 97 if (IsThrottled()) |
93 return; | 98 return; |
94 | 99 |
95 local_nudge_count_ = 0; | 100 local_nudge_count_ = 0; |
96 local_refresh_request_count_ = 0; | 101 local_refresh_request_count_ = 0; |
97 | 102 |
98 // TODO(rlarocque): If we want this to be correct even if we should happen to | 103 // TODO(rlarocque): If we want this to be correct even if we should happen to |
99 // crash before writing all our state, we should wait until the results of | 104 // crash before writing all our state, we should wait until the results of |
100 // this sync cycle have been written to disk before updating the invalidations | 105 // this sync cycle have been written to disk before updating the invalidations |
101 // state. See crbug.com/324996. | 106 // state. See crbug.com/324996. |
102 for (ScopedVector<InvalidationInterface>::const_iterator it = | 107 for (ScopedVector<InvalidationInterface>::const_iterator it = |
103 pending_invalidations_.begin(); | 108 pending_invalidations_.begin(); |
104 it != pending_invalidations_.end(); | 109 it != pending_invalidations_.end(); |
105 ++it) { | 110 ++it) { |
106 (*it)->Acknowledge(); | 111 (*it)->Acknowledge(); |
107 } | 112 } |
108 pending_invalidations_.clear(); | 113 pending_invalidations_.clear(); |
109 | 114 |
110 if (last_dropped_invalidation_) { | 115 if (last_dropped_invalidation_) { |
111 last_dropped_invalidation_->Acknowledge(); | 116 last_dropped_invalidation_->Acknowledge(); |
112 last_dropped_invalidation_.reset(); | 117 last_dropped_invalidation_.reset(); |
113 } | 118 } |
| 119 |
| 120 initial_sync_required_ = false; |
114 } | 121 } |
115 | 122 |
116 // This limit will take effect on all future invalidations received. | 123 // This limit will take effect on all future invalidations received. |
117 void DataTypeTracker::UpdatePayloadBufferSize(size_t new_size) { | 124 void DataTypeTracker::UpdatePayloadBufferSize(size_t new_size) { |
118 payload_buffer_size_ = new_size; | 125 payload_buffer_size_ = new_size; |
119 } | 126 } |
120 | 127 |
121 bool DataTypeTracker::IsSyncRequired() const { | 128 bool DataTypeTracker::IsSyncRequired() const { |
122 return !IsThrottled() && (HasLocalChangePending() || IsGetUpdatesRequired()); | 129 return !IsThrottled() && (HasLocalChangePending() || IsGetUpdatesRequired()); |
123 } | 130 } |
124 | 131 |
125 bool DataTypeTracker::IsGetUpdatesRequired() const { | 132 bool DataTypeTracker::IsGetUpdatesRequired() const { |
126 return !IsThrottled() && | 133 return !IsThrottled() && |
127 (HasRefreshRequestPending() || HasPendingInvalidation()); | 134 (HasRefreshRequestPending() || HasPendingInvalidation() || |
| 135 IsInitialSyncRequired()); |
128 } | 136 } |
129 | 137 |
130 bool DataTypeTracker::HasLocalChangePending() const { | 138 bool DataTypeTracker::HasLocalChangePending() const { |
131 return local_nudge_count_ > 0; | 139 return local_nudge_count_ > 0; |
132 } | 140 } |
133 | 141 |
134 bool DataTypeTracker::HasRefreshRequestPending() const { | 142 bool DataTypeTracker::HasRefreshRequestPending() const { |
135 return local_refresh_request_count_ > 0; | 143 return local_refresh_request_count_ > 0; |
136 } | 144 } |
137 | 145 |
138 bool DataTypeTracker::HasPendingInvalidation() const { | 146 bool DataTypeTracker::HasPendingInvalidation() const { |
139 return !pending_invalidations_.empty() || last_dropped_invalidation_; | 147 return !pending_invalidations_.empty() || last_dropped_invalidation_; |
140 } | 148 } |
141 | 149 |
| 150 bool DataTypeTracker::IsInitialSyncRequired() const { |
| 151 return initial_sync_required_; |
| 152 } |
| 153 |
142 void DataTypeTracker::SetLegacyNotificationHint( | 154 void DataTypeTracker::SetLegacyNotificationHint( |
143 sync_pb::DataTypeProgressMarker* progress) const { | 155 sync_pb::DataTypeProgressMarker* progress) const { |
144 DCHECK(!IsThrottled()) | 156 DCHECK(!IsThrottled()) |
145 << "We should not make requests if the type is throttled."; | 157 << "We should not make requests if the type is throttled."; |
146 | 158 |
147 if (!pending_invalidations_.empty() && | 159 if (!pending_invalidations_.empty() && |
148 !pending_invalidations_.back()->IsUnknownVersion()) { | 160 !pending_invalidations_.back()->IsUnknownVersion()) { |
149 // The old-style source info can contain only one hint per type. We grab | 161 // The old-style source info can contain only one hint per type. We grab |
150 // the most recent, to mimic the old coalescing behaviour. | 162 // the most recent, to mimic the old coalescing behaviour. |
151 progress->set_notification_hint( | 163 progress->set_notification_hint( |
(...skipping 19 matching lines...) Expand all Loading... |
171 msg->add_notification_hint((*it)->GetPayload()); | 183 msg->add_notification_hint((*it)->GetPayload()); |
172 } | 184 } |
173 } | 185 } |
174 | 186 |
175 msg->set_server_dropped_hints( | 187 msg->set_server_dropped_hints( |
176 !pending_invalidations_.empty() && | 188 !pending_invalidations_.empty() && |
177 (*pending_invalidations_.begin())->IsUnknownVersion()); | 189 (*pending_invalidations_.begin())->IsUnknownVersion()); |
178 msg->set_client_dropped_hints(last_dropped_invalidation_); | 190 msg->set_client_dropped_hints(last_dropped_invalidation_); |
179 msg->set_local_modification_nudges(local_nudge_count_); | 191 msg->set_local_modification_nudges(local_nudge_count_); |
180 msg->set_datatype_refresh_nudges(local_refresh_request_count_); | 192 msg->set_datatype_refresh_nudges(local_refresh_request_count_); |
| 193 msg->set_initial_sync_in_progress(initial_sync_required_); |
181 } | 194 } |
182 | 195 |
183 bool DataTypeTracker::IsThrottled() const { | 196 bool DataTypeTracker::IsThrottled() const { |
184 return !unthrottle_time_.is_null(); | 197 return !unthrottle_time_.is_null(); |
185 } | 198 } |
186 | 199 |
187 base::TimeDelta DataTypeTracker::GetTimeUntilUnthrottle( | 200 base::TimeDelta DataTypeTracker::GetTimeUntilUnthrottle( |
188 base::TimeTicks now) const { | 201 base::TimeTicks now) const { |
189 if (!IsThrottled()) { | 202 if (!IsThrottled()) { |
190 NOTREACHED(); | 203 NOTREACHED(); |
191 return base::TimeDelta::FromSeconds(0); | 204 return base::TimeDelta::FromSeconds(0); |
192 } | 205 } |
193 return std::max(base::TimeDelta::FromSeconds(0), | 206 return std::max(base::TimeDelta::FromSeconds(0), |
194 unthrottle_time_ - now); | 207 unthrottle_time_ - now); |
195 } | 208 } |
196 | 209 |
197 void DataTypeTracker::ThrottleType(base::TimeDelta duration, | 210 void DataTypeTracker::ThrottleType(base::TimeDelta duration, |
198 base::TimeTicks now) { | 211 base::TimeTicks now) { |
199 unthrottle_time_ = std::max(unthrottle_time_, now + duration); | 212 unthrottle_time_ = std::max(unthrottle_time_, now + duration); |
200 } | 213 } |
201 | 214 |
202 void DataTypeTracker::UpdateThrottleState(base::TimeTicks now) { | 215 void DataTypeTracker::UpdateThrottleState(base::TimeTicks now) { |
203 if (now >= unthrottle_time_) { | 216 if (now >= unthrottle_time_) { |
204 unthrottle_time_ = base::TimeTicks(); | 217 unthrottle_time_ = base::TimeTicks(); |
205 } | 218 } |
206 } | 219 } |
207 | 220 |
208 } // namespace sessions | 221 } // namespace sessions |
209 } // namespace syncer | 222 } // namespace syncer |
OLD | NEW |