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