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