| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/engine/build_and_process_conflict_sets_command.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 #include <sstream> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/format_macros.h" | |
| 14 #include "base/location.h" | |
| 15 #include "chrome/browser/sync/engine/syncer_util.h" | |
| 16 #include "chrome/browser/sync/engine/update_applicator.h" | |
| 17 #include "chrome/browser/sync/sessions/sync_session.h" | |
| 18 #include "chrome/browser/sync/syncable/directory_manager.h" | |
| 19 | |
| 20 namespace browser_sync { | |
| 21 | |
| 22 using sessions::ConflictProgress; | |
| 23 using sessions::StatusController; | |
| 24 using sessions::SyncSession; | |
| 25 using sessions::UpdateProgress; | |
| 26 using std::set; | |
| 27 using std::string; | |
| 28 using std::vector; | |
| 29 | |
| 30 BuildAndProcessConflictSetsCommand::BuildAndProcessConflictSetsCommand() {} | |
| 31 BuildAndProcessConflictSetsCommand::~BuildAndProcessConflictSetsCommand() {} | |
| 32 | |
| 33 std::set<ModelSafeGroup> BuildAndProcessConflictSetsCommand::GetGroupsToChange( | |
| 34 const sessions::SyncSession& session) const { | |
| 35 return session.GetEnabledGroupsWithConflicts(); | |
| 36 } | |
| 37 | |
| 38 SyncerError BuildAndProcessConflictSetsCommand::ModelChangingExecuteImpl( | |
| 39 SyncSession* session) { | |
| 40 syncable::ScopedDirLookup dir(session->context()->directory_manager(), | |
| 41 session->context()->account_name()); | |
| 42 if (!dir.good()) | |
| 43 return DIRECTORY_LOOKUP_FAILED; | |
| 44 | |
| 45 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); | |
| 46 BuildConflictSets(&trans, | |
| 47 session->mutable_status_controller()->mutable_conflict_progress()); | |
| 48 | |
| 49 return SYNCER_OK; | |
| 50 } | |
| 51 | |
| 52 void BuildAndProcessConflictSetsCommand::BuildConflictSets( | |
| 53 syncable::BaseTransaction* trans, | |
| 54 ConflictProgress* conflict_progress) { | |
| 55 conflict_progress->CleanupSets(); | |
| 56 set<syncable::Id>::const_iterator i = | |
| 57 conflict_progress->ConflictingItemsBegin(); | |
| 58 while (i != conflict_progress->ConflictingItemsEnd()) { | |
| 59 syncable::Entry entry(trans, syncable::GET_BY_ID, *i); | |
| 60 if (!entry.good() || | |
| 61 (!entry.Get(syncable::IS_UNSYNCED) && | |
| 62 !entry.Get(syncable::IS_UNAPPLIED_UPDATE))) { | |
| 63 // This can happen very rarely. It means we had a simply conflicting item | |
| 64 // that randomly committed; its ID could have changed during the commit. | |
| 65 // We drop the entry as it's no longer conflicting. | |
| 66 conflict_progress->EraseConflictingItemById(*(i++)); | |
| 67 continue; | |
| 68 } | |
| 69 if (entry.ExistsOnClientBecauseNameIsNonEmpty() && | |
| 70 (entry.Get(syncable::IS_DEL) || entry.Get(syncable::SERVER_IS_DEL))) { | |
| 71 // If we're deleted on client or server we can't be in a complex set. | |
| 72 ++i; | |
| 73 continue; | |
| 74 } | |
| 75 bool new_parent = | |
| 76 entry.Get(syncable::PARENT_ID) != entry.Get(syncable::SERVER_PARENT_ID); | |
| 77 if (new_parent) | |
| 78 MergeSetsForIntroducedLoops(trans, &entry, conflict_progress); | |
| 79 MergeSetsForNonEmptyDirectories(trans, &entry, conflict_progress); | |
| 80 ++i; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void BuildAndProcessConflictSetsCommand::MergeSetsForIntroducedLoops( | |
| 85 syncable::BaseTransaction* trans, syncable::Entry* entry, | |
| 86 ConflictProgress* conflict_progress) { | |
| 87 // This code crawls up from the item in question until it gets to the root | |
| 88 // or itself. If it gets to the root it does nothing. If it finds a loop all | |
| 89 // moved unsynced entries in the list of crawled entries have their sets | |
| 90 // merged with the entry. | |
| 91 // TODO(sync): Build test cases to cover this function when the argument list | |
| 92 // has settled. | |
| 93 syncable::Id parent_id = entry->Get(syncable::SERVER_PARENT_ID); | |
| 94 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | |
| 95 if (!parent.good()) { | |
| 96 return; | |
| 97 } | |
| 98 // Don't check for loop if the server parent is deleted. | |
| 99 if (parent.Get(syncable::IS_DEL)) | |
| 100 return; | |
| 101 vector<syncable::Id> conflicting_entries; | |
| 102 while (!parent_id.IsRoot()) { | |
| 103 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | |
| 104 if (!parent.good()) { | |
| 105 DVLOG(1) << "Bad parent in loop check, skipping. Bad parent id: " | |
| 106 << parent_id << " entry: " << *entry; | |
| 107 return; | |
| 108 } | |
| 109 if (parent.Get(syncable::IS_UNSYNCED) && | |
| 110 entry->Get(syncable::PARENT_ID) != | |
| 111 entry->Get(syncable::SERVER_PARENT_ID)) | |
| 112 conflicting_entries.push_back(parent_id); | |
| 113 parent_id = parent.Get(syncable::PARENT_ID); | |
| 114 if (parent_id == entry->Get(syncable::ID)) | |
| 115 break; | |
| 116 } | |
| 117 if (parent_id.IsRoot()) | |
| 118 return; | |
| 119 for (size_t i = 0; i < conflicting_entries.size(); i++) { | |
| 120 conflict_progress->MergeSets(entry->Get(syncable::ID), | |
| 121 conflicting_entries[i]); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 namespace { | |
| 126 | |
| 127 class ServerDeletedPathChecker { | |
| 128 public: | |
| 129 static bool CausingConflict(const syncable::Entry& e, | |
| 130 const syncable::Entry& log_entry) { | |
| 131 CHECK(e.good()) << "Missing parent in path of: " << log_entry; | |
| 132 if (e.Get(syncable::IS_UNAPPLIED_UPDATE) && | |
| 133 e.Get(syncable::SERVER_IS_DEL)) { | |
| 134 CHECK(!e.Get(syncable::IS_DEL)) << " Inconsistency in local tree. " | |
| 135 "syncable::Entry: " << e << " Leaf: " << log_entry; | |
| 136 return true; | |
| 137 } else { | |
| 138 CHECK(!e.Get(syncable::IS_DEL)) << " Deleted entry has children. " | |
| 139 "syncable::Entry: " << e << " Leaf: " << log_entry; | |
| 140 return false; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 // returns 0 if we should stop investigating the path. | |
| 145 static syncable::Id GetAndExamineParent(syncable::BaseTransaction* trans, | |
| 146 const syncable::Id& id, | |
| 147 const syncable::Id& check_id, | |
| 148 const syncable::Entry& log_entry) { | |
| 149 syncable::Entry parent(trans, syncable::GET_BY_ID, id); | |
| 150 CHECK(parent.good()) << "Tree inconsitency, missing id" << id << " " | |
| 151 << log_entry; | |
| 152 syncable::Id parent_id = parent.Get(syncable::PARENT_ID); | |
| 153 CHECK(parent_id != check_id) << "Loop in dir tree! " | |
| 154 << log_entry << " " << parent; | |
| 155 return parent_id; | |
| 156 } | |
| 157 }; | |
| 158 | |
| 159 class LocallyDeletedPathChecker { | |
| 160 public: | |
| 161 static bool CausingConflict(const syncable::Entry& e, | |
| 162 const syncable::Entry& log_entry) { | |
| 163 return e.good() && e.Get(syncable::IS_DEL) && e.Get(syncable::IS_UNSYNCED); | |
| 164 } | |
| 165 | |
| 166 // returns 0 if we should stop investigating the path. | |
| 167 static syncable::Id GetAndExamineParent(syncable::BaseTransaction* trans, | |
| 168 const syncable::Id& id, | |
| 169 const syncable::Id& check_id, | |
| 170 const syncable::Entry& log_entry) { | |
| 171 syncable::Entry parent(trans, syncable::GET_BY_ID, id); | |
| 172 if (!parent.good()) | |
| 173 return syncable::GetNullId(); | |
| 174 syncable::Id parent_id = parent.Get(syncable::PARENT_ID); | |
| 175 if (parent_id == check_id) | |
| 176 return syncable::GetNullId(); | |
| 177 return parent_id; | |
| 178 } | |
| 179 }; | |
| 180 | |
| 181 template <typename Checker> | |
| 182 void CrawlDeletedTreeMergingSets(syncable::BaseTransaction* trans, | |
| 183 const syncable::Entry& entry, | |
| 184 ConflictProgress* conflict_progress, | |
| 185 Checker checker) { | |
| 186 syncable::Id parent_id = entry.Get(syncable::PARENT_ID); | |
| 187 syncable::Id double_step_parent_id = parent_id; | |
| 188 // This block builds sets where we've got an entry in a directory the server | |
| 189 // wants to delete. | |
| 190 // | |
| 191 // Here we're walking up the tree to find all entries that the pass checks | |
| 192 // deleted. We can be extremely strict here as anything unexpected means | |
| 193 // invariants in the local hierarchy have been broken. | |
| 194 while (!parent_id.IsRoot()) { | |
| 195 if (!double_step_parent_id.IsRoot()) { | |
| 196 // Checks to ensure we don't loop. | |
| 197 double_step_parent_id = checker.GetAndExamineParent( | |
| 198 trans, double_step_parent_id, parent_id, entry); | |
| 199 double_step_parent_id = checker.GetAndExamineParent( | |
| 200 trans, double_step_parent_id, parent_id, entry); | |
| 201 } | |
| 202 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | |
| 203 if (checker.CausingConflict(parent, entry)) { | |
| 204 conflict_progress->MergeSets(entry.Get(syncable::ID), | |
| 205 parent.Get(syncable::ID)); | |
| 206 } else { | |
| 207 break; | |
| 208 } | |
| 209 parent_id = parent.Get(syncable::PARENT_ID); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 } // namespace | |
| 214 | |
| 215 void BuildAndProcessConflictSetsCommand::MergeSetsForNonEmptyDirectories( | |
| 216 syncable::BaseTransaction* trans, syncable::Entry* entry, | |
| 217 ConflictProgress* conflict_progress) { | |
| 218 if (entry->Get(syncable::IS_UNSYNCED) && !entry->Get(syncable::IS_DEL)) { | |
| 219 ServerDeletedPathChecker checker; | |
| 220 CrawlDeletedTreeMergingSets(trans, *entry, conflict_progress, checker); | |
| 221 } | |
| 222 if (entry->Get(syncable::IS_UNAPPLIED_UPDATE) && | |
| 223 !entry->Get(syncable::SERVER_IS_DEL)) { | |
| 224 syncable::Entry parent(trans, syncable::GET_BY_ID, | |
| 225 entry->Get(syncable::SERVER_PARENT_ID)); | |
| 226 syncable::Id parent_id = entry->Get(syncable::SERVER_PARENT_ID); | |
| 227 if (!parent.good()) | |
| 228 return; | |
| 229 LocallyDeletedPathChecker checker; | |
| 230 if (!checker.CausingConflict(parent, *entry)) | |
| 231 return; | |
| 232 conflict_progress->MergeSets(entry->Get(syncable::ID), | |
| 233 parent.Get(syncable::ID)); | |
| 234 CrawlDeletedTreeMergingSets(trans, parent, conflict_progress, checker); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 } // namespace browser_sync | |
| OLD | NEW |