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 "ios/web/public/web_thread.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_CURRENTLY_ON(web::WebThread::UI); |
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_CURRENTLY_ON(web::WebThread::UI); |
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_CURRENTLY_ON(web::WebThread::UI); |
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> |
33 ReadingListModel::BeginBatchUpdates() { | 40 ReadingListModel::BeginBatchUpdates() { |
| 41 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
34 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> token( | 42 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> token( |
35 new ReadingListModel::ScopedReadingListBatchUpdate(this)); | 43 new ReadingListModel::ScopedReadingListBatchUpdate(this)); |
36 | 44 |
37 ++current_batch_updates_count_; | 45 ++current_batch_updates_count_; |
38 if (current_batch_updates_count_ == 1) { | 46 if (current_batch_updates_count_ == 1) { |
39 for (auto& observer : observers_) | 47 EnteringBatchUpdates(); |
40 observer.ReadingListModelBeganBatchUpdates(this); | |
41 } | 48 } |
42 return token; | 49 return token; |
43 } | 50 } |
44 | 51 |
| 52 void ReadingListModel::EnteringBatchUpdates() { |
| 53 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 54 for (auto& observer : observers_) |
| 55 observer.ReadingListModelBeganBatchUpdates(this); |
| 56 } |
| 57 |
45 void ReadingListModel::EndBatchUpdates() { | 58 void ReadingListModel::EndBatchUpdates() { |
| 59 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
46 DCHECK(IsPerformingBatchUpdates()); | 60 DCHECK(IsPerformingBatchUpdates()); |
47 --current_batch_updates_count_; | 61 --current_batch_updates_count_; |
48 if (current_batch_updates_count_ == 0) { | 62 if (current_batch_updates_count_ == 0) { |
49 for (auto& observer : observers_) | 63 LeavingBatchUpdates(); |
50 observer.ReadingListModelCompletedBatchUpdates(this); | |
51 } | 64 } |
52 } | 65 } |
| 66 |
| 67 void ReadingListModel::LeavingBatchUpdates() { |
| 68 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 69 for (auto& observer : observers_) |
| 70 observer.ReadingListModelCompletedBatchUpdates(this); |
| 71 } |
OLD | NEW |