OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 // Utility functions manipulating syncable::Entries, intended for use by |
| 6 // the syncer. |
| 7 |
| 8 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_UTIL_H_ |
| 9 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_UTIL_H_ |
| 10 |
| 11 #include <set> |
| 12 #include <string> |
| 13 #include <vector> |
| 14 |
| 15 #include "chrome/browser/sync/engine/syncer.h" |
| 16 #include "chrome/browser/sync/engine/syncer_types.h" |
| 17 #include "chrome/browser/sync/syncable/syncable_id.h" |
| 18 #include "chrome/browser/sync/syncable/syncable.h" |
| 19 #include "chrome/browser/sync/util/path_helpers.h" |
| 20 #include "chrome/browser/sync/util/sync_types.h" |
| 21 |
| 22 namespace browser_sync { |
| 23 |
| 24 class SyncerSession; |
| 25 class SyncEntity; |
| 26 |
| 27 |
| 28 class SyncerUtil { |
| 29 public: |
| 30 // TODO(ncarter): Remove unique-in-parent title support and name conflicts. |
| 31 static syncable::Id GetNameConflictingItemId( |
| 32 syncable::BaseTransaction* trans, |
| 33 const syncable::Id& parent_id, |
| 34 const PathString& server_name); |
| 35 |
| 36 static void ChangeEntryIDAndUpdateChildren( |
| 37 syncable::WriteTransaction* trans, |
| 38 syncable::MutableEntry* entry, |
| 39 const syncable::Id& new_id, |
| 40 syncable::Directory::ChildHandles* children); |
| 41 |
| 42 // Returns the number of unsynced entries. |
| 43 static int GetUnsyncedEntries(syncable::BaseTransaction* trans, |
| 44 std::vector<int64> *handles); |
| 45 |
| 46 static void ChangeEntryIDAndUpdateChildren(syncable::WriteTransaction* trans, |
| 47 syncable::MutableEntry* entry, |
| 48 const syncable::Id& new_id); |
| 49 |
| 50 static void AttemptReuniteLostCommitResponses( |
| 51 syncable::WriteTransaction* trans, |
| 52 const SyncEntity& server_entry, |
| 53 const std::string& client_id); |
| 54 |
| 55 static UpdateAttemptResponse AttemptToUpdateEntry( |
| 56 syncable::WriteTransaction* const trans, |
| 57 syncable::MutableEntry* const entry, |
| 58 SyncerSession* const session); |
| 59 |
| 60 static UpdateAttemptResponse AttemptToUpdateEntryWithoutMerge( |
| 61 syncable::WriteTransaction* const trans, |
| 62 syncable::MutableEntry* const entry, |
| 63 SyncerSession* const session, syncable::Id* const conflicting_id); |
| 64 |
| 65 // Pass in name to avoid redundant UTF8 conversion. |
| 66 static void UpdateServerFieldsFromUpdate( |
| 67 syncable::MutableEntry* local_entry, |
| 68 const SyncEntity& server_entry, |
| 69 const syncable::SyncName& name); |
| 70 |
| 71 static void ApplyExtendedAttributes( |
| 72 syncable::MutableEntry* local_entry, |
| 73 const SyncEntity& server_entry); |
| 74 |
| 75 // Creates a new Entry iff no Entry exists with the given id. |
| 76 static void CreateNewEntry(syncable::WriteTransaction *trans, |
| 77 const syncable::Id& id); |
| 78 |
| 79 static bool ServerAndLocalEntriesMatch(syncable::Entry* entry); |
| 80 |
| 81 static void SplitServerInformationIntoNewEntry( |
| 82 syncable::WriteTransaction* trans, |
| 83 syncable::MutableEntry* entry); |
| 84 |
| 85 // This function is called on an entry when we can update the user-facing data |
| 86 // from the server data. |
| 87 static void UpdateLocalDataFromServerData(syncable::WriteTransaction* trans, |
| 88 syncable::MutableEntry* entry); |
| 89 |
| 90 static VerifyCommitResult ValidateCommitEntry(syncable::MutableEntry* entry); |
| 91 |
| 92 static VerifyResult VerifyNewEntry(const SyncEntity& entry, |
| 93 syncable::MutableEntry* same_id, |
| 94 const bool deleted); |
| 95 |
| 96 // Assumes we have an existing entry; check here for updates that break |
| 97 // consistency rules. |
| 98 static VerifyResult VerifyUpdateConsistency(syncable::WriteTransaction* trans, |
| 99 const SyncEntity& entry, |
| 100 syncable::MutableEntry* same_id, |
| 101 const bool deleted, |
| 102 const bool is_directory, |
| 103 const bool is_bookmark); |
| 104 |
| 105 // Assumes we have an existing entry; verify an update that seems to be |
| 106 // expressing an 'undelete' |
| 107 static VerifyResult VerifyUndelete(syncable::WriteTransaction* trans, |
| 108 const SyncEntity& entry, |
| 109 syncable::MutableEntry* same_id); |
| 110 |
| 111 // Compute a local predecessor position for |update_item|. The position |
| 112 // is determined by the SERVER_POSITION_IN_PARENT value of |update_item|, |
| 113 // as well as the SERVER_POSITION_IN_PARENT values of any up-to-date |
| 114 // children of |parent_id|. |
| 115 static syncable::Id ComputePrevIdFromServerPosition( |
| 116 syncable::BaseTransaction* trans, |
| 117 syncable::Entry* update_item, |
| 118 const syncable::Id& parent_id); |
| 119 |
| 120 // Append |item|, followed by a chain of its predecessors selected by |
| 121 // |inclusion_filter|, to the |commit_ids| vector and tag them as included by |
| 122 // storing in the set |inserted_items|. |inclusion_filter| (typically one of |
| 123 // IS_UNAPPLIED_UPDATE or IS_UNSYNCED) selects which type of predecessors to |
| 124 // include. Returns true if |item| was added, and false if it was already in |
| 125 // the list. |
| 126 // |
| 127 // Use AddPredecessorsThenItem instead of this method if you want the |
| 128 // item to be the last, rather than first, item appended. |
| 129 static bool AddItemThenPredecessors( |
| 130 syncable::BaseTransaction* trans, |
| 131 syncable::Entry* item, |
| 132 syncable::IndexedBitField inclusion_filter, |
| 133 syncable::MetahandleSet* inserted_items, |
| 134 std::vector<syncable::Id>* commit_ids); |
| 135 |
| 136 // Exactly like AddItemThenPredecessors, except items are appended in the |
| 137 // reverse (and generally more useful) order: a chain of predecessors from |
| 138 // far to near, and finally the item. |
| 139 static void AddPredecessorsThenItem( |
| 140 syncable::BaseTransaction* trans, |
| 141 syncable::Entry* item, |
| 142 syncable::IndexedBitField inclusion_filter, |
| 143 syncable::MetahandleSet* inserted_items, |
| 144 std::vector<syncable::Id>* commit_ids); |
| 145 |
| 146 static void AddUncommittedParentsAndTheirPredecessors( |
| 147 syncable::BaseTransaction* trans, |
| 148 syncable::MetahandleSet* inserted_items, |
| 149 std::vector<syncable::Id>* commit_ids, |
| 150 syncable::Id parent_id); |
| 151 |
| 152 static void MarkDeletedChildrenSynced( |
| 153 const syncable::ScopedDirLookup &dir, |
| 154 std::set<syncable::Id>* deleted_folders); |
| 155 |
| 156 // Examine the up-to-date predecessors of this item according to the server |
| 157 // position, and then again according to the local position. Return true |
| 158 // if they match. For an up-to-date item, this should be the case. |
| 159 static bool ServerAndLocalOrdersMatch(syncable::Entry* entry); |
| 160 |
| 161 private: |
| 162 // Private ctor/dtor since this class shouldn't be instantiated. |
| 163 SyncerUtil() {} |
| 164 virtual ~SyncerUtil() {} |
| 165 DISALLOW_COPY_AND_ASSIGN(SyncerUtil); |
| 166 }; |
| 167 |
| 168 #ifndef OS_WINDOWS |
| 169 |
| 170 // time.h on Linux and Mac both return seconds since the epoch, this should |
| 171 // be converted to milliseconds. |
| 172 inline int64 ServerTimeToClientTime(int64 server_time) { |
| 173 return server_time / GG_LONGLONG(1000); |
| 174 } |
| 175 |
| 176 inline int64 ClientTimeToServerTime(int64 client_time) { |
| 177 return client_time * GG_LONGLONG(1000); |
| 178 } |
| 179 |
| 180 // As we truncate server times on the client for posix and on the server for |
| 181 // windows we need two ClientAndServerTimeMatch fucntions. |
| 182 inline bool ClientAndServerTimeMatch(int64 client_time, int64 server_time) { |
| 183 // Compare at the coarser timescale (client) |
| 184 return client_time == ServerTimeToClientTime(server_time); |
| 185 } |
| 186 #else |
| 187 // The sync server uses Java Times (ms since 1970) |
| 188 // and the client uses FILETIMEs (ns since 1601) so we need to convert |
| 189 // between the timescales. |
| 190 inline int64 ServerTimeToClientTime(int64 server_time) { |
| 191 return server_time * GG_LONGLONG(10000) + GG_LONGLONG(116444736000000000); |
| 192 } |
| 193 |
| 194 inline int64 ClientTimeToServerTime(int64 client_time) { |
| 195 return (client_time - GG_LONGLONG(116444736000000000)) / GG_LONGLONG(10000); |
| 196 } |
| 197 |
| 198 inline bool ClientAndServerTimeMatch(int64 client_time, int64 server_time) { |
| 199 // Compare at the coarser timescale (server) |
| 200 return ClientTimeToServerTime(client_time) == server_time; |
| 201 } |
| 202 #endif |
| 203 |
| 204 } // namespace browser_sync |
| 205 |
| 206 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_UTIL_H_ |
OLD | NEW |