| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/engine/get_commit_ids_command.h" | 5 #include "chrome/browser/sync/engine/get_commit_ids_command.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 &all_unsynced_handles); | 37 &all_unsynced_handles); |
| 38 | 38 |
| 39 Cryptographer* cryptographer = | 39 Cryptographer* cryptographer = |
| 40 session->context()->directory_manager()->GetCryptographer( | 40 session->context()->directory_manager()->GetCryptographer( |
| 41 session->write_transaction()); | 41 session->write_transaction()); |
| 42 if (cryptographer) { | 42 if (cryptographer) { |
| 43 encrypted_types_ = cryptographer->GetEncryptedTypes(); | 43 encrypted_types_ = cryptographer->GetEncryptedTypes(); |
| 44 passphrase_missing_ = cryptographer->has_pending_keys(); | 44 passphrase_missing_ = cryptographer->has_pending_keys(); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 const syncable::ModelTypeSet& throttled_types = | 47 const syncable::ModelEnumSet throttled_types = |
| 48 session->context()->GetThrottledTypes(); | 48 session->context()->GetThrottledTypes(); |
| 49 // We filter out all unready entries from the set of unsynced handles to | 49 // We filter out all unready entries from the set of unsynced handles to |
| 50 // ensure we don't trigger useless sync cycles attempting to retry due to | 50 // ensure we don't trigger useless sync cycles attempting to retry due to |
| 51 // there being work to do. (see ScheduleNextSync in sync_scheduler) | 51 // there being work to do. (see ScheduleNextSync in sync_scheduler) |
| 52 FilterUnreadyEntries(session->write_transaction(), | 52 FilterUnreadyEntries(session->write_transaction(), |
| 53 throttled_types, | 53 throttled_types, |
| 54 &all_unsynced_handles); | 54 &all_unsynced_handles); |
| 55 | 55 |
| 56 StatusController* status = session->mutable_status_controller(); | 56 StatusController* status = session->mutable_status_controller(); |
| 57 status->set_unsynced_handles(all_unsynced_handles); | 57 status->set_unsynced_handles(all_unsynced_handles); |
| 58 BuildCommitIds(status->unsynced_handles(), session->write_transaction(), | 58 BuildCommitIds(status->unsynced_handles(), session->write_transaction(), |
| 59 session->routing_info(), throttled_types); | 59 session->routing_info(), throttled_types); |
| 60 | 60 |
| 61 const vector<syncable::Id>& verified_commit_ids = | 61 const vector<syncable::Id>& verified_commit_ids = |
| 62 ordered_commit_set_->GetAllCommitIds(); | 62 ordered_commit_set_->GetAllCommitIds(); |
| 63 | 63 |
| 64 for (size_t i = 0; i < verified_commit_ids.size(); i++) | 64 for (size_t i = 0; i < verified_commit_ids.size(); i++) |
| 65 DVLOG(1) << "Debug commit batch result:" << verified_commit_ids[i]; | 65 DVLOG(1) << "Debug commit batch result:" << verified_commit_ids[i]; |
| 66 | 66 |
| 67 status->set_commit_set(*ordered_commit_set_.get()); | 67 status->set_commit_set(*ordered_commit_set_.get()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 namespace { | 70 namespace { |
| 71 | 71 |
| 72 // An entry ready for commit is defined as: | 72 // An entry ready for commit is defined as: |
| 73 // 1. Not in conflict (SERVER_VERSION == BASE_VERSION || SERVER_VERSION == 0) | 73 // 1. Not in conflict (SERVER_VERSION == BASE_VERSION || SERVER_VERSION == 0) |
| 74 // and not requiring encryption (any entry containing an encrypted datatype | 74 // and not requiring encryption (any entry containing an encrypted datatype |
| 75 // while the cryptographer requires a passphrase is not ready for commit.) | 75 // while the cryptographer requires a passphrase is not ready for commit.) |
| 76 // 2. Its type is not currently throttled. | 76 // 2. Its type is not currently throttled. |
| 77 bool IsEntryReadyForCommit(const syncable::ModelTypeSet& encrypted_types, | 77 bool IsEntryReadyForCommit(syncable::ModelEnumSet encrypted_types, |
| 78 bool passphrase_missing, | 78 bool passphrase_missing, |
| 79 const syncable::Entry& entry, | 79 const syncable::Entry& entry, |
| 80 const syncable::ModelTypeSet& throttled_types) { | 80 syncable::ModelEnumSet throttled_types) { |
| 81 if (!entry.Get(syncable::IS_UNSYNCED)) | 81 if (!entry.Get(syncable::IS_UNSYNCED)) |
| 82 return false; | 82 return false; |
| 83 | 83 |
| 84 if (entry.Get(syncable::SERVER_VERSION) > 0 && | 84 if (entry.Get(syncable::SERVER_VERSION) > 0 && |
| 85 (entry.Get(syncable::SERVER_VERSION) > | 85 (entry.Get(syncable::SERVER_VERSION) > |
| 86 entry.Get(syncable::BASE_VERSION))) { | 86 entry.Get(syncable::BASE_VERSION))) { |
| 87 // The local and server versions don't match. The item must be in | 87 // The local and server versions don't match. The item must be in |
| 88 // conflict, so there's no point in attempting to commit. | 88 // conflict, so there's no point in attempting to commit. |
| 89 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); // In conflict. | 89 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); // In conflict. |
| 90 // TODO(zea): switch this to DVLOG once it's clear bug 100660 is fixed. | 90 // TODO(zea): switch this to DVLOG once it's clear bug 100660 is fixed. |
| 91 DVLOG(1) << "Excluding entry from commit due to version mismatch " | 91 DVLOG(1) << "Excluding entry from commit due to version mismatch " |
| 92 << entry; | 92 << entry; |
| 93 return false; | 93 return false; |
| 94 } | 94 } |
| 95 | 95 |
| 96 syncable::ModelType type = entry.GetModelType(); | 96 const syncable::ModelType type = entry.GetModelType(); |
| 97 // We special case the nigori node because even though it is considered an | 97 // We special case the nigori node because even though it is considered an |
| 98 // "encrypted type", not all nigori node changes require valid encryption | 98 // "encrypted type", not all nigori node changes require valid encryption |
| 99 // (ex: sync_tabs). | 99 // (ex: sync_tabs). |
| 100 if (type != syncable::NIGORI && | 100 if (syncable::IsRealDataType(type) && |
| 101 encrypted_types.count(type) > 0 && | 101 (type != syncable::NIGORI) && |
| 102 encrypted_types.Has(type) && |
| 102 (passphrase_missing || | 103 (passphrase_missing || |
| 103 syncable::EntryNeedsEncryption(encrypted_types, entry))) { | 104 syncable::EntryNeedsEncryption(encrypted_types, entry))) { |
| 104 // This entry requires encryption but is not properly encrypted (possibly | 105 // This entry requires encryption but is not properly encrypted (possibly |
| 105 // due to the cryptographer not being initialized or the user hasn't | 106 // due to the cryptographer not being initialized or the user hasn't |
| 106 // provided the most recent passphrase). | 107 // provided the most recent passphrase). |
| 107 // TODO(zea): switch this to DVLOG once it's clear bug 100660 is fixed. | 108 // TODO(zea): switch this to DVLOG once it's clear bug 100660 is fixed. |
| 108 DVLOG(1) << "Excluding entry from commit due to lack of encryption " | 109 DVLOG(1) << "Excluding entry from commit due to lack of encryption " |
| 109 << entry; | 110 << entry; |
| 110 return false; | 111 return false; |
| 111 } | 112 } |
| 112 | 113 |
| 113 // Look at the throttled types. | 114 // Look at the throttled types. |
| 114 if (throttled_types.count(type) > 0) | 115 if (syncable::IsRealDataType(type) && throttled_types.Has(type)) |
| 115 return false; | 116 return false; |
| 116 | 117 |
| 117 return true; | 118 return true; |
| 118 } | 119 } |
| 119 | 120 |
| 120 } // namespace | 121 } // namespace |
| 121 | 122 |
| 122 void GetCommitIdsCommand::FilterUnreadyEntries( | 123 void GetCommitIdsCommand::FilterUnreadyEntries( |
| 123 syncable::BaseTransaction* trans, | 124 syncable::BaseTransaction* trans, |
| 124 const syncable::ModelTypeSet& throttled_types, | 125 syncable::ModelEnumSet throttled_types, |
| 125 syncable::Directory::UnsyncedMetaHandles* unsynced_handles) { | 126 syncable::Directory::UnsyncedMetaHandles* unsynced_handles) { |
| 126 syncable::Directory::UnsyncedMetaHandles::iterator iter; | 127 syncable::Directory::UnsyncedMetaHandles::iterator iter; |
| 127 syncable::Directory::UnsyncedMetaHandles new_unsynced_handles; | 128 syncable::Directory::UnsyncedMetaHandles new_unsynced_handles; |
| 128 new_unsynced_handles.reserve(unsynced_handles->size()); | 129 new_unsynced_handles.reserve(unsynced_handles->size()); |
| 129 for (iter = unsynced_handles->begin(); | 130 for (iter = unsynced_handles->begin(); |
| 130 iter != unsynced_handles->end(); | 131 iter != unsynced_handles->end(); |
| 131 ++iter) { | 132 ++iter) { |
| 132 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); | 133 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); |
| 133 if (IsEntryReadyForCommit(encrypted_types_, | 134 if (IsEntryReadyForCommit(encrypted_types_, |
| 134 passphrase_missing_, | 135 passphrase_missing_, |
| 135 entry, | 136 entry, |
| 136 throttled_types)) | 137 throttled_types)) |
| 137 new_unsynced_handles.push_back(*iter); | 138 new_unsynced_handles.push_back(*iter); |
| 138 } | 139 } |
| 139 if (new_unsynced_handles.size() != unsynced_handles->size()) | 140 if (new_unsynced_handles.size() != unsynced_handles->size()) |
| 140 unsynced_handles->swap(new_unsynced_handles); | 141 unsynced_handles->swap(new_unsynced_handles); |
| 141 } | 142 } |
| 142 | 143 |
| 143 void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( | 144 void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( |
| 144 syncable::BaseTransaction* trans, | 145 syncable::BaseTransaction* trans, |
| 145 syncable::Id parent_id, | 146 syncable::Id parent_id, |
| 146 const ModelSafeRoutingInfo& routes, | 147 const ModelSafeRoutingInfo& routes, |
| 147 const syncable::ModelTypeSet& throttled_types) { | 148 syncable::ModelEnumSet throttled_types) { |
| 148 OrderedCommitSet item_dependencies(routes); | 149 OrderedCommitSet item_dependencies(routes); |
| 149 | 150 |
| 150 // Climb the tree adding entries leaf -> root. | 151 // Climb the tree adding entries leaf -> root. |
| 151 while (!parent_id.ServerKnows()) { | 152 while (!parent_id.ServerKnows()) { |
| 152 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | 153 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); |
| 153 CHECK(parent.good()) << "Bad user-only parent in item path."; | 154 CHECK(parent.good()) << "Bad user-only parent in item path."; |
| 154 int64 handle = parent.Get(syncable::META_HANDLE); | 155 int64 handle = parent.Get(syncable::META_HANDLE); |
| 155 if (ordered_commit_set_->HaveCommitItem(handle) || | 156 if (ordered_commit_set_->HaveCommitItem(handle) || |
| 156 item_dependencies.HaveCommitItem(handle)) { | 157 item_dependencies.HaveCommitItem(handle)) { |
| 157 break; | 158 break; |
| 158 } | 159 } |
| 159 if (!AddItemThenPredecessors(trans, throttled_types, &parent, | 160 if (!AddItemThenPredecessors(trans, throttled_types, &parent, |
| 160 syncable::IS_UNSYNCED, | 161 syncable::IS_UNSYNCED, |
| 161 &item_dependencies)) { | 162 &item_dependencies)) { |
| 162 break; // Parent was already present in the set. | 163 break; // Parent was already present in the set. |
| 163 } | 164 } |
| 164 parent_id = parent.Get(syncable::PARENT_ID); | 165 parent_id = parent.Get(syncable::PARENT_ID); |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Reverse what we added to get the correct order. | 168 // Reverse what we added to get the correct order. |
| 168 ordered_commit_set_->AppendReverse(item_dependencies); | 169 ordered_commit_set_->AppendReverse(item_dependencies); |
| 169 } | 170 } |
| 170 | 171 |
| 171 bool GetCommitIdsCommand::AddItem(syncable::Entry* item, | 172 bool GetCommitIdsCommand::AddItem(syncable::Entry* item, |
| 172 const syncable::ModelTypeSet& throttled_types, | 173 syncable::ModelEnumSet throttled_types, |
| 173 OrderedCommitSet* result) { | 174 OrderedCommitSet* result) { |
| 174 if (!IsEntryReadyForCommit(encrypted_types_, passphrase_missing_, *item, | 175 if (!IsEntryReadyForCommit(encrypted_types_, passphrase_missing_, *item, |
| 175 throttled_types)) | 176 throttled_types)) |
| 176 return false; | 177 return false; |
| 177 int64 item_handle = item->Get(syncable::META_HANDLE); | 178 int64 item_handle = item->Get(syncable::META_HANDLE); |
| 178 if (result->HaveCommitItem(item_handle) || | 179 if (result->HaveCommitItem(item_handle) || |
| 179 ordered_commit_set_->HaveCommitItem(item_handle)) { | 180 ordered_commit_set_->HaveCommitItem(item_handle)) { |
| 180 return false; | 181 return false; |
| 181 } | 182 } |
| 182 result->AddCommitItem(item_handle, item->Get(syncable::ID), | 183 result->AddCommitItem(item_handle, item->Get(syncable::ID), |
| 183 item->GetModelType()); | 184 item->GetModelType()); |
| 184 return true; | 185 return true; |
| 185 } | 186 } |
| 186 | 187 |
| 187 bool GetCommitIdsCommand::AddItemThenPredecessors( | 188 bool GetCommitIdsCommand::AddItemThenPredecessors( |
| 188 syncable::BaseTransaction* trans, | 189 syncable::BaseTransaction* trans, |
| 189 const syncable::ModelTypeSet& throttled_types, | 190 syncable::ModelEnumSet throttled_types, |
| 190 syncable::Entry* item, | 191 syncable::Entry* item, |
| 191 syncable::IndexedBitField inclusion_filter, | 192 syncable::IndexedBitField inclusion_filter, |
| 192 OrderedCommitSet* result) { | 193 OrderedCommitSet* result) { |
| 193 if (!AddItem(item, throttled_types, result)) | 194 if (!AddItem(item, throttled_types, result)) |
| 194 return false; | 195 return false; |
| 195 if (item->Get(syncable::IS_DEL)) | 196 if (item->Get(syncable::IS_DEL)) |
| 196 return true; // Deleted items have no predecessors. | 197 return true; // Deleted items have no predecessors. |
| 197 | 198 |
| 198 syncable::Id prev_id = item->Get(syncable::PREV_ID); | 199 syncable::Id prev_id = item->Get(syncable::PREV_ID); |
| 199 while (!prev_id.IsRoot()) { | 200 while (!prev_id.IsRoot()) { |
| 200 syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id); | 201 syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id); |
| 201 CHECK(prev.good()) << "Bad id when walking predecessors."; | 202 CHECK(prev.good()) << "Bad id when walking predecessors."; |
| 202 if (!prev.Get(inclusion_filter)) | 203 if (!prev.Get(inclusion_filter)) |
| 203 break; | 204 break; |
| 204 if (!AddItem(&prev, throttled_types, result)) | 205 if (!AddItem(&prev, throttled_types, result)) |
| 205 break; | 206 break; |
| 206 prev_id = prev.Get(syncable::PREV_ID); | 207 prev_id = prev.Get(syncable::PREV_ID); |
| 207 } | 208 } |
| 208 return true; | 209 return true; |
| 209 } | 210 } |
| 210 | 211 |
| 211 void GetCommitIdsCommand::AddPredecessorsThenItem( | 212 void GetCommitIdsCommand::AddPredecessorsThenItem( |
| 212 syncable::BaseTransaction* trans, | 213 syncable::BaseTransaction* trans, |
| 213 const syncable::ModelTypeSet& throttled_types, | 214 syncable::ModelEnumSet throttled_types, |
| 214 syncable::Entry* item, | 215 syncable::Entry* item, |
| 215 syncable::IndexedBitField inclusion_filter, | 216 syncable::IndexedBitField inclusion_filter, |
| 216 const ModelSafeRoutingInfo& routes) { | 217 const ModelSafeRoutingInfo& routes) { |
| 217 OrderedCommitSet item_dependencies(routes); | 218 OrderedCommitSet item_dependencies(routes); |
| 218 AddItemThenPredecessors(trans, throttled_types, item, inclusion_filter, | 219 AddItemThenPredecessors(trans, throttled_types, item, inclusion_filter, |
| 219 &item_dependencies); | 220 &item_dependencies); |
| 220 | 221 |
| 221 // Reverse what we added to get the correct order. | 222 // Reverse what we added to get the correct order. |
| 222 ordered_commit_set_->AppendReverse(item_dependencies); | 223 ordered_commit_set_->AppendReverse(item_dependencies); |
| 223 } | 224 } |
| 224 | 225 |
| 225 bool GetCommitIdsCommand::IsCommitBatchFull() { | 226 bool GetCommitIdsCommand::IsCommitBatchFull() { |
| 226 return ordered_commit_set_->Size() >= requested_commit_batch_size_; | 227 return ordered_commit_set_->Size() >= requested_commit_batch_size_; |
| 227 } | 228 } |
| 228 | 229 |
| 229 void GetCommitIdsCommand::AddCreatesAndMoves( | 230 void GetCommitIdsCommand::AddCreatesAndMoves( |
| 230 const vector<int64>& unsynced_handles, | 231 const vector<int64>& unsynced_handles, |
| 231 syncable::WriteTransaction* write_transaction, | 232 syncable::WriteTransaction* write_transaction, |
| 232 const ModelSafeRoutingInfo& routes, | 233 const ModelSafeRoutingInfo& routes, |
| 233 const syncable::ModelTypeSet& throttled_types) { | 234 syncable::ModelEnumSet throttled_types) { |
| 234 // Add moves and creates, and prepend their uncommitted parents. | 235 // Add moves and creates, and prepend their uncommitted parents. |
| 235 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, | 236 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, |
| 236 ordered_commit_set_.get()); | 237 ordered_commit_set_.get()); |
| 237 !IsCommitBatchFull() && iterator.Valid(); | 238 !IsCommitBatchFull() && iterator.Valid(); |
| 238 iterator.Increment()) { | 239 iterator.Increment()) { |
| 239 int64 metahandle = iterator.Current(); | 240 int64 metahandle = iterator.Current(); |
| 240 | 241 |
| 241 syncable::Entry entry(write_transaction, | 242 syncable::Entry entry(write_transaction, |
| 242 syncable::GET_BY_HANDLE, | 243 syncable::GET_BY_HANDLE, |
| 243 metahandle); | 244 metahandle); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 ordered_commit_set_->AddCommitItem(metahandle, entry.Get(syncable::ID), | 335 ordered_commit_set_->AddCommitItem(metahandle, entry.Get(syncable::ID), |
| 335 entry.GetModelType()); | 336 entry.GetModelType()); |
| 336 } | 337 } |
| 337 } | 338 } |
| 338 } | 339 } |
| 339 } | 340 } |
| 340 | 341 |
| 341 void GetCommitIdsCommand::BuildCommitIds(const vector<int64>& unsynced_handles, | 342 void GetCommitIdsCommand::BuildCommitIds(const vector<int64>& unsynced_handles, |
| 342 syncable::WriteTransaction* write_transaction, | 343 syncable::WriteTransaction* write_transaction, |
| 343 const ModelSafeRoutingInfo& routes, | 344 const ModelSafeRoutingInfo& routes, |
| 344 const syncable::ModelTypeSet& throttled_types) { | 345 syncable::ModelEnumSet throttled_types) { |
| 345 ordered_commit_set_.reset(new OrderedCommitSet(routes)); | 346 ordered_commit_set_.reset(new OrderedCommitSet(routes)); |
| 346 // Commits follow these rules: | 347 // Commits follow these rules: |
| 347 // 1. Moves or creates are preceded by needed folder creates, from | 348 // 1. Moves or creates are preceded by needed folder creates, from |
| 348 // root to leaf. For folders whose contents are ordered, moves | 349 // root to leaf. For folders whose contents are ordered, moves |
| 349 // and creates appear in order. | 350 // and creates appear in order. |
| 350 // 2. Moves/Creates before deletes. | 351 // 2. Moves/Creates before deletes. |
| 351 // 3. Deletes, collapsed. | 352 // 3. Deletes, collapsed. |
| 352 // We commit deleted moves under deleted items as moves when collapsing | 353 // We commit deleted moves under deleted items as moves when collapsing |
| 353 // delete trees. | 354 // delete trees. |
| 354 | 355 |
| 355 // Add moves and creates, and prepend their uncommitted parents. | 356 // Add moves and creates, and prepend their uncommitted parents. |
| 356 AddCreatesAndMoves(unsynced_handles, write_transaction, routes, | 357 AddCreatesAndMoves(unsynced_handles, write_transaction, routes, |
| 357 throttled_types); | 358 throttled_types); |
| 358 | 359 |
| 359 // Add all deletes. | 360 // Add all deletes. |
| 360 AddDeletes(unsynced_handles, write_transaction); | 361 AddDeletes(unsynced_handles, write_transaction); |
| 361 } | 362 } |
| 362 | 363 |
| 363 } // namespace browser_sync | 364 } // namespace browser_sync |
| OLD | NEW |