OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "sync/internal_api/public/base_node.h" | 5 #include "components/sync/core/base_node.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <stack> | 9 #include <stack> |
10 | 10 |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "sync/internal_api/public/base_transaction.h" | 13 #include "components/sync/base/time.h" |
14 #include "sync/internal_api/syncapi_internal.h" | 14 #include "components/sync/core/base_transaction.h" |
15 #include "sync/protocol/app_specifics.pb.h" | 15 #include "components/sync/core_impl/syncapi_internal.h" |
16 #include "sync/protocol/autofill_specifics.pb.h" | 16 #include "components/sync/protocol/app_specifics.pb.h" |
17 #include "sync/protocol/bookmark_specifics.pb.h" | 17 #include "components/sync/protocol/autofill_specifics.pb.h" |
18 #include "sync/protocol/extension_specifics.pb.h" | 18 #include "components/sync/protocol/bookmark_specifics.pb.h" |
19 #include "sync/protocol/nigori_specifics.pb.h" | 19 #include "components/sync/protocol/extension_specifics.pb.h" |
20 #include "sync/protocol/password_specifics.pb.h" | 20 #include "components/sync/protocol/nigori_specifics.pb.h" |
21 #include "sync/protocol/session_specifics.pb.h" | 21 #include "components/sync/protocol/password_specifics.pb.h" |
22 #include "sync/protocol/theme_specifics.pb.h" | 22 #include "components/sync/protocol/session_specifics.pb.h" |
23 #include "sync/protocol/typed_url_specifics.pb.h" | 23 #include "components/sync/protocol/theme_specifics.pb.h" |
24 #include "sync/syncable/directory.h" | 24 #include "components/sync/protocol/typed_url_specifics.pb.h" |
25 #include "sync/syncable/entry.h" | 25 #include "components/sync/syncable/directory.h" |
26 #include "sync/syncable/syncable_base_transaction.h" | 26 #include "components/sync/syncable/entry.h" |
27 #include "sync/syncable/syncable_id.h" | 27 #include "components/sync/syncable/syncable_base_transaction.h" |
28 #include "sync/util/time.h" | 28 #include "components/sync/syncable/syncable_id.h" |
29 | 29 |
30 using sync_pb::AutofillProfileSpecifics; | 30 using sync_pb::AutofillProfileSpecifics; |
31 | 31 |
32 namespace syncer { | 32 namespace syncer { |
33 | 33 |
34 using syncable::SPECIFICS; | 34 using syncable::SPECIFICS; |
35 | 35 |
36 // Helper function to look up the int64_t metahandle of an object given the ID | 36 // Helper function to look up the int64_t metahandle of an object given the ID |
37 // string. | 37 // string. |
38 static int64_t IdToMetahandle(syncable::BaseTransaction* trans, | 38 static int64_t IdToMetahandle(syncable::BaseTransaction* trans, |
39 const syncable::Id& id) { | 39 const syncable::Id& id) { |
40 if (id.IsNull()) | 40 if (id.IsNull()) |
41 return kInvalidId; | 41 return kInvalidId; |
42 syncable::Entry entry(trans, syncable::GET_BY_ID, id); | 42 syncable::Entry entry(trans, syncable::GET_BY_ID, id); |
43 if (!entry.good()) | 43 if (!entry.good()) |
44 return kInvalidId; | 44 return kInvalidId; |
45 return entry.GetMetahandle(); | 45 return entry.GetMetahandle(); |
46 } | 46 } |
47 | 47 |
48 BaseNode::BaseNode() : password_data_(new sync_pb::PasswordSpecificsData) {} | 48 BaseNode::BaseNode() : password_data_(new sync_pb::PasswordSpecificsData) {} |
49 | 49 |
50 BaseNode::~BaseNode() {} | 50 BaseNode::~BaseNode() {} |
51 | 51 |
52 bool BaseNode::DecryptIfNecessary() { | 52 bool BaseNode::DecryptIfNecessary() { |
53 if (GetIsPermanentFolder()) | 53 if (GetIsPermanentFolder()) |
54 return true; // Ignore unique folders. | 54 return true; // Ignore unique folders. |
55 const sync_pb::EntitySpecifics& specifics = | 55 const sync_pb::EntitySpecifics& specifics = GetEntry()->GetSpecifics(); |
56 GetEntry()->GetSpecifics(); | |
57 if (specifics.has_password()) { | 56 if (specifics.has_password()) { |
58 // Passwords have their own legacy encryption structure. | 57 // Passwords have their own legacy encryption structure. |
59 std::unique_ptr<sync_pb::PasswordSpecificsData> data( | 58 std::unique_ptr<sync_pb::PasswordSpecificsData> data( |
60 DecryptPasswordSpecifics(specifics, | 59 DecryptPasswordSpecifics(specifics, |
61 GetTransaction()->GetCryptographer())); | 60 GetTransaction()->GetCryptographer())); |
62 if (!data) { | 61 if (!data) { |
63 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( | 62 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( |
64 FROM_HERE, std::string("Failed to decrypt encrypted node of type ") + | 63 FROM_HERE, std::string("Failed to decrypt encrypted node of type ") + |
65 ModelTypeToString(GetModelType())); | 64 ModelTypeToString(GetModelType())); |
66 return false; | 65 return false; |
67 } | 66 } |
68 password_data_.swap(data); | 67 password_data_.swap(data); |
69 return true; | 68 return true; |
70 } | 69 } |
71 | 70 |
72 // We assume any node with the encrypted field set has encrypted data and if | 71 // 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 | 72 // 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, | 73 // 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 | 74 // we fill the unencrypted_data_ with a copy of the bookmark specifics that |
76 // follows the new bookmarks format. | 75 // follows the new bookmarks format. |
77 if (!specifics.has_encrypted()) { | 76 if (!specifics.has_encrypted()) { |
78 if (GetModelType() == BOOKMARKS && | 77 if (GetModelType() == BOOKMARKS && !specifics.bookmark().has_title() && |
79 !specifics.bookmark().has_title() && | |
80 !GetTitle().empty()) { // Last check ensures this isn't a new node. | 78 !GetTitle().empty()) { // Last check ensures this isn't a new node. |
81 // We need to fill in the title. | 79 // We need to fill in the title. |
82 std::string title = GetTitle(); | 80 std::string title = GetTitle(); |
83 std::string server_legal_title; | 81 std::string server_legal_title; |
84 SyncAPINameToServerName(title, &server_legal_title); | 82 SyncAPINameToServerName(title, &server_legal_title); |
85 DVLOG(1) << "Reading from legacy bookmark, manually returning title " | 83 DVLOG(1) << "Reading from legacy bookmark, manually returning title " |
86 << title; | 84 << title; |
87 unencrypted_data_.CopyFrom(specifics); | 85 unencrypted_data_.CopyFrom(specifics); |
88 unencrypted_data_.mutable_bookmark()->set_title( | 86 unencrypted_data_.mutable_bookmark()->set_title(server_legal_title); |
89 server_legal_title); | |
90 } | 87 } |
91 return true; | 88 return true; |
92 } | 89 } |
93 | 90 |
94 const sync_pb::EncryptedData& encrypted = specifics.encrypted(); | 91 const sync_pb::EncryptedData& encrypted = specifics.encrypted(); |
95 std::string plaintext_data = GetTransaction()->GetCryptographer()-> | 92 std::string plaintext_data = |
96 DecryptToString(encrypted); | 93 GetTransaction()->GetCryptographer()->DecryptToString(encrypted); |
97 if (plaintext_data.length() == 0) { | 94 if (plaintext_data.length() == 0) { |
98 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( | 95 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( |
99 FROM_HERE, std::string("Failed to decrypt encrypted node of type ") + | 96 FROM_HERE, std::string("Failed to decrypt encrypted node of type ") + |
100 ModelTypeToString(GetModelType())); | 97 ModelTypeToString(GetModelType())); |
101 return false; | 98 return false; |
102 } else if (!unencrypted_data_.ParseFromString(plaintext_data)) { | 99 } else if (!unencrypted_data_.ParseFromString(plaintext_data)) { |
103 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( | 100 GetTransaction()->GetWrappedTrans()->OnUnrecoverableError( |
104 FROM_HERE, std::string("Failed to parse encrypted node of type ") + | 101 FROM_HERE, std::string("Failed to parse encrypted node of type ") + |
105 ModelTypeToString(GetModelType())); | 102 ModelTypeToString(GetModelType())); |
106 return false; | 103 return false; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 168 } |
172 | 169 |
173 std::string BaseNode::GetTitle() const { | 170 std::string BaseNode::GetTitle() const { |
174 std::string result; | 171 std::string result; |
175 // TODO(zea): refactor bookmarks to not need this functionality. | 172 // TODO(zea): refactor bookmarks to not need this functionality. |
176 if (BOOKMARKS == GetModelType() && | 173 if (BOOKMARKS == GetModelType() && |
177 GetEntry()->GetSpecifics().has_encrypted()) { | 174 GetEntry()->GetSpecifics().has_encrypted()) { |
178 // Special case for legacy bookmarks dealing with encryption. | 175 // Special case for legacy bookmarks dealing with encryption. |
179 ServerNameToSyncAPIName(GetBookmarkSpecifics().title(), &result); | 176 ServerNameToSyncAPIName(GetBookmarkSpecifics().title(), &result); |
180 } else { | 177 } else { |
181 ServerNameToSyncAPIName(GetEntry()->GetNonUniqueName(), | 178 ServerNameToSyncAPIName(GetEntry()->GetNonUniqueName(), &result); |
182 &result); | |
183 } | 179 } |
184 return result; | 180 return result; |
185 } | 181 } |
186 | 182 |
187 bool BaseNode::HasChildren() const { | 183 bool BaseNode::HasChildren() const { |
188 syncable::Directory* dir = GetTransaction()->GetDirectory(); | 184 syncable::Directory* dir = GetTransaction()->GetDirectory(); |
189 syncable::BaseTransaction* trans = GetTransaction()->GetWrappedTrans(); | 185 syncable::BaseTransaction* trans = GetTransaction()->GetWrappedTrans(); |
190 return dir->HasChildren(trans, GetEntry()->GetId()); | 186 return dir->HasChildren(trans, GetEntry()->GetId()); |
191 } | 187 } |
192 | 188 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 const sync_pb::EntitySpecifics& specifics) { | 278 const sync_pb::EntitySpecifics& specifics) { |
283 ModelType type = GetModelTypeFromSpecifics(specifics); | 279 ModelType type = GetModelTypeFromSpecifics(specifics); |
284 DCHECK_NE(UNSPECIFIED, type); | 280 DCHECK_NE(UNSPECIFIED, type); |
285 if (GetModelType() != UNSPECIFIED) { | 281 if (GetModelType() != UNSPECIFIED) { |
286 DCHECK_EQ(GetModelType(), type); | 282 DCHECK_EQ(GetModelType(), type); |
287 } | 283 } |
288 unencrypted_data_.CopyFrom(specifics); | 284 unencrypted_data_.CopyFrom(specifics); |
289 } | 285 } |
290 | 286 |
291 } // namespace syncer | 287 } // namespace syncer |
OLD | NEW |