OLD | NEW |
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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/time/time.h" | 11 #include "base/time/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/commit.h" | 14 #include "sync/engine/commit.h" |
| 15 #include "sync/engine/commit_processor.h" |
15 #include "sync/engine/conflict_resolver.h" | 16 #include "sync/engine/conflict_resolver.h" |
16 #include "sync/engine/download.h" | 17 #include "sync/engine/download.h" |
| 18 #include "sync/engine/get_updates_processor.h" |
17 #include "sync/engine/net/server_connection_manager.h" | 19 #include "sync/engine/net/server_connection_manager.h" |
18 #include "sync/engine/syncer_types.h" | 20 #include "sync/engine/syncer_types.h" |
19 #include "sync/internal_api/public/base/cancelation_signal.h" | 21 #include "sync/internal_api/public/base/cancelation_signal.h" |
20 #include "sync/internal_api/public/base/unique_position.h" | 22 #include "sync/internal_api/public/base/unique_position.h" |
21 #include "sync/internal_api/public/util/syncer_error.h" | 23 #include "sync/internal_api/public/util/syncer_error.h" |
22 #include "sync/sessions/nudge_tracker.h" | 24 #include "sync/sessions/nudge_tracker.h" |
23 #include "sync/syncable/directory.h" | 25 #include "sync/syncable/directory.h" |
24 #include "sync/syncable/mutable_entry.h" | 26 #include "sync/syncable/mutable_entry.h" |
25 #include "sync/syncable/syncable-inl.h" | 27 #include "sync/syncable/syncable-inl.h" |
26 | 28 |
(...skipping 22 matching lines...) Expand all Loading... |
49 Syncer::~Syncer() {} | 51 Syncer::~Syncer() {} |
50 | 52 |
51 bool Syncer::ExitRequested() { | 53 bool Syncer::ExitRequested() { |
52 return cancelation_signal_->IsSignalled(); | 54 return cancelation_signal_->IsSignalled(); |
53 } | 55 } |
54 | 56 |
55 bool Syncer::NormalSyncShare(ModelTypeSet request_types, | 57 bool Syncer::NormalSyncShare(ModelTypeSet request_types, |
56 const NudgeTracker& nudge_tracker, | 58 const NudgeTracker& nudge_tracker, |
57 SyncSession* session) { | 59 SyncSession* session) { |
58 HandleCycleBegin(session); | 60 HandleCycleBegin(session); |
| 61 GetUpdatesProcessor get_updates_processor( |
| 62 session->context()->model_type_registry()->update_handler_map()); |
59 VLOG(1) << "Downloading types " << ModelTypeSetToString(request_types); | 63 VLOG(1) << "Downloading types " << ModelTypeSetToString(request_types); |
60 if (nudge_tracker.IsGetUpdatesRequired(base::TimeTicks::Now()) || | 64 if (nudge_tracker.IsGetUpdatesRequired(base::TimeTicks::Now()) || |
61 session->context()->ShouldFetchUpdatesBeforeCommit()) { | 65 session->context()->ShouldFetchUpdatesBeforeCommit()) { |
62 if (!DownloadAndApplyUpdates( | 66 if (!DownloadAndApplyUpdates( |
63 request_types, | 67 request_types, |
64 session, | 68 session, |
| 69 &get_updates_processor, |
65 base::Bind(&download::BuildNormalDownloadUpdates, | 70 base::Bind(&download::BuildNormalDownloadUpdates, |
66 session, | 71 session, |
| 72 &get_updates_processor, |
67 kCreateMobileBookmarksFolder, | 73 kCreateMobileBookmarksFolder, |
68 request_types, | 74 request_types, |
69 base::ConstRef(nudge_tracker)))) { | 75 base::ConstRef(nudge_tracker)))) { |
70 return HandleCycleEnd(session, nudge_tracker.updates_source()); | 76 return HandleCycleEnd(session, nudge_tracker.updates_source()); |
71 } | 77 } |
72 } | 78 } |
73 | 79 |
74 VLOG(1) << "Committing from types " << ModelTypeSetToString(request_types); | 80 VLOG(1) << "Committing from types " << ModelTypeSetToString(request_types); |
75 SyncerError commit_result = BuildAndPostCommits(request_types, session); | 81 CommitProcessor commit_processor( |
| 82 session->context()->model_type_registry()->commit_contributor_map()); |
| 83 SyncerError commit_result = |
| 84 BuildAndPostCommits(request_types, session, &commit_processor); |
76 session->mutable_status_controller()->set_commit_result(commit_result); | 85 session->mutable_status_controller()->set_commit_result(commit_result); |
77 | 86 |
78 return HandleCycleEnd(session, nudge_tracker.updates_source()); | 87 return HandleCycleEnd(session, nudge_tracker.updates_source()); |
79 } | 88 } |
80 | 89 |
81 bool Syncer::ConfigureSyncShare( | 90 bool Syncer::ConfigureSyncShare( |
82 ModelTypeSet request_types, | 91 ModelTypeSet request_types, |
83 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source, | 92 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source, |
84 SyncSession* session) { | 93 SyncSession* session) { |
85 HandleCycleBegin(session); | 94 HandleCycleBegin(session); |
| 95 GetUpdatesProcessor get_updates_processor( |
| 96 session->context()->model_type_registry()->update_handler_map()); |
86 VLOG(1) << "Configuring types " << ModelTypeSetToString(request_types); | 97 VLOG(1) << "Configuring types " << ModelTypeSetToString(request_types); |
87 DownloadAndApplyUpdates( | 98 DownloadAndApplyUpdates( |
88 request_types, | 99 request_types, |
89 session, | 100 session, |
| 101 &get_updates_processor, |
90 base::Bind(&download::BuildDownloadUpdatesForConfigure, | 102 base::Bind(&download::BuildDownloadUpdatesForConfigure, |
91 session, | 103 session, |
| 104 &get_updates_processor, |
92 kCreateMobileBookmarksFolder, | 105 kCreateMobileBookmarksFolder, |
93 source, | 106 source, |
94 request_types)); | 107 request_types)); |
95 return HandleCycleEnd(session, source); | 108 return HandleCycleEnd(session, source); |
96 } | 109 } |
97 | 110 |
98 bool Syncer::PollSyncShare(ModelTypeSet request_types, | 111 bool Syncer::PollSyncShare(ModelTypeSet request_types, |
99 SyncSession* session) { | 112 SyncSession* session) { |
100 HandleCycleBegin(session); | 113 HandleCycleBegin(session); |
| 114 GetUpdatesProcessor get_updates_processor( |
| 115 session->context()->model_type_registry()->update_handler_map()); |
101 VLOG(1) << "Polling types " << ModelTypeSetToString(request_types); | 116 VLOG(1) << "Polling types " << ModelTypeSetToString(request_types); |
102 DownloadAndApplyUpdates( | 117 DownloadAndApplyUpdates( |
103 request_types, | 118 request_types, |
104 session, | 119 session, |
| 120 &get_updates_processor, |
105 base::Bind(&download::BuildDownloadUpdatesForPoll, | 121 base::Bind(&download::BuildDownloadUpdatesForPoll, |
106 session, | 122 session, |
| 123 &get_updates_processor, |
107 kCreateMobileBookmarksFolder, | 124 kCreateMobileBookmarksFolder, |
108 request_types)); | 125 request_types)); |
109 return HandleCycleEnd(session, sync_pb::GetUpdatesCallerInfo::PERIODIC); | 126 return HandleCycleEnd(session, sync_pb::GetUpdatesCallerInfo::PERIODIC); |
110 } | 127 } |
111 | 128 |
112 bool Syncer::RetrySyncShare(ModelTypeSet request_types, | 129 bool Syncer::RetrySyncShare(ModelTypeSet request_types, |
113 SyncSession* session) { | 130 SyncSession* session) { |
114 HandleCycleBegin(session); | 131 HandleCycleBegin(session); |
| 132 GetUpdatesProcessor get_updates_processor( |
| 133 session->context()->model_type_registry()->update_handler_map()); |
115 VLOG(1) << "Retrying types " << ModelTypeSetToString(request_types); | 134 VLOG(1) << "Retrying types " << ModelTypeSetToString(request_types); |
116 DownloadAndApplyUpdates( | 135 DownloadAndApplyUpdates( |
117 request_types, | 136 request_types, |
118 session, | 137 session, |
| 138 &get_updates_processor, |
119 base::Bind(&download::BuildDownloadUpdatesForRetry, | 139 base::Bind(&download::BuildDownloadUpdatesForRetry, |
120 session, | 140 session, |
| 141 &get_updates_processor, |
121 kCreateMobileBookmarksFolder, | 142 kCreateMobileBookmarksFolder, |
122 request_types)); | 143 request_types)); |
123 return HandleCycleEnd(session, sync_pb::GetUpdatesCallerInfo::RETRY); | 144 return HandleCycleEnd(session, sync_pb::GetUpdatesCallerInfo::RETRY); |
124 } | 145 } |
125 | 146 |
126 void Syncer::ApplyUpdates(SyncSession* session) { | 147 void Syncer::ApplyUpdates(SyncSession* session, |
| 148 GetUpdatesProcessor* get_updates_processor) { |
127 TRACE_EVENT0("sync", "ApplyUpdates"); | 149 TRACE_EVENT0("sync", "ApplyUpdates"); |
128 | 150 |
129 ApplyControlDataUpdates(session->context()->directory()); | 151 ApplyControlDataUpdates(session->context()->directory()); |
130 | 152 |
131 UpdateHandlerMap* handler_map = session->context()->update_handler_map(); | 153 get_updates_processor->ApplyUpdatesForAllTypes( |
132 for (UpdateHandlerMap::iterator it = handler_map->begin(); | 154 session->mutable_status_controller()); |
133 it != handler_map->end(); ++it) { | |
134 it->second->ApplyUpdates(session->mutable_status_controller()); | |
135 } | |
136 | 155 |
137 session->context()->set_hierarchy_conflict_detected( | 156 session->context()->set_hierarchy_conflict_detected( |
138 session->status_controller().num_hierarchy_conflicts() > 0); | 157 session->status_controller().num_hierarchy_conflicts() > 0); |
139 | 158 |
140 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED); | 159 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED); |
141 } | 160 } |
142 | 161 |
143 bool Syncer::DownloadAndApplyUpdates( | 162 bool Syncer::DownloadAndApplyUpdates( |
144 ModelTypeSet request_types, | 163 ModelTypeSet request_types, |
145 SyncSession* session, | 164 SyncSession* session, |
| 165 GetUpdatesProcessor* get_updates_processor, |
146 base::Callback<void(sync_pb::ClientToServerMessage*)> build_fn) { | 166 base::Callback<void(sync_pb::ClientToServerMessage*)> build_fn) { |
147 SyncerError download_result = UNSET; | 167 SyncerError download_result = UNSET; |
148 do { | 168 do { |
149 TRACE_EVENT0("sync", "DownloadUpdates"); | 169 TRACE_EVENT0("sync", "DownloadUpdates"); |
150 sync_pb::ClientToServerMessage msg; | 170 sync_pb::ClientToServerMessage msg; |
151 build_fn.Run(&msg); | 171 build_fn.Run(&msg); |
152 download_result = | 172 download_result = download::ExecuteDownloadUpdates(request_types, |
153 download::ExecuteDownloadUpdates(request_types, session, &msg); | 173 session, |
| 174 get_updates_processor, |
| 175 &msg); |
154 session->mutable_status_controller()->set_last_download_updates_result( | 176 session->mutable_status_controller()->set_last_download_updates_result( |
155 download_result); | 177 download_result); |
156 } while (download_result == SERVER_MORE_TO_DOWNLOAD); | 178 } while (download_result == SERVER_MORE_TO_DOWNLOAD); |
157 | 179 |
158 // Exit without applying if we're shutting down or an error was detected. | 180 // Exit without applying if we're shutting down or an error was detected. |
159 if (download_result != SYNCER_OK) | 181 if (download_result != SYNCER_OK) |
160 return false; | 182 return false; |
161 if (ExitRequested()) | 183 if (ExitRequested()) |
162 return false; | 184 return false; |
163 | 185 |
164 ApplyUpdates(session); | 186 ApplyUpdates(session, get_updates_processor); |
165 if (ExitRequested()) | 187 if (ExitRequested()) |
166 return false; | 188 return false; |
167 return true; | 189 return true; |
168 } | 190 } |
169 | 191 |
170 SyncerError Syncer::BuildAndPostCommits(ModelTypeSet requested_types, | 192 SyncerError Syncer::BuildAndPostCommits(ModelTypeSet requested_types, |
171 sessions::SyncSession* session) { | 193 sessions::SyncSession* session, |
| 194 CommitProcessor* commit_processor) { |
172 // The ExitRequested() check is unnecessary, since we should start getting | 195 // The ExitRequested() check is unnecessary, since we should start getting |
173 // errors from the ServerConnectionManager if an exist has been requested. | 196 // errors from the ServerConnectionManager if an exist has been requested. |
174 // However, it doesn't hurt to check it anyway. | 197 // However, it doesn't hurt to check it anyway. |
175 while (!ExitRequested()) { | 198 while (!ExitRequested()) { |
176 scoped_ptr<Commit> commit( | 199 scoped_ptr<Commit> commit( |
177 Commit::Init( | 200 Commit::Init( |
178 requested_types, | 201 requested_types, |
| 202 session->context()->enabled_types(), |
179 session->context()->max_commit_batch_size(), | 203 session->context()->max_commit_batch_size(), |
180 session->context()->account_name(), | 204 session->context()->account_name(), |
181 session->context()->directory()->cache_guid(), | 205 session->context()->directory()->cache_guid(), |
182 session->context()->commit_contributor_map(), | 206 commit_processor, |
183 session->context()->extensions_activity())); | 207 session->context()->extensions_activity())); |
184 if (!commit) { | 208 if (!commit) { |
185 break; | 209 break; |
186 } | 210 } |
187 | 211 |
188 SyncerError error = commit->PostAndProcessResponse( | 212 SyncerError error = commit->PostAndProcessResponse( |
189 session, | 213 session, |
190 session->mutable_status_controller(), | 214 session->mutable_status_controller(), |
191 session->context()->extensions_activity()); | 215 session->context()->extensions_activity()); |
192 commit->CleanUp(); | 216 commit->CleanUp(); |
(...skipping 15 matching lines...) Expand all Loading... |
208 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) { | 232 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) { |
209 if (!ExitRequested()) { | 233 if (!ExitRequested()) { |
210 session->SendSyncCycleEndEventNotification(source); | 234 session->SendSyncCycleEndEventNotification(source); |
211 return true; | 235 return true; |
212 } else { | 236 } else { |
213 return false; | 237 return false; |
214 } | 238 } |
215 } | 239 } |
216 | 240 |
217 } // namespace syncer | 241 } // namespace syncer |
OLD | NEW |