Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(944)

Side by Side Diff: chrome/browser/sync/internal_api/sync_manager.cc

Issue 7795002: [Sync] Gracefully handle writing to a node when the cryptographer is not ready. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewer comments Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/internal_api/sync_manager.h" 5 #include "chrome/browser/sync/internal_api/sync_manager.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 cryptographer->Update(node.GetNigoriSpecifics()); 836 cryptographer->Update(node.GetNigoriSpecifics());
837 if (result == Cryptographer::NEEDS_PASSPHRASE) { 837 if (result == Cryptographer::NEEDS_PASSPHRASE) {
838 ObserverList<SyncManager::Observer> temp_obs_list; 838 ObserverList<SyncManager::Observer> temp_obs_list;
839 CopyObservers(&temp_obs_list); 839 CopyObservers(&temp_obs_list);
840 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, 840 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list,
841 OnPassphraseRequired(sync_api::REASON_DECRYPTION)); 841 OnPassphraseRequired(sync_api::REASON_DECRYPTION));
842 } 842 }
843 843
844 allstatus_.SetCryptographerReady(cryptographer->is_ready()); 844 allstatus_.SetCryptographerReady(cryptographer->is_ready());
845 allstatus_.SetCryptoHasPendingKeys(cryptographer->has_pending_keys()); 845 allstatus_.SetCryptoHasPendingKeys(cryptographer->has_pending_keys());
846 allstatus_.SetEncryptedTypes(cryptographer->GetEncryptedTypes());
846 847
847 return cryptographer->is_ready(); 848 return cryptographer->is_ready();
848 } 849 }
849 850
850 void SyncManager::SyncInternal::StartSyncingNormally() { 851 void SyncManager::SyncInternal::StartSyncingNormally() {
851 // Start the sync scheduler. This won't actually result in any 852 // Start the sync scheduler. This won't actually result in any
852 // syncing until at least the DirectoryManager broadcasts the OPENED 853 // syncing until at least the DirectoryManager broadcasts the OPENED
853 // event, and a valid server connection is detected. 854 // event, and a valid server connection is detected.
854 if (scheduler()) // NULL during certain unittests. 855 if (scheduler()) // NULL during certain unittests.
855 scheduler()->Start(SyncScheduler::NORMAL_MODE, NULL); 856 scheduler()->Start(SyncScheduler::NORMAL_MODE, NULL);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 1019
1019 // TODO(tim): Bug 58231. It would be nice if SetPassphrase didn't require 1020 // TODO(tim): Bug 58231. It would be nice if SetPassphrase didn't require
1020 // messing with the Nigori node, because we can't call SetPassphrase until 1021 // messing with the Nigori node, because we can't call SetPassphrase until
1021 // download conditions are met vs Cryptographer init. It seems like it's 1022 // download conditions are met vs Cryptographer init. It seems like it's
1022 // safe to defer this work. 1023 // safe to defer this work.
1023 sync_pb::NigoriSpecifics specifics(node.GetNigoriSpecifics()); 1024 sync_pb::NigoriSpecifics specifics(node.GetNigoriSpecifics());
1024 specifics.clear_encrypted(); 1025 specifics.clear_encrypted();
1025 cryptographer->GetKeys(specifics.mutable_encrypted()); 1026 cryptographer->GetKeys(specifics.mutable_encrypted());
1026 specifics.set_using_explicit_passphrase(is_explicit); 1027 specifics.set_using_explicit_passphrase(is_explicit);
1027 node.SetNigoriSpecifics(specifics); 1028 node.SetNigoriSpecifics(specifics);
1028 ReEncryptEverything(&trans);
1029 } 1029 }
1030 1030
1031 // Does nothing if everything is already encrypted or the cryptographer has
1032 // pending keys.
1033 ReEncryptEverything(&trans);
1034
1031 VLOG(1) << "Passphrase accepted, bootstrapping encryption."; 1035 VLOG(1) << "Passphrase accepted, bootstrapping encryption.";
1032 std::string bootstrap_token; 1036 std::string bootstrap_token;
1033 cryptographer->GetBootstrapToken(&bootstrap_token); 1037 cryptographer->GetBootstrapToken(&bootstrap_token);
1034 ObserverList<SyncManager::Observer> temp_obs_list; 1038 ObserverList<SyncManager::Observer> temp_obs_list;
1035 CopyObservers(&temp_obs_list); 1039 CopyObservers(&temp_obs_list);
1036 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, 1040 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list,
1037 OnPassphraseAccepted(bootstrap_token)); 1041 OnPassphraseAccepted(bootstrap_token));
1038 } 1042 }
1039 1043
1040 bool SyncManager::SyncInternal::IsUsingExplicitPassphrase() { 1044 bool SyncManager::SyncInternal::IsUsingExplicitPassphrase() {
(...skipping 17 matching lines...) Expand all
1058 WriteTransaction trans(FROM_HERE, GetUserShare()); 1062 WriteTransaction trans(FROM_HERE, GetUserShare());
1059 WriteNode node(&trans); 1063 WriteNode node(&trans);
1060 if (!node.InitByTagLookup(kNigoriTag)) { 1064 if (!node.InitByTagLookup(kNigoriTag)) {
1061 NOTREACHED() << "Unable to set encrypted datatypes because Nigori node not " 1065 NOTREACHED() << "Unable to set encrypted datatypes because Nigori node not "
1062 << "found."; 1066 << "found.";
1063 return; 1067 return;
1064 } 1068 }
1065 1069
1066 Cryptographer* cryptographer = trans.GetCryptographer(); 1070 Cryptographer* cryptographer = trans.GetCryptographer();
1067 1071
1068 if (!cryptographer->is_initialized()) { 1072 if (!cryptographer->is_ready()) {
1069 VLOG(1) << "Attempting to encrypt datatypes when cryptographer not " 1073 VLOG(1) << "Attempting to encrypt datatypes when cryptographer not "
1070 << "initialized, prompting for passphrase."; 1074 << "initialized, prompting for passphrase.";
1071 ObserverList<SyncManager::Observer> temp_obs_list; 1075 ObserverList<SyncManager::Observer> temp_obs_list;
1072 CopyObservers(&temp_obs_list); 1076 CopyObservers(&temp_obs_list);
1073 // TODO(zea): this isn't really decryption, but that's the only way we have 1077 // TODO(zea): this isn't really decryption, but that's the only way we have
1074 // to prompt the user for a passsphrase. See http://crbug.com/91379. 1078 // to prompt the user for a passsphrase. See http://crbug.com/91379.
1075 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list, 1079 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list,
1076 OnPassphraseRequired(sync_api::REASON_DECRYPTION)); 1080 OnPassphraseRequired(sync_api::REASON_DECRYPTION));
1077 return; 1081 return;
1078 } 1082 }
1079 1083
1080 // Update the Nigori node's set of encrypted datatypes. 1084 // Update the Nigori node's set of encrypted datatypes.
1081 // Note, we merge the current encrypted types with those requested. Once a 1085 // Note, we merge the current encrypted types with those requested. Once a
1082 // datatypes is marked as needing encryption, it is never unmarked. 1086 // datatypes is marked as needing encryption, it is never unmarked.
1083 sync_pb::NigoriSpecifics nigori; 1087 sync_pb::NigoriSpecifics nigori;
1084 nigori.CopyFrom(node.GetNigoriSpecifics()); 1088 nigori.CopyFrom(node.GetNigoriSpecifics());
1085 syncable::ModelTypeSet current_encrypted_types = GetEncryptedTypes(&trans); 1089 syncable::ModelTypeSet current_encrypted_types = GetEncryptedTypes(&trans);
1086 syncable::ModelTypeSet newly_encrypted_types; 1090 syncable::ModelTypeSet newly_encrypted_types;
1087 std::set_union(current_encrypted_types.begin(), current_encrypted_types.end(), 1091 std::set_union(current_encrypted_types.begin(), current_encrypted_types.end(),
1088 encrypted_types.begin(), encrypted_types.end(), 1092 encrypted_types.begin(), encrypted_types.end(),
1089 std::inserter(newly_encrypted_types, 1093 std::inserter(newly_encrypted_types,
1090 newly_encrypted_types.begin())); 1094 newly_encrypted_types.begin()));
1091 allstatus_.SetEncryptedTypes(newly_encrypted_types); 1095 allstatus_.SetEncryptedTypes(newly_encrypted_types);
1092 if (newly_encrypted_types == current_encrypted_types) {
1093 // Set of encrypted types has not changed, just notify and return.
1094 ObserverList<SyncManager::Observer> temp_obs_list;
1095 CopyObservers(&temp_obs_list);
1096 FOR_EACH_OBSERVER(SyncManager::Observer, temp_obs_list,
1097 OnEncryptionComplete(current_encrypted_types));
1098 return;
1099 }
1100 syncable::FillNigoriEncryptedTypes(newly_encrypted_types, &nigori); 1096 syncable::FillNigoriEncryptedTypes(newly_encrypted_types, &nigori);
1101 node.SetNigoriSpecifics(nigori); 1097 node.SetNigoriSpecifics(nigori);
1102
1103 cryptographer->SetEncryptedTypes(nigori); 1098 cryptographer->SetEncryptedTypes(nigori);
1104 1099
1105 // TODO(zea): only reencrypt this datatype? ReEncrypting everything is a 1100 // We reencrypt everything regardless of whether the set of encrypted
1106 // safer approach, and should not impact anything that is already encrypted 1101 // types changed to ensure that any stray unencrypted entries are overwritten.
1107 // (redundant changes are ignored).
1108 ReEncryptEverything(&trans); 1102 ReEncryptEverything(&trans);
1109 return; 1103 return;
1110 } 1104 }
1111 1105
1112 // TODO(zea): Add unit tests that ensure no sync changes are made when not 1106 // TODO(zea): Add unit tests that ensure no sync changes are made when not
1113 // needed. 1107 // needed.
1114 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) { 1108 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) {
1115 syncable::ModelTypeSet encrypted_types = 1109 Cryptographer* cryptographer = trans->GetCryptographer();
1116 GetEncryptedTypes(trans); 1110 if (!cryptographer || !cryptographer->is_ready())
1111 return;
1112 syncable::ModelTypeSet encrypted_types = GetEncryptedTypes(trans);
1117 ModelSafeRoutingInfo routes; 1113 ModelSafeRoutingInfo routes;
1118 registrar_->GetModelSafeRoutingInfo(&routes); 1114 registrar_->GetModelSafeRoutingInfo(&routes);
1119 std::string tag; 1115 std::string tag;
1120 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin(); 1116 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin();
1121 iter != encrypted_types.end(); ++iter) { 1117 iter != encrypted_types.end(); ++iter) {
1122 if (*iter == syncable::PASSWORDS || routes.count(*iter) == 0) 1118 if (*iter == syncable::PASSWORDS || routes.count(*iter) == 0)
1123 continue; 1119 continue;
1124 ReadNode type_root(trans); 1120 ReadNode type_root(trans);
1125 tag = syncable::ModelTypeToRootTag(*iter); 1121 tag = syncable::ModelTypeToRootTag(*iter);
1126 if (!type_root.InitByTagLookup(tag)) { 1122 if (!type_root.InitByTagLookup(tag)) {
(...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after
2058 2054
2059 for (syncable::ModelTypeSet::const_iterator i = types.begin(); 2055 for (syncable::ModelTypeSet::const_iterator i = types.begin();
2060 i != types.end(); ++i) { 2056 i != types.end(); ++i) {
2061 if (!lookup->initial_sync_ended_for_type(*i)) 2057 if (!lookup->initial_sync_ended_for_type(*i))
2062 return false; 2058 return false;
2063 } 2059 }
2064 return true; 2060 return true;
2065 } 2061 }
2066 2062
2067 } // namespace sync_api 2063 } // namespace sync_api
OLDNEW
« no previous file with comments | « chrome/browser/sync/engine/syncer_unittest.cc ('k') | chrome/browser/sync/internal_api/write_node.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698