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