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

Side by Side Diff: chrome/browser/sync/engine/syncer.cc

Issue 194065: Initial commit of sync engine code to browser/sync.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fixes to gtest include path, reverted syncapi. Created 11 years, 3 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE entry.
4
5 #include "chrome/browser/sync/engine/syncer.h"
6
7 #include "base/format_macros.h"
8 #include "chrome/browser/sync/engine/apply_updates_command.h"
9 #include "chrome/browser/sync/engine/build_and_process_conflict_sets_command.h"
10 #include "chrome/browser/sync/engine/build_commit_command.h"
11 #include "chrome/browser/sync/engine/conflict_resolver.h"
12 #include "chrome/browser/sync/engine/download_updates_command.h"
13 #include "chrome/browser/sync/engine/get_commit_ids_command.h"
14 #include "chrome/browser/sync/engine/net/server_connection_manager.h"
15 #include "chrome/browser/sync/engine/post_commit_message_command.h"
16 #include "chrome/browser/sync/engine/process_commit_response_command.h"
17 #include "chrome/browser/sync/engine/process_updates_command.h"
18 #include "chrome/browser/sync/engine/resolve_conflicts_command.h"
19 #include "chrome/browser/sync/engine/syncer_end_command.h"
20 #include "chrome/browser/sync/engine/syncer_types.h"
21 #include "chrome/browser/sync/engine/syncer_util.h"
22 #include "chrome/browser/sync/engine/syncproto.h"
23 #include "chrome/browser/sync/engine/verify_updates_command.h"
24 #include "chrome/browser/sync/syncable/directory_manager.h"
25 #include "chrome/browser/sync/syncable/syncable-inl.h"
26 #include "chrome/browser/sync/syncable/syncable.h"
27 #include "chrome/browser/sync/util/character_set_converters.h"
28
29 using sync_pb::ClientCommand;
30 using syncable::Blob;
31 using syncable::IS_UNAPPLIED_UPDATE;
32 using syncable::SERVER_BOOKMARK_FAVICON;
33 using syncable::SERVER_BOOKMARK_URL;
34 using syncable::SERVER_CTIME;
35 using syncable::SERVER_IS_BOOKMARK_OBJECT;
36 using syncable::SERVER_IS_DEL;
37 using syncable::SERVER_IS_DIR;
38 using syncable::SERVER_MTIME;
39 using syncable::SERVER_NAME;
40 using syncable::SERVER_NON_UNIQUE_NAME;
41 using syncable::SERVER_PARENT_ID;
42 using syncable::SERVER_POSITION_IN_PARENT;
43 using syncable::SERVER_VERSION;
44 using syncable::SYNCER;
45 using syncable::ScopedDirLookup;
46 using syncable::WriteTransaction;
47
48 namespace browser_sync {
49
50 Syncer::Syncer(
51 syncable::DirectoryManager* dirman,
52 const PathString &account_name,
idana 2009/09/10 05:44:37 "const PathString &account_name," -> "const PathSt
53 ServerConnectionManager* connection_manager,
54 ModelSafeWorker* model_safe_worker)
55 : account_name_(account_name),
56 early_exit_requested_(false),
57 max_commit_batch_size_(kDefaultMaxCommitBatchSize),
58 connection_manager_(connection_manager),
59 dirman_(dirman),
60 silenced_until_(0),
61 command_channel_(NULL),
62 model_safe_worker_(model_safe_worker),
63 updates_source_(sync_pb::GetUpdatesCallerInfo::UNKNOWN),
64 notifications_enabled_(false),
65 pre_conflict_resolution_function_(NULL) {
66 SyncerEvent shutdown = { SyncerEvent::SHUTDOWN_USE_WITH_CARE };
67 syncer_event_channel_.reset(new SyncerEventChannel(shutdown));
68 shutdown_channel_.reset(new ShutdownChannel(this));
69
70 ScopedDirLookup dir(dirman_, account_name_);
71 // The directory must be good here.
72 CHECK(dir.good());
73 }
74
75 Syncer::~Syncer() {}
76
77 void Syncer::RequestNudge(int milliseconds) {
78 SyncerEvent event;
79 event.what_happened = SyncerEvent::REQUEST_SYNC_NUDGE;
80 event.nudge_delay_milliseconds = milliseconds;
81 channel()->NotifyListeners(event);
82 }
83
84 bool Syncer::SyncShare() {
85 SyncProcessState state(dirman_, account_name_, connection_manager_,
86 &resolver_, syncer_event_channel_.get(),
87 model_safe_worker());
88 return SyncShare(&state);
89 }
90
91 bool Syncer::SyncShare(SyncProcessState *process_state) {
idana 2009/09/10 05:44:37 "SyncProcessState *process_state" -> "SyncProcessS
92 SyncCycleState cycle_state;
93 SyncerSession session(&cycle_state, process_state);
94 session.set_source(TestAndSetUpdatesSource());
95 session.set_notifications_enabled(notifications_enabled());
96 SyncShare(&session, SYNCER_BEGIN, SYNCER_END);
97 return session.ShouldSyncAgain();
98 }
99
100 bool Syncer::SyncShare(SyncerStep first_step, SyncerStep last_step) {
101 SyncCycleState cycle_state;
102 SyncProcessState state(dirman_, account_name_, connection_manager_,
103 &resolver_, syncer_event_channel_.get(),
104 model_safe_worker());
105 SyncerSession session(&cycle_state, &state);
106 SyncShare(&session, first_step, last_step);
107 return session.ShouldSyncAgain();
108 }
109
110 void Syncer::SyncShare(SyncerSession *session) {
idana 2009/09/10 05:44:37 "SyncerSession *session" -> "SyncerSession* sessio
111 SyncShare(session, SYNCER_BEGIN, SYNCER_END);
112 }
113
114 void Syncer::SyncShare(SyncerSession *session,
115 const SyncerStep first_step,
116 const SyncerStep last_step) {
117 SyncerStep current_step = first_step;
118
119 SyncerStep next_step;
120 while (!ExitRequested()) {
121 switch (current_step) {
122 case SYNCER_BEGIN:
123 LOG(INFO) << "Syncer Begin";
124 next_step = DOWNLOAD_UPDATES;
125 break;
126 case DOWNLOAD_UPDATES: {
127 LOG(INFO) << "Downloading Updates";
128 DownloadUpdatesCommand download_updates;
129 download_updates.Execute(session);
130 next_step = PROCESS_CLIENT_COMMAND;
131 break;
132 }
133 case PROCESS_CLIENT_COMMAND: {
134 LOG(INFO) << "Processing Client Command";
135 ProcessClientCommand(session);
136 next_step = VERIFY_UPDATES;
137 break;
138 }
139 case VERIFY_UPDATES: {
140 LOG(INFO) << "Verifying Updates";
141 VerifyUpdatesCommand verify_updates;
142 verify_updates.Execute(session);
143 next_step = PROCESS_UPDATES;
144 break;
145 }
146 case PROCESS_UPDATES: {
147 LOG(INFO) << "Processing Updates";
148 ProcessUpdatesCommand process_updates;
149 process_updates.Execute(session);
150 // We should download all of the updates before attempting to process
151 // them.
152 if (session->CountUpdates() == 0) {
153 next_step = APPLY_UPDATES;
154 } else {
155 next_step = DOWNLOAD_UPDATES;
156 }
157 break;
158 }
159 case APPLY_UPDATES: {
160 LOG(INFO) << "Applying Updates";
161 ApplyUpdatesCommand apply_updates;
162 apply_updates.Execute(session);
163 next_step = BUILD_COMMIT_REQUEST;
164 break;
165 }
166 // These two steps are combined since they are executed within the same
167 // write transaction.
168 case BUILD_COMMIT_REQUEST: {
169 SyncerStatus status(session);
170 status.set_syncing(true);
171
172 LOG(INFO) << "Processing Commit Request";
173 ScopedDirLookup dir(session->dirman(), session->account_name());
174 if (!dir.good()) {
175 LOG(ERROR) << "Scoped dir lookup failed!";
176 return;
177 }
178 WriteTransaction trans(dir, SYNCER, __FILE__, __LINE__);
179 SyncerSession::ScopedSetWriteTransaction set_trans(session, &trans);
180
181 LOG(INFO) << "Getting the Commit IDs";
182 GetCommitIdsCommand get_commit_ids_command(max_commit_batch_size_);
183 get_commit_ids_command.Execute(session);
184
185 if (!session->commit_ids().empty()) {
186 LOG(INFO) << "Building a commit message";
187 BuildCommitCommand build_commit_command;
188 build_commit_command.Execute(session);
189
190 next_step = POST_COMMIT_MESSAGE;
191 } else {
192 next_step = BUILD_AND_PROCESS_CONFLICT_SETS;
193 }
194
195 break;
196 }
197 case POST_COMMIT_MESSAGE: {
198 LOG(INFO) << "Posting a commit request";
199 PostCommitMessageCommand post_commit_command;
200 post_commit_command.Execute(session);
201 next_step = PROCESS_COMMIT_RESPONSE;
202 break;
203 }
204 case PROCESS_COMMIT_RESPONSE: {
205 LOG(INFO) << "Processing the commit response";
206 ProcessCommitResponseCommand process_response_command;
207 process_response_command.Execute(session);
208 next_step = BUILD_AND_PROCESS_CONFLICT_SETS;
209 break;
210 }
211 case BUILD_AND_PROCESS_CONFLICT_SETS: {
212 LOG(INFO) << "Building and Processing Conflict Sets";
213 BuildAndProcessConflictSetsCommand build_process_conflict_sets;
214 build_process_conflict_sets.Execute(session);
215 if (session->conflict_sets_built())
216 next_step = SYNCER_END;
217 else
218 next_step = RESOLVE_CONFLICTS;
219 break;
220 }
221 case RESOLVE_CONFLICTS: {
222 LOG(INFO) << "Resolving Conflicts";
223
224 // Trigger the pre_conflict_resolution_function_, which is a testing
225 // hook for the unit tests, if it is non-NULL.
226 if (pre_conflict_resolution_function_) {
227 ScopedDirLookup dir(dirman_, account_name_);
228 if (!dir.good()) {
229 LOG(ERROR) << "Bad dir lookup in syncer loop";
230 return;
231 }
232 pre_conflict_resolution_function_(dir);
233 }
234
235 ResolveConflictsCommand resolve_conflicts_command;
236 resolve_conflicts_command.Execute(session);
237 if (session->HasConflictingUpdates())
238 next_step = APPLY_UPDATES_TO_RESOLVE_CONFLICTS;
239 else
240 next_step = SYNCER_END;
241 break;
242 }
243 case APPLY_UPDATES_TO_RESOLVE_CONFLICTS: {
244 LOG(INFO) << "Applying updates to resolve conflicts";
245 ApplyUpdatesCommand apply_updates;
246 int num_conflicting_updates = session->conflicting_update_count();
247 apply_updates.Execute(session);
248 int post_facto_conflicting_updates =
249 session->conflicting_update_count();
250 session->set_conflicts_resolved(session->conflicts_resolved() ||
251 num_conflicting_updates > post_facto_conflicting_updates);
252 if (session->conflicts_resolved())
253 next_step = RESOLVE_CONFLICTS;
254 else
255 next_step = SYNCER_END;
256 break;
257 }
258 case SYNCER_END: {
259 LOG(INFO) << "Syncer End";
260 SyncerEndCommand syncer_end_command;
261 // This will set "syncing" to false, and send out a notification.
262 syncer_end_command.Execute(session);
263 goto post_while;
264 }
265 default:
266 LOG(ERROR) << "Unknown command: " << current_step;
267 }
268 if (last_step == current_step)
269 break;
270 current_step = next_step;
271 }
272 post_while:
273 // Copy any lingering useful state out of the session.
274 silenced_until_ = session->silenced_until();
275 return;
276 }
277
278 void Syncer::ProcessClientCommand(SyncerSession *session) {
279 if (!session->update_response().has_client_command())
280 return;
281 const ClientCommand command = session->update_response().client_command();
282 if (command_channel_)
283 command_channel_->NotifyListeners(&command);
284
285 // The server limits the number of items a client can commit in one batch.
286 if (command.has_max_commit_batch_size())
287 max_commit_batch_size_ = command.max_commit_batch_size();
288 }
289
290 void CopyServerFields(syncable::Entry* src, syncable::MutableEntry* dest) {
291 dest->Put(SERVER_NAME, src->Get(SERVER_NAME));
292 dest->Put(SERVER_NON_UNIQUE_NAME, src->Get(SERVER_NON_UNIQUE_NAME));
293 dest->Put(SERVER_PARENT_ID, src->Get(SERVER_PARENT_ID));
294 dest->Put(SERVER_MTIME, src->Get(SERVER_MTIME));
295 dest->Put(SERVER_CTIME, src->Get(SERVER_CTIME));
296 dest->Put(SERVER_VERSION, src->Get(SERVER_VERSION));
297 dest->Put(SERVER_IS_DIR, src->Get(SERVER_IS_DIR));
298 dest->Put(SERVER_IS_DEL, src->Get(SERVER_IS_DEL));
299 dest->Put(SERVER_IS_BOOKMARK_OBJECT, src->Get(SERVER_IS_BOOKMARK_OBJECT));
300 dest->Put(IS_UNAPPLIED_UPDATE, src->Get(IS_UNAPPLIED_UPDATE));
301 dest->Put(SERVER_BOOKMARK_URL, src->Get(SERVER_BOOKMARK_URL));
302 dest->Put(SERVER_BOOKMARK_FAVICON, src->Get(SERVER_BOOKMARK_FAVICON));
303 dest->Put(SERVER_POSITION_IN_PARENT, src->Get(SERVER_POSITION_IN_PARENT));
304 }
305
306 void ClearServerData(syncable::MutableEntry* entry) {
307 entry->Put(SERVER_NAME, PSTR(""));
308 entry->Put(SERVER_NON_UNIQUE_NAME, PSTR(""));
309 entry->Put(SERVER_PARENT_ID, syncable::kNullId);
310 entry->Put(SERVER_MTIME, 0);
311 entry->Put(SERVER_CTIME, 0);
312 entry->Put(SERVER_VERSION, 0);
313 entry->Put(SERVER_IS_DIR, false);
314 entry->Put(SERVER_IS_DEL, false);
315 entry->Put(SERVER_IS_BOOKMARK_OBJECT, false);
316 entry->Put(IS_UNAPPLIED_UPDATE, false);
317 entry->Put(SERVER_BOOKMARK_URL, PSTR(""));
318 entry->Put(SERVER_BOOKMARK_FAVICON, Blob());
319 entry->Put(SERVER_POSITION_IN_PARENT, 0);
320 }
321
322 std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry) {
323 return StringPrintf("id: %s, parent_id: %s, "
324 "version: %"PRId64"d, "
325 "mtime: %" PRId64"d (client: %" PRId64"d), "
326 "ctime: %" PRId64"d (client: %" PRId64"d), "
327 "name: %s, sync_timestamp: %" PRId64"d, "
328 "%s ",
329 entry.id_string().c_str(),
330 entry.parent_id_string().c_str(),
331 entry.version(),
332 entry.mtime(), ServerTimeToClientTime(entry.mtime()),
333 entry.ctime(), ServerTimeToClientTime(entry.ctime()),
334 entry.name().c_str(), entry.sync_timestamp(),
335 entry.deleted() ? "deleted, ":"");
336 }
337
338 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698