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

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

Issue 7453014: [Sync] Refactor sync datatype error handling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add operator= Created 9 years, 5 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 "base/tracked.h" 7 #include "base/tracked.h"
8 #include "chrome/browser/sync/api/syncable_service.h" 8 #include "chrome/browser/sync/api/syncable_service.h"
9 #include "chrome/browser/sync/api/sync_change.h" 9 #include "chrome/browser/sync/api/sync_change.h"
10 #include "chrome/browser/sync/api/sync_error.h"
10 #include "chrome/browser/sync/engine/syncapi.h" 11 #include "chrome/browser/sync/engine/syncapi.h"
11 #include "chrome/browser/sync/syncable/nigori_util.h" 12 #include "chrome/browser/sync/syncable/nigori_util.h"
12 13
13 namespace browser_sync { 14 namespace browser_sync {
14 15
15 GenericChangeProcessor::GenericChangeProcessor( 16 GenericChangeProcessor::GenericChangeProcessor(
16 SyncableService* local_service, 17 SyncableService* local_service,
17 UnrecoverableErrorHandler* error_handler, 18 UnrecoverableErrorHandler* error_handler,
18 sync_api::UserShare* user_share) 19 sync_api::UserShare* user_share)
19 : ChangeProcessor(error_handler), 20 : ChangeProcessor(error_handler),
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 SyncChange(action, SyncData::CreateRemoteData(*specifics))); 63 SyncChange(action, SyncData::CreateRemoteData(*specifics)));
63 } 64 }
64 } 65 }
65 } 66 }
66 67
67 void GenericChangeProcessor::CommitChangesFromSyncModel() { 68 void GenericChangeProcessor::CommitChangesFromSyncModel() {
68 if (!running()) 69 if (!running())
69 return; 70 return;
70 if (syncer_changes_.empty()) 71 if (syncer_changes_.empty())
71 return; 72 return;
72 local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_); 73 SyncError error;
74 if (!local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_, &error)) {
75 error_handler()->OnUnrecoverableError(error.location(), error.message());
76 }
73 syncer_changes_.clear(); 77 syncer_changes_.clear();
74 } 78 }
75 79
76 bool GenericChangeProcessor::GetSyncDataForType( 80 bool GenericChangeProcessor::GetSyncDataForType(
77 syncable::ModelType type, 81 syncable::ModelType type,
78 SyncDataList* current_sync_data) { 82 SyncDataList* current_sync_data,
83 SyncError* error) {
79 std::string type_name = syncable::ModelTypeToString(type); 84 std::string type_name = syncable::ModelTypeToString(type);
80 sync_api::ReadTransaction trans(FROM_HERE, share_handle()); 85 sync_api::ReadTransaction trans(FROM_HERE, share_handle());
81 sync_api::ReadNode root(&trans); 86 sync_api::ReadNode root(&trans);
82 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) { 87 if (!root.InitByTagLookup(syncable::ModelTypeToRootTag(type))) {
83 LOG(ERROR) << "Server did not create the top-level " + type_name + " node." 88 error->Reset(FROM_HERE,
84 << " We might be running against an out-of-date server."; 89 "Server did not create the top-level " + type_name + " node."
90 " We might be running against an out-of-date server.",
91 type);
85 return false; 92 return false;
86 } 93 }
87 94
88 int64 sync_child_id = root.GetFirstChildId(); 95 int64 sync_child_id = root.GetFirstChildId();
89 while (sync_child_id != sync_api::kInvalidId) { 96 while (sync_child_id != sync_api::kInvalidId) {
90 sync_api::ReadNode sync_child_node(&trans); 97 sync_api::ReadNode sync_child_node(&trans);
91 if (!sync_child_node.InitByIdLookup(sync_child_id)) { 98 if (!sync_child_node.InitByIdLookup(sync_child_id)) {
92 LOG(ERROR) << "Failed to fetch child node for type " + type_name + "."; 99 error->Reset(FROM_HERE,
100 "Failed to fetch child node for type " + type_name + ".",
101 type);
93 return false; 102 return false;
94 } 103 }
95 current_sync_data->push_back(SyncData::CreateRemoteData( 104 current_sync_data->push_back(SyncData::CreateRemoteData(
96 sync_child_node.GetEntitySpecifics())); 105 sync_child_node.GetEntitySpecifics()));
97 sync_child_id = sync_child_node.GetSuccessorId(); 106 sync_child_id = sync_child_node.GetSuccessorId();
98 } 107 }
99 return true; 108 return true;
100 } 109 }
101 110
102 void GenericChangeProcessor::ProcessSyncChanges( 111 bool GenericChangeProcessor::ProcessSyncChanges(
103 const tracked_objects::Location& from_here, 112 const tracked_objects::Location& from_here,
104 const SyncChangeList& list_of_changes) { 113 const SyncChangeList& list_of_changes,
114 SyncError* error) {
105 sync_api::WriteTransaction trans(from_here, share_handle()); 115 sync_api::WriteTransaction trans(from_here, share_handle());
106 116
107 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); 117 for (SyncChangeList::const_iterator iter = list_of_changes.begin();
108 iter != list_of_changes.end(); 118 iter != list_of_changes.end();
109 ++iter) { 119 ++iter) {
110 const SyncChange& change = *iter; 120 const SyncChange& change = *iter;
111 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); 121 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED);
112 std::string type_str = syncable::ModelTypeToString( 122 syncable::ModelType type = change.sync_data().GetDataType();
113 change.sync_data().GetDataType()); 123 std::string type_str = syncable::ModelTypeToString(type);
114 sync_api::WriteNode sync_node(&trans); 124 sync_api::WriteNode sync_node(&trans);
115 if (change.change_type() == SyncChange::ACTION_DELETE) { 125 if (change.change_type() == SyncChange::ACTION_DELETE) {
116 if (change.sync_data().GetTag() == "" || 126 if (change.sync_data().GetTag() == "" ||
117 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(), 127 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(),
118 change.sync_data().GetTag())) { 128 change.sync_data().GetTag())) {
119 NOTREACHED(); 129 NOTREACHED();
120 error_handler()->OnUnrecoverableError(FROM_HERE, 130 error->Reset(FROM_HERE,
121 "Failed to delete " + type_str + " node."); 131 "Failed to delete " + type_str + " node.",
122 return; 132 type);
133 error_handler()->OnUnrecoverableError(error->location(),
134 error->message());
135 return false;
123 } 136 }
124 sync_node.Remove(); 137 sync_node.Remove();
125 } else if (change.change_type() == SyncChange::ACTION_ADD) { 138 } else if (change.change_type() == SyncChange::ACTION_ADD) {
126 // TODO(sync): Handle other types of creation (custom parents, folders, 139 // TODO(sync): Handle other types of creation (custom parents, folders,
127 // etc.). 140 // etc.).
128 sync_api::ReadNode root_node(&trans); 141 sync_api::ReadNode root_node(&trans);
129 if (!root_node.InitByTagLookup( 142 if (!root_node.InitByTagLookup(
130 syncable::ModelTypeToRootTag(change.sync_data().GetDataType()))) { 143 syncable::ModelTypeToRootTag(change.sync_data().GetDataType()))) {
131 NOTREACHED(); 144 NOTREACHED();
132 error_handler()->OnUnrecoverableError(FROM_HERE, 145 error->Reset(FROM_HERE,
133 "Failed to look up root node for type " + type_str); 146 "Failed to look up root node for type " + type_str,
134 return; 147 type);
148 error_handler()->OnUnrecoverableError(error->location(),
149 error->message());
150 return false;
135 } 151 }
136 if (!sync_node.InitUniqueByCreation(change.sync_data().GetDataType(), 152 if (!sync_node.InitUniqueByCreation(change.sync_data().GetDataType(),
137 root_node, 153 root_node,
138 change.sync_data().GetTag())) { 154 change.sync_data().GetTag())) {
139 error_handler()->OnUnrecoverableError(FROM_HERE, 155 NOTREACHED();
140 "Failed to create " + type_str + " node."); 156 error->Reset(FROM_HERE,
141 return; 157 "Failed to create " + type_str + " node.",
158 type);
159 error_handler()->OnUnrecoverableError(error->location(),
160 error->message());
161 return false;
142 } 162 }
143 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle())); 163 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle()));
144 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics()); 164 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics());
145 } else if (change.change_type() == SyncChange::ACTION_UPDATE) { 165 } else if (change.change_type() == SyncChange::ACTION_UPDATE) {
146 if (change.sync_data().GetTag() == "" || 166 if (change.sync_data().GetTag() == "" ||
147 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(), 167 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(),
148 change.sync_data().GetTag())) { 168 change.sync_data().GetTag())) {
149 NOTREACHED(); 169 NOTREACHED();
150 error_handler()->OnUnrecoverableError(FROM_HERE, 170 error->Reset(FROM_HERE,
151 "Failed to update " + type_str + " node"); 171 "Failed to update " + type_str + " node.",
152 return; 172 type);
173 error_handler()->OnUnrecoverableError(error->location(),
174 error->message());
175 return false;
153 } 176 }
154 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle())); 177 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle()));
155 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics()); 178 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics());
156 // TODO(sync): Support updating other parts of the sync node (title, 179 // TODO(sync): Support updating other parts of the sync node (title,
157 // successor, parent, etc.). 180 // successor, parent, etc.).
158 } else { 181 } else {
159 NOTREACHED(); 182 NOTREACHED();
160 error_handler()->OnUnrecoverableError(FROM_HERE, 183 error->Reset(FROM_HERE,
161 "Received unset SyncChange in the change processor."); 184 "Received unset SyncChange in the change processor.",
162 return; 185 type);
186 error_handler()->OnUnrecoverableError(error->location(),
187 error->message());
188 return false;
163 } 189 }
164 } 190 }
191 return true;
165 } 192 }
166 193
167 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( 194 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(
168 syncable::ModelType type, 195 syncable::ModelType type,
169 bool* has_nodes) { 196 bool* has_nodes) {
170 DCHECK(has_nodes); 197 DCHECK(has_nodes);
171 DCHECK_NE(type, syncable::UNSPECIFIED); 198 DCHECK_NE(type, syncable::UNSPECIFIED);
172 std::string type_name = syncable::ModelTypeToString(type); 199 std::string type_name = syncable::ModelTypeToString(type);
173 std::string err_str = "Server did not create the top-level " + type_name + 200 std::string err_str = "Server did not create the top-level " + type_name +
174 " node. We might be running against an out-of-date server."; 201 " node. We might be running against an out-of-date server.";
(...skipping 23 matching lines...) Expand all
198 225
199 void GenericChangeProcessor::StartImpl(Profile* profile) {} 226 void GenericChangeProcessor::StartImpl(Profile* profile) {}
200 227
201 void GenericChangeProcessor::StopImpl() {} 228 void GenericChangeProcessor::StopImpl() {}
202 229
203 sync_api::UserShare* GenericChangeProcessor::share_handle() { 230 sync_api::UserShare* GenericChangeProcessor::share_handle() {
204 return user_share_; 231 return user_share_;
205 } 232 }
206 233
207 } // namespace browser_sync 234 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698