Home | History | Annotate | Download | only in engine
      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/engine/process_updates_util.h"
      6 
      7 #include "base/location.h"
      8 #include "sync/engine/syncer_proto_util.h"
      9 #include "sync/engine/syncer_util.h"
     10 #include "sync/syncable/directory.h"
     11 #include "sync/syncable/model_neutral_mutable_entry.h"
     12 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
     13 #include "sync/syncable/syncable_proto_util.h"
     14 #include "sync/syncable/syncable_util.h"
     15 #include "sync/util/cryptographer.h"
     16 
     17 namespace syncer {
     18 
     19 using sessions::StatusController;
     20 
     21 using syncable::GET_BY_ID;
     22 
     23 namespace {
     24 
     25 // This function attempts to determine whether or not this update is genuinely
     26 // new, or if it is a reflection of one of our own commits.
     27 //
     28 // There is a known inaccuracy in its implementation.  If this update ends up
     29 // being applied to a local item with a different ID, we will count the change
     30 // as being a non-reflection update.  Fortunately, the server usually updates
     31 // our IDs correctly in its commit response, so a new ID during GetUpdate should
     32 // be rare.
     33 //
     34 // The only secnarios I can think of where this might happen are:
     35 // - We commit a  new item to the server, but we don't persist the
     36 // server-returned new ID to the database before we shut down.  On the GetUpdate
     37 // following the next restart, we will receive an update from the server that
     38 // updates its local ID.
     39 // - When two attempts to create an item with identical UNIQUE_CLIENT_TAG values
     40 // collide at the server.  I have seen this in testing.  When it happens, the
     41 // test server will send one of the clients a response to upate its local ID so
     42 // that both clients will refer to the item using the same ID going forward.  In
     43 // this case, we're right to assume that the update is not a reflection.
     44 //
     45 // For more information, see FindLocalIdToUpdate().
     46 bool UpdateContainsNewVersion(syncable::BaseTransaction *trans,
     47                               const sync_pb::SyncEntity &update) {
     48   int64 existing_version = -1; // The server always sends positive versions.
     49   syncable::Entry existing_entry(trans, GET_BY_ID,
     50                                  SyncableIdFromProto(update.id_string()));
     51   if (existing_entry.good())
     52     existing_version = existing_entry.GetBaseVersion();
     53 
     54   if (!existing_entry.good() && update.deleted()) {
     55     // There are several possible explanations for this.  The most common cases
     56     // will be first time sync and the redelivery of deletions we've already
     57     // synced, accepted, and purged from our database.  In either case, the
     58     // update is useless to us.  Let's count them all as "not new", even though
     59     // that may not always be entirely accurate.
     60     return false;
     61   }
     62 
     63   if (existing_entry.good() &&
     64       !existing_entry.GetUniqueClientTag().empty() &&
     65       existing_entry.GetIsDel() &&
     66       update.deleted()) {
     67     // Unique client tags will have their version set to zero when they're
     68     // deleted.  The usual version comparison logic won't be able to detect
     69     // reflections of these items.  Instead, we assume any received tombstones
     70     // are reflections.  That should be correct most of the time.
     71     return false;
     72   }
     73 
     74   return existing_version < update.version();
     75 }
     76 
     77 }  // namespace
     78 
     79 void PartitionUpdatesByType(
     80     const sync_pb::GetUpdatesResponse& updates,
     81     ModelTypeSet requested_types,
     82     TypeSyncEntityMap* updates_by_type) {
     83   int update_count = updates.entries().size();
     84   for (ModelTypeSet::Iterator it = requested_types.First();
     85        it.Good(); it.Inc()) {
     86     updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList()));
     87   }
     88   for (int i = 0; i < update_count; ++i) {
     89     const sync_pb::SyncEntity& update = updates.entries(i);
     90     ModelType type = GetModelType(update);
     91     if (!IsRealDataType(type)) {
     92       NOTREACHED() << "Received update with invalid type.";
     93       continue;
     94     }
     95 
     96     TypeSyncEntityMap::iterator it = updates_by_type->find(type);
     97     if (it == updates_by_type->end()) {
     98       DLOG(WARNING) << "Skipping update for unexpected type "
     99                     << ModelTypeToString(type);
    100       continue;
    101     }
    102 
    103     it->second.push_back(&update);
    104   }
    105 }
    106 
    107 void ProcessDownloadedUpdates(
    108     syncable::Directory* dir,
    109     syncable::ModelNeutralWriteTransaction* trans,
    110     ModelType type,
    111     const SyncEntityList& applicable_updates,
    112     sessions::StatusController* status) {
    113   for (SyncEntityList::const_iterator update_it = applicable_updates.begin();
    114        update_it != applicable_updates.end(); ++update_it) {
    115     DCHECK_EQ(type, GetModelType(**update_it));
    116     if (!UpdateContainsNewVersion(trans, **update_it))
    117       status->increment_num_reflected_updates_downloaded_by(1);
    118     if ((*update_it)->deleted())
    119       status->increment_num_tombstone_updates_downloaded_by(1);
    120     VerifyResult verify_result = VerifyUpdate(trans, **update_it, type);
    121     if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE)
    122       continue;
    123     ProcessUpdate(**update_it, dir->GetCryptographer(trans), trans);
    124   }
    125 }
    126 
    127 namespace {
    128 
    129 // In the event that IDs match, but tags differ AttemptReuniteClient tag
    130 // will have refused to unify the update.
    131 // We should not attempt to apply it at all since it violates consistency
    132 // rules.
    133 VerifyResult VerifyTagConsistency(
    134     const sync_pb::SyncEntity& entry,
    135     const syncable::ModelNeutralMutableEntry& same_id) {
    136   if (entry.has_client_defined_unique_tag() &&
    137       entry.client_defined_unique_tag() !=
    138           same_id.GetUniqueClientTag()) {
    139     return VERIFY_FAIL;
    140   }
    141   return VERIFY_UNDECIDED;
    142 }
    143 
    144 }  // namespace
    145 
    146 VerifyResult VerifyUpdate(
    147     syncable::ModelNeutralWriteTransaction* trans,
    148     const sync_pb::SyncEntity& entry,
    149     ModelType requested_type) {
    150   syncable::Id id = SyncableIdFromProto(entry.id_string());
    151   VerifyResult result = VERIFY_FAIL;
    152 
    153   const bool deleted = entry.has_deleted() && entry.deleted();
    154   const bool is_directory = IsFolder(entry);
    155   const ModelType model_type = GetModelType(entry);
    156 
    157   if (!id.ServerKnows()) {
    158     LOG(ERROR) << "Illegal negative id in received updates";
    159     return result;
    160   }
    161   {
    162     const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry);
    163     if (name.empty() && !deleted) {
    164       LOG(ERROR) << "Zero length name in non-deleted update";
    165       return result;
    166     }
    167   }
    168 
    169   syncable::ModelNeutralMutableEntry same_id(trans, GET_BY_ID, id);
    170   result = VerifyNewEntry(entry, &same_id, deleted);
    171 
    172   ModelType placement_type = !deleted ? GetModelType(entry)
    173       : same_id.good() ? same_id.GetModelType() : UNSPECIFIED;
    174 
    175   if (VERIFY_UNDECIDED == result) {
    176     result = VerifyTagConsistency(entry, same_id);
    177   }
    178 
    179   if (VERIFY_UNDECIDED == result) {
    180     if (deleted) {
    181       // For deletes the server could send tombostones for items that
    182       // the client did not request. If so ignore those items.
    183       if (IsRealDataType(placement_type) && requested_type != placement_type) {
    184         result = VERIFY_SKIP;
    185       } else {
    186         result = VERIFY_SUCCESS;
    187       }
    188     }
    189   }
    190 
    191   // If we have an existing entry, we check here for updates that break
    192   // consistency rules.
    193   if (VERIFY_UNDECIDED == result) {
    194     result = VerifyUpdateConsistency(trans, entry, deleted,
    195                                      is_directory, model_type, &same_id);
    196   }
    197 
    198   if (VERIFY_UNDECIDED == result)
    199     result = VERIFY_SUCCESS;  // No news is good news.
    200 
    201   return result;  // This might be VERIFY_SUCCESS as well
    202 }
    203 
    204 namespace {
    205 // Returns true if the entry is still ok to process.
    206 bool ReverifyEntry(syncable::ModelNeutralWriteTransaction* trans,
    207                    const sync_pb::SyncEntity& entry,
    208                    syncable::ModelNeutralMutableEntry* same_id) {
    209 
    210   const bool deleted = entry.has_deleted() && entry.deleted();
    211   const bool is_directory = IsFolder(entry);
    212   const ModelType model_type = GetModelType(entry);
    213 
    214   return VERIFY_SUCCESS == VerifyUpdateConsistency(trans,
    215                                                    entry,
    216                                                    deleted,
    217                                                    is_directory,
    218                                                    model_type,
    219                                                    same_id);
    220 }
    221 }  // namespace
    222 
    223 // Process a single update. Will avoid touching global state.
    224 void ProcessUpdate(
    225     const sync_pb::SyncEntity& update,
    226     const Cryptographer* cryptographer,
    227     syncable::ModelNeutralWriteTransaction* const trans) {
    228   const syncable::Id& server_id = SyncableIdFromProto(update.id_string());
    229   const std::string name = SyncerProtoUtil::NameFromSyncEntity(update);
    230 
    231   // Look to see if there's a local item that should recieve this update,
    232   // maybe due to a duplicate client tag or a lost commit response.
    233   syncable::Id local_id = FindLocalIdToUpdate(trans, update);
    234 
    235   // FindLocalEntryToUpdate has veto power.
    236   if (local_id.IsNull()) {
    237     return;  // The entry has become irrelevant.
    238   }
    239 
    240   CreateNewEntry(trans, local_id);
    241 
    242   // We take a two step approach. First we store the entries data in the
    243   // server fields of a local entry and then move the data to the local fields
    244   syncable::ModelNeutralMutableEntry target_entry(trans, GET_BY_ID, local_id);
    245 
    246   // We need to run the Verify checks again; the world could have changed
    247   // since we last verified.
    248   if (!ReverifyEntry(trans, update, &target_entry)) {
    249     return;  // The entry has become irrelevant.
    250   }
    251 
    252   // If we're repurposing an existing local entry with a new server ID,
    253   // change the ID now, after we're sure that the update can succeed.
    254   if (local_id != server_id) {
    255     DCHECK(!update.deleted());
    256     ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id);
    257     // When IDs change, versions become irrelevant.  Forcing BASE_VERSION
    258     // to zero would ensure that this update gets applied, but would indicate
    259     // creation or undeletion if it were committed that way.  Instead, prefer
    260     // forcing BASE_VERSION to entry.version() while also forcing
    261     // IS_UNAPPLIED_UPDATE to true.  If the item is UNSYNCED, it's committable
    262     // from the new state; it may commit before the conflict resolver gets
    263     // a crack at it.
    264     if (target_entry.GetIsUnsynced() || target_entry.GetBaseVersion() > 0) {
    265       // If either of these conditions are met, then we can expect valid client
    266       // fields for this entry.  When BASE_VERSION is positive, consistency is
    267       // enforced on the client fields at update-application time.  Otherwise,
    268       // we leave the BASE_VERSION field alone; it'll get updated the first time
    269       // we successfully apply this update.
    270       target_entry.PutBaseVersion(update.version());
    271     }
    272     // Force application of this update, no matter what.
    273     target_entry.PutIsUnappliedUpdate(true);
    274   }
    275 
    276   // If this is a newly received undecryptable update, and the only thing that
    277   // has changed are the specifics, store the original decryptable specifics,
    278   // (on which any current or future local changes are based) before we
    279   // overwrite SERVER_SPECIFICS.
    280   // MTIME, CTIME, and NON_UNIQUE_NAME are not enforced.
    281 
    282   bool position_matches = false;
    283   if (target_entry.ShouldMaintainPosition() && !update.deleted()) {
    284     std::string update_tag = GetUniqueBookmarkTagFromUpdate(update);
    285     if (UniquePosition::IsValidSuffix(update_tag)) {
    286       position_matches = GetUpdatePosition(update, update_tag).Equals(
    287           target_entry.GetServerUniquePosition());
    288     } else {
    289       NOTREACHED();
    290     }
    291   } else {
    292     // If this item doesn't care about positions, then set this flag to true.
    293     position_matches = true;
    294   }
    295 
    296   if (!update.deleted() && !target_entry.GetServerIsDel() &&
    297       (SyncableIdFromProto(update.parent_id_string()) ==
    298           target_entry.GetServerParentId()) &&
    299       position_matches &&
    300       update.has_specifics() && update.specifics().has_encrypted() &&
    301       !cryptographer->CanDecrypt(update.specifics().encrypted())) {
    302     sync_pb::EntitySpecifics prev_specifics =
    303         target_entry.GetServerSpecifics();
    304     // We only store the old specifics if they were decryptable and applied and
    305     // there is no BASE_SERVER_SPECIFICS already. Else do nothing.
    306     if (!target_entry.GetIsUnappliedUpdate() &&
    307         !IsRealDataType(GetModelTypeFromSpecifics(
    308             target_entry.GetBaseServerSpecifics())) &&
    309         (!prev_specifics.has_encrypted() ||
    310          cryptographer->CanDecrypt(prev_specifics.encrypted()))) {
    311       DVLOG(2) << "Storing previous server specifcs: "
    312                << prev_specifics.SerializeAsString();
    313       target_entry.PutBaseServerSpecifics(prev_specifics);
    314     }
    315   } else if (IsRealDataType(GetModelTypeFromSpecifics(
    316                  target_entry.GetBaseServerSpecifics()))) {
    317     // We have a BASE_SERVER_SPECIFICS, but a subsequent non-specifics-only
    318     // change arrived. As a result, we can't use the specifics alone to detect
    319     // changes, so we clear BASE_SERVER_SPECIFICS.
    320     target_entry.PutBaseServerSpecifics(
    321                      sync_pb::EntitySpecifics());
    322   }
    323 
    324   UpdateServerFieldsFromUpdate(&target_entry, update, name);
    325 
    326   return;
    327 }
    328 
    329 }  // namespace syncer
    330