| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Defines ChangeReorderBuffer, which can be used to sort a list of item | |
| 6 // actions to achieve the ordering constraint required by the SyncObserver | |
| 7 // interface of the SyncAPI. | |
| 8 | |
| 9 #ifndef CHROME_BROWSER_SYNC_ENGINE_CHANGE_REORDER_BUFFER_H_ | |
| 10 #define CHROME_BROWSER_SYNC_ENGINE_CHANGE_REORDER_BUFFER_H_ | |
| 11 #pragma once | |
| 12 | |
| 13 #include <map> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/memory/linked_ptr.h" | |
| 17 #include "chrome/browser/sync/internal_api/base_transaction.h" | |
| 18 #include "chrome/browser/sync/internal_api/sync_manager.h" | |
| 19 #include "chrome/browser/sync/protocol/sync.pb.h" | |
| 20 | |
| 21 namespace sync_api { | |
| 22 | |
| 23 // ChangeReorderBuffer is a utility type which accepts an unordered set | |
| 24 // of changes (via its Push methods), and yields a vector of ChangeRecords | |
| 25 // (via the GetAllChangesInTreeOrder method) that are in the order that | |
| 26 // the SyncObserver expects them to be. A buffer is initially empty. | |
| 27 // | |
| 28 // The ordering produced by ChangeReorderBuffer is as follows: | |
| 29 // (a) All Deleted items appear first. | |
| 30 // (b) For Updated and/or Added items, parents appear before their children. | |
| 31 // (c) When there are changes to the sibling order (this means Added items, | |
| 32 // or Updated items with the |position_changed| parameter set to true), | |
| 33 // all siblings under a parent will appear in the output, even if they | |
| 34 // are not explicitly pushed. The sibling order will be preserved in | |
| 35 // the output list -- items will appear before their sibling-order | |
| 36 // successors. | |
| 37 // (d) When there are no changes to the sibling order under a parent node, | |
| 38 // the sibling order is not necessarily preserved in the output for | |
| 39 // its children. | |
| 40 class ChangeReorderBuffer { | |
| 41 public: | |
| 42 typedef SyncManager::ChangeRecord ChangeRecord; | |
| 43 typedef SyncManager::ExtraPasswordChangeRecordData ExtraChangeRecordData; | |
| 44 | |
| 45 ChangeReorderBuffer(); | |
| 46 ~ChangeReorderBuffer(); | |
| 47 | |
| 48 // Insert an item, identified by the metahandle |id|, into the reorder | |
| 49 // buffer. This item will appear in the output list as an ACTION_ADD | |
| 50 // ChangeRecord. | |
| 51 void PushAddedItem(int64 id) { | |
| 52 operations_[id] = OP_ADD; | |
| 53 } | |
| 54 | |
| 55 // Insert an item, identified by the metahandle |id|, into the reorder | |
| 56 // buffer. This item will appear in the output list as an ACTION_DELETE | |
| 57 // ChangeRecord. | |
| 58 void PushDeletedItem(int64 id) { | |
| 59 operations_[id] = OP_DELETE; | |
| 60 } | |
| 61 | |
| 62 // Insert an item, identified by the metahandle |id|, into the reorder | |
| 63 // buffer. This item will appear in the output list as an ACTION_UPDATE | |
| 64 // ChangeRecord. Also, if |position_changed| is true, all siblings of this | |
| 65 // item will appear in the output list as well; if it wasn't explicitly | |
| 66 // pushed, the siblings will have an ACTION_UPDATE ChangeRecord. | |
| 67 void PushUpdatedItem(int64 id, bool position_changed) { | |
| 68 operations_[id] = position_changed ? OP_UPDATE_POSITION_AND_PROPERTIES : | |
| 69 OP_UPDATE_PROPERTIES_ONLY; | |
| 70 } | |
| 71 | |
| 72 void SetExtraDataForId(int64 id, ExtraChangeRecordData* extra) { | |
| 73 extra_data_[id] = make_linked_ptr<ExtraChangeRecordData>(extra); | |
| 74 } | |
| 75 | |
| 76 void SetSpecificsForId(int64 id, const sync_pb::EntitySpecifics& specifics) { | |
| 77 specifics_[id] = specifics; | |
| 78 } | |
| 79 | |
| 80 // Reset the buffer, forgetting any pushed items, so that it can be used | |
| 81 // again to reorder a new set of changes. | |
| 82 void Clear() { | |
| 83 operations_.clear(); | |
| 84 } | |
| 85 | |
| 86 bool IsEmpty() const { | |
| 87 return operations_.empty(); | |
| 88 } | |
| 89 | |
| 90 // Output a reordered list of changes to |changelist| using the items that | |
| 91 // were pushed into the reorder buffer. |sync_trans| is used to determine the | |
| 92 // ordering. | |
| 93 void GetAllChangesInTreeOrder(const BaseTransaction* sync_trans, | |
| 94 std::vector<ChangeRecord>* changelist); | |
| 95 | |
| 96 private: | |
| 97 class Traversal; | |
| 98 enum Operation { | |
| 99 OP_ADD, // AddedItem. | |
| 100 OP_DELETE, // DeletedItem. | |
| 101 OP_UPDATE_PROPERTIES_ONLY, // UpdatedItem with position_changed=0. | |
| 102 OP_UPDATE_POSITION_AND_PROPERTIES, // UpdatedItem with position_changed=1. | |
| 103 }; | |
| 104 typedef std::map<int64, Operation> OperationMap; | |
| 105 typedef std::map<int64, sync_pb::EntitySpecifics> SpecificsMap; | |
| 106 typedef std::map<int64, linked_ptr<ExtraChangeRecordData> > ExtraDataMap; | |
| 107 | |
| 108 // Stores the items that have been pushed into the buffer, and the type of | |
| 109 // operation that was associated with them. | |
| 110 OperationMap operations_; | |
| 111 | |
| 112 // Stores entity-specific ChangeRecord data per-ID. | |
| 113 SpecificsMap specifics_; | |
| 114 | |
| 115 // Stores type-specific extra data per-ID. | |
| 116 ExtraDataMap extra_data_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(ChangeReorderBuffer); | |
| 119 }; | |
| 120 | |
| 121 } // namespace sync_api | |
| 122 | |
| 123 #endif // CHROME_BROWSER_SYNC_ENGINE_CHANGE_REORDER_BUFFER_H_ | |
| OLD | NEW |