| 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_LAZY_SYNCABLE_ADAPTER_H_ | |
| 6 #define BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "blimp/helium/blimp_helium_export.h" | |
| 12 #include "blimp/helium/syncable.h" | |
| 13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 14 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace helium { | |
| 18 | |
| 19 // The LazySyncableAdapter is used to wrap LazySyncables of any changeset type | |
| 20 // into a LazySyncable with a string changeset by serializing the changesets | |
| 21 // into a string and parsing them from strings. That way, ObjectSyncState can | |
| 22 // just work with LazySyncables of type string. | |
| 23 template <class ChangesetType> | |
| 24 class BLIMP_HELIUM_EXPORT LazySyncableAdapter | |
| 25 : public LazySyncable<std::string> { | |
| 26 public: | |
| 27 explicit LazySyncableAdapter(LazySyncable<ChangesetType>* inner_syncable) | |
| 28 : inner_syncable_(inner_syncable) {} | |
| 29 ~LazySyncableAdapter() = default; | |
| 30 | |
| 31 // LazySyncable implementation. | |
| 32 std::unique_ptr<std::string> CreateChangeset(Revision from) const override; | |
| 33 void ApplyChangeset(const std::string& changeset) override; | |
| 34 bool ValidateChangeset(const std::string& changeset) const override; | |
| 35 void SetLocalUpdateCallback( | |
| 36 const base::Closure& local_update_callback) override; | |
| 37 void ReleaseBefore(Revision before) override; | |
| 38 Revision GetRevision() const override; | |
| 39 void PrepareToCreateChangeset(Revision from, base::Closure done) override; | |
| 40 | |
| 41 private: | |
| 42 static std::unique_ptr<ChangesetType> ParseChangesetFromString( | |
| 43 const std::string& changeset); | |
| 44 | |
| 45 LazySyncable<ChangesetType>* inner_syncable_; | |
| 46 | |
| 47 DISALLOW_COPY_AND_ASSIGN(LazySyncableAdapter); | |
| 48 }; | |
| 49 | |
| 50 template <class ChangesetType> | |
| 51 std::unique_ptr<std::string> | |
| 52 LazySyncableAdapter<ChangesetType>::CreateChangeset(Revision from) const { | |
| 53 std::unique_ptr<ChangesetType> changeset = | |
| 54 inner_syncable_->CreateChangeset(from); | |
| 55 std::unique_ptr<std::string> output = base::MakeUnique<std::string>(); | |
| 56 google::protobuf::io::StringOutputStream raw_output_stream(output.get()); | |
| 57 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); | |
| 58 changeset->Serialize(&output_stream); | |
| 59 return output; | |
| 60 } | |
| 61 | |
| 62 template <class ChangesetType> | |
| 63 void LazySyncableAdapter<ChangesetType>::ApplyChangeset( | |
| 64 const std::string& changeset) { | |
| 65 std::unique_ptr<ChangesetType> parsed_changeset = | |
| 66 ParseChangesetFromString(changeset); | |
| 67 inner_syncable_->ApplyChangeset(*parsed_changeset); | |
| 68 } | |
| 69 | |
| 70 template <class ChangesetType> | |
| 71 bool LazySyncableAdapter<ChangesetType>::ValidateChangeset( | |
| 72 const std::string& changeset) const { | |
| 73 std::unique_ptr<ChangesetType> parsed_changeset = | |
| 74 ParseChangesetFromString(changeset); | |
| 75 return inner_syncable_->ValidateChangeset(*parsed_changeset); | |
| 76 } | |
| 77 | |
| 78 template <class ChangesetType> | |
| 79 void LazySyncableAdapter<ChangesetType>::SetLocalUpdateCallback( | |
| 80 const base::Closure& local_update_callback) { | |
| 81 inner_syncable_->SetLocalUpdateCallback(local_update_callback); | |
| 82 } | |
| 83 | |
| 84 template <class ChangesetType> | |
| 85 void LazySyncableAdapter<ChangesetType>::ReleaseBefore(Revision before) { | |
| 86 inner_syncable_->ReleaseBefore(before); | |
| 87 } | |
| 88 | |
| 89 template <class ChangesetType> | |
| 90 Revision LazySyncableAdapter<ChangesetType>::GetRevision() const { | |
| 91 return inner_syncable_->GetRevision(); | |
| 92 } | |
| 93 | |
| 94 template <class ChangesetType> | |
| 95 void LazySyncableAdapter<ChangesetType>::PrepareToCreateChangeset( | |
| 96 Revision from, | |
| 97 base::Closure done) { | |
| 98 inner_syncable_->PrepareToCreateChangeset(from, done); | |
| 99 } | |
| 100 | |
| 101 template <class ChangesetType> | |
| 102 std::unique_ptr<ChangesetType> | |
| 103 LazySyncableAdapter<ChangesetType>::ParseChangesetFromString( | |
| 104 const std::string& changeset) { | |
| 105 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), | |
| 106 changeset.size()); | |
| 107 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); | |
| 108 std::unique_ptr<ChangesetType> parsed_changeset = | |
| 109 base::MakeUnique<ChangesetType>(); | |
| 110 parsed_changeset->Parse(&input_stream); | |
| 111 return parsed_changeset; | |
| 112 } | |
| 113 | |
| 114 } // namespace helium | |
| 115 } // namespace blimp | |
| 116 #endif // BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_ | |
| OLD | NEW |