| 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 #ifndef IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_H_ | 5 #ifndef IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_H_ |
| 6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_H_ | 6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 14 #include "base/threading/non_thread_safe.h" | |
| 15 #include "ios/chrome/browser/reading_list/reading_list_entry.h" | 14 #include "ios/chrome/browser/reading_list/reading_list_entry.h" |
| 16 #include "ios/chrome/browser/reading_list/reading_list_model_observer.h" | 15 #include "ios/chrome/browser/reading_list/reading_list_model_observer.h" |
| 17 | 16 |
| 18 class GURL; | 17 class GURL; |
| 19 class ReadingListEntry; | |
| 20 class ReadingListModel; | 18 class ReadingListModel; |
| 21 class ReadingListStore; | |
| 22 class ScopedReadingListBatchUpdate; | |
| 23 | 19 |
| 24 namespace ios { | 20 namespace ios { |
| 25 class ChromeBrowserState; | 21 class ChromeBrowserState; |
| 26 } | 22 } |
| 27 | 23 |
| 28 namespace syncer { | |
| 29 class ModelTypeSyncBridge; | |
| 30 } | |
| 31 | |
| 32 // The reading list model contains two list of entries: one of unread urls, the | 24 // The reading list model contains two list of entries: one of unread urls, the |
| 33 // other of read ones. This object should only be accessed from one thread | 25 // other of read ones. This object should only be accessed from one thread |
| 34 // (Usually the main thread). The observers callbacks are also sent on the main | 26 // (Usually the main thread). The observers callbacks are also sent on the main |
| 35 // thread. | 27 // thread. |
| 36 class ReadingListModel : public base::NonThreadSafe { | 28 class ReadingListModel { |
| 37 public: | 29 public: |
| 38 class ScopedReadingListBatchUpdate; | 30 class ScopedReadingListBatchUpdate; |
| 39 | |
| 40 // Returns true if the model finished loading. Until this returns true the | 31 // Returns true if the model finished loading. Until this returns true the |
| 41 // reading list is not ready for use. | 32 // reading list is not ready for use. |
| 42 virtual bool loaded() const = 0; | 33 virtual bool loaded() const = 0; |
| 43 | 34 |
| 44 // Returns true if the model is performing batch updates right now. | 35 // Returns true if the model is performing batch updates right now. |
| 45 bool IsPerformingBatchUpdates() const; | 36 bool IsPerformingBatchUpdates() const; |
| 46 | 37 |
| 47 // Returns the ModelTypeSyncBridge responsible for handling sync message. | |
| 48 virtual syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() = 0; | |
| 49 | |
| 50 // Tells model to prepare for batch updates. | 38 // Tells model to prepare for batch updates. |
| 51 // This method is reentrant, i.e. several batch updates may take place at the | 39 // This method is reentrant, i.e. several batch updates may take place at the |
| 52 // same time. | 40 // same time. |
| 53 // Returns a scoped batch update object that should be retained while the | 41 // Returns a scoped batch update object that should be retained while the |
| 54 // batch update is performed. Deallocating this object will inform model that | 42 // batch update is performed. Deallocating this object will inform model that |
| 55 // the batch update has completed. | 43 // the batch update has completed. |
| 56 std::unique_ptr<ScopedReadingListBatchUpdate> BeginBatchUpdates(); | 44 std::unique_ptr<ScopedReadingListBatchUpdate> BeginBatchUpdates(); |
| 57 | 45 |
| 58 // Creates a batch token that will freeze the model while in scope. | |
| 59 virtual std::unique_ptr<ScopedReadingListBatchUpdate> CreateBatchToken(); | |
| 60 | |
| 61 // Returns the size of read and unread entries. | 46 // Returns the size of read and unread entries. |
| 62 virtual size_t unread_size() const = 0; | 47 virtual size_t unread_size() const = 0; |
| 63 virtual size_t read_size() const = 0; | 48 virtual size_t read_size() const = 0; |
| 64 | 49 |
| 65 // Returns true if there are entries in the model that were not seen by the | 50 // Returns true if there are entries in the model that were not seen by the |
| 66 // user yet. Reset to true when new unread entries are added. Reset to false | 51 // user yet. Reset to true when new unread entries are added. Reset to false |
| 67 // when ResetUnseenEntries is called. | 52 // when ResetUnseenEntries is called. |
| 68 virtual bool HasUnseenEntries() const = 0; | 53 virtual bool HasUnseenEntries() const = 0; |
| 69 virtual void ResetUnseenEntries() = 0; | 54 virtual void ResetUnseenEntries() = 0; |
| 70 | 55 |
| 71 // TODO(659099): Remove methods. | 56 // TODO(659099): Remove methods. |
| 72 // Returns a specific entry. | 57 // Returns a specific entry. |
| 73 virtual const ReadingListEntry& GetUnreadEntryAtIndex(size_t index) const = 0; | 58 virtual const ReadingListEntry& GetUnreadEntryAtIndex(size_t index) const = 0; |
| 74 virtual const ReadingListEntry& GetReadEntryAtIndex(size_t index) const = 0; | 59 virtual const ReadingListEntry& GetReadEntryAtIndex(size_t index) const = 0; |
| 75 | 60 |
| 76 // Returns a specific entry. Returns null if the entry does not exist. | 61 // Returns a specific entry. Returns null if the entry does not exist. |
| 77 // If |read| is not null and the entry is found, |*read| is the read status | 62 virtual const ReadingListEntry* GetEntryFromURL(const GURL& gurl) const = 0; |
| 78 // of the entry. | |
| 79 virtual const ReadingListEntry* GetEntryFromURL(const GURL& gurl, | |
| 80 bool* read) const = 0; | |
| 81 | 63 |
| 82 // Synchronously calls the |callback| with entry associated with this |url|. | 64 // Synchronously calls the |callback| with entry associated with this |url|. |
| 83 // Does nothing if there is no entry associated. | 65 // Does nothing if there is no entry associated. |
| 84 // Returns whether the callback has been called. | 66 // Returns whether the callback has been called. |
| 85 virtual bool CallbackEntryURL( | 67 virtual bool CallbackEntryURL( |
| 86 const GURL& url, | 68 const GURL& url, |
| 87 base::Callback<void(const ReadingListEntry&)> callback) const = 0; | 69 base::Callback<void(const ReadingListEntry&)> callback) const = 0; |
| 88 | 70 |
| 89 // Adds |url| at the top of the unread entries, and removes entries with the | 71 // Adds |url| at the top of the unread entries, and removes entries with the |
| 90 // same |url| from everywhere else if they exist. The entry title will be a | 72 // same |url| from everywhere else if they exist. The entry title will be a |
| (...skipping 29 matching lines...) Expand all Loading... |
| 120 // destruction automatically. | 102 // destruction automatically. |
| 121 void AddObserver(ReadingListModelObserver* observer); | 103 void AddObserver(ReadingListModelObserver* observer); |
| 122 void RemoveObserver(ReadingListModelObserver* observer); | 104 void RemoveObserver(ReadingListModelObserver* observer); |
| 123 | 105 |
| 124 // Helper class that is used to scope batch updates. | 106 // Helper class that is used to scope batch updates. |
| 125 class ScopedReadingListBatchUpdate { | 107 class ScopedReadingListBatchUpdate { |
| 126 public: | 108 public: |
| 127 explicit ScopedReadingListBatchUpdate(ReadingListModel* model) | 109 explicit ScopedReadingListBatchUpdate(ReadingListModel* model) |
| 128 : model_(model) {} | 110 : model_(model) {} |
| 129 | 111 |
| 130 virtual ~ScopedReadingListBatchUpdate(); | 112 ~ScopedReadingListBatchUpdate() { model_->EndBatchUpdates(); } |
| 131 | 113 |
| 132 private: | 114 private: |
| 133 ReadingListModel* model_; | 115 ReadingListModel* model_; |
| 134 | 116 |
| 135 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate); | 117 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate); |
| 136 }; | 118 }; |
| 137 | 119 |
| 138 protected: | 120 protected: |
| 139 ReadingListModel(); | 121 ReadingListModel(); |
| 140 virtual ~ReadingListModel(); | 122 virtual ~ReadingListModel(); |
| 141 | 123 |
| 142 // The observers. | 124 // The observers. |
| 143 base::ObserverList<ReadingListModelObserver> observers_; | 125 base::ObserverList<ReadingListModelObserver> observers_; |
| 144 | 126 |
| 145 // Tells model that batch updates have completed. Called from | 127 // Tells model that batch updates have completed. Called from |
| 146 // ReadingListBatchUpdateToken dtor. | 128 // ReadingListBatchUpdateToken dtor. |
| 147 virtual void EndBatchUpdates(); | 129 virtual void EndBatchUpdates(); |
| 148 | 130 |
| 149 // Called when model is entering batch update mode. | |
| 150 virtual void EnteringBatchUpdates(); | |
| 151 | |
| 152 // Called when model is leaving batch update mode. | |
| 153 virtual void LeavingBatchUpdates(); | |
| 154 | |
| 155 private: | 131 private: |
| 156 unsigned int current_batch_updates_count_; | 132 unsigned int current_batch_updates_count_; |
| 157 | 133 |
| 158 DISALLOW_COPY_AND_ASSIGN(ReadingListModel); | 134 DISALLOW_COPY_AND_ASSIGN(ReadingListModel); |
| 159 }; | 135 }; |
| 160 | 136 |
| 161 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_H_ | 137 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_H_ |
| OLD | NEW |