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