| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/nudge_tracker.h" | 5 #include "sync/sessions/nudge_tracker.h" | 
|  | 6 | 
|  | 7 #include "sync/internal_api/public/base/invalidation.h" | 
|  | 8 #include "sync/internal_api/public/sessions/sync_source_info.h" | 
| 6 #include "sync/protocol/sync.pb.h" | 9 #include "sync/protocol/sync.pb.h" | 
| 7 | 10 | 
| 8 namespace syncer { | 11 namespace syncer { | 
| 9 namespace sessions { | 12 namespace sessions { | 
| 10 | 13 | 
| 11 NudgeTracker::NudgeTracker() { } | 14 size_t NudgeTracker::kDefaultMaxPayloadsPerType = 10; | 
|  | 15 | 
|  | 16 NudgeTracker::NudgeTracker() | 
|  | 17     : updates_source_(sync_pb::GetUpdatesCallerInfo::UNKNOWN), | 
|  | 18       invalidations_enabled_(false), | 
|  | 19       invalidations_out_of_sync_(true), | 
|  | 20       num_payloads_per_type_(kDefaultMaxPayloadsPerType) { } | 
| 12 | 21 | 
| 13 NudgeTracker::~NudgeTracker() { } | 22 NudgeTracker::~NudgeTracker() { } | 
| 14 | 23 | 
| 15 void NudgeTracker::CoalesceSources(const SyncSourceInfo& source) { | 24 bool NudgeTracker::IsSyncRequired() { | 
| 16   CoalesceStates(source.types, &source_info_.types); | 25   if (!local_nudge_counts_.empty()) { | 
| 17   source_info_.updates_source = source.updates_source; | 26     return true; | 
|  | 27   } else if (!refresh_requested_counts_.empty()) { | 
|  | 28     return true; | 
|  | 29   } else if (!payload_list_map_.empty()) { | 
|  | 30     return true; | 
|  | 31   } else { | 
|  | 32     return false; | 
|  | 33   } | 
| 18 } | 34 } | 
| 19 | 35 | 
| 20 bool NudgeTracker::IsEmpty() { | 36 void NudgeTracker::RecordSuccessfulSyncCycle() { | 
| 21   return source_info_.types.empty(); | 37   updates_source_ = sync_pb::GetUpdatesCallerInfo::UNKNOWN; | 
|  | 38   local_nudge_counts_.clear(); | 
|  | 39   refresh_requested_counts_.clear(); | 
|  | 40   payload_list_map_.clear(); | 
|  | 41   locally_dropped_payload_types_.Clear(); | 
|  | 42   server_dropped_payload_types_.Clear(); | 
|  | 43 | 
|  | 44   // A successful cycle while invalidations are enabled puts us back into sync. | 
|  | 45   invalidations_out_of_sync_ = !invalidations_enabled_; | 
| 22 } | 46 } | 
| 23 | 47 | 
| 24 void NudgeTracker::Reset() { | 48 void NudgeTracker::RecordLocalChange(ModelTypeSet types) { | 
| 25   source_info_ = SyncSourceInfo(); | 49   // Don't overwrite an NOTIFICATION or DATATYPE_REFRESH source.  The server | 
|  | 50   // makes some assumptions about the source; overriding these sources with | 
|  | 51   // LOCAL could lead to incorrect behaviour.  This is part of the reason why | 
|  | 52   // we're deprecating 'source' in favor of 'origin'. | 
|  | 53   if (updates_source_ != sync_pb::GetUpdatesCallerInfo::NOTIFICATION | 
|  | 54       && updates_source_ != sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH) { | 
|  | 55     updates_source_ = sync_pb::GetUpdatesCallerInfo::LOCAL; | 
|  | 56   } | 
|  | 57 | 
|  | 58   for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { | 
|  | 59     local_nudge_counts_[it.Get()]++; | 
|  | 60   } | 
| 26 } | 61 } | 
| 27 | 62 | 
| 28 // TODO(rlarocque): This function often reports incorrect results.  However, it | 63 void NudgeTracker::RecordLocalRefreshRequest(ModelTypeSet types) { | 
| 29 // is compatible with the "classic" behaviour.  We would need to make the nudge | 64   // Don't overwrite an NOTIFICATION source.  The server makes some assumptions | 
| 30 // tracker stop overwriting its own information (ie. fix crbug.com/231693) | 65   // about the source.  Overriding this source with LOCAL could lead to | 
| 31 // before we could even try to report correct results.  The main issue is that | 66   // incorrect behaviour.  This is part of the reason why we're deprecating | 
| 32 // an notifications and local modifications nudges may overlap with each other | 67   // 'source' in favor of 'origin'. | 
| 33 // in sych a way that we lose track of which types were or were not locally | 68   if (updates_source_ != sync_pb::GetUpdatesCallerInfo::NOTIFICATION) { | 
| 34 // modified. | 69     updates_source_ = sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH; | 
| 35 ModelTypeSet NudgeTracker::GetLocallyModifiedTypes() const { |  | 
| 36   ModelTypeSet locally_modified; |  | 
| 37 |  | 
| 38   if (source_info_.updates_source != sync_pb::GetUpdatesCallerInfo::LOCAL) { |  | 
| 39     return locally_modified; |  | 
| 40   } | 70   } | 
| 41 | 71 | 
| 42   for (ModelTypeInvalidationMap::const_iterator i = source_info_.types.begin(); | 72   for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { | 
| 43        i != source_info().types.end(); ++i) { | 73     refresh_requested_counts_[it.Get()]++; | 
| 44     locally_modified.Put(i->first); |  | 
| 45   } | 74   } | 
| 46   return locally_modified; | 75 } | 
|  | 76 | 
|  | 77 void NudgeTracker::RecordRemoteInvalidation( | 
|  | 78     const ModelTypeInvalidationMap& invalidation_map) { | 
|  | 79   updates_source_ = sync_pb::GetUpdatesCallerInfo::NOTIFICATION; | 
|  | 80 | 
|  | 81   for (ModelTypeInvalidationMap::const_iterator i = invalidation_map.begin(); | 
|  | 82        i != invalidation_map.end(); ++i) { | 
|  | 83     const ModelType type = i->first; | 
|  | 84     const std::string& payload = i->second.payload; | 
|  | 85 | 
|  | 86     payload_list_map_[type].push_back(payload); | 
|  | 87 | 
|  | 88     // Respect the size limits on locally queued invalidation hints. | 
|  | 89     if (payload_list_map_[type].size() > num_payloads_per_type_) { | 
|  | 90       payload_list_map_[type].pop_front(); | 
|  | 91       locally_dropped_payload_types_.Put(type); | 
|  | 92     } | 
|  | 93   } | 
|  | 94 } | 
|  | 95 | 
|  | 96 void NudgeTracker::OnInvalidationsEnabled() { | 
|  | 97   invalidations_enabled_ = true; | 
|  | 98 } | 
|  | 99 | 
|  | 100 void NudgeTracker::OnInvalidationsDisabled() { | 
|  | 101   invalidations_enabled_ = false; | 
|  | 102   invalidations_out_of_sync_ = true; | 
|  | 103 } | 
|  | 104 | 
|  | 105 SyncSourceInfo NudgeTracker::GetSourceInfo() const { | 
|  | 106   // The old-style source added an empty hint for all locally nudge types. | 
|  | 107   // This will be overwritten with the proper hint for any types that were both | 
|  | 108   // locally nudged and invalidated. | 
|  | 109   ModelTypeSet nudged_types; | 
|  | 110   for (NudgeMap::const_iterator it = local_nudge_counts_.begin(); | 
|  | 111        it != local_nudge_counts_.end(); ++it) { | 
|  | 112     nudged_types.Put(it->first); | 
|  | 113   } | 
|  | 114   ModelTypeInvalidationMap invalidation_map = | 
|  | 115       ModelTypeSetToInvalidationMap(nudged_types, std::string()); | 
|  | 116 | 
|  | 117   // The old-style source info can contain only one hint per type.  We grab the | 
|  | 118   // most recent, to mimic the old coalescing behaviour. | 
|  | 119   for (PayloadListMap::const_iterator it = payload_list_map_.begin(); | 
|  | 120        it != payload_list_map_.end(); ++it) { | 
|  | 121     ModelType type = it->first; | 
|  | 122     const PayloadList& list = it->second; | 
|  | 123 | 
|  | 124     if (!list.empty()) { | 
|  | 125       Invalidation invalidation; | 
|  | 126       invalidation.payload = list.back(); | 
|  | 127       if (invalidation_map.find(type) != invalidation_map.end()) { | 
|  | 128         invalidation_map[type] = invalidation; | 
|  | 129       } else { | 
|  | 130         invalidation_map.insert(std::make_pair(type, invalidation)); | 
|  | 131       } | 
|  | 132     } | 
|  | 133   } | 
|  | 134 | 
|  | 135   return SyncSourceInfo(updates_source_, invalidation_map); | 
|  | 136 } | 
|  | 137 | 
|  | 138 ModelTypeSet NudgeTracker::GetLocallyModifiedTypes() const { | 
|  | 139   ModelTypeSet nudged_types; | 
|  | 140   for (NudgeMap::const_iterator it = local_nudge_counts_.begin(); | 
|  | 141        it != local_nudge_counts_.end(); ++it) { | 
|  | 142     nudged_types.Put(it->first); | 
|  | 143   } | 
|  | 144   return nudged_types; | 
|  | 145 } | 
|  | 146 | 
|  | 147 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource NudgeTracker::updates_source() | 
|  | 148     const { | 
|  | 149   return updates_source_; | 
|  | 150 } | 
|  | 151 | 
|  | 152 void NudgeTracker::FillProtoMessage( | 
|  | 153     ModelType type, | 
|  | 154     sync_pb::GetUpdateTriggers* msg) const { | 
|  | 155   // Fill the list of payloads, if applicable.  The payloads must be ordered | 
|  | 156   // oldest to newest, so we insert them in the same order as we've been storing | 
|  | 157   // them internally. | 
|  | 158   PayloadListMap::const_iterator type_it = payload_list_map_.find(type); | 
|  | 159   if (type_it != payload_list_map_.end()) { | 
|  | 160     const PayloadList& payload_list = type_it->second; | 
|  | 161     for (PayloadList::const_iterator payload_it = payload_list.begin(); | 
|  | 162          payload_it != payload_list.end(); ++payload_it) { | 
|  | 163       msg->add_notification_hint(*payload_it); | 
|  | 164     } | 
|  | 165   } | 
|  | 166 | 
|  | 167   // TODO(rlarocque): Support Tango trickles.  See crbug.com/223437. | 
|  | 168   // msg->set_server_dropped_hints(server_dropped_payload_types_.Has(type)); | 
|  | 169 | 
|  | 170   msg->set_client_dropped_hints(locally_dropped_payload_types_.Has(type)); | 
|  | 171   msg->set_invalidations_out_of_sync(invalidations_out_of_sync_); | 
|  | 172 | 
|  | 173   { | 
|  | 174     NudgeMap::const_iterator it = local_nudge_counts_.find(type); | 
|  | 175     if (it == local_nudge_counts_.end()) { | 
|  | 176       msg->set_local_modification_nudges(0); | 
|  | 177     } else { | 
|  | 178       msg->set_local_modification_nudges(it->second); | 
|  | 179     } | 
|  | 180   } | 
|  | 181 | 
|  | 182   { | 
|  | 183     NudgeMap::const_iterator it = refresh_requested_counts_.find(type); | 
|  | 184     if (it == refresh_requested_counts_.end()) { | 
|  | 185       msg->set_datatype_refresh_nudges(0); | 
|  | 186     } else { | 
|  | 187       msg->set_datatype_refresh_nudges(it->second); | 
|  | 188     } | 
|  | 189   } | 
|  | 190 } | 
|  | 191 | 
|  | 192 void NudgeTracker::SetHintBufferSize(size_t size) { | 
|  | 193   // We could iterate through the list and trim it down to size here, but that | 
|  | 194   // seems unnecessary.  This limit will take effect on all future invalidations | 
|  | 195   // received. | 
|  | 196   num_payloads_per_type_ = size; | 
| 47 } | 197 } | 
| 48 | 198 | 
| 49 }  // namespace sessions | 199 }  // namespace sessions | 
| 50 }  // namespace syncer | 200 }  // namespace syncer | 
| OLD | NEW | 
|---|