| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/chrome/browser/reading_list/reading_list_model.h" | 5 #include "ios/chrome/browser/reading_list/reading_list_model.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 | |
| 10 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} | 7 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} |
| 11 | |
| 12 ReadingListModel::~ReadingListModel() { | 8 ReadingListModel::~ReadingListModel() { |
| 13 for (auto& observer : observers_) { | 9 for (auto& observer : observers_) { |
| 14 observer.ReadingListModelBeingDeleted(this); | 10 observer.ReadingListModelBeingDeleted(this); |
| 15 } | 11 } |
| 16 } | 12 } |
| 17 | 13 |
| 18 // Observer methods. | 14 // Observer methods. |
| 19 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { | 15 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { |
| 20 DCHECK(CalledOnValidThread()); | |
| 21 DCHECK(observer); | 16 DCHECK(observer); |
| 22 observers_.AddObserver(observer); | 17 observers_.AddObserver(observer); |
| 23 if (loaded()) { | 18 if (loaded()) { |
| 24 observer->ReadingListModelLoaded(this); | 19 observer->ReadingListModelLoaded(this); |
| 25 } | 20 } |
| 26 } | 21 } |
| 27 | 22 |
| 28 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { | 23 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { |
| 29 DCHECK(CalledOnValidThread()); | |
| 30 observers_.RemoveObserver(observer); | 24 observers_.RemoveObserver(observer); |
| 31 } | 25 } |
| 32 | 26 |
| 33 // Batch update methods. | 27 // Batch update methods. |
| 34 bool ReadingListModel::IsPerformingBatchUpdates() const { | 28 bool ReadingListModel::IsPerformingBatchUpdates() const { |
| 35 DCHECK(CalledOnValidThread()); | |
| 36 return current_batch_updates_count_ > 0; | 29 return current_batch_updates_count_ > 0; |
| 37 } | 30 } |
| 38 | 31 |
| 39 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | 32 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() { | 33 ReadingListModel::BeginBatchUpdates() { |
| 46 DCHECK(CalledOnValidThread()); | 34 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> token( |
| 47 auto token = CreateBatchToken(); | 35 new ReadingListModel::ScopedReadingListBatchUpdate(this)); |
| 48 | 36 |
| 49 ++current_batch_updates_count_; | 37 ++current_batch_updates_count_; |
| 50 if (current_batch_updates_count_ == 1) { | 38 if (current_batch_updates_count_ == 1) { |
| 51 EnteringBatchUpdates(); | 39 for (auto& observer : observers_) |
| 40 observer.ReadingListModelBeganBatchUpdates(this); |
| 52 } | 41 } |
| 53 return token; | 42 return token; |
| 54 } | 43 } |
| 55 | 44 |
| 56 void ReadingListModel::EnteringBatchUpdates() { | |
| 57 DCHECK(CalledOnValidThread()); | |
| 58 for (auto& observer : observers_) | |
| 59 observer.ReadingListModelBeganBatchUpdates(this); | |
| 60 } | |
| 61 | |
| 62 void ReadingListModel::EndBatchUpdates() { | 45 void ReadingListModel::EndBatchUpdates() { |
| 63 DCHECK(CalledOnValidThread()); | |
| 64 DCHECK(IsPerformingBatchUpdates()); | 46 DCHECK(IsPerformingBatchUpdates()); |
| 65 DCHECK(current_batch_updates_count_ > 0); | |
| 66 --current_batch_updates_count_; | 47 --current_batch_updates_count_; |
| 67 if (current_batch_updates_count_ == 0) { | 48 if (current_batch_updates_count_ == 0) { |
| 68 LeavingBatchUpdates(); | 49 for (auto& observer : observers_) |
| 50 observer.ReadingListModelCompletedBatchUpdates(this); |
| 69 } | 51 } |
| 70 } | 52 } |
| 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 |