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_WRITE_NODE_H_ | |
6 #define COMPONENTS_SYNC_CORE_WRITE_NODE_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/compiler_specific.h" | |
15 #include "base/gtest_prod_util.h" | |
16 #include "base/macros.h" | |
17 #include "components/sync/base/model_type.h" | |
18 #include "components/sync/core/base_node.h" | |
19 | |
20 namespace sync_pb { | |
21 class BookmarkSpecifics; | |
22 class EntitySpecifics; | |
23 class NigoriSpecifics; | |
24 class PasswordSpecificsData; | |
25 class TypedUrlSpecifics; | |
26 } | |
27 | |
28 namespace syncer { | |
29 | |
30 class Cryptographer; | |
31 class WriteTransaction; | |
32 | |
33 namespace syncable { | |
34 class Id; | |
35 class Entry; | |
36 class MutableEntry; | |
37 } | |
38 | |
39 // WriteNode extends BaseNode to add mutation, and wraps | |
40 // syncable::MutableEntry. A WriteTransaction is needed to create a WriteNode. | |
41 class WriteNode : public BaseNode { | |
42 public: | |
43 enum InitUniqueByCreationResult { | |
44 INIT_SUCCESS, | |
45 // The tag passed into this method was empty. | |
46 INIT_FAILED_EMPTY_TAG, | |
47 // The constructor for a new MutableEntry with the specified data failed. | |
48 INIT_FAILED_COULD_NOT_CREATE_ENTRY, | |
49 // Setting the predecessor failed | |
50 INIT_FAILED_SET_PREDECESSOR, | |
51 // Found existing entry, but was unable to decrypt. | |
52 INIT_FAILED_DECRYPT_EXISTING_ENTRY, | |
53 }; | |
54 | |
55 // Create a WriteNode using the given transaction. | |
56 explicit WriteNode(WriteTransaction* transaction); | |
57 ~WriteNode() override; | |
58 | |
59 // A client must use one (and only one) of the following Init variants to | |
60 // populate the node. | |
61 | |
62 // BaseNode implementation. | |
63 InitByLookupResult InitByIdLookup(int64_t id) override; | |
64 InitByLookupResult InitByClientTagLookup(ModelType model_type, | |
65 const std::string& tag) override; | |
66 | |
67 // Create a new bookmark node with the specified parent and predecessor. Use | |
68 // a NULL |predecessor| to indicate that this is to be the first child. | |
69 // |predecessor| must be a child of |new_parent| or NULL. Returns false on | |
70 // failure. | |
71 bool InitBookmarkByCreation(const BaseNode& parent, | |
72 const BaseNode* predecessor); | |
73 | |
74 // Create nodes using this function if they're unique items that | |
75 // you want to fetch using client_tag. Note that the behavior of these | |
76 // items is slightly different than that of normal items. | |
77 // Most importantly, if it exists locally but is deleted, this function will | |
78 // actually undelete it. Otherwise it will reuse the existing node. | |
79 // Client unique tagged nodes must NOT be folders. | |
80 InitUniqueByCreationResult InitUniqueByCreation( | |
81 ModelType model_type, | |
82 const BaseNode& parent, | |
83 const std::string& client_tag); | |
84 | |
85 // InitUniqueByCreation overload for model types without hierarchy. | |
86 // The parent node isn't stored but is assumed to be the type root folder. | |
87 InitUniqueByCreationResult InitUniqueByCreation( | |
88 ModelType model_type, | |
89 const std::string& client_tag); | |
90 | |
91 // Looks up the type's root folder. This is usually created by the sync | |
92 // server during initial sync, though we do eventually wish to remove it from | |
93 // the protocol and have the client "fake it" instead. | |
94 InitByLookupResult InitTypeRoot(ModelType type); | |
95 | |
96 // These Set() functions correspond to the Get() functions of BaseNode. | |
97 void SetIsFolder(bool folder); | |
98 void SetTitle(const std::string& title); | |
99 | |
100 // External ID is a client-only field, so setting it doesn't cause the item to | |
101 // be synced again. | |
102 void SetExternalId(int64_t external_id); | |
103 | |
104 // Remove this node and its children and sync deletion to server. | |
105 void Tombstone(); | |
106 | |
107 // If the node is known by server, remove it and its children but don't sync | |
108 // deletion to server. Do nothing if the node is not known by server so that | |
109 // server can have a record of the node. | |
110 void Drop(); | |
111 | |
112 // Set a new parent and position. Position is specified by |predecessor|; if | |
113 // it is NULL, the node is moved to the first position. |predecessor| must | |
114 // be a child of |new_parent| or NULL. Returns false on failure.. | |
115 bool SetPosition(const BaseNode& new_parent, const BaseNode* predecessor); | |
116 | |
117 // Set the bookmark specifics (url and favicon). | |
118 // Should only be called if GetModelType() == BOOKMARK. | |
119 void SetBookmarkSpecifics(const sync_pb::BookmarkSpecifics& specifics); | |
120 | |
121 // Generic set specifics method. Will extract the model type from |specifics|. | |
122 void SetEntitySpecifics(const sync_pb::EntitySpecifics& specifics); | |
123 | |
124 // Resets the EntitySpecifics for this node based on the unencrypted data. | |
125 // Will encrypt if necessary. | |
126 void ResetFromSpecifics(); | |
127 | |
128 // TODO(sync): Remove the setters below when the corresponding data | |
129 // types are ported to the new sync service API. | |
130 | |
131 // Set the nigori specifics. | |
132 // Should only be called if GetModelType() == NIGORI. | |
133 void SetNigoriSpecifics(const sync_pb::NigoriSpecifics& specifics); | |
134 | |
135 // Set the password specifics. | |
136 // Should only be called if GetModelType() == PASSWORD. | |
137 void SetPasswordSpecifics(const sync_pb::PasswordSpecificsData& specifics); | |
138 | |
139 // Set the typed_url specifics (url, title, typed_count, etc). | |
140 // Should only be called if GetModelType() == TYPED_URLS. | |
141 void SetTypedUrlSpecifics(const sync_pb::TypedUrlSpecifics& specifics); | |
142 | |
143 // Set the attachment metadata. | |
144 void SetAttachmentMetadata( | |
145 const sync_pb::AttachmentMetadata& attachment_metadata); | |
146 | |
147 // Implementation of BaseNode's abstract virtual accessors. | |
148 const syncable::Entry* GetEntry() const override; | |
149 | |
150 const BaseTransaction* GetTransaction() const override; | |
151 | |
152 syncable::MutableEntry* GetMutableEntryForTest(); | |
153 | |
154 private: | |
155 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, EncryptBookmarksWithLegacyData); | |
156 | |
157 void* operator new(size_t size); // Node is meant for stack use only. | |
158 | |
159 InitUniqueByCreationResult InitUniqueByCreationImpl( | |
160 ModelType model_type, | |
161 const syncable::Id& parent_id, | |
162 const std::string& client_tag); | |
163 | |
164 // Helper to set the previous node. | |
165 bool PutPredecessor(const BaseNode* predecessor) WARN_UNUSED_RESULT; | |
166 | |
167 // Sets IS_UNSYNCED and SYNCING to ensure this entry is considered in an | |
168 // upcoming commit pass. | |
169 void MarkForSyncing(); | |
170 | |
171 // The underlying syncable object which this class wraps. | |
172 syncable::MutableEntry* entry_; | |
173 | |
174 // The sync API transaction that is the parent of this node. | |
175 WriteTransaction* transaction_; | |
176 | |
177 DISALLOW_COPY_AND_ASSIGN(WriteNode); | |
178 }; | |
179 | |
180 } // namespace syncer | |
181 | |
182 #endif // COMPONENTS_SYNC_CORE_WRITE_NODE_H_ | |
OLD | NEW |