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

Unified Diff: sync/internal_api/sync_encryption_handler_impl.cc

Issue 1177853002: [Sync] Add ability to save/restore Nigori to SyncEncryptionHandlerImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: sync/internal_api/sync_encryption_handler_impl.cc
diff --git a/sync/internal_api/sync_encryption_handler_impl.cc b/sync/internal_api/sync_encryption_handler_impl.cc
index 4bf559fd8580c958fe0caa8d18ddfa01ceb933a5..b28ee8599bba5dbffd00fbb299b9e075d7a6e552 100644
--- a/sync/internal_api/sync_encryption_handler_impl.cc
+++ b/sync/internal_api/sync_encryption_handler_impl.cc
@@ -26,8 +26,11 @@
#include "sync/protocol/sync.pb.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/entry.h"
+#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/nigori_util.h"
#include "sync/syncable/syncable_base_transaction.h"
+#include "sync/syncable/syncable_model_neutral_write_transaction.h"
+#include "sync/syncable/syncable_write_transaction.h"
#include "sync/util/cryptographer.h"
#include "sync/util/encryptor.h"
#include "sync/util/time.h"
@@ -783,6 +786,37 @@ base::Time SyncEncryptionHandlerImpl::custom_passphrase_time() const {
return custom_passphrase_time_;
}
+void SyncEncryptionHandlerImpl::RestoreNigori(
+ const SyncEncryptionHandler::NigoriState& nigori_state) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ WriteTransaction trans(FROM_HERE, user_share_);
+
+ // See that we don't already have a nigori node.
+ WriteNode nigori_node(&trans);
+ BaseNode::InitByLookupResult init_result = nigori_node.InitTypeRoot(NIGORI);
+ DCHECK(init_result == BaseNode::INIT_FAILED_ENTRY_NOT_GOOD);
+
+ // Create one.
+ syncable::ModelNeutralMutableEntry model_neutral_mutable_entry(
maniscalco 2015/06/10 22:01:45 I tried a number of ways of creating the nigori no
Nicolas Zea 2015/06/11 17:43:07 Acknowledged.
+ trans.GetWrappedWriteTrans(), syncable::CREATE_NEW_TYPE_ROOT, NIGORI);
+ DCHECK(model_neutral_mutable_entry.good());
+ model_neutral_mutable_entry.PutServerIsDir(true);
+ model_neutral_mutable_entry.PutUniqueServerTag(ModelTypeToRootTag(NIGORI));
+ model_neutral_mutable_entry.PutIsUnsynced(true);
+
+ // Update it with the saved nigori specifics.
+ syncable::MutableEntry mutable_entry(trans.GetWrappedWriteTrans(),
+ syncable::GET_TYPE_ROOT, NIGORI);
+ DCHECK(mutable_entry.good());
+ sync_pb::EntitySpecifics specifics;
+ *specifics.mutable_nigori() = nigori_state.nigori_specifics;
Nicolas Zea 2015/06/11 17:43:07 You can just do mutable_nigori()->CopyFrom(...)
maniscalco 2015/06/15 16:49:18 Done.
+ mutable_entry.PutSpecifics(specifics);
+
+ // Update our state based on the saved nigori node.
+ ApplyNigoriUpdate(nigori_state.nigori_specifics, trans.GetWrappedTrans());
+ WriteEncryptionStateToNigori(&trans);
+}
+
// This function iterates over all encrypted types. There are many scenarios in
// which data for some or all types is not currently available. In that case,
// the lookup of the root node will fail and we will skip encryption for that
@@ -1128,20 +1162,32 @@ void SyncEncryptionHandlerImpl::SetCustomPassphrase(
}
std::string bootstrap_token;
- if (cryptographer->AddKey(key_params)) {
- DVLOG(1) << "Setting custom passphrase.";
- cryptographer->GetBootstrapToken(&bootstrap_token);
- passphrase_type_ = CUSTOM_PASSPHRASE;
- custom_passphrase_time_ = base::Time::Now();
- FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
- OnPassphraseTypeChanged(
- passphrase_type_,
- GetExplicitPassphraseTime()));
- } else {
+ if (!cryptographer->AddKey(key_params)) {
NOTREACHED() << "Failed to add key to cryptographer.";
return;
}
+
+ DVLOG(1) << "Setting custom passphrase.";
+ cryptographer->GetBootstrapToken(&bootstrap_token);
+ passphrase_type_ = CUSTOM_PASSPHRASE;
+ custom_passphrase_time_ = base::Time::Now();
+ FOR_EACH_OBSERVER(
+ SyncEncryptionHandler::Observer, observers_,
+ OnPassphraseTypeChanged(passphrase_type_, GetExplicitPassphraseTime()));
FinishSetPassphrase(true, bootstrap_token, trans, nigori_node);
+ NotifyObserversOfLocalCustomPassphrase(trans);
+}
+
+void SyncEncryptionHandlerImpl::NotifyObserversOfLocalCustomPassphrase(
+ WriteTransaction* trans) {
+ WriteEncryptionStateToNigori(trans);
Nicolas Zea 2015/06/11 17:43:07 Is this necessary? Doesn't FinishSetPassphrase alr
maniscalco 2015/06/15 16:49:18 You're right. FinishSetPassphrase calls SetNigori
+ WriteNode nigori_node(trans);
+ BaseNode::InitByLookupResult init_result = nigori_node.InitTypeRoot(NIGORI);
+ DCHECK_EQ(init_result, BaseNode::INIT_OK);
+ NigoriState nigori_state;
+ nigori_state.nigori_specifics = nigori_node.GetNigoriSpecifics();
Nicolas Zea 2015/06/11 17:43:07 maybe DCHECK that the nigori reflects a custom pas
maniscalco 2015/06/15 16:49:18 Good idea. Done.
+ FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
+ OnLocalSetCustomPassphrase(nigori_state));
}
void SyncEncryptionHandlerImpl::DecryptPendingKeysWithExplicitPassphrase(

Powered by Google App Engine
This is Rietveld 408576698