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 #ifndef CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ | |
6 #define CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/gtest_prod_util.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "googleurl/src/gurl.h" | |
16 #include "sync/protocol/sync.pb.h" | |
17 #include "sync/syncable/model_type.h" | |
18 | |
19 // Forward declarations of internal class types so that sync API objects | |
20 // may have opaque pointers to these types. | |
21 namespace base { | |
22 class DictionaryValue; | |
23 } | |
24 | |
25 namespace syncable { | |
26 class BaseTransaction; | |
27 class Entry; | |
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 } | |
44 | |
45 namespace sync_api { | |
46 | |
47 class BaseTransaction; | |
48 | |
49 // A valid BaseNode will never have an ID of zero. | |
50 static const int64 kInvalidId = 0; | |
51 | |
52 // BaseNode wraps syncable::Entry, and corresponds to a single object's state. | |
53 // This, like syncable::Entry, is intended for use on the stack. A valid | |
54 // transaction is necessary to create a BaseNode or any of its children. | |
55 // Unlike syncable::Entry, a sync API BaseNode is identified primarily by its | |
56 // int64 metahandle, which we call an ID here. | |
57 class BaseNode { | |
58 public: | |
59 // Enumerates the possible outcomes of trying to initialize a sync node. | |
60 enum InitByLookupResult { | |
61 INIT_OK, | |
62 // Could not find an entry matching the lookup criteria. | |
63 INIT_FAILED_ENTRY_NOT_GOOD, | |
64 // Found an entry, but it is already deleted. | |
65 INIT_FAILED_ENTRY_IS_DEL, | |
66 // Found an entry, but was unable to decrypt. | |
67 INIT_FAILED_DECRYPT_IF_NECESSARY, | |
68 // A precondition was not met for calling init, such as legal input | |
69 // arguments. | |
70 INIT_FAILED_PRECONDITION, | |
71 }; | |
72 | |
73 // All subclasses of BaseNode must provide a way to initialize themselves by | |
74 // doing an ID lookup. Returns false on failure. An invalid or deleted | |
75 // ID will result in failure. | |
76 virtual InitByLookupResult InitByIdLookup(int64 id) = 0; | |
77 | |
78 // All subclasses of BaseNode must also provide a way to initialize themselves | |
79 // by doing a client tag lookup. Returns false on failure. A deleted node | |
80 // will return FALSE. | |
81 virtual InitByLookupResult InitByClientTagLookup( | |
82 syncable::ModelType model_type, | |
83 const std::string& tag) = 0; | |
84 | |
85 // Each object is identified by a 64-bit id (internally, the syncable | |
86 // metahandle). These ids are strictly local handles. They will persist | |
87 // on this client, but the same object on a different client may have a | |
88 // different ID value. | |
89 virtual int64 GetId() const; | |
90 | |
91 // Returns the modification time of the object. | |
92 const base::Time& GetModificationTime() const; | |
93 | |
94 // Nodes are hierarchically arranged into a single-rooted tree. | |
95 // InitByRootLookup on ReadNode allows access to the root. GetParentId is | |
96 // how you find a node's parent. | |
97 int64 GetParentId() const; | |
98 | |
99 // Nodes are either folders or not. This corresponds to the IS_DIR property | |
100 // of syncable::Entry. | |
101 bool GetIsFolder() const; | |
102 | |
103 // Returns the title of the object. | |
104 // Uniqueness of the title is not enforced on siblings -- it is not an error | |
105 // for two children to share a title. | |
106 std::string GetTitle() const; | |
107 | |
108 // Returns the model type of this object. The model type is set at node | |
109 // creation time and is expected never to change. | |
110 syncable::ModelType GetModelType() const; | |
111 | |
112 // Getter specific to the BOOKMARK datatype. Returns protobuf | |
113 // data. Can only be called if GetModelType() == BOOKMARK. | |
114 const sync_pb::BookmarkSpecifics& GetBookmarkSpecifics() const; | |
115 | |
116 // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above. | |
117 // Returns the URL of a bookmark object. | |
118 // TODO(ncarter): Remove this datatype-specific accessor. | |
119 GURL GetURL() const; | |
120 | |
121 // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above. | |
122 // Fill in a vector with the byte data of this node's favicon. Assumes | |
123 // that the node is a bookmark. | |
124 // Favicons are expected to be PNG images, and though no verification is | |
125 // done on the syncapi client of this, the server may reject favicon updates | |
126 // that are invalid for whatever reason. | |
127 // TODO(ncarter): Remove this datatype-specific accessor. | |
128 void GetFaviconBytes(std::vector<unsigned char>* output) const; | |
129 | |
130 // Getter specific to the APPS datatype. Returns protobuf | |
131 // data. Can only be called if GetModelType() == APPS. | |
132 const sync_pb::AppSpecifics& GetAppSpecifics() const; | |
133 | |
134 // Getter specific to the AUTOFILL datatype. Returns protobuf | |
135 // data. Can only be called if GetModelType() == AUTOFILL. | |
136 const sync_pb::AutofillSpecifics& GetAutofillSpecifics() const; | |
137 | |
138 virtual const sync_pb::AutofillProfileSpecifics& | |
139 GetAutofillProfileSpecifics() const; | |
140 | |
141 // Getter specific to the NIGORI datatype. Returns protobuf | |
142 // data. Can only be called if GetModelType() == NIGORI. | |
143 const sync_pb::NigoriSpecifics& GetNigoriSpecifics() const; | |
144 | |
145 // Getter specific to the PASSWORD datatype. Returns protobuf | |
146 // data. Can only be called if GetModelType() == PASSWORD. | |
147 const sync_pb::PasswordSpecificsData& GetPasswordSpecifics() const; | |
148 | |
149 // Getter specific to the PREFERENCE datatype. Returns protobuf | |
150 // data. Can only be called if GetModelType() == PREFERENCE. | |
151 const sync_pb::PreferenceSpecifics& GetPreferenceSpecifics() const; | |
152 | |
153 // Getter specific to the THEME datatype. Returns protobuf | |
154 // data. Can only be called if GetModelType() == THEME. | |
155 const sync_pb::ThemeSpecifics& GetThemeSpecifics() const; | |
156 | |
157 // Getter specific to the TYPED_URLS datatype. Returns protobuf | |
158 // data. Can only be called if GetModelType() == TYPED_URLS. | |
159 const sync_pb::TypedUrlSpecifics& GetTypedUrlSpecifics() const; | |
160 | |
161 // Getter specific to the EXTENSIONS datatype. Returns protobuf | |
162 // data. Can only be called if GetModelType() == EXTENSIONS. | |
163 const sync_pb::ExtensionSpecifics& GetExtensionSpecifics() const; | |
164 | |
165 // Getter specific to the SESSIONS datatype. Returns protobuf | |
166 // data. Can only be called if GetModelType() == SESSIONS. | |
167 const sync_pb::SessionSpecifics& GetSessionSpecifics() const; | |
168 | |
169 const sync_pb::EntitySpecifics& GetEntitySpecifics() const; | |
170 | |
171 // Returns the local external ID associated with the node. | |
172 int64 GetExternalId() const; | |
173 | |
174 // Returns true iff this node has children. | |
175 bool HasChildren() const; | |
176 | |
177 // Return the ID of the node immediately before this in the sibling order. | |
178 // For the first node in the ordering, return 0. | |
179 int64 GetPredecessorId() const; | |
180 | |
181 // Return the ID of the node immediately after this in the sibling order. | |
182 // For the last node in the ordering, return 0. | |
183 int64 GetSuccessorId() const; | |
184 | |
185 // Return the ID of the first child of this node. If this node has no | |
186 // children, return 0. | |
187 int64 GetFirstChildId() const; | |
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 // Dumps a summary of node info into a DictionaryValue and returns it. | |
194 // Transfers ownership of the DictionaryValue to the caller. | |
195 base::DictionaryValue* GetSummaryAsValue() const; | |
196 | |
197 // Dumps all node details into a DictionaryValue and returns it. | |
198 // Transfers ownership of the DictionaryValue to the caller. | |
199 base::DictionaryValue* GetDetailsAsValue() const; | |
200 | |
201 protected: | |
202 BaseNode(); | |
203 virtual ~BaseNode(); | |
204 // The server has a size limit on client tags, so we generate a fixed length | |
205 // hash locally. This also ensures that ModelTypes have unique namespaces. | |
206 static std::string GenerateSyncableHash(syncable::ModelType model_type, | |
207 const std::string& client_tag); | |
208 | |
209 // Determines whether part of the entry is encrypted, and if so attempts to | |
210 // decrypt it. Unless decryption is necessary and fails, this will always | |
211 // return |true|. If the contents are encrypted, the decrypted data will be | |
212 // stored in |unencrypted_data_|. | |
213 // This method is invoked once when the BaseNode is initialized. | |
214 bool DecryptIfNecessary(); | |
215 | |
216 // Returns the unencrypted specifics associated with |entry|. If |entry| was | |
217 // not encrypted, it directly returns |entry|'s EntitySpecifics. Otherwise, | |
218 // returns |unencrypted_data_|. | |
219 const sync_pb::EntitySpecifics& GetUnencryptedSpecifics( | |
220 const syncable::Entry* entry) const; | |
221 | |
222 // Copy |specifics| into |unencrypted_data_|. | |
223 void SetUnencryptedSpecifics(const sync_pb::EntitySpecifics& specifics); | |
224 | |
225 private: | |
226 // Have to friend the test class as well to allow member functions to access | |
227 // protected/private BaseNode methods. | |
228 friend class SyncManagerTest; | |
229 FRIEND_TEST_ALL_PREFIXES(SyncApiTest, GenerateSyncableHash); | |
230 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdateEntryWithEncryption); | |
231 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, | |
232 UpdatePasswordSetEntitySpecificsNoChange); | |
233 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordSetPasswordSpecifics); | |
234 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordNewPassphrase); | |
235 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordReencryptEverything); | |
236 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetBookmarkTitle); | |
237 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetBookmarkTitleWithEncryption); | |
238 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetNonBookmarkTitle); | |
239 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetNonBookmarkTitleWithEncryption); | |
240 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetPreviouslyEncryptedSpecifics); | |
241 | |
242 void* operator new(size_t size); // Node is meant for stack use only. | |
243 | |
244 // A holder for the unencrypted data stored in an encrypted node. | |
245 sync_pb::EntitySpecifics unencrypted_data_; | |
246 | |
247 // Same as |unencrypted_data_|, but for legacy password encryption. | |
248 scoped_ptr<sync_pb::PasswordSpecificsData> password_data_; | |
249 | |
250 DISALLOW_COPY_AND_ASSIGN(BaseNode); | |
251 }; | |
252 | |
253 } // namespace sync_api | |
254 | |
255 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ | |
OLD | NEW |