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

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: Rebase 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 = local_service_->ProcessSyncChanges(FROM_HERE,
74 syncer_changes_);
73 syncer_changes_.clear(); 75 syncer_changes_.clear();
76 if (error.IsSet()) {
77 error_handler()->OnUnrecoverableError(error.location(), error.message());
78 }
74 } 79 }
75 80
76 bool GenericChangeProcessor::GetSyncDataForType( 81 SyncError GenericChangeProcessor::GetSyncDataForType(
77 syncable::ModelType type, 82 syncable::ModelType type,
78 SyncDataList* current_sync_data) { 83 SyncDataList* current_sync_data) {
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 SyncError error(FROM_HERE,
84 << " We might be running against an out-of-date server."; 89 "Server did not create the top-level " + type_name +
85 return false; 90 " node. We might be running against an out-of-date server.",
91 type);
92 return error;
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 SyncError error(FROM_HERE,
93 return false; 100 "Failed to fetch child node for type " + type_name + ".",
101 type);
102 return error;
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 SyncError();
100 } 109 }
101 110
102 void GenericChangeProcessor::ProcessSyncChanges( 111 SyncError 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) {
105 sync_api::WriteTransaction trans(from_here, share_handle()); 114 sync_api::WriteTransaction trans(from_here, share_handle());
106 115
107 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); 116 for (SyncChangeList::const_iterator iter = list_of_changes.begin();
108 iter != list_of_changes.end(); 117 iter != list_of_changes.end();
109 ++iter) { 118 ++iter) {
110 const SyncChange& change = *iter; 119 const SyncChange& change = *iter;
111 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED); 120 DCHECK_NE(change.sync_data().GetDataType(), syncable::UNSPECIFIED);
112 std::string type_str = syncable::ModelTypeToString( 121 syncable::ModelType type = change.sync_data().GetDataType();
113 change.sync_data().GetDataType()); 122 std::string type_str = syncable::ModelTypeToString(type);
114 sync_api::WriteNode sync_node(&trans); 123 sync_api::WriteNode sync_node(&trans);
115 if (change.change_type() == SyncChange::ACTION_DELETE) { 124 if (change.change_type() == SyncChange::ACTION_DELETE) {
116 if (change.sync_data().GetTag() == "" || 125 if (change.sync_data().GetTag() == "" ||
117 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(), 126 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(),
118 change.sync_data().GetTag())) { 127 change.sync_data().GetTag())) {
119 NOTREACHED(); 128 NOTREACHED();
120 error_handler()->OnUnrecoverableError(FROM_HERE, 129 SyncError error(FROM_HERE,
121 "Failed to delete " + type_str + " node."); 130 "Failed to delete " + type_str + " node.",
122 return; 131 type);
132 error_handler()->OnUnrecoverableError(error.location(),
133 error.message());
134 return error;
123 } 135 }
124 sync_node.Remove(); 136 sync_node.Remove();
125 } else if (change.change_type() == SyncChange::ACTION_ADD) { 137 } else if (change.change_type() == SyncChange::ACTION_ADD) {
126 // TODO(sync): Handle other types of creation (custom parents, folders, 138 // TODO(sync): Handle other types of creation (custom parents, folders,
127 // etc.). 139 // etc.).
128 sync_api::ReadNode root_node(&trans); 140 sync_api::ReadNode root_node(&trans);
129 if (!root_node.InitByTagLookup( 141 if (!root_node.InitByTagLookup(
130 syncable::ModelTypeToRootTag(change.sync_data().GetDataType()))) { 142 syncable::ModelTypeToRootTag(change.sync_data().GetDataType()))) {
131 NOTREACHED(); 143 NOTREACHED();
132 error_handler()->OnUnrecoverableError(FROM_HERE, 144 SyncError error(FROM_HERE,
133 "Failed to look up root node for type " + type_str); 145 "Failed to look up root node for type " + type_str,
134 return; 146 type);
147 error_handler()->OnUnrecoverableError(error.location(),
148 error.message());
149 return error;
135 } 150 }
136 if (!sync_node.InitUniqueByCreation(change.sync_data().GetDataType(), 151 if (!sync_node.InitUniqueByCreation(change.sync_data().GetDataType(),
137 root_node, 152 root_node,
138 change.sync_data().GetTag())) { 153 change.sync_data().GetTag())) {
139 error_handler()->OnUnrecoverableError(FROM_HERE, 154 NOTREACHED();
140 "Failed to create " + type_str + " node."); 155 SyncError error(FROM_HERE,
141 return; 156 "Failed to create " + type_str + " node.",
157 type);
158 error_handler()->OnUnrecoverableError(error.location(),
159 error.message());
160 return error;
142 } 161 }
143 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle())); 162 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle()));
144 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics()); 163 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics());
145 } else if (change.change_type() == SyncChange::ACTION_UPDATE) { 164 } else if (change.change_type() == SyncChange::ACTION_UPDATE) {
146 if (change.sync_data().GetTag() == "" || 165 if (change.sync_data().GetTag() == "" ||
147 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(), 166 !sync_node.InitByClientTagLookup(change.sync_data().GetDataType(),
148 change.sync_data().GetTag())) { 167 change.sync_data().GetTag())) {
149 NOTREACHED(); 168 NOTREACHED();
150 error_handler()->OnUnrecoverableError(FROM_HERE, 169 SyncError error(FROM_HERE,
151 "Failed to update " + type_str + " node"); 170 "Failed to update " + type_str + " node.",
152 return; 171 type);
172 error_handler()->OnUnrecoverableError(error.location(),
173 error.message());
174 return error;
153 } 175 }
154 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle())); 176 sync_node.SetTitle(UTF8ToWide(change.sync_data().GetTitle()));
155 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics()); 177 sync_node.SetEntitySpecifics(change.sync_data().GetSpecifics());
156 // TODO(sync): Support updating other parts of the sync node (title, 178 // TODO(sync): Support updating other parts of the sync node (title,
157 // successor, parent, etc.). 179 // successor, parent, etc.).
158 } else { 180 } else {
159 NOTREACHED(); 181 NOTREACHED();
160 error_handler()->OnUnrecoverableError(FROM_HERE, 182 SyncError error(FROM_HERE,
161 "Received unset SyncChange in the change processor."); 183 "Received unset SyncChange in the change processor.",
162 return; 184 type);
185 error_handler()->OnUnrecoverableError(error.location(),
186 error.message());
187 return error;
163 } 188 }
164 } 189 }
190 return SyncError();
165 } 191 }
166 192
167 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes( 193 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(
168 syncable::ModelType type, 194 syncable::ModelType type,
169 bool* has_nodes) { 195 bool* has_nodes) {
170 DCHECK(has_nodes); 196 DCHECK(has_nodes);
171 DCHECK_NE(type, syncable::UNSPECIFIED); 197 DCHECK_NE(type, syncable::UNSPECIFIED);
172 std::string type_name = syncable::ModelTypeToString(type); 198 std::string type_name = syncable::ModelTypeToString(type);
173 std::string err_str = "Server did not create the top-level " + type_name + 199 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."; 200 " node. We might be running against an out-of-date server.";
(...skipping 23 matching lines...) Expand all
198 224
199 void GenericChangeProcessor::StartImpl(Profile* profile) {} 225 void GenericChangeProcessor::StartImpl(Profile* profile) {}
200 226
201 void GenericChangeProcessor::StopImpl() {} 227 void GenericChangeProcessor::StopImpl() {}
202 228
203 sync_api::UserShare* GenericChangeProcessor::share_handle() { 229 sync_api::UserShare* GenericChangeProcessor::share_handle() {
204 return user_share_; 230 return user_share_;
205 } 231 }
206 232
207 } // namespace browser_sync 233 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698