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_IOS_READING_LIST_MODEL_H_ | |
6 #define COMPONENTS_READING_LIST_IOS_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/ios/reading_list_entry.h" | |
16 #include "components/reading_list/ios/reading_list_model_observer.h" | |
17 | |
18 class GURL; | |
19 class ReadingListModel; | |
20 class ScopedReadingListBatchUpdate; | |
21 | |
22 namespace syncer { | |
23 class ModelTypeSyncBridge; | |
24 } | |
25 | |
26 // The reading list model contains two list of entries: one of unread urls, the | |
27 // other of read ones. This object should only be accessed from one thread | |
28 // (Usually the main thread). The observers callbacks are also sent on the main | |
29 // thread. | |
30 class ReadingListModel : public base::NonThreadSafe { | |
31 public: | |
32 class ScopedReadingListBatchUpdate; | |
33 | |
34 // Returns true if the model finished loading. Until this returns true the | |
35 // reading list is not ready for use. | |
36 virtual bool loaded() const = 0; | |
37 | |
38 // Returns true if the model is performing batch updates right now. | |
39 bool IsPerformingBatchUpdates() const; | |
40 | |
41 // Returns the ModelTypeSyncBridge responsible for handling sync message. | |
42 virtual syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() = 0; | |
43 | |
44 // Tells model to prepare for batch updates. | |
45 // This method is reentrant, i.e. several batch updates may take place at the | |
46 // same time. | |
47 // Returns a scoped batch update object that should be retained while the | |
48 // batch update is performed. Deallocating this object will inform model that | |
49 // the batch update has completed. | |
50 std::unique_ptr<ScopedReadingListBatchUpdate> BeginBatchUpdates(); | |
51 | |
52 // Creates a batch token that will freeze the model while in scope. | |
53 virtual std::unique_ptr<ScopedReadingListBatchUpdate> CreateBatchToken(); | |
54 | |
55 // Returns a vector of URLs in the model. The order of the URL is not | |
56 // specified and can vary on successive calls. | |
57 virtual const std::vector<GURL> Keys() const = 0; | |
58 | |
59 // Returns the total number of entries in the model. | |
60 virtual size_t size() const = 0; | |
61 | |
62 // Returns the total number of unread entries in the model. | |
63 virtual size_t unread_size() const = 0; | |
64 | |
65 // Returns the total number of unseen entries in the model. Note: These | |
66 // entries are also unread so unseen_size() <= unread_size(). | |
67 virtual size_t unseen_size() const = 0; | |
68 | |
69 // Mark all unseen entries as unread. | |
70 virtual void MarkAllSeen() = 0; | |
71 | |
72 // Delete all the Reading List entries. Return true if entries where indeed | |
73 // deleted. | |
74 virtual bool DeleteAllEntries() = 0; | |
75 | |
76 // Returns the flag about unseen entries on the device. | |
77 // This flag is raised if some unseen items are added on this device. | |
78 // The flag is reset if |ResetLocalUnseenFlag| is called or if all unseen | |
79 // entries are removed. | |
80 // This is a local flag and it can have different values on different devices, | |
81 // even if they are synced. | |
82 // (unseen_size() == 0 => GetLocalUnseenFlag() == false) | |
83 virtual bool GetLocalUnseenFlag() const = 0; | |
84 | |
85 // Set the unseen flag to false. | |
86 virtual void ResetLocalUnseenFlag() = 0; | |
87 | |
88 // Returns a specific entry. Returns null if the entry does not exist. | |
89 virtual const ReadingListEntry* GetEntryByURL(const GURL& gurl) const = 0; | |
90 | |
91 // Returns the first unread entry. If |distilled| is true, prioritize the | |
92 // entries available offline. | |
93 virtual const ReadingListEntry* GetFirstUnreadEntry(bool distilled) const = 0; | |
94 | |
95 // Adds |url| at the top of the unread entries, and removes entries with the | |
96 // same |url| from everywhere else if they exist. The entry title will be a | |
97 // trimmed copy of |title|. | |
98 // The addition may be asynchronous, and the data will be available only once | |
99 // the observers are notified. | |
100 virtual const ReadingListEntry& AddEntry( | |
101 const GURL& url, | |
102 const std::string& title, | |
103 reading_list::EntrySource source) = 0; | |
104 | |
105 // Removes an entry. The removal may be asynchronous, and not happen | |
106 // immediately. | |
107 virtual void RemoveEntryByURL(const GURL& url) = 0; | |
108 | |
109 // If the |url| is in the reading list and entry(|url|).read != |read|, sets | |
110 // the read state of the URL to read. This will also update the update time of | |
111 // the entry. | |
112 virtual void SetReadStatus(const GURL& url, bool read) = 0; | |
113 | |
114 // Methods to mutate an entry. Will locate the relevant entry by URL. Does | |
115 // nothing if the entry is not found. | |
116 virtual void SetEntryTitle(const GURL& url, const std::string& title) = 0; | |
117 virtual void SetEntryDistilledState( | |
118 const GURL& url, | |
119 ReadingListEntry::DistillationState state) = 0; | |
120 | |
121 // Sets the Distilled info for the entry |url|. This method sets the | |
122 // DistillationState of the entry to PROCESSED and sets the |distilled_path| | |
123 // (path of the file on disk), the |distilled_url| (url of the page that | |
124 // was distilled, the |distillation_size| (the size of the offline data) and | |
125 // the |distillation_date| (date of distillation in microseconds since Jan 1st | |
126 // 1970. | |
127 virtual void SetEntryDistilledInfo(const GURL& url, | |
128 const base::FilePath& distilled_path, | |
129 const GURL& distilled_url, | |
130 int64_t distilation_size, | |
131 const base::Time& distilation_time) = 0; | |
132 | |
133 // Observer registration methods. The model will remove all observers upon | |
134 // destruction automatically. | |
135 void AddObserver(ReadingListModelObserver* observer); | |
136 void RemoveObserver(ReadingListModelObserver* observer); | |
137 | |
138 // Helper class that is used to scope batch updates. | |
139 class ScopedReadingListBatchUpdate { | |
140 public: | |
141 explicit ScopedReadingListBatchUpdate(ReadingListModel* model) | |
142 : model_(model) {} | |
143 | |
144 virtual ~ScopedReadingListBatchUpdate(); | |
145 | |
146 private: | |
147 ReadingListModel* model_; | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate); | |
150 }; | |
151 | |
152 protected: | |
153 ReadingListModel(); | |
154 virtual ~ReadingListModel(); | |
155 | |
156 // The observers. | |
157 base::ObserverList<ReadingListModelObserver> observers_; | |
158 | |
159 // Tells model that batch updates have completed. Called from | |
160 // ReadingListBatchUpdateToken dtor. | |
161 virtual void EndBatchUpdates(); | |
162 | |
163 // Called when model is entering batch update mode. | |
164 virtual void EnteringBatchUpdates(); | |
165 | |
166 // Called when model is leaving batch update mode. | |
167 virtual void LeavingBatchUpdates(); | |
168 | |
169 private: | |
170 unsigned int current_batch_updates_count_; | |
171 | |
172 DISALLOW_COPY_AND_ASSIGN(ReadingListModel); | |
173 }; | |
174 | |
175 #endif // COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_H_ | |
OLD | NEW |