| 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 #include "blimp/helium/compound_syncable.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <bitset> | |
| 9 #include <map> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace helium { | |
| 19 | |
| 20 CompoundChangeset::CompoundChangeset() | |
| 21 : compound_changesets(this, std::map<int, std::string>()) {} | |
| 22 | |
| 23 CompoundChangeset::~CompoundChangeset() {} | |
| 24 | |
| 25 CompoundSyncable::CompoundSyncable() {} | |
| 26 | |
| 27 CompoundSyncable::~CompoundSyncable() {} | |
| 28 | |
| 29 std::unique_ptr<CompoundChangeset> CompoundSyncable::CreateChangeset( | |
| 30 Revision from) const { | |
| 31 // Populate a map with the serialized changesets of modified member | |
| 32 // Syncables. | |
| 33 std::map<int, std::string> compound_changesets_map; | |
| 34 for (size_t i = 0; i < members_.size(); ++i) { | |
| 35 if (members_[i]->GetRevision() >= from) { | |
| 36 std::string changeset_data; | |
| 37 google::protobuf::io::StringOutputStream raw_output_stream( | |
| 38 &changeset_data); | |
| 39 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream); | |
| 40 members_[i]->CreateChangeset(from, &output_stream); | |
| 41 output_stream.Trim(); | |
| 42 if (!changeset_data.empty()) { | |
| 43 compound_changesets_map[i] = std::move(changeset_data); | |
| 44 } | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 std::unique_ptr<CompoundChangeset> output_changeset = | |
| 49 base::MakeUnique<CompoundChangeset>(); | |
| 50 output_changeset->compound_changesets.Set(std::move(compound_changesets_map)); | |
| 51 return output_changeset; | |
| 52 } | |
| 53 | |
| 54 void CompoundSyncable::ApplyChangeset(const CompoundChangeset& changeset) { | |
| 55 for (const auto& current_changeset : changeset.compound_changesets()) { | |
| 56 members_[current_changeset.first]->ApplyChangeset(); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 Revision CompoundSyncable::GetRevision() const { | |
| 61 Revision merged_revision = {}; | |
| 62 for (const auto& member : members_) { | |
| 63 merged_revision = std::max(merged_revision, member->GetRevision()); | |
| 64 } | |
| 65 return merged_revision; | |
| 66 } | |
| 67 | |
| 68 void CompoundSyncable::SetLocalUpdateCallback( | |
| 69 const base::Closure& local_update_callback) { | |
| 70 for (auto* member : members_) { | |
| 71 member->SetLocalUpdateCallback(local_update_callback); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void CompoundSyncable::ReleaseBefore(Revision from) { | |
| 76 for (auto* member : members_) { | |
| 77 member->ReleaseBefore(from); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 bool CompoundSyncable::ValidateChangeset( | |
| 82 const CompoundChangeset& changeset) const { | |
| 83 for (const auto& current_changeset : changeset.compound_changesets()) { | |
| 84 int32_t syncable_id = current_changeset.first; | |
| 85 auto changeset = current_changeset.second; | |
| 86 if (syncable_id >= static_cast<int>(members_.size())) { | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(), | |
| 91 changeset.size()); | |
| 92 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream); | |
| 93 | |
| 94 if (!members_[syncable_id]->ParseAndValidate(&input_stream)) { | |
| 95 return false; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 } // namespace helium | |
| 103 } // namespace blimp | |
| OLD | NEW |