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.
5 #ifndef SYNC_INTERNAL_API_PUBLIC_WRITE_NODE_H_
6 #define SYNC_INTERNAL_API_PUBLIC_WRITE_NODE_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "sync/base/sync_export.h"
14 #include "sync/internal_api/public/base/model_type.h"
15 #include "sync/internal_api/public/base_node.h"
19 class AutofillSpecifics
;
20 class AutofillProfileSpecifics
;
21 class BookmarkSpecifics
;
22 class EntitySpecifics
;
23 class ExtensionSpecifics
;
24 class SessionSpecifics
;
25 class NigoriSpecifics
;
26 class PasswordSpecificsData
;
28 class TypedUrlSpecifics
;
34 class WriteTransaction
;
42 // WriteNode extends BaseNode to add mutation, and wraps
43 // syncable::MutableEntry. A WriteTransaction is needed to create a WriteNode.
44 class SYNC_EXPORT WriteNode
: public BaseNode
{
46 enum InitUniqueByCreationResult
{
48 // The tag passed into this method was empty.
49 INIT_FAILED_EMPTY_TAG
,
50 // An entry with this tag already exists.
51 INIT_FAILED_ENTRY_ALREADY_EXISTS
,
52 // The constructor for a new MutableEntry with the specified data failed.
53 INIT_FAILED_COULD_NOT_CREATE_ENTRY
,
54 // Setting the predecessor failed
55 INIT_FAILED_SET_PREDECESSOR
,
58 // Create a WriteNode using the given transaction.
59 explicit WriteNode(WriteTransaction
* transaction
);
60 ~WriteNode() override
;
62 // A client must use one (and only one) of the following Init variants to
65 // BaseNode implementation.
66 InitByLookupResult
InitByIdLookup(int64 id
) override
;
67 InitByLookupResult
InitByClientTagLookup(ModelType model_type
,
68 const std::string
& tag
) override
;
70 // Create a new bookmark node with the specified parent and predecessor. Use
71 // a NULL |predecessor| to indicate that this is to be the first child.
72 // |predecessor| must be a child of |new_parent| or NULL. Returns false on
74 bool InitBookmarkByCreation(const BaseNode
& parent
,
75 const BaseNode
* predecessor
);
77 // Create nodes using this function if they're unique items that
78 // you want to fetch using client_tag. Note that the behavior of these
79 // items is slightly different than that of normal items.
80 // Most importantly, if it exists locally, this function will
81 // actually undelete it
82 // Client unique tagged nodes must NOT be folders.
83 InitUniqueByCreationResult
InitUniqueByCreation(
85 const BaseNode
& parent
,
86 const std::string
& client_tag
);
88 // InitUniqueByCreation overload for model types without hierarchy.
89 // The parent node isn't stored but is assumed to be the type root folder.
90 InitUniqueByCreationResult
InitUniqueByCreation(
92 const std::string
& client_tag
);
94 // Looks up the type's root folder. This is usually created by the sync
95 // server during initial sync, though we do eventually wish to remove it from
96 // the protocol and have the client "fake it" instead.
97 InitByLookupResult
InitTypeRoot(ModelType type
);
99 // These Set() functions correspond to the Get() functions of BaseNode.
100 void SetIsFolder(bool folder
);
101 void SetTitle(const std::string
& title
);
103 // External ID is a client-only field, so setting it doesn't cause the item to
105 void SetExternalId(int64 external_id
);
107 // Remove this node and its children and sync deletion to server.
110 // If the node is known by server, remove it and its children but don't sync
111 // deletion to server. Do nothing if the node is not known by server so that
112 // server can have a record of the node.
115 // Set a new parent and position. Position is specified by |predecessor|; if
116 // it is NULL, the node is moved to the first position. |predecessor| must
117 // be a child of |new_parent| or NULL. Returns false on failure..
118 bool SetPosition(const BaseNode
& new_parent
, const BaseNode
* predecessor
);
120 // Set the bookmark specifics (url and favicon).
121 // Should only be called if GetModelType() == BOOKMARK.
122 void SetBookmarkSpecifics(const sync_pb::BookmarkSpecifics
& specifics
);
124 // Generic set specifics method. Will extract the model type from |specifics|.
125 void SetEntitySpecifics(const sync_pb::EntitySpecifics
& specifics
);
127 // Resets the EntitySpecifics for this node based on the unencrypted data.
128 // Will encrypt if necessary.
129 void ResetFromSpecifics();
131 // TODO(sync): Remove the setters below when the corresponding data
132 // types are ported to the new sync service API.
134 // Set the app specifics (id, update url, enabled state, etc).
135 // Should only be called if GetModelType() == APPS.
136 void SetAppSpecifics(const sync_pb::AppSpecifics
& specifics
);
138 // Set the autofill specifics (name and value).
139 // Should only be called if GetModelType() == AUTOFILL.
140 void SetAutofillSpecifics(const sync_pb::AutofillSpecifics
& specifics
);
142 void SetAutofillProfileSpecifics(
143 const sync_pb::AutofillProfileSpecifics
& specifics
);
145 // Set the nigori specifics.
146 // Should only be called if GetModelType() == NIGORI.
147 void SetNigoriSpecifics(const sync_pb::NigoriSpecifics
& specifics
);
149 // Set the password specifics.
150 // Should only be called if GetModelType() == PASSWORD.
151 void SetPasswordSpecifics(const sync_pb::PasswordSpecificsData
& specifics
);
153 // Set the theme specifics (name and value).
154 // Should only be called if GetModelType() == THEME.
155 void SetThemeSpecifics(const sync_pb::ThemeSpecifics
& specifics
);
157 // Set the typed_url specifics (url, title, typed_count, etc).
158 // Should only be called if GetModelType() == TYPED_URLS.
159 void SetTypedUrlSpecifics(const sync_pb::TypedUrlSpecifics
& specifics
);
161 // Set the extension specifics (id, update url, enabled state, etc).
162 // Should only be called if GetModelType() == EXTENSIONS.
163 void SetExtensionSpecifics(const sync_pb::ExtensionSpecifics
& specifics
);
165 // Set the session specifics (windows, tabs, navigations etc.).
166 // Should only be called if GetModelType() == SESSIONS.
167 void SetSessionSpecifics(const sync_pb::SessionSpecifics
& specifics
);
169 // Set the device info specifics.
170 // Should only be called if GetModelType() == DEVICE_INFO.
171 void SetDeviceInfoSpecifics(const sync_pb::DeviceInfoSpecifics
& specifics
);
173 // Set the experiments specifics.
174 // Should only be called if GetModelType() == EXPERIMENTS.
175 void SetExperimentsSpecifics(const sync_pb::ExperimentsSpecifics
& specifics
);
177 // Set the priority preference specifics.
178 // Should only be called if GetModelType() == PRIORITY_PREFERENCE.
179 void SetPriorityPreferenceSpecifics(
180 const sync_pb::PriorityPreferenceSpecifics
& specifics
);
182 // Set the attachment metadata.
183 void SetAttachmentMetadata(
184 const sync_pb::AttachmentMetadata
& attachment_metadata
);
186 // Implementation of BaseNode's abstract virtual accessors.
187 const syncable::Entry
* GetEntry() const override
;
189 const BaseTransaction
* GetTransaction() const override
;
191 syncable::MutableEntry
* GetMutableEntryForTest();
194 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest
, EncryptBookmarksWithLegacyData
);
196 void* operator new(size_t size
); // Node is meant for stack use only.
198 InitUniqueByCreationResult
InitUniqueByCreationImpl(
199 ModelType model_type
,
200 const syncable::Id
& parent_id
,
201 const std::string
& client_tag
);
203 // Helper to set the previous node.
204 bool PutPredecessor(const BaseNode
* predecessor
) WARN_UNUSED_RESULT
;
206 // Sets IS_UNSYNCED and SYNCING to ensure this entry is considered in an
207 // upcoming commit pass.
208 void MarkForSyncing();
210 // The underlying syncable object which this class wraps.
211 syncable::MutableEntry
* entry_
;
213 // The sync API transaction that is the parent of this node.
214 WriteTransaction
* transaction_
;
216 DISALLOW_COPY_AND_ASSIGN(WriteNode
);
219 } // namespace syncer
221 #endif // SYNC_INTERNAL_API_PUBLIC_WRITE_NODE_H_