| 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 "components/reading_list/ios/reading_list_model.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 | |
| 10 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} | |
| 11 | |
| 12 ReadingListModel::~ReadingListModel() { | |
| 13 for (auto& observer : observers_) { | |
| 14 observer.ReadingListModelBeingDeleted(this); | |
| 15 } | |
| 16 } | |
| 17 | |
| 18 // Observer methods. | |
| 19 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { | |
| 20 DCHECK(CalledOnValidThread()); | |
| 21 DCHECK(observer); | |
| 22 observers_.AddObserver(observer); | |
| 23 if (loaded()) { | |
| 24 observer->ReadingListModelLoaded(this); | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { | |
| 29 DCHECK(CalledOnValidThread()); | |
| 30 observers_.RemoveObserver(observer); | |
| 31 } | |
| 32 | |
| 33 // Batch update methods. | |
| 34 bool ReadingListModel::IsPerformingBatchUpdates() const { | |
| 35 DCHECK(CalledOnValidThread()); | |
| 36 return current_batch_updates_count_ > 0; | |
| 37 } | |
| 38 | |
| 39 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | |
| 40 ReadingListModel::CreateBatchToken() { | |
| 41 return base::MakeUnique<ReadingListModel::ScopedReadingListBatchUpdate>(this); | |
| 42 } | |
| 43 | |
| 44 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | |
| 45 ReadingListModel::BeginBatchUpdates() { | |
| 46 DCHECK(CalledOnValidThread()); | |
| 47 auto token = CreateBatchToken(); | |
| 48 | |
| 49 ++current_batch_updates_count_; | |
| 50 if (current_batch_updates_count_ == 1) { | |
| 51 EnteringBatchUpdates(); | |
| 52 } | |
| 53 return token; | |
| 54 } | |
| 55 | |
| 56 void ReadingListModel::EnteringBatchUpdates() { | |
| 57 DCHECK(CalledOnValidThread()); | |
| 58 for (auto& observer : observers_) | |
| 59 observer.ReadingListModelBeganBatchUpdates(this); | |
| 60 } | |
| 61 | |
| 62 void ReadingListModel::EndBatchUpdates() { | |
| 63 DCHECK(CalledOnValidThread()); | |
| 64 DCHECK(IsPerformingBatchUpdates()); | |
| 65 DCHECK(current_batch_updates_count_ > 0); | |
| 66 --current_batch_updates_count_; | |
| 67 if (current_batch_updates_count_ == 0) { | |
| 68 LeavingBatchUpdates(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void ReadingListModel::LeavingBatchUpdates() { | |
| 73 DCHECK(CalledOnValidThread()); | |
| 74 for (auto& observer : observers_) | |
| 75 observer.ReadingListModelCompletedBatchUpdates(this); | |
| 76 } | |
| 77 | |
| 78 ReadingListModel::ScopedReadingListBatchUpdate:: | |
| 79 ~ScopedReadingListBatchUpdate() { | |
| 80 model_->EndBatchUpdates(); | |
| 81 } | |
| OLD | NEW |