| 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(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> | 
| 33 ReadingListModel::BeginBatchUpdates() { | 40 ReadingListModel::BeginBatchUpdates() { | 
|  | 41   DCHECK(CalledOnValidThread()); | 
| 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(CalledOnValidThread()); | 
|  | 54   for (auto& observer : observers_) | 
|  | 55     observer.ReadingListModelBeganBatchUpdates(this); | 
|  | 56 } | 
|  | 57 | 
| 45 void ReadingListModel::EndBatchUpdates() { | 58 void ReadingListModel::EndBatchUpdates() { | 
|  | 59   DCHECK(CalledOnValidThread()); | 
| 46   DCHECK(IsPerformingBatchUpdates()); | 60   DCHECK(IsPerformingBatchUpdates()); | 
|  | 61   DCHECK(current_batch_updates_count_ > 0); | 
| 47   --current_batch_updates_count_; | 62   --current_batch_updates_count_; | 
| 48   if (current_batch_updates_count_ == 0) { | 63   if (current_batch_updates_count_ == 0) { | 
| 49     for (auto& observer : observers_) | 64     LeavingBatchUpdates(); | 
| 50       observer.ReadingListModelCompletedBatchUpdates(this); |  | 
| 51   } | 65   } | 
| 52 } | 66 } | 
|  | 67 | 
|  | 68 void ReadingListModel::LeavingBatchUpdates() { | 
|  | 69   DCHECK(CalledOnValidThread()); | 
|  | 70   for (auto& observer : observers_) | 
|  | 71     observer.ReadingListModelCompletedBatchUpdates(this); | 
|  | 72 } | 
| OLD | NEW | 
|---|