| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "sync/internal_api/public/base_node.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <stack> | |
| 10 | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "sync/internal_api/public/base_transaction.h" | |
| 14 #include "sync/internal_api/syncapi_internal.h" | |
| 15 #include "sync/protocol/app_specifics.pb.h" | |
| 16 #include "sync/protocol/autofill_specifics.pb.h" | |
| 17 #include "sync/protocol/bookmark_specifics.pb.h" | |
| 18 #include "sync/protocol/extension_specifics.pb.h" | |
| 19 #include "sync/protocol/nigori_specifics.pb.h" | |
| 20 #include "sync/protocol/password_specifics.pb.h" | |
| 21 #include "sync/protocol/session_specifics.pb.h" | |
| 22 #include "sync/protocol/theme_specifics.pb.h" | |
| 23 #include "sync/protocol/typed_url_specifics.pb.h" | |
| 24 #include "sync/syncable/directory.h" | |
| 25 #include "sync/syncable/entry.h" | |
| 26 #include "sync/syncable/syncable_base_transaction.h" | |
| 27 #include "sync/syncable/syncable_id.h" | |
| 28 #include "sync/util/time.h" | |
| 29 | |
| 30 using sync_pb::AutofillProfileSpecifics; | |
| 31 | |
| 32 namespace syncer { | |
| 33 | |
| 34 using syncable::SPECIFICS; | |
| 35 | |
| 36 // Helper function to look up the int64_t metahandle of an object given the ID | |
| 37 // string. | |
| 38 static int64_t IdToMetahandle(syncable::BaseTransaction* trans, | |
| 39 const syncable::Id& id) { | |
| 40 if (id.IsNull()) | |
| 41 return kInvalidId; | |
| 42 syncable::Entry entry(trans, syncable::GET_BY_ID, id); | |
| 43 if (!entry.good()) | |
| 44 return kInvalidId; | |
| 45 return entry.GetMetahandle(); | |
| 46 } | |
| 47 | |
| 48 BaseNode::BaseNode() : password_data_(new sync_pb::PasswordSpecificsData) {} | |
| 49 | |
| 50 BaseNode::~BaseNode() {} | |
| 51 | |
| 52 bool BaseNode::DecryptIfNecessary() { | |
| 53 if (GetIsPermanentFolder()) | |
| 54 return true; // Ignore unique folders. | |
| 55 const sync_pb::EntitySpecifics& specifics = | |
| 56 GetEntry()->GetSpecifics(); | |
| 57 if (specifics.has_password()) { | |
| 58 // Passwords have their own legacy encryption structure. | |
| 59 std::unique_ptr<sync_pb::PasswordSpecificsData> data( | |
| 60 DecryptPasswordSpecifics(specifics, | |
| 61 GetTransaction()->GetCryptographer())); | |
| 62 if (!data) { | |
| 63 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( | |
| 64 FROM_HERE, std::string("Failed to decrypt encrypted node of type ") + | |
| 65 ModelTypeToString(GetModelType())); | |
| 66 return false; | |
| 67 } | |
| 68 password_data_.swap(data); | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 // We assume any node with the encrypted field set has encrypted data and if | |
| 73 // not we have no work to do, with the exception of bookmarks. For bookmarks | |
| 74 // we must make sure the bookmarks data has the title field supplied. If not, | |
| 75 // we fill the unencrypted_data_ with a copy of the bookmark specifics that | |
| 76 // follows the new bookmarks format. | |
| 77 if (!specifics.has_encrypted()) { | |
| 78 if (GetModelType() == BOOKMARKS && | |
| 79 !specifics.bookmark().has_title() && | |
| 80 !GetTitle().empty()) { // Last check ensures this isn't a new node. | |
| 81 // We need to fill in the title. | |
| 82 std::string title = GetTitle(); | |
| 83 std::string server_legal_title; | |
| 84 SyncAPINameToServerName(title, &server_legal_title); | |
| 85 DVLOG(1) << "Reading from legacy bookmark, manually returning title " | |
| 86 << title; | |
| 87 unencrypted_data_.CopyFrom(specifics); | |
| 88 unencrypted_data_.mutable_bookmark()->set_title( | |
| 89 server_legal_title); | |
| 90 } | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 const sync_pb::EncryptedData& encrypted = specifics.encrypted(); | |
| 95 std::string plaintext_data = GetTransaction()->GetCryptographer()-> | |
| 96 DecryptToString(encrypted); | |
| 97 if (plaintext_data.length() == 0) { | |
| 98 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( | |
| 99 FROM_HERE, std::string("Failed to decrypt encrypted node of type ") + | |
| 100 ModelTypeToString(GetModelType())); | |
| 101 return false; | |
| 102 } else if (!unencrypted_data_.ParseFromString(plaintext_data)) { | |
| 103 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( | |
| 104 FROM_HERE, std::string("Failed to parse encrypted node of type ") + | |
| 105 ModelTypeToString(GetModelType())); | |
| 106 return false; | |
| 107 } | |
| 108 DVLOG(2) << "Decrypted specifics of type " | |
| 109 << ModelTypeToString(GetModelType()) | |
| 110 << " with content: " << plaintext_data; | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 const sync_pb::EntitySpecifics& BaseNode::GetUnencryptedSpecifics( | |
| 115 const syncable::Entry* entry) const { | |
| 116 const sync_pb::EntitySpecifics& specifics = entry->GetSpecifics(); | |
| 117 if (specifics.has_encrypted()) { | |
| 118 DCHECK_NE(GetModelTypeFromSpecifics(unencrypted_data_), UNSPECIFIED); | |
| 119 return unencrypted_data_; | |
| 120 } else { | |
| 121 // Due to the change in bookmarks format, we need to check to see if this is | |
| 122 // a legacy bookmarks (and has no title field in the proto). If it is, we | |
| 123 // return the unencrypted_data_, which was filled in with the title by | |
| 124 // DecryptIfNecessary(). | |
| 125 if (GetModelType() == BOOKMARKS) { | |
| 126 const sync_pb::BookmarkSpecifics& bookmark_specifics = | |
| 127 specifics.bookmark(); | |
| 128 if (bookmark_specifics.has_title() || | |
| 129 GetTitle().empty() || // For the empty node case | |
| 130 GetIsPermanentFolder()) { | |
| 131 // It's possible we previously had to convert and set | |
| 132 // |unencrypted_data_| but then wrote our own data, so we allow | |
| 133 // |unencrypted_data_| to be non-empty. | |
| 134 return specifics; | |
| 135 } else { | |
| 136 DCHECK_EQ(GetModelTypeFromSpecifics(unencrypted_data_), BOOKMARKS); | |
| 137 return unencrypted_data_; | |
| 138 } | |
| 139 } else { | |
| 140 DCHECK_EQ(GetModelTypeFromSpecifics(unencrypted_data_), UNSPECIFIED); | |
| 141 return specifics; | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 int64_t BaseNode::GetParentId() const { | |
| 147 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), | |
| 148 GetEntry()->GetParentId()); | |
| 149 } | |
| 150 | |
| 151 int64_t BaseNode::GetId() const { | |
| 152 return GetEntry()->GetMetahandle(); | |
| 153 } | |
| 154 | |
| 155 base::Time BaseNode::GetModificationTime() const { | |
| 156 return GetEntry()->GetMtime(); | |
| 157 } | |
| 158 | |
| 159 bool BaseNode::GetIsFolder() const { | |
| 160 return GetEntry()->GetIsDir(); | |
| 161 } | |
| 162 | |
| 163 bool BaseNode::GetIsPermanentFolder() const { | |
| 164 bool is_permanent_folder = !GetEntry()->GetUniqueServerTag().empty(); | |
| 165 if (is_permanent_folder) { | |
| 166 // If the node is a permanent folder it must also have IS_DIR bit set, | |
| 167 // except some nigori nodes on old accounts. | |
| 168 DCHECK(GetIsFolder() || GetModelType() == NIGORI); | |
| 169 } | |
| 170 return is_permanent_folder; | |
| 171 } | |
| 172 | |
| 173 std::string BaseNode::GetTitle() const { | |
| 174 std::string result; | |
| 175 // TODO(zea): refactor bookmarks to not need this functionality. | |
| 176 if (BOOKMARKS == GetModelType() && | |
| 177 GetEntry()->GetSpecifics().has_encrypted()) { | |
| 178 // Special case for legacy bookmarks dealing with encryption. | |
| 179 ServerNameToSyncAPIName(GetBookmarkSpecifics().title(), &result); | |
| 180 } else { | |
| 181 ServerNameToSyncAPIName(GetEntry()->GetNonUniqueName(), | |
| 182 &result); | |
| 183 } | |
| 184 return result; | |
| 185 } | |
| 186 | |
| 187 bool BaseNode::HasChildren() const { | |
| 188 syncable::Directory* dir = GetTransaction()->GetDirectory(); | |
| 189 syncable::BaseTransaction* trans = GetTransaction()->GetWrappedTrans(); | |
| 190 return dir->HasChildren(trans, GetEntry()->GetId()); | |
| 191 } | |
| 192 | |
| 193 int64_t BaseNode::GetPredecessorId() const { | |
| 194 syncable::Id id_string = GetEntry()->GetPredecessorId(); | |
| 195 if (id_string.IsNull()) | |
| 196 return kInvalidId; | |
| 197 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string); | |
| 198 } | |
| 199 | |
| 200 int64_t BaseNode::GetSuccessorId() const { | |
| 201 syncable::Id id_string = GetEntry()->GetSuccessorId(); | |
| 202 if (id_string.IsNull()) | |
| 203 return kInvalidId; | |
| 204 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string); | |
| 205 } | |
| 206 | |
| 207 int64_t BaseNode::GetFirstChildId() const { | |
| 208 syncable::Id id_string = GetEntry()->GetFirstChildId(); | |
| 209 if (id_string.IsNull()) | |
| 210 return kInvalidId; | |
| 211 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string); | |
| 212 } | |
| 213 | |
| 214 void BaseNode::GetChildIds(std::vector<int64_t>* result) const { | |
| 215 GetEntry()->GetChildHandles(result); | |
| 216 } | |
| 217 | |
| 218 int BaseNode::GetTotalNodeCount() const { | |
| 219 return GetEntry()->GetTotalNodeCount(); | |
| 220 } | |
| 221 | |
| 222 int BaseNode::GetPositionIndex() const { | |
| 223 return GetEntry()->GetPositionIndex(); | |
| 224 } | |
| 225 | |
| 226 base::DictionaryValue* BaseNode::ToValue() const { | |
| 227 return GetEntry()->ToValue(GetTransaction()->GetCryptographer()); | |
| 228 } | |
| 229 | |
| 230 int64_t BaseNode::GetExternalId() const { | |
| 231 return GetEntry()->GetLocalExternalId(); | |
| 232 } | |
| 233 | |
| 234 const syncable::Id& BaseNode::GetSyncId() const { | |
| 235 return GetEntry()->GetId(); | |
| 236 } | |
| 237 | |
| 238 const sync_pb::BookmarkSpecifics& BaseNode::GetBookmarkSpecifics() const { | |
| 239 DCHECK_EQ(GetModelType(), BOOKMARKS); | |
| 240 return GetEntitySpecifics().bookmark(); | |
| 241 } | |
| 242 | |
| 243 const sync_pb::NigoriSpecifics& BaseNode::GetNigoriSpecifics() const { | |
| 244 DCHECK_EQ(GetModelType(), NIGORI); | |
| 245 return GetEntitySpecifics().nigori(); | |
| 246 } | |
| 247 | |
| 248 const sync_pb::PasswordSpecificsData& BaseNode::GetPasswordSpecifics() const { | |
| 249 DCHECK_EQ(GetModelType(), PASSWORDS); | |
| 250 return *password_data_; | |
| 251 } | |
| 252 | |
| 253 const sync_pb::TypedUrlSpecifics& BaseNode::GetTypedUrlSpecifics() const { | |
| 254 DCHECK_EQ(GetModelType(), TYPED_URLS); | |
| 255 return GetEntitySpecifics().typed_url(); | |
| 256 } | |
| 257 | |
| 258 const sync_pb::ExperimentsSpecifics& BaseNode::GetExperimentsSpecifics() const { | |
| 259 DCHECK_EQ(GetModelType(), EXPERIMENTS); | |
| 260 return GetEntitySpecifics().experiments(); | |
| 261 } | |
| 262 | |
| 263 const sync_pb::EntitySpecifics& BaseNode::GetEntitySpecifics() const { | |
| 264 return GetUnencryptedSpecifics(GetEntry()); | |
| 265 } | |
| 266 | |
| 267 ModelType BaseNode::GetModelType() const { | |
| 268 return GetEntry()->GetModelType(); | |
| 269 } | |
| 270 | |
| 271 const syncer::AttachmentIdList BaseNode::GetAttachmentIds() const { | |
| 272 AttachmentIdList result; | |
| 273 const sync_pb::AttachmentMetadata& metadata = | |
| 274 GetEntry()->GetAttachmentMetadata(); | |
| 275 for (int i = 0; i < metadata.record_size(); ++i) { | |
| 276 result.push_back(AttachmentId::CreateFromProto(metadata.record(i).id())); | |
| 277 } | |
| 278 return result; | |
| 279 } | |
| 280 | |
| 281 void BaseNode::SetUnencryptedSpecifics( | |
| 282 const sync_pb::EntitySpecifics& specifics) { | |
| 283 ModelType type = GetModelTypeFromSpecifics(specifics); | |
| 284 DCHECK_NE(UNSPECIFIED, type); | |
| 285 if (GetModelType() != UNSPECIFIED) { | |
| 286 DCHECK_EQ(GetModelType(), type); | |
| 287 } | |
| 288 unencrypted_data_.CopyFrom(specifics); | |
| 289 } | |
| 290 | |
| 291 } // namespace syncer | |
| OLD | NEW |