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// Utility functions manipulating syncable::Entries, intended for use by the
6// syncer.
7
8#ifndef SYNC_ENGINE_SYNCER_UTIL_H_
9#define SYNC_ENGINE_SYNCER_UTIL_H_
10
11#include <set>
12#include <string>
13#include <vector>
14
15#include "sync/engine/syncer.h"
16#include "sync/engine/syncer_types.h"
17#include "sync/syncable/entry_kernel.h"
18#include "sync/syncable/metahandle_set.h"
19#include "sync/syncable/mutable_entry.h"
20#include "sync/syncable/syncable_id.h"
21
22namespace sync_pb {
23class SyncEntity;
24}  // namespace sync_pb
25
26namespace syncer {
27
28namespace syncable {
29class BaseTransaction;
30}  // namespace syncable
31
32class Cryptographer;
33
34// If the server sent down a client-tagged entry, or an entry whose
35// commit response was lost, it is necessary to update a local entry
36// with an ID that doesn't match the ID of the update.  Here, we
37// find the ID of such an entry, if it exists.  This function may
38// determine that |server_entry| should be dropped; if so, it returns
39// the null ID -- callers must handle this case.  When update application
40// should proceed normally with a new local entry, this function will
41// return server_entry.id(); the caller must create an entry with that
42// ID.  This function does not alter the database.
43syncable::Id FindLocalIdToUpdate(
44    syncable::BaseTransaction* trans,
45    const sync_pb::SyncEntity& server_entry);
46
47UpdateAttemptResponse AttemptToUpdateEntry(
48    syncable::WriteTransaction* const trans,
49    syncable::MutableEntry* const entry,
50    Cryptographer* cryptographer);
51
52// Returns the most accurate position information available in this update.  It
53// prefers to use the unique_position() field, but will fall back to using the
54// int64-based position_in_parent if necessary.
55//
56// The suffix parameter is the unique bookmark tag for the item being updated.
57//
58// Will return an invalid position if no valid position can be constructed, or
59// if this type does not support positioning.
60UniquePosition GetUpdatePosition(const sync_pb::SyncEntity& update,
61                                 const std::string& suffix);
62
63// Fetch the cache_guid and item_id-based unique bookmark tag from an update.
64// Will return an empty string if someting unexpected happens.
65std::string GetUniqueBookmarkTagFromUpdate(const sync_pb::SyncEntity& update);
66
67// Pass in name to avoid redundant UTF8 conversion.
68void UpdateServerFieldsFromUpdate(
69    syncable::MutableEntry* local_entry,
70    const sync_pb::SyncEntity& server_entry,
71    const std::string& name);
72
73// Creates a new Entry iff no Entry exists with the given id.
74void CreateNewEntry(syncable::WriteTransaction *trans,
75                    const syncable::Id& id);
76
77// This function is called on an entry when we can update the user-facing data
78// from the server data.
79void UpdateLocalDataFromServerData(syncable::WriteTransaction* trans,
80                                   syncable::MutableEntry* entry);
81
82VerifyCommitResult ValidateCommitEntry(syncable::Entry* entry);
83
84VerifyResult VerifyNewEntry(const sync_pb::SyncEntity& update,
85                            syncable::Entry* target,
86                            const bool deleted);
87
88// Assumes we have an existing entry; check here for updates that break
89// consistency rules.
90VerifyResult VerifyUpdateConsistency(syncable::WriteTransaction* trans,
91                                     const sync_pb::SyncEntity& update,
92                                     syncable::MutableEntry* target,
93                                     const bool deleted,
94                                     const bool is_directory,
95                                     ModelType model_type);
96
97// Assumes we have an existing entry; verify an update that seems to be
98// expressing an 'undelete'
99VerifyResult VerifyUndelete(syncable::WriteTransaction* trans,
100                            const sync_pb::SyncEntity& update,
101                            syncable::MutableEntry* target);
102
103void MarkDeletedChildrenSynced(
104    syncable::Directory* dir,
105    std::set<syncable::Id>* deleted_folders);
106
107}  // namespace syncer
108
109#endif  // SYNC_ENGINE_SYNCER_UTIL_H_
110