OLD | NEW |
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/sessions/sync_session.h" | 5 #include "chrome/browser/sync/sessions/sync_session.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/logging.h" |
9 #include "chrome/browser/sync/syncable/directory_manager.h" | 10 #include "chrome/browser/sync/syncable/directory_manager.h" |
10 #include "chrome/browser/sync/syncable/model_type.h" | 11 #include "chrome/browser/sync/syncable/model_type.h" |
11 | 12 |
12 namespace browser_sync { | 13 namespace browser_sync { |
13 namespace sessions { | 14 namespace sessions { |
14 | 15 |
| 16 namespace { |
| 17 |
| 18 std::set<ModelSafeGroup> ComputeEnabledGroups( |
| 19 const ModelSafeRoutingInfo& routing_info, |
| 20 const std::vector<ModelSafeWorker*>& workers) { |
| 21 std::set<ModelSafeGroup> enabled_groups; |
| 22 // Project the list of enabled types (i.e., types in the routing |
| 23 // info) to a list of enabled groups. |
| 24 for (ModelSafeRoutingInfo::const_iterator it = routing_info.begin(); |
| 25 it != routing_info.end(); ++it) { |
| 26 enabled_groups.insert(it->second); |
| 27 } |
| 28 // GROUP_PASSIVE is always enabled, since that's the group that |
| 29 // top-level folders map to. |
| 30 enabled_groups.insert(GROUP_PASSIVE); |
| 31 if (DCHECK_IS_ON()) { |
| 32 // We sometimes create dummy SyncSession objects (see |
| 33 // SyncScheduler::InitialSnapshot) so don't check in that case. |
| 34 if (!routing_info.empty() || !workers.empty()) { |
| 35 std::set<ModelSafeGroup> groups_with_workers; |
| 36 for (std::vector<ModelSafeWorker*>::const_iterator it = workers.begin(); |
| 37 it != workers.end(); ++it) { |
| 38 groups_with_workers.insert((*it)->GetModelSafeGroup()); |
| 39 } |
| 40 // All enabled groups should have a corresponding worker. |
| 41 DCHECK(std::includes( |
| 42 groups_with_workers.begin(), groups_with_workers.end(), |
| 43 enabled_groups.begin(), enabled_groups.end())); |
| 44 } |
| 45 } |
| 46 return enabled_groups; |
| 47 } |
| 48 |
| 49 } // namesepace |
| 50 |
15 SyncSession::SyncSession(SyncSessionContext* context, Delegate* delegate, | 51 SyncSession::SyncSession(SyncSessionContext* context, Delegate* delegate, |
16 const SyncSourceInfo& source, | 52 const SyncSourceInfo& source, |
17 const ModelSafeRoutingInfo& routing_info, | 53 const ModelSafeRoutingInfo& routing_info, |
18 const std::vector<ModelSafeWorker*>& workers) | 54 const std::vector<ModelSafeWorker*>& workers) |
19 : context_(context), | 55 : context_(context), |
20 source_(source), | 56 source_(source), |
21 write_transaction_(NULL), | 57 write_transaction_(NULL), |
22 delegate_(delegate), | 58 delegate_(delegate), |
23 workers_(workers), | 59 workers_(workers), |
24 routing_info_(routing_info) { | 60 routing_info_(routing_info), |
| 61 enabled_groups_(ComputeEnabledGroups(routing_info_, workers_)) { |
25 status_controller_.reset(new StatusController(routing_info_)); | 62 status_controller_.reset(new StatusController(routing_info_)); |
26 std::sort(workers_.begin(), workers_.end()); | 63 std::sort(workers_.begin(), workers_.end()); |
27 } | 64 } |
28 | 65 |
29 SyncSession::~SyncSession() {} | 66 SyncSession::~SyncSession() {} |
30 | 67 |
31 void SyncSession::Coalesce(const SyncSession& session) { | 68 void SyncSession::Coalesce(const SyncSession& session) { |
32 if (context_ != session.context() || delegate_ != session.delegate_) { | 69 if (context_ != session.context() || delegate_ != session.delegate_) { |
33 NOTREACHED(); | 70 NOTREACHED(); |
34 return; | 71 return; |
(...skipping 11 matching lines...) Expand all Loading... |
46 workers_.swap(temp); | 83 workers_.swap(temp); |
47 | 84 |
48 // We have to update the model safe routing info to the union. In case the | 85 // We have to update the model safe routing info to the union. In case the |
49 // same key is present in both pick the one from session. | 86 // same key is present in both pick the one from session. |
50 for (ModelSafeRoutingInfo::const_iterator it = | 87 for (ModelSafeRoutingInfo::const_iterator it = |
51 session.routing_info_.begin(); | 88 session.routing_info_.begin(); |
52 it != session.routing_info_.end(); | 89 it != session.routing_info_.end(); |
53 ++it) { | 90 ++it) { |
54 routing_info_[it->first] = it->second; | 91 routing_info_[it->first] = it->second; |
55 } | 92 } |
| 93 |
| 94 // Now update enabled groups. |
| 95 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); |
56 } | 96 } |
57 | 97 |
58 void SyncSession::RebaseRoutingInfoWithLatest(SyncSession* session) { | 98 void SyncSession::RebaseRoutingInfoWithLatest(const SyncSession& session) { |
59 ModelSafeRoutingInfo temp_routing_info; | 99 ModelSafeRoutingInfo temp_routing_info; |
60 | 100 |
61 // Take the intersecion and also set the routing info(it->second) from the | 101 // Take the intersecion and also set the routing info(it->second) from the |
62 // passed in session. | 102 // passed in session. |
63 for (ModelSafeRoutingInfo::const_iterator it = | 103 for (ModelSafeRoutingInfo::const_iterator it = |
64 session->routing_info_.begin(); it != session->routing_info_.end(); | 104 session.routing_info_.begin(); it != session.routing_info_.end(); |
65 ++it) { | 105 ++it) { |
66 if (routing_info_.find(it->first) != routing_info_.end()) { | 106 if (routing_info_.find(it->first) != routing_info_.end()) { |
67 temp_routing_info[it->first] = it->second; | 107 temp_routing_info[it->first] = it->second; |
68 } | 108 } |
69 } | 109 } |
70 | 110 |
71 // Now swap it. | 111 // Now swap it. |
72 routing_info_.swap(temp_routing_info); | 112 routing_info_.swap(temp_routing_info); |
73 | 113 |
74 // Now update the payload map. | 114 // Now update the payload map. |
75 PurgeStalePayload(&source_.types, session->routing_info_); | 115 PurgeStalePayload(&source_.types, session.routing_info_); |
76 | 116 |
77 // Now update the workers. | 117 // Now update the workers. |
78 std::vector<ModelSafeWorker*> temp; | 118 std::vector<ModelSafeWorker*> temp; |
79 std::set_intersection(workers_.begin(), workers_.end(), | 119 std::set_intersection(workers_.begin(), workers_.end(), |
80 session->workers_.begin(), session->workers_.end(), | 120 session.workers_.begin(), session.workers_.end(), |
81 std::back_inserter(temp)); | 121 std::back_inserter(temp)); |
82 workers_.swap(temp); | 122 workers_.swap(temp); |
| 123 |
| 124 // Now update enabled groups. |
| 125 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); |
83 } | 126 } |
84 | 127 |
85 void SyncSession::ResetTransientState() { | 128 void SyncSession::ResetTransientState() { |
86 status_controller_.reset(new StatusController(routing_info_)); | 129 status_controller_.reset(new StatusController(routing_info_)); |
87 } | 130 } |
88 | 131 |
89 SyncSessionSnapshot SyncSession::TakeSnapshot() const { | 132 SyncSessionSnapshot SyncSession::TakeSnapshot() const { |
90 syncable::ScopedDirLookup dir(context_->directory_manager(), | 133 syncable::ScopedDirLookup dir(context_->directory_manager(), |
91 context_->account_name()); | 134 context_->account_name()); |
92 if (!dir.good()) | 135 if (!dir.good()) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 177 } |
135 | 178 |
136 bool SyncSession::HasMoreToSync() const { | 179 bool SyncSession::HasMoreToSync() const { |
137 const StatusController* status = status_controller_.get(); | 180 const StatusController* status = status_controller_.get(); |
138 return ((status->commit_ids().size() < status->unsynced_handles().size()) && | 181 return ((status->commit_ids().size() < status->unsynced_handles().size()) && |
139 status->syncer_status().num_successful_commits > 0) || | 182 status->syncer_status().num_successful_commits > 0) || |
140 status->conflict_sets_built() || | 183 status->conflict_sets_built() || |
141 status->conflicts_resolved(); | 184 status->conflicts_resolved(); |
142 // Or, we have conflicting updates, but we're making progress on | 185 // Or, we have conflicting updates, but we're making progress on |
143 // resolving them... | 186 // resolving them... |
| 187 } |
| 188 |
| 189 const std::set<ModelSafeGroup>& SyncSession::GetEnabledGroups() const { |
| 190 return enabled_groups_; |
| 191 } |
| 192 |
| 193 std::set<ModelSafeGroup> SyncSession::GetEnabledGroupsWithConflicts() const { |
| 194 const std::set<ModelSafeGroup>& enabled_groups = GetEnabledGroups(); |
| 195 std::set<ModelSafeGroup> enabled_groups_with_conflicts; |
| 196 for (std::set<ModelSafeGroup>::const_iterator it = |
| 197 enabled_groups.begin(); it != enabled_groups.end(); ++it) { |
| 198 const sessions::ConflictProgress* conflict_progress = |
| 199 status_controller_->GetUnrestrictedConflictProgress(*it); |
| 200 if (conflict_progress && |
| 201 (conflict_progress->ConflictingItemsBegin() != |
| 202 conflict_progress->ConflictingItemsEnd())) { |
| 203 enabled_groups_with_conflicts.insert(*it); |
| 204 } |
144 } | 205 } |
| 206 return enabled_groups_with_conflicts; |
| 207 } |
| 208 |
| 209 std::set<ModelSafeGroup> |
| 210 SyncSession::GetEnabledGroupsWithVerifiedUpdates() const { |
| 211 const std::set<ModelSafeGroup>& enabled_groups = GetEnabledGroups(); |
| 212 std::set<ModelSafeGroup> enabled_groups_with_verified_updates; |
| 213 for (std::set<ModelSafeGroup>::const_iterator it = |
| 214 enabled_groups.begin(); it != enabled_groups.end(); ++it) { |
| 215 const UpdateProgress* update_progress = |
| 216 status_controller_->GetUnrestrictedUpdateProgress(*it); |
| 217 if (update_progress && |
| 218 (update_progress->VerifiedUpdatesBegin() != |
| 219 update_progress->VerifiedUpdatesEnd())) { |
| 220 enabled_groups_with_verified_updates.insert(*it); |
| 221 } |
| 222 } |
| 223 |
| 224 return enabled_groups_with_verified_updates; |
| 225 } |
| 226 |
145 | 227 |
146 } // namespace sessions | 228 } // namespace sessions |
147 } // namespace browser_sync | 229 } // namespace browser_sync |
OLD | NEW |