Fix Android sync regression as a result of https://codereview.chromium.org/1144543009
[chromium-blink-merge.git] / sync / test / fake_sync_encryption_handler.cc
blob3a60b07540d19764cd005dc064dd8c26f8a19934
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/test/fake_sync_encryption_handler.h"
7 #include "sync/protocol/nigori_specifics.pb.h"
8 #include "sync/syncable/nigori_util.h"
10 namespace syncer {
12 FakeSyncEncryptionHandler::FakeSyncEncryptionHandler()
13 : encrypted_types_(SensitiveTypes()),
14 encrypt_everything_(false),
15 passphrase_type_(IMPLICIT_PASSPHRASE),
16 cryptographer_(&encryptor_) {
18 FakeSyncEncryptionHandler::~FakeSyncEncryptionHandler() {}
20 void FakeSyncEncryptionHandler::Init() {
21 // Set up a basic cryptographer.
22 KeyParams keystore_params = {"localhost", "dummy", "keystore_key"};
23 cryptographer_.AddKey(keystore_params);
26 void FakeSyncEncryptionHandler::ApplyNigoriUpdate(
27 const sync_pb::NigoriSpecifics& nigori,
28 syncable::BaseTransaction* const trans) {
29 if (nigori.encrypt_everything())
30 EnableEncryptEverything();
31 if (nigori.keybag_is_frozen())
32 passphrase_type_ = CUSTOM_PASSPHRASE;
34 // TODO(zea): consider adding fake support for migration.
35 if (cryptographer_.CanDecrypt(nigori.encryption_keybag()))
36 cryptographer_.InstallKeys(nigori.encryption_keybag());
37 else if (nigori.has_encryption_keybag())
38 cryptographer_.SetPendingKeys(nigori.encryption_keybag());
40 if (cryptographer_.has_pending_keys()) {
41 DVLOG(1) << "OnPassPhraseRequired Sent";
42 sync_pb::EncryptedData pending_keys = cryptographer_.GetPendingKeys();
43 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
44 OnPassphraseRequired(REASON_DECRYPTION,
45 pending_keys));
46 } else if (!cryptographer_.is_ready()) {
47 DVLOG(1) << "OnPassphraseRequired sent because cryptographer is not "
48 << "ready";
49 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
50 OnPassphraseRequired(REASON_ENCRYPTION,
51 sync_pb::EncryptedData()));
55 void FakeSyncEncryptionHandler::UpdateNigoriFromEncryptedTypes(
56 sync_pb::NigoriSpecifics* nigori,
57 syncable::BaseTransaction* const trans) const {
58 syncable::UpdateNigoriFromEncryptedTypes(encrypted_types_,
59 encrypt_everything_,
60 nigori);
63 bool FakeSyncEncryptionHandler::NeedKeystoreKey(
64 syncable::BaseTransaction* const trans) const {
65 return keystore_key_.empty();
68 bool FakeSyncEncryptionHandler::SetKeystoreKeys(
69 const google::protobuf::RepeatedPtrField<google::protobuf::string>& keys,
70 syncable::BaseTransaction* const trans) {
71 if (keys.size() == 0)
72 return false;
73 std::string new_key = keys.Get(keys.size()-1);
74 if (new_key.empty())
75 return false;
76 keystore_key_ = new_key;
79 DVLOG(1) << "Keystore bootstrap token updated.";
80 FOR_EACH_OBSERVER(SyncEncryptionHandler::Observer, observers_,
81 OnBootstrapTokenUpdated(keystore_key_,
82 KEYSTORE_BOOTSTRAP_TOKEN));
83 return true;
86 ModelTypeSet FakeSyncEncryptionHandler::GetEncryptedTypes(
87 syncable::BaseTransaction* const trans) const {
88 return encrypted_types_;
91 void FakeSyncEncryptionHandler::AddObserver(Observer* observer) {
92 observers_.AddObserver(observer);
95 void FakeSyncEncryptionHandler::RemoveObserver(Observer* observer) {
96 observers_.RemoveObserver(observer);
99 void FakeSyncEncryptionHandler::SetEncryptionPassphrase(
100 const std::string& passphrase,
101 bool is_explicit) {
102 if (is_explicit)
103 passphrase_type_ = CUSTOM_PASSPHRASE;
106 void FakeSyncEncryptionHandler::SetDecryptionPassphrase(
107 const std::string& passphrase) {
108 // Do nothing.
111 void FakeSyncEncryptionHandler::EnableEncryptEverything() {
112 if (encrypt_everything_)
113 return;
114 encrypt_everything_ = true;
115 encrypted_types_ = ModelTypeSet::All();
116 FOR_EACH_OBSERVER(
117 Observer, observers_,
118 OnEncryptedTypesChanged(encrypted_types_, encrypt_everything_));
121 bool FakeSyncEncryptionHandler::EncryptEverythingEnabled() const {
122 return encrypt_everything_;
125 PassphraseType FakeSyncEncryptionHandler::GetPassphraseType() const {
126 return passphrase_type_;
129 } // namespace syncer