1 // Copyright 2020 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 "components/feed/core/v2/stream_model/ephemeral_change.h" 6 7 namespace feed { 8 namespace stream_model { 9 EphemeralChange(EphemeralChangeId id,std::vector<feedstore::DataOperation> operations)10EphemeralChange::EphemeralChange( 11 EphemeralChangeId id, 12 std::vector<feedstore::DataOperation> operations) 13 : id_(id), operations_(std::move(operations)) {} 14 EphemeralChange::~EphemeralChange() = default; 15 16 EphemeralChangeList::EphemeralChangeList() = default; 17 EphemeralChangeList::~EphemeralChangeList() = default; AddEphemeralChange(std::vector<feedstore::DataOperation> operations)18EphemeralChange* EphemeralChangeList::AddEphemeralChange( 19 std::vector<feedstore::DataOperation> operations) { 20 change_list_.push_back(std::make_unique<EphemeralChange>( 21 id_generator_.GenerateNextId(), operations)); 22 return change_list_.back().get(); 23 } Find(EphemeralChangeId id)24EphemeralChange* EphemeralChangeList::Find(EphemeralChangeId id) { 25 for (std::unique_ptr<EphemeralChange>& change : change_list_) { 26 if (change->id() == id) 27 return change.get(); 28 } 29 return nullptr; 30 } 31 ApplyEphemeralChanges(const FeatureTree & tree,const EphemeralChangeList & changes)32std::unique_ptr<FeatureTree> ApplyEphemeralChanges( 33 const FeatureTree& tree, 34 const EphemeralChangeList& changes) { 35 auto tree_with_changes = std::make_unique<FeatureTree>(&tree); 36 37 for (const std::unique_ptr<EphemeralChange>& change : 38 changes.GetChangeList()) { 39 for (const feedstore::DataOperation& operation : change->GetOperations()) { 40 if (operation.has_structure()) { 41 tree_with_changes->ApplyStreamStructure(operation.structure()); 42 } 43 if (operation.has_content()) { 44 tree_with_changes->CopyAndAddContent(operation.content()); 45 } 46 } 47 } 48 return tree_with_changes; 49 } 50 Remove(EphemeralChangeId id)51std::unique_ptr<EphemeralChange> EphemeralChangeList::Remove( 52 EphemeralChangeId id) { 53 for (size_t i = 0; i < change_list_.size(); ++i) { 54 if (change_list_[i]->id() == id) { 55 std::unique_ptr<EphemeralChange> result = std::move(change_list_[i]); 56 change_list_.erase(change_list_.begin() + i); 57 return result; 58 } 59 } 60 return nullptr; 61 } 62 63 } // namespace stream_model 64 } // namespace feed 65