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