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

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: Foward Declare++ 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_event.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 SyncEventList list_of_changes;
35 for (int i = 0; i < change_count; ++i) {
36 SyncEvent::SyncEventType action;
37 sync_pb::EntitySpecifics const* specifics = NULL;
38 if (sync_api::SyncManager::ChangeRecord::ACTION_DELETE ==
39 changes[i].action) {
40 action = SyncEvent::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 = SyncEvent::ACTION_ADD;
46 } else { // ACTION_UPDATE.
47 action = SyncEvent::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 SyncEvent(action, SyncData::CreateRemoteData(*specifics)));
63 }
64 local_service_->ProcessSyncEvents(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::ProcessSyncEvents(
94 const SyncEventList& list_of_changes) {
95 sync_api::WriteTransaction trans(share_handle());
96
97 SyncEventList::const_iterator event_iterator;
akalin 2011/05/19 00:58:45 usual style is to just use 'it' for iterators, rig
Nicolas Zea 2011/05/19 21:17:45 I think "iter" is more common, but there's no real
98 for (event_iterator = list_of_changes.begin();
99 event_iterator != list_of_changes.end();
100 ++event_iterator) {
101 const SyncEvent& event = *event_iterator;
102 DCHECK_NE(event.sync_data().GetDataType(), syncable::UNSPECIFIED);
103 std::string type_str = syncable::ModelTypeToString(
104 event.sync_data().GetDataType());
105 sync_api::WriteNode sync_node(&trans);
106 if (event.event_type() == SyncEvent::ACTION_DELETE) {
107 if (event.sync_data().GetTag() == "" ||
108 !sync_node.InitByClientTagLookup(event.sync_data().GetDataType(),
109 event.sync_data().GetTag())) {
110 NOTREACHED();
akalin 2011/05/19 00:58:45 I feel like this is a recoverable error -- maybe w
Nicolas Zea 2011/05/19 21:17:45 This is how change processors normally handle fail
111 error_handler()->OnUnrecoverableError(FROM_HERE,
112 "Failed to delete " + type_str + " node.");
113 return;
114 }
115 sync_node.Remove();
116 } else if (event.event_type() == SyncEvent::ACTION_ADD) {
117 // TODO(sync): Handle other types of creation (custom parents, folders,
118 // etc.).
119 sync_api::ReadNode root_node(&trans);
120 if (!root_node.InitByTagLookup(
121 syncable::ModelTypeToRootTag(event.sync_data().GetDataType()))) {
122 NOTREACHED();
123 error_handler()->OnUnrecoverableError(FROM_HERE,
124 "Failed to look up root node for type " + type_str);
125 return;
126 }
127 if (!sync_node.InitUniqueByCreation(event.sync_data().GetDataType(),
128 root_node,
129 event.sync_data().GetTag())) {
130 error_handler()->OnUnrecoverableError(FROM_HERE,
131 "Failed to create " + type_str + " node.");
132 return;
133 }
134 sync_node.SetEntitySpecifics(event.sync_data().GetSpecifics());
135 } else if (event.event_type() == SyncEvent::ACTION_UPDATE) {
136 if (event.sync_data().GetTag() == "" ||
akalin 2011/05/19 00:58:45 i think the empty tag case is a recoverable error
Nicolas Zea 2011/05/19 21:17:45 See previous comment.
137 !sync_node.InitByClientTagLookup(event.sync_data().GetDataType(),
138 event.sync_data().GetTag())) {
139 NOTREACHED();
140 error_handler()->OnUnrecoverableError(FROM_HERE,
141 "Failed to update " + type_str + " node");
142 return;
143 }
144 sync_node.SetEntitySpecifics(event.sync_data().GetSpecifics());
145 // TODO(sync): Support updating other parts of the sync node (title,
146 // successor, parent, etc.).
147 } else {
148 NOTREACHED();
149 error_handler()->OnUnrecoverableError(FROM_HERE,
akalin 2011/05/19 00:58:45 I think we can be resilient to bad clients and jus
Nicolas Zea 2011/05/19 21:17:45 See previous comment.
150 "Received unset SyncEvent in the change processor.");
151 return;
152 }
153 }
154 }
155
156 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(
157 syncable::ModelType type,
158 bool* has_nodes) {
159 DCHECK(has_nodes);
160 DCHECK_NE(type, syncable::UNSPECIFIED);
161 std::string type_name = syncable::ModelTypeToString(type);
162 std::string err_str = "Server did not create the top-level " + type_name +
163 " node. We might be running against an out-of-date server.";
164 *has_nodes = false;
165 sync_api::ReadTransaction trans(share_handle());
166 sync_api::ReadNode type_root_node(&trans);
167 if (!type_root_node.InitByTagLookup(syncable::ModelTypeToRootTag(type))) {
168 LOG(ERROR) << err_str;
169 return false;
170 }
171
172 // The sync model has user created nodes if the type's root node has any
173 // children.
174 *has_nodes = sync_api::kInvalidId != type_root_node.GetFirstChildId();
175 return true;
176 }
177
178 bool GenericChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) {
179 DCHECK_NE(type, syncable::UNSPECIFIED);
180 // We only access the cryptographer while holding a transaction.
181 sync_api::ReadTransaction trans(share_handle());
182 const syncable::ModelTypeSet& encrypted_types =
183 syncable::GetEncryptedDataTypes(trans.GetWrappedTrans());
184 return encrypted_types.count(type) == 0 ||
185 trans.GetCryptographer()->is_ready();
29 } 186 }
30 187
31 void GenericChangeProcessor::StartImpl(Profile* profile) {} 188 void GenericChangeProcessor::StartImpl(Profile* profile) {}
32 189
33 void GenericChangeProcessor::StopImpl() {} 190 void GenericChangeProcessor::StopImpl() {}
34 191
192 sync_api::UserShare* GenericChangeProcessor::share_handle() {
193 return user_share_;
194 }
195
35 } // namespace browser_sync 196 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698