We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / sync / internal_api / base_node.cc
blob4d6d56fadbe88df2921a15c9b114d5595168158b
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/internal_api/public/base_node.h"
7 #include <stack>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "sync/internal_api/public/base_transaction.h"
12 #include "sync/internal_api/syncapi_internal.h"
13 #include "sync/protocol/app_specifics.pb.h"
14 #include "sync/protocol/autofill_specifics.pb.h"
15 #include "sync/protocol/bookmark_specifics.pb.h"
16 #include "sync/protocol/extension_specifics.pb.h"
17 #include "sync/protocol/nigori_specifics.pb.h"
18 #include "sync/protocol/password_specifics.pb.h"
19 #include "sync/protocol/session_specifics.pb.h"
20 #include "sync/protocol/theme_specifics.pb.h"
21 #include "sync/protocol/typed_url_specifics.pb.h"
22 #include "sync/syncable/directory.h"
23 #include "sync/syncable/entry.h"
24 #include "sync/syncable/syncable_id.h"
25 #include "sync/util/time.h"
27 using sync_pb::AutofillProfileSpecifics;
29 namespace syncer {
31 using syncable::SPECIFICS;
33 // Helper function to look up the int64 metahandle of an object given the ID
34 // string.
35 static int64 IdToMetahandle(syncable::BaseTransaction* trans,
36 const syncable::Id& id) {
37 if (id.IsNull())
38 return kInvalidId;
39 syncable::Entry entry(trans, syncable::GET_BY_ID, id);
40 if (!entry.good())
41 return kInvalidId;
42 return entry.GetMetahandle();
45 BaseNode::BaseNode() : password_data_(new sync_pb::PasswordSpecificsData) {}
47 BaseNode::~BaseNode() {}
49 bool BaseNode::DecryptIfNecessary() {
50 if (!GetEntry()->GetUniqueServerTag().empty())
51 return true; // Ignore unique folders.
52 const sync_pb::EntitySpecifics& specifics =
53 GetEntry()->GetSpecifics();
54 if (specifics.has_password()) {
55 // Passwords have their own legacy encryption structure.
56 scoped_ptr<sync_pb::PasswordSpecificsData> data(DecryptPasswordSpecifics(
57 specifics, GetTransaction()->GetCryptographer()));
58 if (!data) {
59 LOG(ERROR) << "Failed to decrypt password specifics.";
60 return false;
62 password_data_.swap(data);
63 return true;
66 // We assume any node with the encrypted field set has encrypted data and if
67 // not we have no work to do, with the exception of bookmarks. For bookmarks
68 // we must make sure the bookmarks data has the title field supplied. If not,
69 // we fill the unencrypted_data_ with a copy of the bookmark specifics that
70 // follows the new bookmarks format.
71 if (!specifics.has_encrypted()) {
72 if (GetModelType() == BOOKMARKS &&
73 !specifics.bookmark().has_title() &&
74 !GetTitle().empty()) { // Last check ensures this isn't a new node.
75 // We need to fill in the title.
76 std::string title = GetTitle();
77 std::string server_legal_title;
78 SyncAPINameToServerName(title, &server_legal_title);
79 DVLOG(1) << "Reading from legacy bookmark, manually returning title "
80 << title;
81 unencrypted_data_.CopyFrom(specifics);
82 unencrypted_data_.mutable_bookmark()->set_title(
83 server_legal_title);
85 return true;
88 const sync_pb::EncryptedData& encrypted = specifics.encrypted();
89 std::string plaintext_data = GetTransaction()->GetCryptographer()->
90 DecryptToString(encrypted);
91 if (plaintext_data.length() == 0) {
92 LOG(ERROR) << "Failed to decrypt encrypted node of type "
93 << ModelTypeToString(GetModelType()) << ".";
94 // Debugging for crbug.com/123223. We failed to decrypt the data, which
95 // means we applied an update without having the key or lost the key at a
96 // later point.
97 CHECK(false);
98 return false;
99 } else if (!unencrypted_data_.ParseFromString(plaintext_data)) {
100 // Debugging for crbug.com/123223. We should never succeed in decrypting
101 // but fail to parse into a protobuf.
102 CHECK(false);
103 return false;
105 DVLOG(2) << "Decrypted specifics of type "
106 << ModelTypeToString(GetModelType())
107 << " with content: " << plaintext_data;
108 return true;
111 const sync_pb::EntitySpecifics& BaseNode::GetUnencryptedSpecifics(
112 const syncable::Entry* entry) const {
113 const sync_pb::EntitySpecifics& specifics = entry->GetSpecifics();
114 if (specifics.has_encrypted()) {
115 DCHECK_NE(GetModelTypeFromSpecifics(unencrypted_data_), UNSPECIFIED);
116 return unencrypted_data_;
117 } else {
118 // Due to the change in bookmarks format, we need to check to see if this is
119 // a legacy bookmarks (and has no title field in the proto). If it is, we
120 // return the unencrypted_data_, which was filled in with the title by
121 // DecryptIfNecessary().
122 if (GetModelType() == BOOKMARKS) {
123 const sync_pb::BookmarkSpecifics& bookmark_specifics =
124 specifics.bookmark();
125 if (bookmark_specifics.has_title() ||
126 GetTitle().empty() || // For the empty node case
127 !GetEntry()->GetUniqueServerTag().empty()) {
128 // It's possible we previously had to convert and set
129 // |unencrypted_data_| but then wrote our own data, so we allow
130 // |unencrypted_data_| to be non-empty.
131 return specifics;
132 } else {
133 DCHECK_EQ(GetModelTypeFromSpecifics(unencrypted_data_), BOOKMARKS);
134 return unencrypted_data_;
136 } else {
137 DCHECK_EQ(GetModelTypeFromSpecifics(unencrypted_data_), UNSPECIFIED);
138 return specifics;
143 int64 BaseNode::GetParentId() const {
144 return IdToMetahandle(GetTransaction()->GetWrappedTrans(),
145 GetEntry()->GetParentId());
148 int64 BaseNode::GetId() const {
149 return GetEntry()->GetMetahandle();
152 base::Time BaseNode::GetModificationTime() const {
153 return GetEntry()->GetMtime();
156 bool BaseNode::GetIsFolder() const {
157 return GetEntry()->GetIsDir();
160 std::string BaseNode::GetTitle() const {
161 std::string result;
162 // TODO(zea): refactor bookmarks to not need this functionality.
163 if (BOOKMARKS == GetModelType() &&
164 GetEntry()->GetSpecifics().has_encrypted()) {
165 // Special case for legacy bookmarks dealing with encryption.
166 ServerNameToSyncAPIName(GetBookmarkSpecifics().title(), &result);
167 } else {
168 ServerNameToSyncAPIName(GetEntry()->GetNonUniqueName(),
169 &result);
171 return result;
174 bool BaseNode::HasChildren() const {
175 syncable::Directory* dir = GetTransaction()->GetDirectory();
176 syncable::BaseTransaction* trans = GetTransaction()->GetWrappedTrans();
177 return dir->HasChildren(trans, GetEntry()->GetId());
180 int64 BaseNode::GetPredecessorId() const {
181 syncable::Id id_string = GetEntry()->GetPredecessorId();
182 if (id_string.IsNull())
183 return kInvalidId;
184 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string);
187 int64 BaseNode::GetSuccessorId() const {
188 syncable::Id id_string = GetEntry()->GetSuccessorId();
189 if (id_string.IsNull())
190 return kInvalidId;
191 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string);
194 int64 BaseNode::GetFirstChildId() const {
195 syncable::Id id_string = GetEntry()->GetFirstChildId();
196 if (id_string.IsNull())
197 return kInvalidId;
198 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), id_string);
201 void BaseNode::GetChildIds(std::vector<int64>* result) const {
202 GetEntry()->GetChildHandles(result);
205 int BaseNode::GetTotalNodeCount() const {
206 return GetEntry()->GetTotalNodeCount();
209 int BaseNode::GetPositionIndex() const {
210 return GetEntry()->GetPositionIndex();
213 base::DictionaryValue* BaseNode::ToValue() const {
214 return GetEntry()->ToValue(GetTransaction()->GetCryptographer());
217 int64 BaseNode::GetExternalId() const {
218 return GetEntry()->GetLocalExternalId();
221 const sync_pb::AppSpecifics& BaseNode::GetAppSpecifics() const {
222 DCHECK_EQ(GetModelType(), APPS);
223 return GetEntitySpecifics().app();
226 const sync_pb::AutofillSpecifics& BaseNode::GetAutofillSpecifics() const {
227 DCHECK_EQ(GetModelType(), AUTOFILL);
228 return GetEntitySpecifics().autofill();
231 const AutofillProfileSpecifics& BaseNode::GetAutofillProfileSpecifics() const {
232 DCHECK_EQ(GetModelType(), AUTOFILL_PROFILE);
233 return GetEntitySpecifics().autofill_profile();
236 const sync_pb::BookmarkSpecifics& BaseNode::GetBookmarkSpecifics() const {
237 DCHECK_EQ(GetModelType(), BOOKMARKS);
238 return GetEntitySpecifics().bookmark();
241 const sync_pb::NigoriSpecifics& BaseNode::GetNigoriSpecifics() const {
242 DCHECK_EQ(GetModelType(), NIGORI);
243 return GetEntitySpecifics().nigori();
246 const sync_pb::PasswordSpecificsData& BaseNode::GetPasswordSpecifics() const {
247 DCHECK_EQ(GetModelType(), PASSWORDS);
248 return *password_data_;
251 const sync_pb::ThemeSpecifics& BaseNode::GetThemeSpecifics() const {
252 DCHECK_EQ(GetModelType(), THEMES);
253 return GetEntitySpecifics().theme();
256 const sync_pb::TypedUrlSpecifics& BaseNode::GetTypedUrlSpecifics() const {
257 DCHECK_EQ(GetModelType(), TYPED_URLS);
258 return GetEntitySpecifics().typed_url();
261 const sync_pb::ExtensionSpecifics& BaseNode::GetExtensionSpecifics() const {
262 DCHECK_EQ(GetModelType(), EXTENSIONS);
263 return GetEntitySpecifics().extension();
266 const sync_pb::SessionSpecifics& BaseNode::GetSessionSpecifics() const {
267 DCHECK_EQ(GetModelType(), SESSIONS);
268 return GetEntitySpecifics().session();
271 const sync_pb::DeviceInfoSpecifics& BaseNode::GetDeviceInfoSpecifics() const {
272 DCHECK_EQ(GetModelType(), DEVICE_INFO);
273 return GetEntitySpecifics().device_info();
276 const sync_pb::ExperimentsSpecifics& BaseNode::GetExperimentsSpecifics() const {
277 DCHECK_EQ(GetModelType(), EXPERIMENTS);
278 return GetEntitySpecifics().experiments();
281 const sync_pb::PriorityPreferenceSpecifics&
282 BaseNode::GetPriorityPreferenceSpecifics() const {
283 DCHECK_EQ(GetModelType(), PRIORITY_PREFERENCES);
284 return GetEntitySpecifics().priority_preference();
287 const sync_pb::EntitySpecifics& BaseNode::GetEntitySpecifics() const {
288 return GetUnencryptedSpecifics(GetEntry());
291 ModelType BaseNode::GetModelType() const {
292 return GetEntry()->GetModelType();
295 const syncer::AttachmentIdList BaseNode::GetAttachmentIds() const {
296 AttachmentIdList result;
297 const sync_pb::AttachmentMetadata& metadata =
298 GetEntry()->GetAttachmentMetadata();
299 for (int i = 0; i < metadata.record_size(); ++i) {
300 result.push_back(AttachmentId::CreateFromProto(metadata.record(i).id()));
302 return result;
305 void BaseNode::SetUnencryptedSpecifics(
306 const sync_pb::EntitySpecifics& specifics) {
307 ModelType type = GetModelTypeFromSpecifics(specifics);
308 DCHECK_NE(UNSPECIFIED, type);
309 if (GetModelType() != UNSPECIFIED) {
310 DCHECK_EQ(GetModelType(), type);
312 unencrypted_data_.CopyFrom(specifics);
315 } // namespace syncer