Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/browser/sync/internal_api/write_node.cc

Issue 10147003: [Sync] Move 'syncapi_core' and 'sync_unit_tests' targets to sync/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync to head Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "chrome/browser/sync/internal_api/write_node.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "base/values.h"
9 #include "chrome/browser/sync/internal_api/syncapi_internal.h"
10 #include "chrome/browser/sync/internal_api/base_transaction.h"
11 #include "chrome/browser/sync/internal_api/write_transaction.h"
12 #include "sync/engine/nigori_util.h"
13 #include "sync/protocol/app_specifics.pb.h"
14 #include "sync/protocol/autofill_specifics.pb.h"
15 #include "sync/protocol/bookmark_specifics.pb.h"
16 #include "sync/protocol/extension_specifics.pb.h"
17 #include "sync/protocol/password_specifics.pb.h"
18 #include "sync/protocol/session_specifics.pb.h"
19 #include "sync/protocol/theme_specifics.pb.h"
20 #include "sync/protocol/typed_url_specifics.pb.h"
21 #include "sync/syncable/syncable.h"
22 #include "sync/util/cryptographer.h"
23
24 using browser_sync::Cryptographer;
25 using std::string;
26 using std::vector;
27 using syncable::kEncryptedString;
28 using syncable::SPECIFICS;
29
30 namespace sync_api {
31
32 static const char kDefaultNameForNewNodes[] = " ";
33
34 void WriteNode::SetIsFolder(bool folder) {
35 if (entry_->Get(syncable::IS_DIR) == folder)
36 return; // Skip redundant changes.
37
38 entry_->Put(syncable::IS_DIR, folder);
39 MarkForSyncing();
40 }
41
42 void WriteNode::SetTitle(const std::wstring& title) {
43 DCHECK_NE(GetModelType(), syncable::UNSPECIFIED);
44 syncable::ModelType type = GetModelType();
45 Cryptographer* cryptographer = GetTransaction()->GetCryptographer();
46 // It's possible the nigori lost the set of encrypted types. If the current
47 // specifics are already encrypted, we want to ensure we continue encrypting.
48 bool needs_encryption = cryptographer->GetEncryptedTypes().Has(type) ||
49 entry_->Get(SPECIFICS).has_encrypted();
50
51 // If this datatype is encrypted and is not a bookmark, we disregard the
52 // specified title in favor of kEncryptedString. For encrypted bookmarks the
53 // NON_UNIQUE_NAME will still be kEncryptedString, but we store the real title
54 // into the specifics. All strings compared are server legal strings.
55 std::string new_legal_title;
56 if (type != syncable::BOOKMARKS && needs_encryption) {
57 new_legal_title = kEncryptedString;
58 } else {
59 SyncAPINameToServerName(WideToUTF8(title), &new_legal_title);
60 }
61
62 std::string current_legal_title;
63 if (syncable::BOOKMARKS == type &&
64 entry_->Get(syncable::SPECIFICS).has_encrypted()) {
65 // Encrypted bookmarks only have their title in the unencrypted specifics.
66 current_legal_title = GetBookmarkSpecifics().title();
67 } else {
68 // Non-bookmarks and legacy bookmarks (those with no title in their
69 // specifics) store their title in NON_UNIQUE_NAME. Non-legacy bookmarks
70 // store their title in specifics as well as NON_UNIQUE_NAME.
71 current_legal_title = entry_->Get(syncable::NON_UNIQUE_NAME);
72 }
73
74 bool title_matches = (current_legal_title == new_legal_title);
75 bool encrypted_without_overwriting_name = (needs_encryption &&
76 entry_->Get(syncable::NON_UNIQUE_NAME) != kEncryptedString);
77
78 // If the title matches and the NON_UNIQUE_NAME is properly overwritten as
79 // necessary, nothing needs to change.
80 if (title_matches && !encrypted_without_overwriting_name) {
81 DVLOG(2) << "Title matches, dropping change.";
82 return;
83 }
84
85 // For bookmarks, we also set the title field in the specifics.
86 // TODO(zea): refactor bookmarks to not need this functionality.
87 if (GetModelType() == syncable::BOOKMARKS) {
88 sync_pb::EntitySpecifics specifics = GetEntitySpecifics();
89 specifics.mutable_bookmark()->set_title(new_legal_title);
90 SetEntitySpecifics(specifics); // Does it's own encryption checking.
91 }
92
93 // For bookmarks, this has to happen after we set the title in the specifics,
94 // because the presence of a title in the NON_UNIQUE_NAME is what controls
95 // the logic deciding whether this is an empty node or a legacy bookmark.
96 // See BaseNode::GetUnencryptedSpecific(..).
97 if (needs_encryption)
98 entry_->Put(syncable::NON_UNIQUE_NAME, kEncryptedString);
99 else
100 entry_->Put(syncable::NON_UNIQUE_NAME, new_legal_title);
101
102 DVLOG(1) << "Overwriting title of type "
103 << syncable::ModelTypeToString(type)
104 << " and marking for syncing.";
105 MarkForSyncing();
106 }
107
108 void WriteNode::SetURL(const GURL& url) {
109 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics();
110 new_value.set_url(url.spec());
111 SetBookmarkSpecifics(new_value);
112 }
113
114 void WriteNode::SetAppSpecifics(
115 const sync_pb::AppSpecifics& new_value) {
116 sync_pb::EntitySpecifics entity_specifics;
117 entity_specifics.mutable_app()->CopyFrom(new_value);
118 SetEntitySpecifics(entity_specifics);
119 }
120
121 void WriteNode::SetAutofillSpecifics(
122 const sync_pb::AutofillSpecifics& new_value) {
123 sync_pb::EntitySpecifics entity_specifics;
124 entity_specifics.mutable_autofill()->CopyFrom(new_value);
125 SetEntitySpecifics(entity_specifics);
126 }
127
128 void WriteNode::SetAutofillProfileSpecifics(
129 const sync_pb::AutofillProfileSpecifics& new_value) {
130 sync_pb::EntitySpecifics entity_specifics;
131 entity_specifics.mutable_autofill_profile()->
132 CopyFrom(new_value);
133 SetEntitySpecifics(entity_specifics);
134 }
135
136 void WriteNode::SetBookmarkSpecifics(
137 const sync_pb::BookmarkSpecifics& new_value) {
138 sync_pb::EntitySpecifics entity_specifics;
139 entity_specifics.mutable_bookmark()->CopyFrom(new_value);
140 SetEntitySpecifics(entity_specifics);
141 }
142
143 void WriteNode::SetNigoriSpecifics(
144 const sync_pb::NigoriSpecifics& new_value) {
145 sync_pb::EntitySpecifics entity_specifics;
146 entity_specifics.mutable_nigori()->CopyFrom(new_value);
147 SetEntitySpecifics(entity_specifics);
148 }
149
150 void WriteNode::SetPasswordSpecifics(
151 const sync_pb::PasswordSpecificsData& data) {
152 DCHECK_EQ(syncable::PASSWORDS, GetModelType());
153
154 Cryptographer* cryptographer = GetTransaction()->GetCryptographer();
155
156 // We have to do the idempotency check here (vs in UpdateEntryWithEncryption)
157 // because Passwords have their encrypted data within the PasswordSpecifics,
158 // vs within the EntitySpecifics like all the other types.
159 const sync_pb::EntitySpecifics& old_specifics = GetEntry()->Get(SPECIFICS);
160 sync_pb::EntitySpecifics entity_specifics;
161 // Copy over the old specifics if they exist.
162 if (syncable::GetModelTypeFromSpecifics(old_specifics) ==
163 syncable::PASSWORDS) {
164 entity_specifics.CopyFrom(old_specifics);
165 } else {
166 syncable::AddDefaultFieldValue(syncable::PASSWORDS,
167 &entity_specifics);
168 }
169 sync_pb::PasswordSpecifics* password_specifics =
170 entity_specifics.mutable_password();
171 // This will only update password_specifics if the underlying unencrypted blob
172 // was different from |data| or was not encrypted with the proper passphrase.
173 if (!cryptographer->Encrypt(data, password_specifics->mutable_encrypted())) {
174 NOTREACHED() << "Failed to encrypt password, possibly due to sync node "
175 << "corruption";
176 return;
177 }
178 SetEntitySpecifics(entity_specifics);
179 }
180
181 void WriteNode::SetThemeSpecifics(
182 const sync_pb::ThemeSpecifics& new_value) {
183 sync_pb::EntitySpecifics entity_specifics;
184 entity_specifics.mutable_theme()->CopyFrom(new_value);
185 SetEntitySpecifics(entity_specifics);
186 }
187
188 void WriteNode::SetSessionSpecifics(
189 const sync_pb::SessionSpecifics& new_value) {
190 sync_pb::EntitySpecifics entity_specifics;
191 entity_specifics.mutable_session()->CopyFrom(new_value);
192 SetEntitySpecifics(entity_specifics);
193 }
194
195 void WriteNode::SetEntitySpecifics(
196 const sync_pb::EntitySpecifics& new_value) {
197 syncable::ModelType new_specifics_type =
198 syncable::GetModelTypeFromSpecifics(new_value);
199 DCHECK_NE(new_specifics_type, syncable::UNSPECIFIED);
200 DVLOG(1) << "Writing entity specifics of type "
201 << syncable::ModelTypeToString(new_specifics_type);
202 // GetModelType() can be unspecified if this is the first time this
203 // node is being initialized (see PutModelType()). Otherwise, it
204 // should match |new_specifics_type|.
205 if (GetModelType() != syncable::UNSPECIFIED) {
206 DCHECK_EQ(new_specifics_type, GetModelType());
207 }
208 browser_sync::Cryptographer* cryptographer =
209 GetTransaction()->GetCryptographer();
210
211 // Preserve unknown fields.
212 const sync_pb::EntitySpecifics& old_specifics = entry_->Get(SPECIFICS);
213 sync_pb::EntitySpecifics new_specifics;
214 new_specifics.CopyFrom(new_value);
215 new_specifics.mutable_unknown_fields()->MergeFrom(
216 old_specifics.unknown_fields());
217
218 // Will update the entry if encryption was necessary.
219 if (!UpdateEntryWithEncryption(cryptographer, new_specifics, entry_)) {
220 return;
221 }
222 if (entry_->Get(SPECIFICS).has_encrypted()) {
223 // EncryptIfNecessary already updated the entry for us and marked for
224 // syncing if it was needed. Now we just make a copy of the unencrypted
225 // specifics so that if this node is updated, we do not have to decrypt the
226 // old data. Note that this only modifies the node's local data, not the
227 // entry itself.
228 SetUnencryptedSpecifics(new_value);
229 }
230
231 DCHECK_EQ(new_specifics_type, GetModelType());
232 }
233
234 void WriteNode::ResetFromSpecifics() {
235 SetEntitySpecifics(GetEntitySpecifics());
236 }
237
238 void WriteNode::SetTypedUrlSpecifics(
239 const sync_pb::TypedUrlSpecifics& new_value) {
240 sync_pb::EntitySpecifics entity_specifics;
241 entity_specifics.mutable_typed_url()->CopyFrom(new_value);
242 SetEntitySpecifics(entity_specifics);
243 }
244
245 void WriteNode::SetExtensionSpecifics(
246 const sync_pb::ExtensionSpecifics& new_value) {
247 sync_pb::EntitySpecifics entity_specifics;
248 entity_specifics.mutable_extension()->CopyFrom(new_value);
249 SetEntitySpecifics(entity_specifics);
250 }
251
252 void WriteNode::SetExternalId(int64 id) {
253 if (GetExternalId() != id)
254 entry_->Put(syncable::LOCAL_EXTERNAL_ID, id);
255 }
256
257 WriteNode::WriteNode(WriteTransaction* transaction)
258 : entry_(NULL), transaction_(transaction) {
259 DCHECK(transaction);
260 }
261
262 WriteNode::~WriteNode() {
263 delete entry_;
264 }
265
266 // Find an existing node matching the ID |id|, and bind this WriteNode to it.
267 // Return true on success.
268 bool WriteNode::InitByIdLookup(int64 id) {
269 DCHECK(!entry_) << "Init called twice";
270 DCHECK_NE(id, kInvalidId);
271 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
272 syncable::GET_BY_HANDLE, id);
273 return (entry_->good() && !entry_->Get(syncable::IS_DEL) &&
274 DecryptIfNecessary());
275 }
276
277 // Find a node by client tag, and bind this WriteNode to it.
278 // Return true if the write node was found, and was not deleted.
279 // Undeleting a deleted node is possible by ClientTag.
280 bool WriteNode::InitByClientTagLookup(syncable::ModelType model_type,
281 const std::string& tag) {
282 DCHECK(!entry_) << "Init called twice";
283 if (tag.empty())
284 return false;
285
286 const std::string hash = GenerateSyncableHash(model_type, tag);
287
288 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
289 syncable::GET_BY_CLIENT_TAG, hash);
290 return (entry_->good() && !entry_->Get(syncable::IS_DEL) &&
291 DecryptIfNecessary());
292 }
293
294 bool WriteNode::InitByTagLookup(const std::string& tag) {
295 DCHECK(!entry_) << "Init called twice";
296 if (tag.empty())
297 return false;
298 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
299 syncable::GET_BY_SERVER_TAG, tag);
300 if (!entry_->good())
301 return false;
302 if (entry_->Get(syncable::IS_DEL))
303 return false;
304 syncable::ModelType model_type = GetModelType();
305 DCHECK_EQ(syncable::NIGORI, model_type);
306 return true;
307 }
308
309 void WriteNode::PutModelType(syncable::ModelType model_type) {
310 // Set an empty specifics of the appropriate datatype. The presence
311 // of the specific field will identify the model type.
312 DCHECK(GetModelType() == model_type ||
313 GetModelType() == syncable::UNSPECIFIED); // Immutable once set.
314
315 sync_pb::EntitySpecifics specifics;
316 syncable::AddDefaultFieldValue(model_type, &specifics);
317 SetEntitySpecifics(specifics);
318 }
319
320 // Create a new node with default properties, and bind this WriteNode to it.
321 // Return true on success.
322 bool WriteNode::InitByCreation(syncable::ModelType model_type,
323 const BaseNode& parent,
324 const BaseNode* predecessor) {
325 DCHECK(!entry_) << "Init called twice";
326 // |predecessor| must be a child of |parent| or NULL.
327 if (predecessor && predecessor->GetParentId() != parent.GetId()) {
328 DCHECK(false);
329 return false;
330 }
331
332 syncable::Id parent_id = parent.GetEntry()->Get(syncable::ID);
333
334 // Start out with a dummy name. We expect
335 // the caller to set a meaningful name after creation.
336 string dummy(kDefaultNameForNewNodes);
337
338 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
339 syncable::CREATE, parent_id, dummy);
340
341 if (!entry_->good())
342 return false;
343
344 // Entries are untitled folders by default.
345 entry_->Put(syncable::IS_DIR, true);
346
347 PutModelType(model_type);
348
349 // Now set the predecessor, which sets IS_UNSYNCED as necessary.
350 return PutPredecessor(predecessor);
351 }
352
353 // Create a new node with default properties and a client defined unique tag,
354 // and bind this WriteNode to it.
355 // Return true on success. If the tag exists in the database, then
356 // we will attempt to undelete the node.
357 // TODO(chron): Code datatype into hash tag.
358 // TODO(chron): Is model type ever lost?
359 bool WriteNode::InitUniqueByCreation(syncable::ModelType model_type,
360 const BaseNode& parent,
361 const std::string& tag) {
362 DCHECK(!entry_) << "Init called twice";
363 if (tag.empty()) {
364 LOG(WARNING) << "InitUniqueByCreation failed due to empty tag.";
365 return false;
366 }
367
368 const std::string hash = GenerateSyncableHash(model_type, tag);
369
370 syncable::Id parent_id = parent.GetEntry()->Get(syncable::ID);
371
372 // Start out with a dummy name. We expect
373 // the caller to set a meaningful name after creation.
374 string dummy(kDefaultNameForNewNodes);
375
376 // Check if we have this locally and need to undelete it.
377 scoped_ptr<syncable::MutableEntry> existing_entry(
378 new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
379 syncable::GET_BY_CLIENT_TAG, hash));
380
381 if (existing_entry->good()) {
382 if (existing_entry->Get(syncable::IS_DEL)) {
383 // Rules for undelete:
384 // BASE_VERSION: Must keep the same.
385 // ID: Essential to keep the same.
386 // META_HANDLE: Must be the same, so we can't "split" the entry.
387 // IS_DEL: Must be set to false, will cause reindexing.
388 // This one is weird because IS_DEL is true for "update only"
389 // items. It should be OK to undelete an update only.
390 // MTIME/CTIME: Seems reasonable to just leave them alone.
391 // IS_UNSYNCED: Must set this to true or face database insurrection.
392 // We do this below this block.
393 // IS_UNAPPLIED_UPDATE: Either keep it the same or also set BASE_VERSION
394 // to SERVER_VERSION. We keep it the same here.
395 // IS_DIR: We'll leave it the same.
396 // SPECIFICS: Reset it.
397
398 existing_entry->Put(syncable::IS_DEL, false);
399
400 // Client tags are immutable and must be paired with the ID.
401 // If a server update comes down with an ID and client tag combo,
402 // and it already exists, always overwrite it and store only one copy.
403 // We have to undelete entries because we can't disassociate IDs from
404 // tags and updates.
405
406 existing_entry->Put(syncable::NON_UNIQUE_NAME, dummy);
407 existing_entry->Put(syncable::PARENT_ID, parent_id);
408 entry_ = existing_entry.release();
409 } else {
410 return false;
411 }
412 } else {
413 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
414 syncable::CREATE, parent_id, dummy);
415 if (!entry_->good()) {
416 return false;
417 }
418
419 // Only set IS_DIR for new entries. Don't bitflip undeleted ones.
420 entry_->Put(syncable::UNIQUE_CLIENT_TAG, hash);
421 }
422
423 // We don't support directory and tag combinations.
424 entry_->Put(syncable::IS_DIR, false);
425
426 // Will clear specifics data.
427 PutModelType(model_type);
428
429 // Now set the predecessor, which sets IS_UNSYNCED as necessary.
430 return PutPredecessor(NULL);
431 }
432
433 bool WriteNode::SetPosition(const BaseNode& new_parent,
434 const BaseNode* predecessor) {
435 // |predecessor| must be a child of |new_parent| or NULL.
436 if (predecessor && predecessor->GetParentId() != new_parent.GetId()) {
437 DCHECK(false);
438 return false;
439 }
440
441 syncable::Id new_parent_id = new_parent.GetEntry()->Get(syncable::ID);
442
443 // Filter out redundant changes if both the parent and the predecessor match.
444 if (new_parent_id == entry_->Get(syncable::PARENT_ID)) {
445 const syncable::Id& old = entry_->Get(syncable::PREV_ID);
446 if ((!predecessor && old.IsRoot()) ||
447 (predecessor && (old == predecessor->GetEntry()->Get(syncable::ID)))) {
448 return true;
449 }
450 }
451
452 // Atomically change the parent. This will fail if it would
453 // introduce a cycle in the hierarchy.
454 if (!entry_->Put(syncable::PARENT_ID, new_parent_id))
455 return false;
456
457 // Now set the predecessor, which sets IS_UNSYNCED as necessary.
458 return PutPredecessor(predecessor);
459 }
460
461 const syncable::Entry* WriteNode::GetEntry() const {
462 return entry_;
463 }
464
465 const BaseTransaction* WriteNode::GetTransaction() const {
466 return transaction_;
467 }
468
469 void WriteNode::Remove() {
470 entry_->Put(syncable::IS_DEL, true);
471 MarkForSyncing();
472 }
473
474 bool WriteNode::PutPredecessor(const BaseNode* predecessor) {
475 syncable::Id predecessor_id = predecessor ?
476 predecessor->GetEntry()->Get(syncable::ID) : syncable::Id();
477 if (!entry_->PutPredecessor(predecessor_id))
478 return false;
479 // Mark this entry as unsynced, to wake up the syncer.
480 MarkForSyncing();
481
482 return true;
483 }
484
485 void WriteNode::SetFaviconBytes(const vector<unsigned char>& bytes) {
486 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics();
487 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size());
488 SetBookmarkSpecifics(new_value);
489 }
490
491 void WriteNode::MarkForSyncing() {
492 syncable::MarkForSyncing(entry_);
493 }
494
495 } // namespace sync_api
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698