| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 #ifndef BLIMP_HELIUM_OBJECT_SYNC_STATE_H_ | |
| 6 #define BLIMP_HELIUM_OBJECT_SYNC_STATE_H_ | |
| 7 | |
| 8 #include "base/cancelable_callback.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "blimp/helium/blimp_helium_export.h" | |
| 11 #include "blimp/helium/lazy_syncable_adapter.h" | |
| 12 #include "blimp/helium/syncable.h" | |
| 13 #include "blimp/helium/update_scheduler.h" | |
| 14 #include "blimp/helium/version_vector.h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace helium { | |
| 18 | |
| 19 // Each registered LazySyncable is associated with an ObjectSyncState object | |
| 20 // which retains state used to manage Changeset delivery and resynchronization | |
| 21 // from potential message loss. | |
| 22 class BLIMP_HELIUM_EXPORT ObjectSyncState { | |
| 23 public: | |
| 24 using MessagePreparedCallback = base::Callback<void(proto::HeliumMessage)>; | |
| 25 | |
| 26 ~ObjectSyncState(); | |
| 27 | |
| 28 // Creates an ObjectSyncState instance for a LazySyncable<ChangesetType>, | |
| 29 // including a specialized adaptor to the generic LazySyncable<string> | |
| 30 // interface. | |
| 31 template <class ChangesetType> | |
| 32 static std::unique_ptr<ObjectSyncState> CreateForObject( | |
| 33 HeliumObjectId object_id, | |
| 34 LazySyncable<ChangesetType>* object) { | |
| 35 return base::WrapUnique<ObjectSyncState>(new ObjectSyncState( | |
| 36 object_id, | |
| 37 base::MakeUnique<LazySyncableAdapter<ChangesetType>>(object))); | |
| 38 } | |
| 39 | |
| 40 // Called by the UpdateScheduler to request a Changeset for the | |
| 41 // LazySyncable. | |
| 42 void PrepareChangesetMessage(MessagePreparedCallback callback); | |
| 43 | |
| 44 // Called by the UpdateScheduler to request an ACK-only message. | |
| 45 void PrepareAckMessage(MessagePreparedCallback callback); | |
| 46 | |
| 47 // Informs the ObjectSyncState that there is a connected Stream and gives an | |
| 48 // UpdateScheduler to send updates to. The scheduler object will not be | |
| 49 // destroyed until after OnStreamDisconnected is called. | |
| 50 void OnStreamConnected(UpdateScheduler* scheduler); | |
| 51 | |
| 52 // Informs the ObjectSyncState that the Stream has been disconnected and | |
| 53 // so the ObjectSyncState should stop any in-process HeliumMessage | |
| 54 // preparation. | |
| 55 void OnStreamDisconnected(); | |
| 56 | |
| 57 // Applies the given Changeset to the LazySyncable and informs the | |
| 58 // UpdateScheduler that an acknowledgement message is needed. | |
| 59 void OnChangesetReceived(Revision end_revision, const std::string& changeset); | |
| 60 | |
| 61 // Informs the ObjectSyncState that a previously sent HeliumMessage has been | |
| 62 // received and acknowledged by the peer. | |
| 63 void OnAckReceived(Revision ack_revision); | |
| 64 | |
| 65 private: | |
| 66 ObjectSyncState(HeliumObjectId object_id, | |
| 67 std::unique_ptr<LazySyncable<std::string>> object); | |
| 68 | |
| 69 // Called by the LazySyncable object when it is updated locally. | |
| 70 void OnLocalUpdate(); | |
| 71 | |
| 72 // Called when |object_| finishes preparing. Pulls a Changeset from |object_| | |
| 73 // and supplies it to |prepare_callback_|. | |
| 74 void OnPreparedChangeset(); | |
| 75 | |
| 76 // Determines the best time for |object_| to generate a Changeset or | |
| 77 // acknowledgement message. | |
| 78 UpdateScheduler* scheduler_ = nullptr; | |
| 79 | |
| 80 // Unique ID for |object_|. | |
| 81 HeliumObjectId object_id_; | |
| 82 | |
| 83 // Object whose sync state is being tracked. | |
| 84 std::unique_ptr<LazySyncable<std::string>> object_; | |
| 85 | |
| 86 // Used to track Changeset receipt and delivery. | |
| 87 Revision sent_revision_ = 0; | |
| 88 Revision remote_revision_ = 0; | |
| 89 Revision acked_revision_ = 0; | |
| 90 | |
| 91 // True if |object_| has local modifications which need delivering. | |
| 92 bool is_dirty_ = false; | |
| 93 | |
| 94 // True if we have received a Changeset but have not yet sent an associated | |
| 95 // acknowledgement HeliumMessage to the peer. | |
| 96 bool remote_revision_needs_ack_ = false; | |
| 97 | |
| 98 // True if all Changesets sent to the peer have been acknowledged. | |
| 99 bool is_confirmed_ = true; | |
| 100 | |
| 101 // Callback received from |scheduler_| when it is requesting a HeliumMessage. | |
| 102 MessagePreparedCallback prepare_callback_; | |
| 103 | |
| 104 // Used to allow in-progress Changeset preparation by |object_| to be canceled | |
| 105 // if the stream is torn down in the meantime. | |
| 106 base::CancelableCallback<void()> prepared_for_changeset_callback_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(ObjectSyncState); | |
| 109 }; | |
| 110 | |
| 111 } // namespace helium | |
| 112 } // namespace blimp | |
| 113 #endif // BLIMP_HELIUM_OBJECT_SYNC_STATE_H_ | |
| OLD | NEW |