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