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