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 // All subclasses of BaseNode must provide a way to initialize themselves by | |
60 // doing an ID lookup. Returns false on failure. An invalid or deleted | |
61 // ID will result in failure. | |
62 virtual bool InitByIdLookup(int64 id) = 0; | |
63 | |
64 // All subclasses of BaseNode must also provide a way to initialize themselves | |
65 // by doing a client tag lookup. Returns false on failure. A deleted node | |
66 // will return FALSE. | |
67 virtual bool InitByClientTagLookup(syncable::ModelType model_type, | |
68 const std::string& tag) = 0; | |
69 | |
70 // Each object is identified by a 64-bit id (internally, the syncable | |
71 // metahandle). These ids are strictly local handles. They will persist | |
72 // on this client, but the same object on a different client may have a | |
73 // different ID value. | |
74 virtual int64 GetId() const; | |
75 | |
76 // Returns the modification time of the object. | |
77 const base::Time& GetModificationTime() const; | |
78 | |
79 // Nodes are hierarchically arranged into a single-rooted tree. | |
80 // InitByRootLookup on ReadNode allows access to the root. GetParentId is | |
81 // how you find a node's parent. | |
82 int64 GetParentId() const; | |
83 | |
84 // Nodes are either folders or not. This corresponds to the IS_DIR property | |
85 // of syncable::Entry. | |
86 bool GetIsFolder() const; | |
87 | |
88 // Returns the title of the object. | |
89 // Uniqueness of the title is not enforced on siblings -- it is not an error | |
90 // for two children to share a title. | |
91 std::string GetTitle() const; | |
92 | |
93 // Returns the model type of this object. The model type is set at node | |
94 // creation time and is expected never to change. | |
95 syncable::ModelType GetModelType() const; | |
96 | |
97 // Getter specific to the BOOKMARK datatype. Returns protobuf | |
98 // data. Can only be called if GetModelType() == BOOKMARK. | |
99 const sync_pb::BookmarkSpecifics& GetBookmarkSpecifics() const; | |
100 | |
101 // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above. | |
102 // Returns the URL of a bookmark object. | |
103 // TODO(ncarter): Remove this datatype-specific accessor. | |
104 GURL GetURL() const; | |
105 | |
106 // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above. | |
107 // Fill in a vector with the byte data of this node's favicon. Assumes | |
108 // that the node is a bookmark. | |
109 // Favicons are expected to be PNG images, and though no verification is | |
110 // done on the syncapi client of this, the server may reject favicon updates | |
111 // that are invalid for whatever reason. | |
112 // TODO(ncarter): Remove this datatype-specific accessor. | |
113 void GetFaviconBytes(std::vector<unsigned char>* output) const; | |
114 | |
115 // Getter specific to the APPS datatype. Returns protobuf | |
116 // data. Can only be called if GetModelType() == APPS. | |
117 const sync_pb::AppSpecifics& GetAppSpecifics() const; | |
118 | |
119 // Getter specific to the AUTOFILL datatype. Returns protobuf | |
120 // data. Can only be called if GetModelType() == AUTOFILL. | |
121 const sync_pb::AutofillSpecifics& GetAutofillSpecifics() const; | |
122 | |
123 virtual const sync_pb::AutofillProfileSpecifics& | |
124 GetAutofillProfileSpecifics() 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 PREFERENCE datatype. Returns protobuf | |
135 // data. Can only be called if GetModelType() == PREFERENCE. | |
136 const sync_pb::PreferenceSpecifics& GetPreferenceSpecifics() const; | |
137 | |
138 // Getter specific to the THEME datatype. Returns protobuf | |
139 // data. Can only be called if GetModelType() == THEME. | |
140 const sync_pb::ThemeSpecifics& GetThemeSpecifics() const; | |
141 | |
142 // Getter specific to the TYPED_URLS datatype. Returns protobuf | |
143 // data. Can only be called if GetModelType() == TYPED_URLS. | |
144 const sync_pb::TypedUrlSpecifics& GetTypedUrlSpecifics() const; | |
145 | |
146 // Getter specific to the EXTENSIONS datatype. Returns protobuf | |
147 // data. Can only be called if GetModelType() == EXTENSIONS. | |
148 const sync_pb::ExtensionSpecifics& GetExtensionSpecifics() const; | |
149 | |
150 // Getter specific to the SESSIONS datatype. Returns protobuf | |
151 // data. Can only be called if GetModelType() == SESSIONS. | |
152 const sync_pb::SessionSpecifics& GetSessionSpecifics() const; | |
153 | |
154 const sync_pb::EntitySpecifics& GetEntitySpecifics() const; | |
155 | |
156 // Returns the local external ID associated with the node. | |
157 int64 GetExternalId() const; | |
158 | |
159 // Returns true iff this node has children. | |
160 bool HasChildren() const; | |
161 | |
162 // Return the ID of the node immediately before this in the sibling order. | |
163 // For the first node in the ordering, return 0. | |
164 int64 GetPredecessorId() const; | |
165 | |
166 // Return the ID of the node immediately after this in the sibling order. | |
167 // For the last node in the ordering, return 0. | |
168 int64 GetSuccessorId() const; | |
169 | |
170 // Return the ID of the first child of this node. If this node has no | |
171 // children, return 0. | |
172 int64 GetFirstChildId() const; | |
173 | |
174 // These virtual accessors provide access to data members of derived classes. | |
175 virtual const syncable::Entry* GetEntry() const = 0; | |
176 virtual const BaseTransaction* GetTransaction() const = 0; | |
177 | |
178 // Dumps a summary of node info into a DictionaryValue and returns it. | |
179 // Transfers ownership of the DictionaryValue to the caller. | |
180 base::DictionaryValue* GetSummaryAsValue() const; | |
181 | |
182 // Dumps all node details into a DictionaryValue and returns it. | |
183 // Transfers ownership of the DictionaryValue to the caller. | |
184 base::DictionaryValue* GetDetailsAsValue() const; | |
185 | |
186 protected: | |
187 BaseNode(); | |
188 virtual ~BaseNode(); | |
189 // The server has a size limit on client tags, so we generate a fixed length | |
190 // hash locally. This also ensures that ModelTypes have unique namespaces. | |
191 static std::string GenerateSyncableHash(syncable::ModelType model_type, | |
192 const std::string& client_tag); | |
193 | |
194 // Determines whether part of the entry is encrypted, and if so attempts to | |
195 // decrypt it. Unless decryption is necessary and fails, this will always | |
196 // return |true|. If the contents are encrypted, the decrypted data will be | |
197 // stored in |unencrypted_data_|. | |
198 // This method is invoked once when the BaseNode is initialized. | |
199 bool DecryptIfNecessary(); | |
200 | |
201 // Returns the unencrypted specifics associated with |entry|. If |entry| was | |
202 // not encrypted, it directly returns |entry|'s EntitySpecifics. Otherwise, | |
203 // returns |unencrypted_data_|. | |
204 const sync_pb::EntitySpecifics& GetUnencryptedSpecifics( | |
205 const syncable::Entry* entry) const; | |
206 | |
207 // Copy |specifics| into |unencrypted_data_|. | |
208 void SetUnencryptedSpecifics(const sync_pb::EntitySpecifics& specifics); | |
209 | |
210 private: | |
211 // Have to friend the test class as well to allow member functions to access | |
212 // protected/private BaseNode methods. | |
213 friend class SyncManagerTest; | |
214 FRIEND_TEST_ALL_PREFIXES(SyncApiTest, GenerateSyncableHash); | |
215 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdateEntryWithEncryption); | |
216 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, | |
217 UpdatePasswordSetEntitySpecificsNoChange); | |
218 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordSetPasswordSpecifics); | |
219 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordNewPassphrase); | |
220 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, UpdatePasswordReencryptEverything); | |
221 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetBookmarkTitle); | |
222 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetBookmarkTitleWithEncryption); | |
223 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetNonBookmarkTitle); | |
224 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetNonBookmarkTitleWithEncryption); | |
225 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, SetPreviouslyEncryptedSpecifics); | |
226 | |
227 void* operator new(size_t size); // Node is meant for stack use only. | |
228 | |
229 // A holder for the unencrypted data stored in an encrypted node. | |
230 sync_pb::EntitySpecifics unencrypted_data_; | |
231 | |
232 // Same as |unencrypted_data_|, but for legacy password encryption. | |
233 scoped_ptr<sync_pb::PasswordSpecificsData> password_data_; | |
234 | |
235 DISALLOW_COPY_AND_ASSIGN(BaseNode); | |
236 }; | |
237 | |
238 } // namespace sync_api | |
239 | |
240 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ | |
OLD | NEW |