1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Utility functions manipulating syncable::Entries, intended for use by the
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// syncer.
7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_UTIL_H_
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_SYNC_ENGINE_SYNCER_UTIL_H_
103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <set>
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string>
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <vector>
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "build/build_config.h"
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/engine/syncer.h"
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/engine/syncer_types.h"
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/syncable/syncable.h"
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/syncable/syncable_id.h"
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace browser_sync {
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass Cryptographer;
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SyncEntity;
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SyncerUtil {
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void ChangeEntryIDAndUpdateChildren(
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::WriteTransaction* trans,
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::MutableEntry* entry,
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const syncable::Id& new_id,
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::Directory::ChildHandles* children);
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the number of unsynced entries.
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static int GetUnsyncedEntries(syncable::BaseTransaction* trans,
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                std::vector<int64> *handles);
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void ChangeEntryIDAndUpdateChildren(syncable::WriteTransaction* trans,
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                             syncable::MutableEntry* entry,
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                             const syncable::Id& new_id);
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If the server sent down a client-tagged entry, or an entry whose
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // commit response was lost, it is necessary to update a local entry
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // with an ID that doesn't match the ID of the update.  Here, we
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // find the ID of such an entry, if it exists.  This function may
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // determine that |server_entry| should be dropped; if so, it returns
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the null ID -- callers must handle this case.  When update application
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // should proceed normally with a new local entry, this function will
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // return server_entry.id(); the caller must create an entry with that
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // ID.  This function does not alter the database.
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static syncable::Id FindLocalIdToUpdate(
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::BaseTransaction* trans,
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const SyncEntity& server_entry);
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static UpdateAttemptResponse AttemptToUpdateEntry(
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::WriteTransaction* const trans,
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::MutableEntry* const entry,
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      ConflictResolver* resolver,
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      Cryptographer* cryptographer);
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Pass in name to avoid redundant UTF8 conversion.
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void UpdateServerFieldsFromUpdate(
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::MutableEntry* local_entry,
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const SyncEntity& server_entry,
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const std::string& name);
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Creates a new Entry iff no Entry exists with the given id.
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void CreateNewEntry(syncable::WriteTransaction *trans,
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                             const syncable::Id& id);
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static bool ServerAndLocalEntriesMatch(syncable::Entry* entry);
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void SplitServerInformationIntoNewEntry(
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::WriteTransaction* trans,
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::MutableEntry* entry);
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This function is called on an entry when we can update the user-facing data
79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // from the server data.
80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void UpdateLocalDataFromServerData(syncable::WriteTransaction* trans,
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                            syncable::MutableEntry* entry);
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static VerifyCommitResult ValidateCommitEntry(syncable::Entry* entry);
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static VerifyResult VerifyNewEntry(const SyncEntity& update,
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                     syncable::Entry* target,
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                     const bool deleted);
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Assumes we have an existing entry; check here for updates that break
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // consistency rules.
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static VerifyResult VerifyUpdateConsistency(syncable::WriteTransaction* trans,
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                              const SyncEntity& update,
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                              syncable::MutableEntry* target,
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                              const bool deleted,
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                              const bool is_directory,
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                              syncable::ModelType model_type);
97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Assumes we have an existing entry; verify an update that seems to be
99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // expressing an 'undelete'
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static VerifyResult VerifyUndelete(syncable::WriteTransaction* trans,
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                     const SyncEntity& update,
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                     syncable::MutableEntry* target);
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Append |item|, followed by a chain of its predecessors selected by
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // |inclusion_filter|, to the |commit_ids| vector and tag them as included by
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // storing in the set |inserted_items|.  |inclusion_filter| (typically one of
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // IS_UNAPPLIED_UPDATE or IS_UNSYNCED) selects which type of predecessors to
108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // include.  Returns true if |item| was added, and false if it was already in
109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the list.
110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Use AddPredecessorsThenItem instead of this method if you want the
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // item to be the last, rather than first, item appended.
113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static bool AddItemThenPredecessors(
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::BaseTransaction* trans,
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::Entry* item,
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::IndexedBitField inclusion_filter,
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::MetahandleSet* inserted_items,
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<syncable::Id>* commit_ids);
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Exactly like AddItemThenPredecessors, except items are appended in the
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // reverse (and generally more useful) order: a chain of predecessors from
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // far to near, and finally the item.
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void AddPredecessorsThenItem(
124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::BaseTransaction* trans,
125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::Entry* item,
126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::IndexedBitField inclusion_filter,
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      syncable::MetahandleSet* inserted_items,
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<syncable::Id>* commit_ids);
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void MarkDeletedChildrenSynced(
131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const syncable::ScopedDirLookup &dir,
132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::set<syncable::Id>* deleted_folders);
133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Examine the up-to-date predecessors of this item according to the server
135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // position, and then again according to the local position.  Return true
136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // if they match.  For an up-to-date item, this should be the case.
137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static bool ServerAndLocalOrdersMatch(syncable::Entry* entry);
138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_IMPLICIT_CONSTRUCTORS(SyncerUtil);
141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef OS_WIN
144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// time.h on Linux and Mac both return seconds since the epoch, this should
146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// be converted to milliseconds.
147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline int64 ServerTimeToClientTime(int64 server_time) {
148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return server_time / GG_LONGLONG(1000);
149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline int64 ClientTimeToServerTime(int64 client_time) {
152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return client_time * GG_LONGLONG(1000);
153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// As we truncate server times on the client for posix and on the server for
156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// windows we need two ClientAndServerTimeMatch fucntions.
157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline bool ClientAndServerTimeMatch(int64 client_time, int64 server_time) {
158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Compare at the coarser timescale (client)
159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return client_time == ServerTimeToClientTime(server_time);
160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#else
162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The sync server uses Java Times (ms since 1970)
163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// and the client uses FILETIMEs (ns since 1601) so we need to convert
164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// between the timescales.
165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// TODO(sync): Fix this. No need to use two timescales.
166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline int64 ServerTimeToClientTime(int64 server_time) {
167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return server_time * GG_LONGLONG(10000) + GG_LONGLONG(116444736000000000);
168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline int64 ClientTimeToServerTime(int64 client_time) {
171c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return (client_time - GG_LONGLONG(116444736000000000)) / GG_LONGLONG(10000);
172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
173c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline bool ClientAndServerTimeMatch(int64 client_time, int64 server_time) {
175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Compare at the coarser timescale (server)
176c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return ClientTimeToServerTime(client_time) == server_time;
177c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
178c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif
179c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
180c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}  // namespace browser_sync
181c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
182c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_SYNC_ENGINE_SYNCER_UTIL_H_
183