| 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() || | 64 if (nudge_tracker.IsGetUpdatesRequired() || |
| 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 void Syncer::ApplyUpdates(SyncSession* session) { | 129 void Syncer::ApplyUpdates(SyncSession* session, |
| 130 GetUpdatesProcessor* get_updates_processor) { |
| 113 TRACE_EVENT0("sync", "ApplyUpdates"); | 131 TRACE_EVENT0("sync", "ApplyUpdates"); |
| 114 | 132 |
| 115 ApplyControlDataUpdates(session->context()->directory()); | 133 ApplyControlDataUpdates(session->context()->directory()); |
| 116 | 134 |
| 117 UpdateHandlerMap* handler_map = session->context()->update_handler_map(); | 135 get_updates_processor->ApplyUpdatesForAllTypes( |
| 118 for (UpdateHandlerMap::iterator it = handler_map->begin(); | 136 session->mutable_status_controller()); |
| 119 it != handler_map->end(); ++it) { | |
| 120 it->second->ApplyUpdates(session->mutable_status_controller()); | |
| 121 } | |
| 122 | 137 |
| 123 session->context()->set_hierarchy_conflict_detected( | 138 session->context()->set_hierarchy_conflict_detected( |
| 124 session->status_controller().num_hierarchy_conflicts() > 0); | 139 session->status_controller().num_hierarchy_conflicts() > 0); |
| 125 | 140 |
| 126 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED); | 141 session->SendEventNotification(SyncEngineEvent::STATUS_CHANGED); |
| 127 } | 142 } |
| 128 | 143 |
| 129 bool Syncer::DownloadAndApplyUpdates( | 144 bool Syncer::DownloadAndApplyUpdates( |
| 130 ModelTypeSet request_types, | 145 ModelTypeSet request_types, |
| 131 SyncSession* session, | 146 SyncSession* session, |
| 147 GetUpdatesProcessor* get_updates_processor, |
| 132 base::Callback<void(sync_pb::ClientToServerMessage*)> build_fn) { | 148 base::Callback<void(sync_pb::ClientToServerMessage*)> build_fn) { |
| 133 SyncerError download_result = UNSET; | 149 SyncerError download_result = UNSET; |
| 134 do { | 150 do { |
| 135 TRACE_EVENT0("sync", "DownloadUpdates"); | 151 TRACE_EVENT0("sync", "DownloadUpdates"); |
| 136 sync_pb::ClientToServerMessage msg; | 152 sync_pb::ClientToServerMessage msg; |
| 137 build_fn.Run(&msg); | 153 build_fn.Run(&msg); |
| 138 download_result = | 154 download_result = download::ExecuteDownloadUpdates(request_types, |
| 139 download::ExecuteDownloadUpdates(request_types, session, &msg); | 155 session, |
| 156 get_updates_processor, |
| 157 &msg); |
| 140 session->mutable_status_controller()->set_last_download_updates_result( | 158 session->mutable_status_controller()->set_last_download_updates_result( |
| 141 download_result); | 159 download_result); |
| 142 } while (download_result == SERVER_MORE_TO_DOWNLOAD); | 160 } while (download_result == SERVER_MORE_TO_DOWNLOAD); |
| 143 | 161 |
| 144 // Exit without applying if we're shutting down or an error was detected. | 162 // Exit without applying if we're shutting down or an error was detected. |
| 145 if (download_result != SYNCER_OK) | 163 if (download_result != SYNCER_OK) |
| 146 return false; | 164 return false; |
| 147 if (ExitRequested()) | 165 if (ExitRequested()) |
| 148 return false; | 166 return false; |
| 149 | 167 |
| 150 ApplyUpdates(session); | 168 ApplyUpdates(session, get_updates_processor); |
| 151 if (ExitRequested()) | 169 if (ExitRequested()) |
| 152 return false; | 170 return false; |
| 153 return true; | 171 return true; |
| 154 } | 172 } |
| 155 | 173 |
| 156 SyncerError Syncer::BuildAndPostCommits(ModelTypeSet requested_types, | 174 SyncerError Syncer::BuildAndPostCommits(ModelTypeSet requested_types, |
| 157 sessions::SyncSession* session) { | 175 sessions::SyncSession* session, |
| 176 CommitProcessor* commit_processor) { |
| 158 // The ExitRequested() check is unnecessary, since we should start getting | 177 // The ExitRequested() check is unnecessary, since we should start getting |
| 159 // errors from the ServerConnectionManager if an exist has been requested. | 178 // errors from the ServerConnectionManager if an exist has been requested. |
| 160 // However, it doesn't hurt to check it anyway. | 179 // However, it doesn't hurt to check it anyway. |
| 161 while (!ExitRequested()) { | 180 while (!ExitRequested()) { |
| 162 scoped_ptr<Commit> commit( | 181 scoped_ptr<Commit> commit( |
| 163 Commit::Init( | 182 Commit::Init( |
| 164 requested_types, | 183 requested_types, |
| 184 session->context()->enabled_types(), |
| 165 session->context()->max_commit_batch_size(), | 185 session->context()->max_commit_batch_size(), |
| 166 session->context()->account_name(), | 186 session->context()->account_name(), |
| 167 session->context()->directory()->cache_guid(), | 187 session->context()->directory()->cache_guid(), |
| 168 session->context()->commit_contributor_map(), | 188 commit_processor, |
| 169 session->context()->extensions_activity())); | 189 session->context()->extensions_activity())); |
| 170 if (!commit) { | 190 if (!commit) { |
| 171 break; | 191 break; |
| 172 } | 192 } |
| 173 | 193 |
| 174 SyncerError error = commit->PostAndProcessResponse( | 194 SyncerError error = commit->PostAndProcessResponse( |
| 175 session, | 195 session, |
| 176 session->mutable_status_controller(), | 196 session->mutable_status_controller(), |
| 177 session->context()->extensions_activity()); | 197 session->context()->extensions_activity()); |
| 178 commit->CleanUp(); | 198 commit->CleanUp(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 194 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) { | 214 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) { |
| 195 if (!ExitRequested()) { | 215 if (!ExitRequested()) { |
| 196 session->SendSyncCycleEndEventNotification(source); | 216 session->SendSyncCycleEndEventNotification(source); |
| 197 return true; | 217 return true; |
| 198 } else { | 218 } else { |
| 199 return false; | 219 return false; |
| 200 } | 220 } |
| 201 } | 221 } |
| 202 | 222 |
| 203 } // namespace syncer | 223 } // namespace syncer |
| OLD | NEW |