OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/engine/apply_control_data_updates.h" | 5 #include "components/sync/engine_impl/apply_control_data_updates.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "sync/engine/conflict_resolver.h" | 12 #include "components/sync/base/cryptographer.h" |
13 #include "sync/engine/conflict_util.h" | 13 #include "components/sync/engine_impl/conflict_resolver.h" |
14 #include "sync/engine/syncer_util.h" | 14 #include "components/sync/engine_impl/conflict_util.h" |
15 #include "sync/syncable/directory.h" | 15 #include "components/sync/engine_impl/syncer_util.h" |
16 #include "sync/syncable/mutable_entry.h" | 16 #include "components/sync/syncable/directory.h" |
17 #include "sync/syncable/nigori_handler.h" | 17 #include "components/sync/syncable/mutable_entry.h" |
18 #include "sync/syncable/nigori_util.h" | 18 #include "components/sync/syncable/nigori_handler.h" |
19 #include "sync/syncable/syncable_write_transaction.h" | 19 #include "components/sync/syncable/nigori_util.h" |
20 #include "sync/util/cryptographer.h" | 20 #include "components/sync/syncable/syncable_write_transaction.h" |
21 | 21 |
22 namespace syncer { | 22 namespace syncer { |
23 | 23 |
24 void ApplyControlDataUpdates(syncable::Directory* dir) { | 24 void ApplyControlDataUpdates(syncable::Directory* dir) { |
25 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); | 25 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir); |
26 | 26 |
27 std::vector<int64_t> handles; | 27 std::vector<int64_t> handles; |
28 dir->GetUnappliedUpdateMetaHandles( | 28 dir->GetUnappliedUpdateMetaHandles(&trans, ToFullModelTypeSet(ControlTypes()), |
29 &trans, ToFullModelTypeSet(ControlTypes()), &handles); | 29 &handles); |
30 | 30 |
31 // First, go through and manually apply any new top level datatype nodes (so | 31 // First, go through and manually apply any new top level datatype nodes (so |
32 // that we don't have to worry about hitting a CONFLICT_HIERARCHY with an | 32 // that we don't have to worry about hitting a CONFLICT_HIERARCHY with an |
33 // entry because we haven't applied its parent yet). | 33 // entry because we haven't applied its parent yet). |
34 // TODO(sync): if at some point we support control datatypes with actual | 34 // TODO(sync): if at some point we support control datatypes with actual |
35 // hierarchies we'll need to revisit this logic. | 35 // hierarchies we'll need to revisit this logic. |
36 ModelTypeSet control_types = ControlTypes(); | 36 ModelTypeSet control_types = ControlTypes(); |
37 for (ModelTypeSet::Iterator iter = control_types.First(); iter.Good(); | 37 for (ModelTypeSet::Iterator iter = control_types.First(); iter.Good(); |
38 iter.Inc()) { | 38 iter.Inc()) { |
39 ModelType type = iter.Get(); | 39 ModelType type = iter.Get(); |
(...skipping 10 matching lines...) Expand all Loading... |
50 // already other than CHANGES_VERSION. | 50 // already other than CHANGES_VERSION. |
51 if (IsTypeWithClientGeneratedRoot(type)) { | 51 if (IsTypeWithClientGeneratedRoot(type)) { |
52 dir->MarkInitialSyncEndedForType(&trans, type); | 52 dir->MarkInitialSyncEndedForType(&trans, type); |
53 } | 53 } |
54 continue; | 54 continue; |
55 } | 55 } |
56 | 56 |
57 DCHECK_EQ(type, entry.GetServerModelType()); | 57 DCHECK_EQ(type, entry.GetServerModelType()); |
58 if (type == NIGORI) { | 58 if (type == NIGORI) { |
59 // Nigori node applications never fail. | 59 // Nigori node applications never fail. |
60 ApplyNigoriUpdate(&trans, | 60 ApplyNigoriUpdate(&trans, &entry, dir->GetCryptographer(&trans)); |
61 &entry, | |
62 dir->GetCryptographer(&trans)); | |
63 } else { | 61 } else { |
64 ApplyControlUpdate(&trans, | 62 ApplyControlUpdate(&trans, &entry, dir->GetCryptographer(&trans)); |
65 &entry, | |
66 dir->GetCryptographer(&trans)); | |
67 } | 63 } |
68 } | 64 } |
69 | 65 |
70 // Go through the rest of the unapplied control updates, skipping over any | 66 // Go through the rest of the unapplied control updates, skipping over any |
71 // top level folders. | 67 // top level folders. |
72 for (std::vector<int64_t>::const_iterator iter = handles.begin(); | 68 for (std::vector<int64_t>::const_iterator iter = handles.begin(); |
73 iter != handles.end(); ++iter) { | 69 iter != handles.end(); ++iter) { |
74 syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE, *iter); | 70 syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE, *iter); |
75 CHECK(entry.good()); | 71 CHECK(entry.good()); |
76 ModelType type = entry.GetServerModelType(); | 72 ModelType type = entry.GetServerModelType(); |
77 CHECK(ControlTypes().Has(type)); | 73 CHECK(ControlTypes().Has(type)); |
78 if (!entry.GetUniqueServerTag().empty()) { | 74 if (!entry.GetUniqueServerTag().empty()) { |
79 // We should have already applied all top level control nodes. | 75 // We should have already applied all top level control nodes. |
80 DCHECK(!entry.GetIsUnappliedUpdate()); | 76 DCHECK(!entry.GetIsUnappliedUpdate()); |
81 continue; | 77 continue; |
82 } | 78 } |
83 | 79 |
84 ApplyControlUpdate(&trans, | 80 ApplyControlUpdate(&trans, &entry, dir->GetCryptographer(&trans)); |
85 &entry, | |
86 dir->GetCryptographer(&trans)); | |
87 } | 81 } |
88 } | 82 } |
89 | 83 |
90 // Update the nigori handler with the server's nigori node. | 84 // Update the nigori handler with the server's nigori node. |
91 // | 85 // |
92 // If we have a locally modified nigori node, we merge them manually. This | 86 // If we have a locally modified nigori node, we merge them manually. This |
93 // handles the case where two clients both set a different passphrase. The | 87 // handles the case where two clients both set a different passphrase. The |
94 // second client to attempt to commit will go into a state of having pending | 88 // second client to attempt to commit will go into a state of having pending |
95 // keys, unioned the set of encrypted types, and eventually re-encrypt | 89 // keys, unioned the set of encrypted types, and eventually re-encrypt |
96 // everything with the passphrase of the first client and commit the set of | 90 // everything with the passphrase of the first client and commit the set of |
97 // merged encryption keys. Until the second client provides the pending | 91 // merged encryption keys. Until the second client provides the pending |
98 // passphrase, the cryptographer will preserve the encryption keys based on the | 92 // passphrase, the cryptographer will preserve the encryption keys based on the |
99 // local passphrase, while the nigori node will preserve the server encryption | 93 // local passphrase, while the nigori node will preserve the server encryption |
100 // keys. | 94 // keys. |
101 void ApplyNigoriUpdate(syncable::WriteTransaction* const trans, | 95 void ApplyNigoriUpdate(syncable::WriteTransaction* const trans, |
102 syncable::MutableEntry* const entry, | 96 syncable::MutableEntry* const entry, |
103 Cryptographer* cryptographer) { | 97 Cryptographer* cryptographer) { |
104 DCHECK(entry->GetIsUnappliedUpdate()); | 98 DCHECK(entry->GetIsUnappliedUpdate()); |
105 | 99 |
106 // We apply the nigori update regardless of whether there's a conflict or | 100 // We apply the nigori update regardless of whether there's a conflict or |
107 // not in order to preserve any new encrypted types or encryption keys. | 101 // not in order to preserve any new encrypted types or encryption keys. |
108 // TODO(zea): consider having this return a bool reflecting whether it was a | 102 // TODO(zea): consider having this return a bool reflecting whether it was a |
109 // valid update or not, and in the case of invalid updates not overwrite the | 103 // valid update or not, and in the case of invalid updates not overwrite the |
110 // local data. | 104 // local data. |
111 const sync_pb::NigoriSpecifics& nigori = | 105 const sync_pb::NigoriSpecifics& nigori = entry->GetServerSpecifics().nigori(); |
112 entry->GetServerSpecifics().nigori(); | |
113 trans->directory()->GetNigoriHandler()->ApplyNigoriUpdate(nigori, trans); | 106 trans->directory()->GetNigoriHandler()->ApplyNigoriUpdate(nigori, trans); |
114 | 107 |
115 // Make sure any unsynced changes are properly encrypted as necessary. | 108 // Make sure any unsynced changes are properly encrypted as necessary. |
116 // We only perform this if the cryptographer is ready. If not, these are | 109 // We only perform this if the cryptographer is ready. If not, these are |
117 // re-encrypted at SetDecryptionPassphrase time (via ReEncryptEverything). | 110 // re-encrypted at SetDecryptionPassphrase time (via ReEncryptEverything). |
118 // This logic covers the case where the nigori update marked new datatypes | 111 // This logic covers the case where the nigori update marked new datatypes |
119 // for encryption, but didn't change the passphrase. | 112 // for encryption, but didn't change the passphrase. |
120 if (cryptographer->is_ready()) { | 113 if (cryptographer->is_ready()) { |
121 // Note that we don't bother to encrypt any data for which IS_UNSYNCED | 114 // Note that we don't bother to encrypt any data for which IS_UNSYNCED |
122 // == false here. The machine that turned on encryption should know about | 115 // == false here. The machine that turned on encryption should know about |
123 // and re-encrypt all synced data. It's possible it could get interrupted | 116 // and re-encrypt all synced data. It's possible it could get interrupted |
124 // during this process, but we currently reencrypt everything at startup | 117 // during this process, but we currently reencrypt everything at startup |
125 // as well, so as soon as a client is restarted with this datatype marked | 118 // as well, so as soon as a client is restarted with this datatype marked |
126 // for encryption, all the data should be updated as necessary. | 119 // for encryption, all the data should be updated as necessary. |
127 | 120 |
128 // If this fails, something is wrong with the cryptographer, but there's | 121 // If this fails, something is wrong with the cryptographer, but there's |
129 // nothing we can do about it here. | 122 // nothing we can do about it here. |
130 DVLOG(1) << "Received new nigori, encrypting unsynced changes."; | 123 DVLOG(1) << "Received new nigori, encrypting unsynced changes."; |
131 syncable::ProcessUnsyncedChangesForEncryption(trans); | 124 syncable::ProcessUnsyncedChangesForEncryption(trans); |
132 } | 125 } |
133 | 126 |
134 if (!entry->GetIsUnsynced()) { // Update only. | 127 if (!entry->GetIsUnsynced()) { // Update only. |
135 UpdateLocalDataFromServerData(trans, entry); | 128 UpdateLocalDataFromServerData(trans, entry); |
136 } else { // Conflict. | 129 } else { // Conflict. |
137 const sync_pb::EntitySpecifics& server_specifics = | 130 const sync_pb::EntitySpecifics& server_specifics = |
138 entry->GetServerSpecifics(); | 131 entry->GetServerSpecifics(); |
139 const sync_pb::NigoriSpecifics& server_nigori = server_specifics.nigori(); | 132 const sync_pb::NigoriSpecifics& server_nigori = server_specifics.nigori(); |
140 const sync_pb::EntitySpecifics& local_specifics = | 133 const sync_pb::EntitySpecifics& local_specifics = entry->GetSpecifics(); |
141 entry->GetSpecifics(); | |
142 const sync_pb::NigoriSpecifics& local_nigori = local_specifics.nigori(); | 134 const sync_pb::NigoriSpecifics& local_nigori = local_specifics.nigori(); |
143 | 135 |
144 // We initialize the new nigori with the server state, and will override | 136 // We initialize the new nigori with the server state, and will override |
145 // it as necessary below. | 137 // it as necessary below. |
146 sync_pb::EntitySpecifics new_specifics = entry->GetServerSpecifics(); | 138 sync_pb::EntitySpecifics new_specifics = entry->GetServerSpecifics(); |
147 sync_pb::NigoriSpecifics* new_nigori = new_specifics.mutable_nigori(); | 139 sync_pb::NigoriSpecifics* new_nigori = new_specifics.mutable_nigori(); |
148 | 140 |
149 // If the cryptographer is not ready, another client set a new encryption | 141 // If the cryptographer is not ready, another client set a new encryption |
150 // passphrase. If we had migrated locally, we will re-migrate when the | 142 // passphrase. If we had migrated locally, we will re-migrate when the |
151 // pending keys are provided. If we had set a new custom passphrase locally | 143 // pending keys are provided. If we had set a new custom passphrase locally |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } else if (local_nigori.has_passphrase_type()) { | 176 } else if (local_nigori.has_passphrase_type()) { |
185 // Local is migrated but server is not. Copy over the local migrated | 177 // Local is migrated but server is not. Copy over the local migrated |
186 // data. | 178 // data. |
187 new_nigori->CopyFrom(local_nigori); | 179 new_nigori->CopyFrom(local_nigori); |
188 cryptographer->GetKeys(new_nigori->mutable_encryption_keybag()); | 180 cryptographer->GetKeys(new_nigori->mutable_encryption_keybag()); |
189 } // else leave the new nigori with the server state. | 181 } // else leave the new nigori with the server state. |
190 } | 182 } |
191 | 183 |
192 // Always update to the safest set of encrypted types. | 184 // Always update to the safest set of encrypted types. |
193 trans->directory()->GetNigoriHandler()->UpdateNigoriFromEncryptedTypes( | 185 trans->directory()->GetNigoriHandler()->UpdateNigoriFromEncryptedTypes( |
194 new_nigori, | 186 new_nigori, trans); |
195 trans); | |
196 | 187 |
197 entry->PutSpecifics(new_specifics); | 188 entry->PutSpecifics(new_specifics); |
198 DVLOG(1) << "Resolving simple conflict, merging nigori nodes: " | 189 DVLOG(1) << "Resolving simple conflict, merging nigori nodes: " << entry; |
199 << entry; | |
200 | 190 |
201 conflict_util::OverwriteServerChanges(entry); | 191 conflict_util::OverwriteServerChanges(entry); |
202 | 192 |
203 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 193 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
204 ConflictResolver::NIGORI_MERGE, | 194 ConflictResolver::NIGORI_MERGE, |
205 ConflictResolver::CONFLICT_RESOLUTION_SIZE); | 195 ConflictResolver::CONFLICT_RESOLUTION_SIZE); |
206 } | 196 } |
207 } | 197 } |
208 | 198 |
209 void ApplyControlUpdate(syncable::WriteTransaction* const trans, | 199 void ApplyControlUpdate(syncable::WriteTransaction* const trans, |
210 syncable::MutableEntry* const entry, | 200 syncable::MutableEntry* const entry, |
211 Cryptographer* cryptographer) { | 201 Cryptographer* cryptographer) { |
212 DCHECK_NE(entry->GetServerModelType(), NIGORI); | 202 DCHECK_NE(entry->GetServerModelType(), NIGORI); |
213 DCHECK(entry->GetIsUnappliedUpdate()); | 203 DCHECK(entry->GetIsUnappliedUpdate()); |
214 if (entry->GetIsUnsynced()) { | 204 if (entry->GetIsUnsynced()) { |
215 // We just let the server win all conflicts with control types. | 205 // We just let the server win all conflicts with control types. |
216 DVLOG(1) << "Ignoring local changes for control update."; | 206 DVLOG(1) << "Ignoring local changes for control update."; |
217 conflict_util::IgnoreLocalChanges(entry); | 207 conflict_util::IgnoreLocalChanges(entry); |
218 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", | 208 UMA_HISTOGRAM_ENUMERATION("Sync.ResolveSimpleConflict", |
219 ConflictResolver::OVERWRITE_LOCAL, | 209 ConflictResolver::OVERWRITE_LOCAL, |
220 ConflictResolver::CONFLICT_RESOLUTION_SIZE); | 210 ConflictResolver::CONFLICT_RESOLUTION_SIZE); |
221 } | 211 } |
222 | 212 |
223 UpdateAttemptResponse response = AttemptToUpdateEntry( | 213 UpdateAttemptResponse response = |
224 trans, entry, cryptographer); | 214 AttemptToUpdateEntry(trans, entry, cryptographer); |
225 DCHECK_EQ(SUCCESS, response); | 215 DCHECK_EQ(SUCCESS, response); |
226 } | 216 } |
227 | 217 |
228 } // namespace syncer | 218 } // namespace syncer |
OLD | NEW |