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 SYNC_INTERNAL_API_WRITE_NODE_H_ | |
6 #define SYNC_INTERNAL_API_WRITE_NODE_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "sync/internal_api/base_node.h" | |
15 #include "sync/internal_api/public/syncable/model_type.h" | |
16 | |
17 namespace browser_sync { | |
18 class Cryptographer; | |
19 class TestBookmarkModelAssociator; | |
20 } | |
21 | |
22 namespace syncable { | |
23 class Entry; | |
24 class MutableEntry; | |
25 } | |
26 | |
27 namespace sync_pb { | |
28 class AppSpecifics; | |
29 class AutofillSpecifics; | |
30 class AutofillProfileSpecifics; | |
31 class BookmarkSpecifics; | |
32 class EntitySpecifics; | |
33 class ExtensionSpecifics; | |
34 class SessionSpecifics; | |
35 class NigoriSpecifics; | |
36 class PasswordSpecificsData; | |
37 class ThemeSpecifics; | |
38 class TypedUrlSpecifics; | |
39 } | |
40 | |
41 namespace sync_api { | |
42 | |
43 class WriteTransaction; | |
44 | |
45 // WriteNode extends BaseNode to add mutation, and wraps | |
46 // syncable::MutableEntry. A WriteTransaction is needed to create a WriteNode. | |
47 class WriteNode : public BaseNode { | |
48 public: | |
49 enum InitUniqueByCreationResult { | |
50 INIT_SUCCESS, | |
51 // The tag passed into this method was empty. | |
52 INIT_FAILED_EMPTY_TAG, | |
53 // An entry with this tag already exists. | |
54 INIT_FAILED_ENTRY_ALREADY_EXISTS, | |
55 // The constructor for a new MutableEntry with the specified data failed. | |
56 INIT_FAILED_COULD_NOT_CREATE_ENTRY, | |
57 // Setting the predecessor failed | |
58 INIT_FAILED_SET_PREDECESSOR, | |
59 }; | |
60 | |
61 // Create a WriteNode using the given transaction. | |
62 explicit WriteNode(WriteTransaction* transaction); | |
63 virtual ~WriteNode(); | |
64 | |
65 // A client must use one (and only one) of the following Init variants to | |
66 // populate the node. | |
67 | |
68 // BaseNode implementation. | |
69 virtual InitByLookupResult InitByIdLookup(int64 id) OVERRIDE; | |
70 virtual InitByLookupResult InitByClientTagLookup( | |
71 syncable::ModelType model_type, | |
72 const std::string& tag) OVERRIDE; | |
73 | |
74 // Create a new node with the specified parent and predecessor. |model_type| | |
75 // dictates the type of the item, and controls which EntitySpecifics proto | |
76 // extension can be used with this item. Use a NULL |predecessor| | |
77 // to indicate that this is to be the first child. | |
78 // |predecessor| must be a child of |new_parent| or NULL. Returns false on | |
79 // failure. | |
80 bool InitByCreation(syncable::ModelType model_type, | |
81 const BaseNode& parent, | |
82 const BaseNode* predecessor); | |
83 | |
84 // Create nodes using this function if they're unique items that | |
85 // you want to fetch using client_tag. Note that the behavior of these | |
86 // items is slightly different than that of normal items. | |
87 // Most importantly, if it exists locally, this function will | |
88 // actually undelete it | |
89 // Client unique tagged nodes must NOT be folders. | |
90 InitUniqueByCreationResult InitUniqueByCreation( | |
91 syncable::ModelType model_type, | |
92 const BaseNode& parent, | |
93 const std::string& client_tag); | |
94 | |
95 // Each server-created permanent node is tagged with a unique string. | |
96 // Look up the node with the particular tag. If it does not exist, | |
97 // return false. | |
98 InitByLookupResult InitByTagLookup(const std::string& tag); | |
99 | |
100 // These Set() functions correspond to the Get() functions of BaseNode. | |
101 void SetIsFolder(bool folder); | |
102 void SetTitle(const std::wstring& title); | |
103 | |
104 // External ID is a client-only field, so setting it doesn't cause the item to | |
105 // be synced again. | |
106 void SetExternalId(int64 external_id); | |
107 | |
108 // Remove this node and its children. | |
109 void Remove(); | |
110 | |
111 // Set a new parent and position. Position is specified by |predecessor|; if | |
112 // it is NULL, the node is moved to the first position. |predecessor| must | |
113 // be a child of |new_parent| or NULL. Returns false on failure.. | |
114 bool SetPosition(const BaseNode& new_parent, const BaseNode* predecessor); | |
115 | |
116 // Set the bookmark specifics (url and favicon). | |
117 // Should only be called if GetModelType() == BOOKMARK. | |
118 void SetBookmarkSpecifics(const sync_pb::BookmarkSpecifics& specifics); | |
119 | |
120 // Legacy, bookmark-specific setters that wrap SetBookmarkSpecifics() above. | |
121 // Should only be called if GetModelType() == BOOKMARK. | |
122 // TODO(ncarter): Remove these two datatype-specific accessors. | |
123 void SetURL(const GURL& url); | |
124 void SetFaviconBytes(const std::vector<unsigned char>& bytes); | |
125 | |
126 // Generic set specifics method. Will extract the model type from |specifics|. | |
127 void SetEntitySpecifics(const sync_pb::EntitySpecifics& specifics); | |
128 | |
129 // Resets the EntitySpecifics for this node based on the unencrypted data. | |
130 // Will encrypt if necessary. | |
131 void ResetFromSpecifics(); | |
132 | |
133 // TODO(sync): Remove the setters below when the corresponding data | |
134 // types are ported to the new sync service API. | |
135 | |
136 // Set the app specifics (id, update url, enabled state, etc). | |
137 // Should only be called if GetModelType() == APPS. | |
138 void SetAppSpecifics(const sync_pb::AppSpecifics& specifics); | |
139 | |
140 // Set the autofill specifics (name and value). | |
141 // Should only be called if GetModelType() == AUTOFILL. | |
142 void SetAutofillSpecifics(const sync_pb::AutofillSpecifics& specifics); | |
143 | |
144 void SetAutofillProfileSpecifics( | |
145 const sync_pb::AutofillProfileSpecifics& specifics); | |
146 | |
147 // Set the nigori specifics. | |
148 // Should only be called if GetModelType() == NIGORI. | |
149 void SetNigoriSpecifics(const sync_pb::NigoriSpecifics& specifics); | |
150 | |
151 // Set the password specifics. | |
152 // Should only be called if GetModelType() == PASSWORD. | |
153 void SetPasswordSpecifics(const sync_pb::PasswordSpecificsData& specifics); | |
154 | |
155 // Set the theme specifics (name and value). | |
156 // Should only be called if GetModelType() == THEME. | |
157 void SetThemeSpecifics(const sync_pb::ThemeSpecifics& specifics); | |
158 | |
159 // Set the typed_url specifics (url, title, typed_count, etc). | |
160 // Should only be called if GetModelType() == TYPED_URLS. | |
161 void SetTypedUrlSpecifics(const sync_pb::TypedUrlSpecifics& specifics); | |
162 | |
163 // Set the extension specifics (id, update url, enabled state, etc). | |
164 // Should only be called if GetModelType() == EXTENSIONS. | |
165 void SetExtensionSpecifics(const sync_pb::ExtensionSpecifics& specifics); | |
166 | |
167 // Set the session specifics (windows, tabs, navigations etc.). | |
168 // Should only be called if GetModelType() == SESSIONS. | |
169 void SetSessionSpecifics(const sync_pb::SessionSpecifics& specifics); | |
170 | |
171 // Implementation of BaseNode's abstract virtual accessors. | |
172 virtual const syncable::Entry* GetEntry() const OVERRIDE; | |
173 | |
174 virtual const BaseTransaction* GetTransaction() const OVERRIDE; | |
175 | |
176 private: | |
177 friend class browser_sync::TestBookmarkModelAssociator; | |
178 FRIEND_TEST_ALL_PREFIXES(SyncManagerTest, EncryptBookmarksWithLegacyData); | |
179 | |
180 void* operator new(size_t size); // Node is meant for stack use only. | |
181 | |
182 // Helper to set model type. This will clear any specifics data. | |
183 void PutModelType(syncable::ModelType model_type); | |
184 | |
185 // Helper to set the previous node. | |
186 bool PutPredecessor(const BaseNode* predecessor) WARN_UNUSED_RESULT; | |
187 | |
188 // Sets IS_UNSYNCED and SYNCING to ensure this entry is considered in an | |
189 // upcoming commit pass. | |
190 void MarkForSyncing(); | |
191 | |
192 // The underlying syncable object which this class wraps. | |
193 syncable::MutableEntry* entry_; | |
194 | |
195 // The sync API transaction that is the parent of this node. | |
196 WriteTransaction* transaction_; | |
197 | |
198 DISALLOW_COPY_AND_ASSIGN(WriteNode); | |
199 }; | |
200 | |
201 } // namespace sync_api | |
202 | |
203 #endif // SYNC_INTERNAL_API_WRITE_NODE_H_ | |
OLD | NEW |