| 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 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} | 7 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} |
| 8 |
| 8 ReadingListModel::~ReadingListModel() {} | 9 ReadingListModel::~ReadingListModel() {} |
| 9 | 10 |
| 10 // Observer methods. | 11 // Observer methods. |
| 11 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { | 12 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { |
| 12 DCHECK(observer); | 13 DCHECK(observer); |
| 13 observers_.AddObserver(observer); | 14 observers_.AddObserver(observer); |
| 14 if (loaded()) { | 15 if (loaded()) { |
| 15 observer->ReadingListModelLoaded(this); | 16 observer->ReadingListModelLoaded(this); |
| 16 } | 17 } |
| 17 } | 18 } |
| 18 | 19 |
| 19 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { | 20 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { |
| 20 observers_.RemoveObserver(observer); | 21 observers_.RemoveObserver(observer); |
| 21 } | 22 } |
| 22 | 23 |
| 23 // Batch update methods. | 24 // Batch update methods. |
| 24 bool ReadingListModel::IsPerformingBatchUpdates() const { | 25 bool ReadingListModel::IsPerformingBatchUpdates() const { |
| 25 return current_batch_updates_count_ > 0; | 26 return current_batch_updates_count_ > 0; |
| 26 } | 27 } |
| 27 | 28 |
| 28 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | 29 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
| 29 ReadingListModel::BeginBatchUpdates() { | 30 ReadingListModel::BeginBatchUpdates() { |
| 30 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> token( | 31 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> token( |
| 31 new ReadingListModel::ScopedReadingListBatchUpdate(this)); | 32 new ReadingListModel::ScopedReadingListBatchUpdate(this)); |
| 32 | 33 |
| 33 ++current_batch_updates_count_; | 34 ++current_batch_updates_count_; |
| 34 if (current_batch_updates_count_ == 1) { | 35 if (current_batch_updates_count_ == 1) { |
| 35 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, | 36 EnteringBatchUpdates(); |
| 36 ReadingListModelBeganBatchUpdates(this)); | |
| 37 } | 37 } |
| 38 return token; | 38 return token; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void ReadingListModel::EnteringBatchUpdates() { |
| 42 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, |
| 43 ReadingListModelBeganBatchUpdates(this)); |
| 44 } |
| 45 |
| 41 void ReadingListModel::EndBatchUpdates() { | 46 void ReadingListModel::EndBatchUpdates() { |
| 42 DCHECK(IsPerformingBatchUpdates()); | 47 DCHECK(IsPerformingBatchUpdates()); |
| 43 --current_batch_updates_count_; | 48 --current_batch_updates_count_; |
| 44 if (current_batch_updates_count_ == 0) { | 49 if (current_batch_updates_count_ == 0) { |
| 45 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, | 50 LeavingBatchUpdates(); |
| 46 ReadingListModelCompletedBatchUpdates(this)); | |
| 47 } | 51 } |
| 48 } | 52 } |
| 53 |
| 54 void ReadingListModel::LeavingBatchUpdates() { |
| 55 FOR_EACH_OBSERVER(ReadingListModelObserver, observers_, |
| 56 ReadingListModelCompletedBatchUpdates(this)); |
| 57 } |
| OLD | NEW |