OLD | NEW |
| (Empty) |
1 // Copyright 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 #ifndef COMPONENTS_SYNC_CORE_BASE_NODE_H_ | |
6 #define COMPONENTS_SYNC_CORE_BASE_NODE_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/gtest_prod_util.h" | |
16 #include "base/macros.h" | |
17 #include "base/time/time.h" | |
18 #include "components/sync/base/model_type.h" | |
19 #include "components/sync/model/attachments/attachment.h" | |
20 #include "components/sync/protocol/sync.pb.h" | |
21 #include "url/gurl.h" | |
22 | |
23 // Forward declarations of internal class types so that sync API objects | |
24 // may have opaque pointers to these types. | |
25 namespace base { | |
26 class DictionaryValue; | |
27 } | |
28 | |
29 namespace sync_pb { | |
30 class AppSpecifics; | |
31 class AutofillSpecifics; | |
32 class AutofillProfileSpecifics; | |
33 class BookmarkSpecifics; | |
34 class EntitySpecifics; | |
35 class ExtensionSpecifics; | |
36 class SessionSpecifics; | |
37 class NigoriSpecifics; | |
38 class PreferenceSpecifics; | |
39 class PasswordSpecificsData; | |
40 class ThemeSpecifics; | |
41 class TypedUrlSpecifics; | |
42 } // namespace sync_pb | |
43 | |
44 namespace syncer { | |
45 | |
46 class BaseTransaction; | |
47 | |
48 namespace syncable { | |
49 class BaseTransaction; | |
50 class Entry; | |
51 class Id; | |
52 } | |
53 | |
54 // A valid BaseNode will never have an ID of zero. | |
55 static const int64_t kInvalidId = 0; | |
56 | |
57 // BaseNode wraps syncable::Entry, and corresponds to a single object's state. | |
58 // This, like syncable::Entry, is intended for use on the stack. A valid | |
59 // transaction is necessary to create a BaseNode or any of its children. | |
60 // Unlike syncable::Entry, a sync API BaseNode is identified primarily by its | |
61 // int64_t metahandle, which we call an ID here. | |
62 class BaseNode { | |
63 public: | |
64 // Enumerates the possible outcomes of trying to initialize a sync node. | |
65 enum InitByLookupResult { | |
66 INIT_OK, | |
67 // Could not find an entry matching the lookup criteria. | |
68 INIT_FAILED_ENTRY_NOT_GOOD, | |
69 // Found an entry, but it is already deleted. | |
70 INIT_FAILED_ENTRY_IS_DEL, | |
71 // Found an entry, but was unable to decrypt. | |
72 INIT_FAILED_DECRYPT_IF_NECESSARY, | |
73 // A precondition was not met for calling init, such as legal input | |
74 // arguments. | |
75 INIT_FAILED_PRECONDITION, | |
76 }; | |
77 | |
78 // All subclasses of BaseNode must provide a way to initialize themselves by | |
79 // doing an ID lookup. Returns false on failure. An invalid or deleted | |
80 // ID will result in failure. | |
81 virtual InitByLookupResult InitByIdLookup(int64_t id) = 0; | |
82 | |
83 // All subclasses of BaseNode must also provide a way to initialize themselves | |
84 // by doing a client tag lookup. Returns false on failure. A deleted node | |
85 // will return FALSE. | |
86 virtual InitByLookupResult InitByClientTagLookup(ModelType model_type, | |
87 const std::string& tag) = 0; | |
88 | |
89 // Each object is identified by a 64-bit id (internally, the syncable | |
90 // metahandle). These ids are strictly local handles. They will persist | |
91 // on this client, but the same object on a different client may have a | |
92 // different ID value. | |
93 virtual int64_t GetId() const; | |
94 | |
95 // Returns the modification time of the object. | |
96 base::Time GetModificationTime() const; | |
97 | |
98 // Nodes are hierarchically arranged into a single-rooted tree. | |
99 // InitByRootLookup on ReadNode allows access to the root. GetParentId is | |
100 // how you find a node's parent. | |
101 int64_t GetParentId() const; | |
102 | |
103 // Nodes are either folders or not. This corresponds to the IS_DIR property | |
104 // of syncable::Entry. | |
105 bool GetIsFolder() const; | |
106 | |
107 // Specifies whether node is a permanent folder. This is true when | |
108 // UNIQUE_SERVER_TAG property of syncable::Entry is non-empty. | |
109 bool GetIsPermanentFolder() const; | |
110 | |
111 // Returns the title of the object. | |
112 // Uniqueness of the title is not enforced on siblings -- it is not an error | |
113 // for two children to share a title. | |
114 std::string GetTitle() const; | |
115 | |
116 // Returns the model type of this object. The model type is set at node | |
117 // creation time and is expected never to change. | |
118 ModelType GetModelType() const; | |
119 | |
120 // Getter specific to the BOOKMARK datatype. Returns protobuf | |
121 // data. Can only be called if GetModelType() == BOOKMARK. | |
122 const sync_pb::BookmarkSpecifics& GetBookmarkSpecifics() const; | |
123 | |
124 // Getter specific to the NIGORI datatype. Returns protobuf | |
125 // data. Can only be called if GetModelType() == NIGORI. | |
126 const sync_pb::NigoriSpecifics& GetNigoriSpecifics() const; | |
127 | |
128 // Getter specific to the PASSWORD datatype. Returns protobuf | |
129 // data. Can only be called if GetModelType() == PASSWORD. | |
130 const sync_pb::PasswordSpecificsData& GetPasswordSpecifics() const; | |
131 | |
132 // Getter specific to the TYPED_URLS datatype. Returns protobuf | |
133 // data. Can only be called if GetModelType() == TYPED_URLS. | |
134 const sync_pb::TypedUrlSpecifics& GetTypedUrlSpecifics() const; | |
135 | |
136 // Getter specific to the EXPERIMENTS datatype. Returns protobuf | |
137 // data. Can only be called if GetModelType() == EXPERIMENTS. | |
138 const sync_pb::ExperimentsSpecifics& GetExperimentsSpecifics() const; | |
139 | |
140 const sync_pb::EntitySpecifics& GetEntitySpecifics() const; | |
141 | |
142 // Returns the local external ID associated with the node. | |
143 int64_t GetExternalId() const; | |
144 | |
145 // Returns the internal syncable ID associated with the node. | |
146 const syncable::Id& GetSyncId() const; | |
147 | |
148 // Returns true iff this node has children. | |
149 bool HasChildren() const; | |
150 | |
151 // Return the ID of the node immediately before this in the sibling order. | |
152 // For the first node in the ordering, return 0. | |
153 int64_t GetPredecessorId() const; | |
154 | |
155 // Return the ID of the node immediately after this in the sibling order. | |
156 // For the last node in the ordering, return 0. | |
157 int64_t GetSuccessorId() const; | |
158 | |
159 // Return the ID of the first child of this node. If this node has no | |
160 // children, return 0. | |
161 int64_t GetFirstChildId() const; | |
162 | |
163 // Returns the IDs of the children of this node. | |
164 // If this type supports user-defined positions the returned IDs will be in | |
165 // the correct order. | |
166 void GetChildIds(std::vector<int64_t>* result) const; | |
167 | |
168 // Returns the total number of nodes including and beneath this node. | |
169 // Recursively iterates through all children. | |
170 int GetTotalNodeCount() const; | |
171 | |
172 // Returns this item's position within its parent. | |
173 // Do not call this function on items that do not support positioning | |
174 // (ie. non-bookmarks). | |
175 int GetPositionIndex() const; | |
176 | |
177 // Returns this item's attachment ids. | |
178 const AttachmentIdList GetAttachmentIds() const; | |
179 | |
180 // Returns a base::DictionaryValue serialization of this node. | |
181 base::DictionaryValue* ToValue() const; | |
182 | |
183 protected: | |
184 BaseNode(); | |
185 virtual ~BaseNode(); | |
186 | |
187 // These virtual accessors provide access to data members of derived classes. | |
188 virtual const syncable::Entry* GetEntry() const = 0; | |
189 virtual const BaseTransaction* GetTransaction() const = 0; | |
190 | |
191 // Determines whether part of the entry is encrypted, and if so attempts to | |
192 // decrypt it. Unless decryption is necessary and fails, this will always | |
193 // return |true|. If the contents are encrypted, the decrypted data will be | |
194 // stored in |unencrypted_data_|. | |
195 // This method is invoked once when the BaseNode is initialized. | |
196 bool DecryptIfNecessary(); | |
197 | |
198 // Returns the unencrypted specifics associated with |entry|. If |entry| was | |
199 // not encrypted, it directly returns |entry|'s EntitySpecifics. Otherwise, | |
200 // returns |unencrypted_data_|. | |
201 const sync_pb::EntitySpecifics& GetUnencryptedSpecifics( | |
202 const syncable::Entry* entry) const; | |
203 | |
204 // Copy |specifics| into |unencrypted_data_|. | |
205 void SetUnencryptedSpecifics(const sync_pb::EntitySpecifics& specifics); | |
206 | |
207 private: | |
208 // Have to friend the test class as well to allow member functions to access | |
209 // protected/private BaseNode methods. | |
210 friend class SyncManagerTest; | |
211 FRIEND_TEST_ALL_PREFIXES(SyncApiTest, GenerateSyncableHash); | |
212 FRIEND_TEST_ALL_PREFIXES(SyncApiTest, WriteEmptyBookmarkTitle); | |
213 FRIEND_TEST_ALL_PREFIXES(SyncApiTest, WriteEncryptedTitle); | |
214 FRIEND_TEST_ALL_PREFIXES(SyncBackupManagerTest, NormalizeEntry); | |
215 FRIEND_TEST_ALL_PREFIXES(SyncBackupManagerTest, | |
216 PersistWithSwitchToSyncShutdown); | |
217 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdateEntryWithEncryption); | |
218 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, | |
219 UpdatePasswordSetEntitySpecificsNoChange); | |
220 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordSetPasswordSpecifics); | |
221 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordNewPassphrase); | |
222 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordReencryptEverything); | |
223 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetBookmarkTitle); | |
224 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetBookmarkTitleWithEncryption); | |
225 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetNonBookmarkTitle); | |
226 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetNonBookmarkTitleWithEncryption); | |
227 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetPreviouslyEncryptedSpecifics); | |
228 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, IncrementTransactionVersion); | |
229 | |
230 void* operator new(size_t size); // Node is meant for stack use only. | |
231 | |
232 // A holder for the unencrypted data stored in an encrypted node. | |
233 sync_pb::EntitySpecifics unencrypted_data_; | |
234 | |
235 // Same as |unencrypted_data_|, but for legacy password encryption. | |
236 std::unique_ptr<sync_pb::PasswordSpecificsData> password_data_; | |
237 | |
238 DISALLOW_COPY_AND_ASSIGN(BaseNode); | |
239 }; | |
240 | |
241 } // namespace syncer | |
242 | |
243 #endif // COMPONENTS_SYNC_CORE_BASE_NODE_H_ | |
OLD | NEW |