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

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

Issue 17052007: sync: Expose sync functionality as functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix more integration tests Created 7 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
« no previous file with comments | « sync/engine/syncer.h ('k') | sync/engine/syncer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "sync/engine/syncer.h" 5 #include "sync/engine/syncer.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/time.h" 11 #include "base/time.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "sync/engine/apply_control_data_updates.h" 13 #include "sync/engine/apply_control_data_updates.h"
14 #include "sync/engine/apply_updates_and_resolve_conflicts_command.h" 14 #include "sync/engine/apply_updates_and_resolve_conflicts_command.h"
15 #include "sync/engine/build_commit_command.h" 15 #include "sync/engine/build_commit_command.h"
16 #include "sync/engine/commit.h" 16 #include "sync/engine/commit.h"
17 #include "sync/engine/conflict_resolver.h" 17 #include "sync/engine/conflict_resolver.h"
18 #include "sync/engine/download_updates_command.h" 18 #include "sync/engine/download.h"
19 #include "sync/engine/net/server_connection_manager.h" 19 #include "sync/engine/net/server_connection_manager.h"
20 #include "sync/engine/process_commit_response_command.h" 20 #include "sync/engine/process_commit_response_command.h"
21 #include "sync/engine/process_updates_command.h"
22 #include "sync/engine/store_timestamps_command.h"
23 #include "sync/engine/syncer_types.h" 21 #include "sync/engine/syncer_types.h"
24 #include "sync/internal_api/public/base/unique_position.h" 22 #include "sync/internal_api/public/base/unique_position.h"
23 #include "sync/internal_api/public/util/syncer_error.h"
24 #include "sync/sessions/nudge_tracker.h"
25 #include "sync/syncable/mutable_entry.h" 25 #include "sync/syncable/mutable_entry.h"
26 #include "sync/syncable/syncable-inl.h" 26 #include "sync/syncable/syncable-inl.h"
27 27
28 using base::Time; 28 using base::Time;
29 using base::TimeDelta; 29 using base::TimeDelta;
30 using sync_pb::ClientCommand; 30 using sync_pb::ClientCommand;
31 31
32 namespace syncer { 32 namespace syncer {
33 33
34 // TODO(akalin): We may want to propagate this switch up
35 // eventually.
36 #if defined(OS_ANDROID) || defined(OS_IOS)
37 static const bool kCreateMobileBookmarksFolder = true;
38 #else
39 static const bool kCreateMobileBookmarksFolder = false;
40 #endif
41
34 using sessions::StatusController; 42 using sessions::StatusController;
35 using sessions::SyncSession; 43 using sessions::SyncSession;
36 using syncable::IS_UNAPPLIED_UPDATE; 44 using sessions::NudgeTracker;
37 using syncable::SERVER_CTIME;
38 using syncable::SERVER_IS_DEL;
39 using syncable::SERVER_IS_DIR;
40 using syncable::SERVER_MTIME;
41 using syncable::SERVER_NON_UNIQUE_NAME;
42 using syncable::SERVER_PARENT_ID;
43 using syncable::SERVER_SPECIFICS;
44 using syncable::SERVER_UNIQUE_POSITION;
45 using syncable::SERVER_VERSION;
46
47 #define ENUM_CASE(x) case x: return #x
48 const char* SyncerStepToString(const SyncerStep step)
49 {
50 switch (step) {
51 ENUM_CASE(SYNCER_BEGIN);
52 ENUM_CASE(DOWNLOAD_UPDATES);
53 ENUM_CASE(PROCESS_UPDATES);
54 ENUM_CASE(STORE_TIMESTAMPS);
55 ENUM_CASE(APPLY_UPDATES);
56 ENUM_CASE(COMMIT);
57 ENUM_CASE(SYNCER_END);
58 }
59 NOTREACHED();
60 return "";
61 }
62 #undef ENUM_CASE
63 45
64 Syncer::Syncer() 46 Syncer::Syncer()
65 : early_exit_requested_(false) { 47 : early_exit_requested_(false) {
66 } 48 }
67 49
68 Syncer::~Syncer() {} 50 Syncer::~Syncer() {}
69 51
70 bool Syncer::ExitRequested() { 52 bool Syncer::ExitRequested() {
71 base::AutoLock lock(early_exit_requested_lock_); 53 base::AutoLock lock(early_exit_requested_lock_);
72 return early_exit_requested_; 54 return early_exit_requested_;
73 } 55 }
74 56
75 void Syncer::RequestEarlyExit() { 57 void Syncer::RequestEarlyExit() {
76 base::AutoLock lock(early_exit_requested_lock_); 58 base::AutoLock lock(early_exit_requested_lock_);
77 early_exit_requested_ = true; 59 early_exit_requested_ = true;
78 } 60 }
79 61
80 bool Syncer::SyncShare(sessions::SyncSession* session, 62 bool Syncer::NormalSyncShare(SyncSession* session,
Nicolas Zea 2013/06/25 23:47:34 nit: session should be the last param (inputs befo
rlarocque 2013/06/26 00:56:08 Done.
81 SyncerStep first_step, 63 ModelTypeSet request_types,
82 SyncerStep last_step) { 64 const NudgeTracker& nudge_tracker) {
83 session->mutable_status_controller()->UpdateStartTime(); 65 session->mutable_status_controller()->UpdateStartTime();
84 SyncerStep current_step = first_step; 66 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_BEGIN);
85 67
86 SyncerStep next_step = current_step; 68 VLOG(1) << "Downloading types " << ModelTypeSetToString(request_types);
87 while (!ExitRequested()) { 69 while (!session->status_controller().ServerSaysNothingMoreToDownload()) {
88 TRACE_EVENT1("sync", "SyncerStateMachine", 70 SyncerError download_result = syncer::NormalDownloadUpdates(
89 "state", SyncerStepToString(current_step)); 71 session,
90 DVLOG(1) << "Syncer step:" << SyncerStepToString(current_step); 72 kCreateMobileBookmarksFolder,
73 request_types,
74 nudge_tracker);
91 75
92 switch (current_step) { 76 session->mutable_status_controller()->set_last_download_updates_result(
93 case SYNCER_BEGIN: 77 download_result);
94 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_BEGIN); 78 if (download_result != SYNCER_OK) {
79 if (!ExitRequested()) {
Nicolas Zea 2013/06/25 23:47:34 Given that this logic is done 6 times in this file
rlarocque 2013/06/26 00:56:08 Done.
80 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
81 return true;
82 } else {
83 return false;
84 }
85 }
86 }
87 if (ExitRequested())
88 return false;
95 89
96 next_step = DOWNLOAD_UPDATES; 90 ApplyUpdates(session);
97 break;
98 case DOWNLOAD_UPDATES: {
99 // TODO(akalin): We may want to propagate this switch up
100 // eventually.
101 #if defined(OS_ANDROID) || defined(OS_IOS)
102 const bool kCreateMobileBookmarksFolder = true;
103 #else
104 const bool kCreateMobileBookmarksFolder = false;
105 #endif
106 DownloadUpdatesCommand download_updates(kCreateMobileBookmarksFolder);
107 session->mutable_status_controller()->set_last_download_updates_result(
108 download_updates.Execute(session));
109 next_step = PROCESS_UPDATES;
110 break;
111 }
112 case PROCESS_UPDATES: {
113 ProcessUpdatesCommand process_updates;
114 process_updates.Execute(session);
115 next_step = STORE_TIMESTAMPS;
116 break;
117 }
118 case STORE_TIMESTAMPS: {
119 StoreTimestampsCommand store_timestamps;
120 store_timestamps.Execute(session);
121 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED);
122 // We download all of the updates before attempting to apply them.
123 if (!session->status_controller().download_updates_succeeded()) {
124 // We may have downloaded some updates, but if the latest download
125 // attempt failed then we don't have all the updates. We'll leave
126 // it to a retry job to pick up where we left off.
127 last_step = SYNCER_END; // Necessary for CONFIGURATION mode.
128 next_step = SYNCER_END;
129 DVLOG(1) << "Aborting sync cycle due to download updates failure";
130 } else if (!session->status_controller()
131 .ServerSaysNothingMoreToDownload()) {
132 next_step = DOWNLOAD_UPDATES;
133 } else {
134 next_step = APPLY_UPDATES;
135 }
136 break;
137 }
138 case APPLY_UPDATES: {
139 // These include encryption updates that should be applied early.
140 ApplyControlDataUpdates(session);
141 91
142 ApplyUpdatesAndResolveConflictsCommand apply_updates; 92 if (ExitRequested())
143 apply_updates.Execute(session); 93 return false;
144 94
145 session->context()->set_hierarchy_conflict_detected( 95 VLOG(1) << "Committing from types " << ModelTypeSetToString(request_types);
146 session->status_controller().num_hierarchy_conflicts() > 0); 96 SyncerError commit_result = BuildAndPostCommits(request_types, this, session);
97 session->mutable_status_controller()->set_commit_result(commit_result);
147 98
148 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED); 99 if (ExitRequested()) {
149 if (last_step == APPLY_UPDATES) { 100 return false;
150 // We're in configuration mode, but we still need to run the 101 } else {
151 // SYNCER_END step. 102 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
152 last_step = SYNCER_END; 103 return true;
153 next_step = SYNCER_END; 104 }
154 } else {
155 next_step = COMMIT;
156 }
157 break;
158 }
159 case COMMIT: {
160 session->mutable_status_controller()->set_commit_result(
161 BuildAndPostCommits(this, session));
162 next_step = SYNCER_END;
163 break;
164 }
165 case SYNCER_END: {
166 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
167 next_step = SYNCER_END;
168 break;
169 }
170 default:
171 LOG(ERROR) << "Unknown command: " << current_step;
172 } // switch
173 DVLOG(2) << "last step: " << SyncerStepToString(last_step) << ", "
174 << "current step: " << SyncerStepToString(current_step) << ", "
175 << "next step: " << SyncerStepToString(next_step) << ", "
176 << "snapshot: " << session->TakeSnapshot().ToString();
177 if (last_step == current_step)
178 return true;
179 current_step = next_step;
180 } // while
181 return false;
182 } 105 }
183 106
184 void CopyServerFields(syncable::Entry* src, syncable::MutableEntry* dest) { 107 bool Syncer::ConfigureSyncShare(SyncSession* session,
185 dest->Put(SERVER_NON_UNIQUE_NAME, src->Get(SERVER_NON_UNIQUE_NAME)); 108 ModelTypeSet request_types) {
186 dest->Put(SERVER_PARENT_ID, src->Get(SERVER_PARENT_ID)); 109 session->mutable_status_controller()->UpdateStartTime();
187 dest->Put(SERVER_MTIME, src->Get(SERVER_MTIME)); 110 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_BEGIN);
188 dest->Put(SERVER_CTIME, src->Get(SERVER_CTIME)); 111
189 dest->Put(SERVER_VERSION, src->Get(SERVER_VERSION)); 112 while (!session->status_controller().ServerSaysNothingMoreToDownload()) {
190 dest->Put(SERVER_IS_DIR, src->Get(SERVER_IS_DIR)); 113 SyncerError download_result = ConfigureDownloadUpdates(
191 dest->Put(SERVER_IS_DEL, src->Get(SERVER_IS_DEL)); 114 session,
192 dest->Put(IS_UNAPPLIED_UPDATE, src->Get(IS_UNAPPLIED_UPDATE)); 115 kCreateMobileBookmarksFolder,
193 dest->Put(SERVER_SPECIFICS, src->Get(SERVER_SPECIFICS)); 116 session->source(),
194 dest->Put(SERVER_UNIQUE_POSITION, src->Get(SERVER_UNIQUE_POSITION)); 117 request_types);
118 session->mutable_status_controller()->set_last_download_updates_result(
119 download_result);
120 if (download_result != SYNCER_OK) {
121 if (!ExitRequested()) {
122 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
123 return true;
124 } else {
125 return false;
126 }
127 }
128 }
129 if (ExitRequested())
130 return false;
131
132 ApplyUpdates(session);
133
134 if (ExitRequested()) {
135 return false;
136 } else {
137 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
138 return true;
139 }
140 }
141
142 bool Syncer::PollSyncShare(SyncSession* session,
143 ModelTypeSet request_types) {
144 session->mutable_status_controller()->UpdateStartTime();
145 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_BEGIN);
146
147 while (!session->status_controller().ServerSaysNothingMoreToDownload()) {
148 SyncerError download_result = PollDownloadUpdates(
149 session,
150 kCreateMobileBookmarksFolder,
151 request_types);
152 session->mutable_status_controller()->set_last_download_updates_result(
153 download_result);
154 if (download_result != SYNCER_OK) {
155 if (!ExitRequested()) {
156 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
157 return true;
158 } else {
159 return false;
160 }
161 }
162 }
163 if (ExitRequested())
164 return false;
165
166 ApplyUpdates(session);
167
168 if (ExitRequested()) {
169 return false;
170 } else {
171 session->SendEventNotification(SyncEngineEvent::SYNC_CYCLE_ENDED);
172 return true;
173 }
174 }
175
176 void Syncer::ApplyUpdates(SyncSession* session) {
177 TRACE_EVENT0("sync", "ApplyUpdates");
178
179 ApplyControlDataUpdates(session);
180
181 ApplyUpdatesAndResolveConflictsCommand apply_updates;
182 apply_updates.Execute(session);
183
184 session->context()->set_hierarchy_conflict_detected(
185 session->status_controller().num_hierarchy_conflicts() > 0);
186
187 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED);
195 } 188 }
196 189
197 } // namespace syncer 190 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/engine/syncer.h ('k') | sync/engine/syncer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698