Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: chrome/browser/sync/glue/generic_change_processor.cc

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

Powered by Google App Engine
This is Rietveld 408576698