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/syncapi.h" | 5 #include "chrome/browser/sync/engine/syncapi.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <bitset> | 8 #include <bitset> |
9 #include <iomanip> | 9 #include <iomanip> |
10 #include <list> | 10 #include <list> |
(...skipping 1909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1920 cryptographer->Update(node.GetNigoriSpecifics()); | 1920 cryptographer->Update(node.GetNigoriSpecifics()); |
1921 if (result == Cryptographer::NEEDS_PASSPHRASE) { | 1921 if (result == Cryptographer::NEEDS_PASSPHRASE) { |
1922 ObserverList<SyncManager::Observer> temp_obs_list; | 1922 ObserverList<SyncManager::Observer> temp_obs_list; |
1923 CopyObservers(&temp_obs_list); | 1923 CopyObservers(&temp_obs_list); |
1924 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, | 1924 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, |
1925 OnPassphraseRequired(sync_api::REASON_DECRYPTION)); | 1925 OnPassphraseRequired(sync_api::REASON_DECRYPTION)); |
1926 } | 1926 } |
1927 | 1927 |
1928 allstatus_.SetCryptographerReady(cryptographer->is_ready()); | 1928 allstatus_.SetCryptographerReady(cryptographer->is_ready()); |
1929 allstatus_.SetCryptoHasPendingKeys(cryptographer->has_pending_keys()); | 1929 allstatus_.SetCryptoHasPendingKeys(cryptographer->has_pending_keys()); |
1930 allstatus_.SetEncryptedTypes(cryptographer->GetEncryptedTypes()); | |
1931 | 1930 |
1932 return cryptographer->is_ready(); | 1931 return cryptographer->is_ready(); |
1933 } | 1932 } |
1934 | 1933 |
1935 void SyncManager::SyncInternal::StartSyncingNormally() { | 1934 void SyncManager::SyncInternal::StartSyncingNormally() { |
1936 // Start the sync scheduler. This won't actually result in any | 1935 // Start the sync scheduler. This won't actually result in any |
1937 // syncing until at least the DirectoryManager broadcasts the OPENED | 1936 // syncing until at least the DirectoryManager broadcasts the OPENED |
1938 // event, and a valid server connection is detected. | 1937 // event, and a valid server connection is detected. |
1939 if (scheduler()) // NULL during certain unittests. | 1938 if (scheduler()) // NULL during certain unittests. |
1940 scheduler()->Start(SyncScheduler::NORMAL_MODE, NULL); | 1939 scheduler()->Start(SyncScheduler::NORMAL_MODE, NULL); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2108 | 2107 |
2109 // TODO(tim): Bug 58231. It would be nice if SetPassphrase didn't require | 2108 // TODO(tim): Bug 58231. It would be nice if SetPassphrase didn't require |
2110 // messing with the Nigori node, because we can't call SetPassphrase until | 2109 // messing with the Nigori node, because we can't call SetPassphrase until |
2111 // download conditions are met vs Cryptographer init. It seems like it's | 2110 // download conditions are met vs Cryptographer init. It seems like it's |
2112 // safe to defer this work. | 2111 // safe to defer this work. |
2113 sync_pb::NigoriSpecifics specifics(node.GetNigoriSpecifics()); | 2112 sync_pb::NigoriSpecifics specifics(node.GetNigoriSpecifics()); |
2114 specifics.clear_encrypted(); | 2113 specifics.clear_encrypted(); |
2115 cryptographer->GetKeys(specifics.mutable_encrypted()); | 2114 cryptographer->GetKeys(specifics.mutable_encrypted()); |
2116 specifics.set_using_explicit_passphrase(is_explicit); | 2115 specifics.set_using_explicit_passphrase(is_explicit); |
2117 node.SetNigoriSpecifics(specifics); | 2116 node.SetNigoriSpecifics(specifics); |
| 2117 ReEncryptEverything(&trans); |
2118 } | 2118 } |
2119 | 2119 |
2120 // Does nothing if everything is already encrypted or the cryptographer has | |
2121 // pending keys. | |
2122 ReEncryptEverything(&trans); | |
2123 | |
2124 VLOG(1) << "Passphrase accepted, bootstrapping encryption."; | 2120 VLOG(1) << "Passphrase accepted, bootstrapping encryption."; |
2125 std::string bootstrap_token; | 2121 std::string bootstrap_token; |
2126 cryptographer->GetBootstrapToken(&bootstrap_token); | 2122 cryptographer->GetBootstrapToken(&bootstrap_token); |
2127 ObserverList<SyncManager::Observer> temp_obs_list; | 2123 ObserverList<SyncManager::Observer> temp_obs_list; |
2128 CopyObservers(&temp_obs_list); | 2124 CopyObservers(&temp_obs_list); |
2129 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, | 2125 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, |
2130 OnPassphraseAccepted(bootstrap_token)); | 2126 OnPassphraseAccepted(bootstrap_token)); |
2131 } | 2127 } |
2132 | 2128 |
2133 bool SyncManager::SyncInternal::IsUsingExplicitPassphrase() { | 2129 bool SyncManager::SyncInternal::IsUsingExplicitPassphrase() { |
(...skipping 17 matching lines...) Expand all Loading... |
2151 WriteTransaction trans(FROM_HERE, GetUserShare()); | 2147 WriteTransaction trans(FROM_HERE, GetUserShare()); |
2152 WriteNode node(&trans); | 2148 WriteNode node(&trans); |
2153 if (!node.InitByTagLookup(kNigoriTag)) { | 2149 if (!node.InitByTagLookup(kNigoriTag)) { |
2154 NOTREACHED() << "Unable to set encrypted datatypes because Nigori node not " | 2150 NOTREACHED() << "Unable to set encrypted datatypes because Nigori node not " |
2155 << "found."; | 2151 << "found."; |
2156 return; | 2152 return; |
2157 } | 2153 } |
2158 | 2154 |
2159 Cryptographer* cryptographer = trans.GetCryptographer(); | 2155 Cryptographer* cryptographer = trans.GetCryptographer(); |
2160 | 2156 |
2161 if (!cryptographer->is_ready()) { | 2157 if (!cryptographer->is_initialized()) { |
2162 VLOG(1) << "Attempting to encrypt datatypes when cryptographer not " | 2158 VLOG(1) << "Attempting to encrypt datatypes when cryptographer not " |
2163 << "initialized, prompting for passphrase."; | 2159 << "initialized, prompting for passphrase."; |
2164 ObserverList<SyncManager::Observer> temp_obs_list; | 2160 ObserverList<SyncManager::Observer> temp_obs_list; |
2165 CopyObservers(&temp_obs_list); | 2161 CopyObservers(&temp_obs_list); |
2166 // TODO(zea): this isn't really decryption, but that's the only way we have | 2162 // TODO(zea): this isn't really decryption, but that's the only way we have |
2167 // to prompt the user for a passsphrase. See http://crbug.com/91379. | 2163 // to prompt the user for a passsphrase. See http://crbug.com/91379. |
2168 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, | 2164 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, |
2169 OnPassphraseRequired(sync_api::REASON_DECRYPTION)); | 2165 OnPassphraseRequired(sync_api::REASON_DECRYPTION)); |
2170 return; | 2166 return; |
2171 } | 2167 } |
2172 | 2168 |
2173 // Update the Nigori node's set of encrypted datatypes. | 2169 // Update the Nigori node's set of encrypted datatypes. |
2174 // Note, we merge the current encrypted types with those requested. Once a | 2170 // Note, we merge the current encrypted types with those requested. Once a |
2175 // datatypes is marked as needing encryption, it is never unmarked. | 2171 // datatypes is marked as needing encryption, it is never unmarked. |
2176 sync_pb::NigoriSpecifics nigori; | 2172 sync_pb::NigoriSpecifics nigori; |
2177 nigori.CopyFrom(node.GetNigoriSpecifics()); | 2173 nigori.CopyFrom(node.GetNigoriSpecifics()); |
2178 syncable::ModelTypeSet current_encrypted_types = GetEncryptedTypes(&trans); | 2174 syncable::ModelTypeSet current_encrypted_types = GetEncryptedTypes(&trans); |
2179 syncable::ModelTypeSet newly_encrypted_types; | 2175 syncable::ModelTypeSet newly_encrypted_types; |
2180 std::set_union(current_encrypted_types.begin(), current_encrypted_types.end(), | 2176 std::set_union(current_encrypted_types.begin(), current_encrypted_types.end(), |
2181 encrypted_types.begin(), encrypted_types.end(), | 2177 encrypted_types.begin(), encrypted_types.end(), |
2182 std::inserter(newly_encrypted_types, | 2178 std::inserter(newly_encrypted_types, |
2183 newly_encrypted_types.begin())); | 2179 newly_encrypted_types.begin())); |
2184 allstatus_.SetEncryptedTypes(newly_encrypted_types); | 2180 allstatus_.SetEncryptedTypes(newly_encrypted_types); |
| 2181 if (newly_encrypted_types == current_encrypted_types) { |
| 2182 // Set of encrypted types has not changed, just notify and return. |
| 2183 ObserverList<SyncManager::Observer> temp_obs_list; |
| 2184 CopyObservers(&temp_obs_list); |
| 2185 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, |
| 2186 OnEncryptionComplete(current_encrypted_types)); |
| 2187 return; |
| 2188 } |
2185 syncable::FillNigoriEncryptedTypes(newly_encrypted_types, &nigori); | 2189 syncable::FillNigoriEncryptedTypes(newly_encrypted_types, &nigori); |
2186 node.SetNigoriSpecifics(nigori); | 2190 node.SetNigoriSpecifics(nigori); |
2187 | 2191 |
2188 cryptographer->SetEncryptedTypes(nigori); | 2192 cryptographer->SetEncryptedTypes(nigori); |
2189 | 2193 |
2190 // We reencrypt everything regardless of whether the set of encrypted | 2194 // TODO(zea): only reencrypt this datatype? ReEncrypting everything is a |
2191 // types changed to ensure that any stray unencrypted entries are overwritten. | 2195 // safer approach, and should not impact anything that is already encrypted |
| 2196 // (redundant changes are ignored). |
2192 ReEncryptEverything(&trans); | 2197 ReEncryptEverything(&trans); |
2193 return; | 2198 return; |
2194 } | 2199 } |
2195 | 2200 |
2196 // TODO(zea): Add unit tests that ensure no sync changes are made when not | 2201 // TODO(zea): Add unit tests that ensure no sync changes are made when not |
2197 // needed. | 2202 // needed. |
2198 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) { | 2203 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) { |
2199 Cryptographer* cryptographer = trans->GetCryptographer(); | 2204 syncable::ModelTypeSet encrypted_types = |
2200 if (!cryptographer || !cryptographer->is_ready()) | 2205 GetEncryptedTypes(trans); |
2201 return; | |
2202 syncable::ModelTypeSet encrypted_types = GetEncryptedTypes(trans); | |
2203 ModelSafeRoutingInfo routes; | 2206 ModelSafeRoutingInfo routes; |
2204 registrar_->GetModelSafeRoutingInfo(&routes); | 2207 registrar_->GetModelSafeRoutingInfo(&routes); |
2205 std::string tag; | 2208 std::string tag; |
2206 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin(); | 2209 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin(); |
2207 iter != encrypted_types.end(); ++iter) { | 2210 iter != encrypted_types.end(); ++iter) { |
2208 if (*iter == syncable::PASSWORDS || routes.count(*iter) == 0) | 2211 if (*iter == syncable::PASSWORDS || routes.count(*iter) == 0) |
2209 continue; | 2212 continue; |
2210 ReadNode type_root(trans); | 2213 ReadNode type_root(trans); |
2211 tag = syncable::ModelTypeToRootTag(*iter); | 2214 tag = syncable::ModelTypeToRootTag(*iter); |
2212 if (!type_root.InitByTagLookup(tag)) { | 2215 if (!type_root.InitByTagLookup(tag)) { |
(...skipping 920 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3133 void SyncManager::TriggerOnIncomingNotificationForTest( | 3136 void SyncManager::TriggerOnIncomingNotificationForTest( |
3134 const syncable::ModelTypeBitSet& model_types) { | 3137 const syncable::ModelTypeBitSet& model_types) { |
3135 syncable::ModelTypePayloadMap model_types_with_payloads = | 3138 syncable::ModelTypePayloadMap model_types_with_payloads = |
3136 syncable::ModelTypePayloadMapFromBitSet(model_types, | 3139 syncable::ModelTypePayloadMapFromBitSet(model_types, |
3137 std::string()); | 3140 std::string()); |
3138 | 3141 |
3139 data_->OnIncomingNotification(model_types_with_payloads); | 3142 data_->OnIncomingNotification(model_types_with_payloads); |
3140 } | 3143 } |
3141 | 3144 |
3142 } // namespace sync_api | 3145 } // namespace sync_api |
OLD | NEW |