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