| 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/engine/build_commit_command.h" | 5 #include "chrome/browser/sync/engine/build_commit_command.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 using syncable::Id; | 28 using syncable::Id; |
| 29 using syncable::MutableEntry; | 29 using syncable::MutableEntry; |
| 30 using syncable::SPECIFICS; | 30 using syncable::SPECIFICS; |
| 31 using syncable::UNSPECIFIED; | 31 using syncable::UNSPECIFIED; |
| 32 | 32 |
| 33 namespace browser_sync { | 33 namespace browser_sync { |
| 34 | 34 |
| 35 using sessions::SyncSession; | 35 using sessions::SyncSession; |
| 36 | 36 |
| 37 // static | 37 // static |
| 38 const int64 BuildCommitCommand::kFirstPosition = | 38 int64 BuildCommitCommand::GetFirstPosition() { |
| 39 std::numeric_limits<int64>::min(); | 39 return std::numeric_limits<int64>::min(); |
| 40 } |
| 40 | 41 |
| 41 // static | 42 // static |
| 42 const int64 BuildCommitCommand::kLastPosition = | 43 int64 BuildCommitCommand::GetLastPosition() { |
| 43 std::numeric_limits<int64>::max(); | 44 return std::numeric_limits<int64>::max(); |
| 45 } |
| 44 | 46 |
| 45 // static | 47 // static |
| 46 const int64 BuildCommitCommand::kGap = 1LL << 20; | 48 int64 BuildCommitCommand::GetGap() { |
| 49 return 1LL << 20; |
| 50 } |
| 47 | 51 |
| 48 BuildCommitCommand::BuildCommitCommand() {} | 52 BuildCommitCommand::BuildCommitCommand() {} |
| 49 BuildCommitCommand::~BuildCommitCommand() {} | 53 BuildCommitCommand::~BuildCommitCommand() {} |
| 50 | 54 |
| 51 void BuildCommitCommand::AddExtensionsActivityToMessage( | 55 void BuildCommitCommand::AddExtensionsActivityToMessage( |
| 52 SyncSession* session, CommitMessage* message) { | 56 SyncSession* session, CommitMessage* message) { |
| 53 // We only send ExtensionsActivity to the server if bookmarks are being | 57 // We only send ExtensionsActivity to the server if bookmarks are being |
| 54 // committed. | 58 // committed. |
| 55 ExtensionsActivityMonitor* monitor = session->context()->extensions_monitor(); | 59 ExtensionsActivityMonitor* monitor = session->context()->extensions_monitor(); |
| 56 if (!session->status_controller()->HasBookmarkCommitActivity()) { | 60 if (!session->status_controller()->HasBookmarkCommitActivity()) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 sync_entry->set_deleted(true); | 182 sync_entry->set_deleted(true); |
| 179 } else { | 183 } else { |
| 180 if (meta_entry.Get(SPECIFICS).HasExtension(sync_pb::bookmark)) { | 184 if (meta_entry.Get(SPECIFICS).HasExtension(sync_pb::bookmark)) { |
| 181 // Common data in both new and old protocol. | 185 // Common data in both new and old protocol. |
| 182 const Id& prev_id = meta_entry.Get(syncable::PREV_ID); | 186 const Id& prev_id = meta_entry.Get(syncable::PREV_ID); |
| 183 string prev_id_string = | 187 string prev_id_string = |
| 184 prev_id.IsRoot() ? string() : prev_id.GetServerId(); | 188 prev_id.IsRoot() ? string() : prev_id.GetServerId(); |
| 185 sync_entry->set_insert_after_item_id(prev_id_string); | 189 sync_entry->set_insert_after_item_id(prev_id_string); |
| 186 | 190 |
| 187 // Compute a numeric position based on what we know locally. | 191 // Compute a numeric position based on what we know locally. |
| 188 std::pair<int64, int64> position_block(kFirstPosition, kLastPosition); | 192 std::pair<int64, int64> position_block( |
| 193 GetFirstPosition(), GetLastPosition()); |
| 189 std::map<Id, std::pair<int64, int64> >::iterator prev_pos = | 194 std::map<Id, std::pair<int64, int64> >::iterator prev_pos = |
| 190 position_map.find(prev_id); | 195 position_map.find(prev_id); |
| 191 if (prev_pos != position_map.end()) { | 196 if (prev_pos != position_map.end()) { |
| 192 position_block = prev_pos->second; | 197 position_block = prev_pos->second; |
| 193 position_map.erase(prev_pos); | 198 position_map.erase(prev_pos); |
| 194 } else { | 199 } else { |
| 195 position_block = std::make_pair( | 200 position_block = std::make_pair( |
| 196 FindAnchorPosition(syncable::PREV_ID, meta_entry), | 201 FindAnchorPosition(syncable::PREV_ID, meta_entry), |
| 197 FindAnchorPosition(syncable::NEXT_ID, meta_entry)); | 202 FindAnchorPosition(syncable::NEXT_ID, meta_entry)); |
| 198 } | 203 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 213 Id next_id = entry.Get(direction); | 218 Id next_id = entry.Get(direction); |
| 214 while (!next_id.IsRoot()) { | 219 while (!next_id.IsRoot()) { |
| 215 Entry next_entry(entry.trans(), | 220 Entry next_entry(entry.trans(), |
| 216 syncable::GET_BY_ID, | 221 syncable::GET_BY_ID, |
| 217 next_id); | 222 next_id); |
| 218 if (!next_entry.Get(IS_UNSYNCED) && !next_entry.Get(IS_UNAPPLIED_UPDATE)) { | 223 if (!next_entry.Get(IS_UNSYNCED) && !next_entry.Get(IS_UNAPPLIED_UPDATE)) { |
| 219 return next_entry.Get(SERVER_POSITION_IN_PARENT); | 224 return next_entry.Get(SERVER_POSITION_IN_PARENT); |
| 220 } | 225 } |
| 221 next_id = next_entry.Get(direction); | 226 next_id = next_entry.Get(direction); |
| 222 } | 227 } |
| 223 return direction == syncable::PREV_ID ? kFirstPosition : kLastPosition; | 228 return |
| 229 direction == syncable::PREV_ID ? |
| 230 GetFirstPosition() : GetLastPosition(); |
| 224 } | 231 } |
| 225 | 232 |
| 226 int64 BuildCommitCommand::InterpolatePosition(const int64 lo, | 233 int64 BuildCommitCommand::InterpolatePosition(const int64 lo, |
| 227 const int64 hi) { | 234 const int64 hi) { |
| 228 DCHECK_LE(lo, hi); | 235 DCHECK_LE(lo, hi); |
| 229 | 236 |
| 230 // The first item to be added under a parent gets a position of zero. | 237 // The first item to be added under a parent gets a position of zero. |
| 231 if (lo == kFirstPosition && hi == kLastPosition) | 238 if (lo == GetFirstPosition() && hi == GetLastPosition()) |
| 232 return 0; | 239 return 0; |
| 233 | 240 |
| 234 // For small gaps, we do linear interpolation. For larger gaps, | 241 // For small gaps, we do linear interpolation. For larger gaps, |
| 235 // we use an additive offset of |kGap|. We are careful to avoid | 242 // we use an additive offset of |GetGap()|. We are careful to avoid |
| 236 // signed integer overflow. | 243 // signed integer overflow. |
| 237 uint64 delta = static_cast<uint64>(hi) - static_cast<uint64>(lo); | 244 uint64 delta = static_cast<uint64>(hi) - static_cast<uint64>(lo); |
| 238 if (delta <= static_cast<uint64>(kGap*2)) | 245 if (delta <= static_cast<uint64>(GetGap()*2)) |
| 239 return lo + (static_cast<int64>(delta) + 7) / 8; // Interpolate. | 246 return lo + (static_cast<int64>(delta) + 7) / 8; // Interpolate. |
| 240 else if (lo == kFirstPosition) | 247 else if (lo == GetFirstPosition()) |
| 241 return hi - kGap; // Extend range just before successor. | 248 return hi - GetGap(); // Extend range just before successor. |
| 242 else | 249 else |
| 243 return lo + kGap; // Use or extend range just after predecessor. | 250 return lo + GetGap(); // Use or extend range just after predecessor. |
| 244 } | 251 } |
| 245 | 252 |
| 246 | 253 |
| 247 } // namespace browser_sync | 254 } // namespace browser_sync |
| OLD | NEW |