OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "sync/syncable/syncable_util.h" |
6 | 6 |
| 7 #include "base/base64.h" |
7 #include "base/location.h" | 8 #include "base/location.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/sha1.h" |
9 #include "sync/syncable/directory.h" | 11 #include "sync/syncable/directory.h" |
10 #include "sync/syncable/entry.h" | 12 #include "sync/syncable/entry.h" |
11 #include "sync/syncable/mutable_entry.h" | 13 #include "sync/syncable/mutable_entry.h" |
12 #include "sync/syncable/syncable_id.h" | 14 #include "sync/syncable/syncable_id.h" |
13 #include "sync/syncable/write_transaction.h" | 15 #include "sync/syncable/write_transaction.h" |
14 | 16 |
15 namespace syncer { | 17 namespace syncer { |
16 namespace syncable { | 18 namespace syncable { |
17 | 19 |
18 // Returns the number of unsynced entries. | 20 // Returns the number of unsynced entries. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 << *entry << "\n\n" << old_entry; | 59 << *entry << "\n\n" << old_entry; |
58 } | 60 } |
59 if (entry->Get(IS_DIR)) { | 61 if (entry->Get(IS_DIR)) { |
60 // Get all child entries of the old id. | 62 // Get all child entries of the old id. |
61 Directory::ChildHandles children; | 63 Directory::ChildHandles children; |
62 trans->directory()->GetChildHandlesById(trans, old_id, &children); | 64 trans->directory()->GetChildHandlesById(trans, old_id, &children); |
63 Directory::ChildHandles::iterator i = children.begin(); | 65 Directory::ChildHandles::iterator i = children.begin(); |
64 while (i != children.end()) { | 66 while (i != children.end()) { |
65 MutableEntry child_entry(trans, GET_BY_HANDLE, *i++); | 67 MutableEntry child_entry(trans, GET_BY_HANDLE, *i++); |
66 CHECK(child_entry.good()); | 68 CHECK(child_entry.good()); |
67 // Use the unchecked setter here to avoid touching the child's NEXT_ID | 69 // Use the unchecked setter here to avoid touching the child's |
68 // and PREV_ID fields (which Put(PARENT_ID) would normally do to | 70 // UNIQUE_POSITION field. In this case, UNIQUE_POSITION among the |
69 // maintain linked-list invariants). In this case, NEXT_ID and PREV_ID | 71 // children will be valid after the loop, since we update all the children |
70 // among the children will be valid after the loop, since we update all | 72 // at once. |
71 // the children at once. | |
72 child_entry.PutParentIdPropertyOnly(new_id); | 73 child_entry.PutParentIdPropertyOnly(new_id); |
73 } | 74 } |
74 } | 75 } |
75 // Update Id references on the previous and next nodes in the sibling | |
76 // order. Do this by reinserting into the linked list; the first | |
77 // step in PutPredecessor is to Unlink from the existing order, which | |
78 // will overwrite the stale Id value from the adjacent nodes. | |
79 if (entry->Get(PREV_ID) == entry->Get(NEXT_ID) && | |
80 entry->Get(PREV_ID) == old_id) { | |
81 // We just need a shallow update to |entry|'s fields since it is already | |
82 // self looped. | |
83 entry->Put(NEXT_ID, new_id); | |
84 entry->Put(PREV_ID, new_id); | |
85 } else { | |
86 entry->PutPredecessor(entry->Get(PREV_ID)); | |
87 } | |
88 } | 76 } |
89 | 77 |
90 // Function to handle runtime failures on syncable code. Rather than crashing, | 78 // Function to handle runtime failures on syncable code. Rather than crashing, |
91 // if the |condition| is false the following will happen: | 79 // if the |condition| is false the following will happen: |
92 // 1. Sets unrecoverable error on transaction. | 80 // 1. Sets unrecoverable error on transaction. |
93 // 2. Returns false. | 81 // 2. Returns false. |
94 bool SyncAssert(bool condition, | 82 bool SyncAssert(bool condition, |
95 const tracked_objects::Location& location, | 83 const tracked_objects::Location& location, |
96 const char* msg, | 84 const char* msg, |
97 BaseTransaction* trans) { | 85 BaseTransaction* trans) { |
98 if (!condition) { | 86 if (!condition) { |
99 trans->OnUnrecoverableError(location, msg); | 87 trans->OnUnrecoverableError(location, msg); |
100 return false; | 88 return false; |
101 } | 89 } |
102 return true; | 90 return true; |
103 } | 91 } |
104 | 92 |
| 93 std::string GenerateSyncableHash( |
| 94 ModelType model_type, const std::string& client_tag) { |
| 95 // Blank PB with just the field in it has termination symbol, |
| 96 // handy for delimiter. |
| 97 sync_pb::EntitySpecifics serialized_type; |
| 98 AddDefaultFieldValue(model_type, &serialized_type); |
| 99 std::string hash_input; |
| 100 serialized_type.AppendToString(&hash_input); |
| 101 hash_input.append(client_tag); |
| 102 |
| 103 std::string encode_output; |
| 104 CHECK(base::Base64Encode(base::SHA1HashString(hash_input), &encode_output)); |
| 105 return encode_output; |
| 106 } |
| 107 |
| 108 std::string GenerateSyncableBookmarkHash( |
| 109 const std::string originator_cache_guid, |
| 110 const std::string originator_client_item_id) { |
| 111 return syncable::GenerateSyncableHash( |
| 112 BOOKMARKS, originator_cache_guid + originator_client_item_id); |
| 113 } |
| 114 |
105 } // namespace syncable | 115 } // namespace syncable |
106 } // namespace syncer | 116 } // namespace syncer |
OLD | NEW |