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 #include "ios/web/public/web_thread.h" | |
pavely
2016/11/14 07:45:33
web_tread.h is not used.
Olivier
2016/11/14 11:36:48
Done.
| |
10 | |
7 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} | 11 ReadingListModel::ReadingListModel() : current_batch_updates_count_(0) {} |
12 | |
8 ReadingListModel::~ReadingListModel() { | 13 ReadingListModel::~ReadingListModel() { |
9 for (auto& observer : observers_) { | 14 for (auto& observer : observers_) { |
10 observer.ReadingListModelBeingDeleted(this); | 15 observer.ReadingListModelBeingDeleted(this); |
11 } | 16 } |
12 } | 17 } |
13 | 18 |
14 // Observer methods. | 19 // Observer methods. |
15 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { | 20 void ReadingListModel::AddObserver(ReadingListModelObserver* observer) { |
21 DCHECK(CalledOnValidThread()); | |
16 DCHECK(observer); | 22 DCHECK(observer); |
17 observers_.AddObserver(observer); | 23 observers_.AddObserver(observer); |
18 if (loaded()) { | 24 if (loaded()) { |
19 observer->ReadingListModelLoaded(this); | 25 observer->ReadingListModelLoaded(this); |
20 } | 26 } |
21 } | 27 } |
22 | 28 |
23 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { | 29 void ReadingListModel::RemoveObserver(ReadingListModelObserver* observer) { |
30 DCHECK(CalledOnValidThread()); | |
24 observers_.RemoveObserver(observer); | 31 observers_.RemoveObserver(observer); |
25 } | 32 } |
26 | 33 |
27 // Batch update methods. | 34 // Batch update methods. |
28 bool ReadingListModel::IsPerformingBatchUpdates() const { | 35 bool ReadingListModel::IsPerformingBatchUpdates() const { |
36 DCHECK(CalledOnValidThread()); | |
29 return current_batch_updates_count_ > 0; | 37 return current_batch_updates_count_ > 0; |
30 } | 38 } |
31 | 39 |
32 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | 40 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
41 ReadingListModel::CreateBatchToken() { | |
42 return base::MakeUnique<ReadingListModel::ScopedReadingListBatchUpdate>(this); | |
43 } | |
44 | |
45 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | |
33 ReadingListModel::BeginBatchUpdates() { | 46 ReadingListModel::BeginBatchUpdates() { |
34 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> token( | 47 DCHECK(CalledOnValidThread()); |
35 new ReadingListModel::ScopedReadingListBatchUpdate(this)); | 48 auto token = CreateBatchToken(); |
36 | 49 |
37 ++current_batch_updates_count_; | 50 ++current_batch_updates_count_; |
38 if (current_batch_updates_count_ == 1) { | 51 if (current_batch_updates_count_ == 1) { |
39 for (auto& observer : observers_) | 52 EnteringBatchUpdates(); |
40 observer.ReadingListModelBeganBatchUpdates(this); | |
41 } | 53 } |
42 return token; | 54 return token; |
43 } | 55 } |
44 | 56 |
57 void ReadingListModel::EnteringBatchUpdates() { | |
58 DCHECK(CalledOnValidThread()); | |
59 for (auto& observer : observers_) | |
60 observer.ReadingListModelBeganBatchUpdates(this); | |
61 } | |
62 | |
45 void ReadingListModel::EndBatchUpdates() { | 63 void ReadingListModel::EndBatchUpdates() { |
64 DCHECK(CalledOnValidThread()); | |
46 DCHECK(IsPerformingBatchUpdates()); | 65 DCHECK(IsPerformingBatchUpdates()); |
66 DCHECK(current_batch_updates_count_ > 0); | |
47 --current_batch_updates_count_; | 67 --current_batch_updates_count_; |
48 if (current_batch_updates_count_ == 0) { | 68 if (current_batch_updates_count_ == 0) { |
49 for (auto& observer : observers_) | 69 LeavingBatchUpdates(); |
50 observer.ReadingListModelCompletedBatchUpdates(this); | |
51 } | 70 } |
52 } | 71 } |
72 | |
73 void ReadingListModel::LeavingBatchUpdates() { | |
74 DCHECK(CalledOnValidThread()); | |
75 for (auto& observer : observers_) | |
76 observer.ReadingListModelCompletedBatchUpdates(this); | |
77 } | |
78 | |
79 ReadingListModel::ScopedReadingListBatchUpdate:: | |
80 ~ScopedReadingListBatchUpdate() { | |
81 model_->EndBatchUpdates(); | |
82 } | |
OLD | NEW |