1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/glue/generic_change_processor.h"
#include "base/tracked.h"
#include "chrome/browser/sync/api/syncable_service.h"
#include "chrome/browser/sync/api/sync_change.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/syncable/nigori_util.h"
namespace browser_sync {
GenericChangeProcessor::GenericChangeProcessor(
SyncableService* local_service,
UnrecoverableErrorHandler* error_handler,
sync_api::UserShare* user_share)
: ChangeProcessor(error_handler),
local_service_(local_service),
user_share_(user_share) {
DCHECK(local_service_);
}
GenericChangeProcessor::~GenericChangeProcessor() {
// Set to null to ensure it's not used after destruction.
local_service_ = NULL;
}
void GenericChangeProcessor::ApplyChangesFromSyncModel(
const sync_api::BaseTransaction* trans,
const sync_api::SyncManager::ChangeRecord* changes,
int change_count) {
DCHECK(running());
DCHECK(syncer_changes_.empty());
for (int i = 0; i < change_count; ++i) {
SyncChange::SyncChangeType action;
sync_pb::EntitySpecifics const* specifics = NULL;
if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE ==
changes[i].action) {
action = SyncChange::ACTION_DELETE;
specifics = &changes[i].specifics;
DCHECK(specifics);
} else if (sync_api::SyncManager::ChangeRecord::ACTION_ADD ==
changes[i].action) {
action = SyncChange::ACTION_ADD;
} else { // ACTION_UPDATE.
action = SyncChange::ACTION_UPDATE;
}
if (!specifics) {
// Need to load from node.
sync_api::ReadNode read_node(trans);
if (!read_node.InitByIdLookup(changes[i].id)) {
error_handler()->OnUnrecoverableError(FROM_HERE, "Failed to look up "
" data for received change with id " + changes[i].id);
return;
}
syncer_changes_.push_back(SyncChange(action,
SyncData::CreateRemoteData(read_node.GetEntitySpecifics())));
} else {
syncer_changes_.push_back(
SyncChange(action, SyncData::CreateRemoteData(*specifics)));
}
}
}
void GenericChangeProcessor::CommitChangesFromSyncModel() {
if (!running())
return;
if (syncer_changes_.empty())
return;
local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_);
syncer_changes_.clear();
}
bool GenericChangeProcessor::GetSyncDataForType(
syncable::ModelType type,
SyncDataList* current_sync_data) {
std::string type_name = syncable::ModelTypeToString(type);
sync_api::ReadTransaction trans(FROM_HERE, share_handle());
sync_api::ReadNode root(&trans);
if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) {
LOG(ERROR) << "Server did not create the top-level " + type_name + " node."
<< " We might be running against an out-of-date server.";
return false;
}
int64 sync_child_id = root.GetFirstChildId();
while (sync_child_id != sync_api::kInvalidId) {
sync_api::ReadNode sync_child_node(&trans);
if (!sync_child_node.InitByIdLookup(sync_child_id)) {
LOG(ERROR) << "Failed to fetch child node for type " + type_name + ".";
return false;
}
current_sync_data->push_back(SyncData::CreateRemoteData(
sync_child_node.GetEntitySpecifics()));
sync_child_id = sync_child_node.GetSuccessorId();
}
return true;
}
void GenericChangeProcessor::ProcessSyncChanges(
const tracked_objects::Location& from_here,
const SyncChangeList& list_of_changes) {
sync_api::WriteTransaction trans(from_here, share_handle());
for (SyncChangeList::const_iterator iter = list_of_changes.begin();
iter != list_of_changes.end();
++iter) {
const SyncChange& change = *iter;
DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED);
std::string type_str = syncable::ModelTypeToString(
change.sync_data().GetDataType());
sync_api::WriteNode sync_node(&trans);
if (change.change_type() == SyncChange::ACTION_DELETE) {
if (change.sync_data().GetTag() == "" ||
!sync_node.InitByClientTagLookup(change.sync_data().GetDataType(),
change.sync_data().GetTag())) {
NOTREACHED();
error_handler()->OnUnrecoverableError(FROM_HERE,
"Failed to delete " + type_str + " node.");
return;
}
sync_node.Remove();
} else if (change.change_type() == SyncChange::ACTION_ADD) {
// TODO(sync): Handle other types of creation (custom parents, folders,
// etc.).
sync_api::ReadNode root_node(&trans);
if (!root_node.InitByTagLookup(
syncable::ModelTypeToRootTag(change.sync_data().GetDataType()))) {
NOTREACHED();
error_handler()->OnUnrecoverableError(FROM_HERE,
"Failed to look up root node for type " + type_str);
return;
}
if (!sync_node.InitUniqueByCreation(change.sync_data().GetDataType(),
root_node,
change.sync_data().GetTag())) {
error_handler()->OnUnrecoverableError(FROM_HERE,
"Failed to create " + type_str + " node.");
return;
}
sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle()));
sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics());
} else if (change.change_type() == SyncChange::ACTION_UPDATE) {
if (change.sync_data().GetTag() == "" ||
!sync_node.InitByClientTagLookup(change.sync_data().GetDataType(),
change.sync_data().GetTag())) {
NOTREACHED();
error_handler()->OnUnrecoverableError(FROM_HERE,
"Failed to update " + type_str + " node");
return;
}
sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle()));
sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics());
// TODO(sync): Support updating other parts of the sync node (title,
// successor, parent, etc.).
} else {
NOTREACHED();
error_handler()->OnUnrecoverableError(FROM_HERE,
"Received unset SyncChange in the change processor.");
return;
}
}
}
bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(
syncable::ModelType type,
bool* has_nodes) {
DCHECK(has_nodes);
DCHECK_NE(type, syncable::UNSPECIFIED);
std::string type_name = syncable::ModelTypeToString(type);
std::string err_str = "Server did not create the top-level " + type_name +
" node. We might be running against an out-of-date server.";
*has_nodes = false;
sync_api::ReadTransaction trans(FROM_HERE, share_handle());
sync_api::ReadNode type_root_node(&trans);
if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) {
LOG(ERROR) << err_str;
return false;
}
// The sync model has user created nodes if the type's root node has any
// children.
*has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId();
return true;
}
bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) {
DCHECK_NE(type, syncable::UNSPECIFIED);
// We only access the cryptographer while holding a transaction.
sync_api::ReadTransaction trans(FROM_HERE, share_handle());
const syncable::ModelTypeSet& encrypted_types =
GetEncryptedTypes(&trans);
return encrypted_types.count(type) == 0 ||
trans.GetCryptographer()->is_ready();
}
void GenericChangeProcessor::StartImpl(Profile* profile) {}
void GenericChangeProcessor::StopImpl() {}
sync_api::UserShare* GenericChangeProcessor::share_handle() {
return user_share_;
}
} // namespace browser_sync
|