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

Side by Side Diff: components/sync/syncable/syncable_util.cc

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 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
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sync/syncable/syncable_util.h" 5 #include "components/sync/syncable/syncable_util.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/sha1.h" 12 #include "base/sha1.h"
13 #include "sync/syncable/directory.h" 13 #include "components/sync/syncable/directory.h"
14 #include "sync/syncable/entry.h" 14 #include "components/sync/syncable/entry.h"
15 #include "sync/syncable/mutable_entry.h" 15 #include "components/sync/syncable/mutable_entry.h"
16 #include "sync/syncable/syncable_id.h" 16 #include "components/sync/syncable/syncable_id.h"
17 #include "sync/syncable/syncable_write_transaction.h" 17 #include "components/sync/syncable/syncable_write_transaction.h"
18 18
19 namespace syncer { 19 namespace syncer {
20 namespace syncable { 20 namespace syncable {
21 21
22 // Returns the number of unsynced entries. 22 // Returns the number of unsynced entries.
23 int GetUnsyncedEntries(BaseTransaction* trans, std::vector<int64_t>* handles) { 23 int GetUnsyncedEntries(BaseTransaction* trans, std::vector<int64_t>* handles) {
24 trans->directory()->GetUnsyncedMetaHandles(trans, handles); 24 trans->directory()->GetUnsyncedMetaHandles(trans, handles);
25 DVLOG_IF(1, !handles->empty()) << "Have " << handles->size() 25 DVLOG_IF(1, !handles->empty()) << "Have " << handles->size()
26 << " unsynced items."; 26 << " unsynced items.";
27 return handles->size(); 27 return handles->size();
28 } 28 }
29 29
30 bool IsLegalNewParent(BaseTransaction* trans, const Id& entry_id, 30 bool IsLegalNewParent(BaseTransaction* trans,
31 const Id& entry_id,
31 const Id& new_parent_id) { 32 const Id& new_parent_id) {
32 DCHECK(!entry_id.IsNull()); 33 DCHECK(!entry_id.IsNull());
33 DCHECK(!new_parent_id.IsNull()); 34 DCHECK(!new_parent_id.IsNull());
34 if (entry_id.IsRoot()) 35 if (entry_id.IsRoot())
35 return false; 36 return false;
36 // we have to ensure that the entry is not an ancestor of the new parent. 37 // we have to ensure that the entry is not an ancestor of the new parent.
37 Id ancestor_id = new_parent_id; 38 Id ancestor_id = new_parent_id;
38 while (!ancestor_id.IsRoot()) { 39 while (!ancestor_id.IsRoot()) {
39 if (entry_id == ancestor_id) 40 if (entry_id == ancestor_id)
40 return false; 41 return false;
41 Entry new_parent(trans, GET_BY_ID, ancestor_id); 42 Entry new_parent(trans, GET_BY_ID, ancestor_id);
42 if (!SyncAssert(new_parent.good(), 43 if (!SyncAssert(new_parent.good(), FROM_HERE, "Invalid new parent", trans))
43 FROM_HERE,
44 "Invalid new parent",
45 trans))
46 return false; 44 return false;
47 ancestor_id = new_parent.GetParentId(); 45 ancestor_id = new_parent.GetParentId();
48 } 46 }
49 return true; 47 return true;
50 } 48 }
51 49
52 void ChangeEntryIDAndUpdateChildren( 50 void ChangeEntryIDAndUpdateChildren(BaseWriteTransaction* trans,
53 BaseWriteTransaction* trans, 51 ModelNeutralMutableEntry* entry,
54 ModelNeutralMutableEntry* entry, 52 const Id& new_id) {
55 const Id& new_id) {
56 Id old_id = entry->GetId(); 53 Id old_id = entry->GetId();
57 if (!entry->PutId(new_id)) { 54 if (!entry->PutId(new_id)) {
58 Entry old_entry(trans, GET_BY_ID, new_id); 55 Entry old_entry(trans, GET_BY_ID, new_id);
59 CHECK(old_entry.good()); 56 CHECK(old_entry.good());
60 LOG(FATAL) << "Attempt to change ID to " << new_id 57 LOG(FATAL) << "Attempt to change ID to " << new_id
61 << " conflicts with existing entry.\n\n" 58 << " conflicts with existing entry.\n\n"
62 << *entry << "\n\n" << old_entry; 59 << *entry << "\n\n"
60 << old_entry;
63 } 61 }
64 if (entry->GetIsDir()) { 62 if (entry->GetIsDir()) {
65 // Get all child entries of the old id. 63 // Get all child entries of the old id.
66 Directory::Metahandles children; 64 Directory::Metahandles children;
67 trans->directory()->GetChildHandlesById(trans, old_id, &children); 65 trans->directory()->GetChildHandlesById(trans, old_id, &children);
68 Directory::Metahandles::iterator i = children.begin(); 66 Directory::Metahandles::iterator i = children.begin();
69 while (i != children.end()) { 67 while (i != children.end()) {
70 ModelNeutralMutableEntry child_entry(trans, GET_BY_HANDLE, *i++); 68 ModelNeutralMutableEntry child_entry(trans, GET_BY_HANDLE, *i++);
71 CHECK(child_entry.good()); 69 CHECK(child_entry.good());
72 // Change the parent ID of the entry unless it was unset (implicit) 70 // Change the parent ID of the entry unless it was unset (implicit)
(...skipping 16 matching lines...) Expand all
89 const tracked_objects::Location& location, 87 const tracked_objects::Location& location,
90 const char* msg, 88 const char* msg,
91 BaseTransaction* trans) { 89 BaseTransaction* trans) {
92 if (!condition) { 90 if (!condition) {
93 trans->OnUnrecoverableError(location, msg); 91 trans->OnUnrecoverableError(location, msg);
94 return false; 92 return false;
95 } 93 }
96 return true; 94 return true;
97 } 95 }
98 96
99 std::string GenerateSyncableHash( 97 std::string GenerateSyncableHash(ModelType model_type,
100 ModelType model_type, const std::string& client_tag) { 98 const std::string& client_tag) {
101 // Blank PB with just the field in it has termination symbol, 99 // Blank PB with just the field in it has termination symbol,
102 // handy for delimiter. 100 // handy for delimiter.
103 sync_pb::EntitySpecifics serialized_type; 101 sync_pb::EntitySpecifics serialized_type;
104 AddDefaultFieldValue(model_type, &serialized_type); 102 AddDefaultFieldValue(model_type, &serialized_type);
105 std::string hash_input; 103 std::string hash_input;
106 serialized_type.AppendToString(&hash_input); 104 serialized_type.AppendToString(&hash_input);
107 hash_input.append(client_tag); 105 hash_input.append(client_tag);
108 106
109 std::string encode_output; 107 std::string encode_output;
110 base::Base64Encode(base::SHA1HashString(hash_input), &encode_output); 108 base::Base64Encode(base::SHA1HashString(hash_input), &encode_output);
111 return encode_output; 109 return encode_output;
112 } 110 }
113 111
114 std::string GenerateSyncableBookmarkHash( 112 std::string GenerateSyncableBookmarkHash(
115 const std::string& originator_cache_guid, 113 const std::string& originator_cache_guid,
116 const std::string& originator_client_item_id) { 114 const std::string& originator_client_item_id) {
117 return syncable::GenerateSyncableHash( 115 return syncable::GenerateSyncableHash(
118 BOOKMARKS, originator_cache_guid + originator_client_item_id); 116 BOOKMARKS, originator_cache_guid + originator_client_item_id);
119 } 117 }
120 118
121 } // namespace syncable 119 } // namespace syncable
122 } // namespace syncer 120 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/syncable/syncable_util.h ('k') | components/sync/syncable/syncable_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698