Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(720)

Side by Side Diff: components/reading_list/ios/reading_list_model.h

Issue 2525663002: Refactor Reading List Model to use URL as key. (Closed)
Patch Set: fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_H_ 5 #ifndef COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_H_
6 #define COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_H_ 6 #define COMPONENTS_READING_LIST_IOS_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 "base/threading/non_thread_safe.h"
15 #include "components/reading_list/ios/reading_list_entry.h" 15 #include "components/reading_list/ios/reading_list_entry.h"
16 #include "components/reading_list/ios/reading_list_model_observer.h" 16 #include "components/reading_list/ios/reading_list_model_observer.h"
17 17
18 class GURL; 18 class GURL;
19 class ReadingListEntry;
20 class ReadingListModel; 19 class ReadingListModel;
21 class ScopedReadingListBatchUpdate; 20 class ScopedReadingListBatchUpdate;
22 21
23 namespace syncer { 22 namespace syncer {
24 class ModelTypeSyncBridge; 23 class ModelTypeSyncBridge;
25 } 24 }
26 25
27 // The reading list model contains two list of entries: one of unread urls, the 26 // 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 27 // 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 28 // (Usually the main thread). The observers callbacks are also sent on the main
(...skipping 16 matching lines...) Expand all
46 // This method is reentrant, i.e. several batch updates may take place at the 45 // This method is reentrant, i.e. several batch updates may take place at the
47 // same time. 46 // same time.
48 // Returns a scoped batch update object that should be retained while the 47 // Returns a scoped batch update object that should be retained while the
49 // batch update is performed. Deallocating this object will inform model that 48 // batch update is performed. Deallocating this object will inform model that
50 // the batch update has completed. 49 // the batch update has completed.
51 std::unique_ptr<ScopedReadingListBatchUpdate> BeginBatchUpdates(); 50 std::unique_ptr<ScopedReadingListBatchUpdate> BeginBatchUpdates();
52 51
53 // Creates a batch token that will freeze the model while in scope. 52 // Creates a batch token that will freeze the model while in scope.
54 virtual std::unique_ptr<ScopedReadingListBatchUpdate> CreateBatchToken(); 53 virtual std::unique_ptr<ScopedReadingListBatchUpdate> CreateBatchToken();
55 54
56 // Returns the size of read and unread entries. 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.
57 virtual size_t unread_size() const = 0; 63 virtual size_t unread_size() const = 0;
58 virtual size_t read_size() const = 0;
59 64
60 // 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
61 // 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
62 // when ResetUnseenEntries is called. 67 // when ResetUnseenEntries is called.
63 virtual bool HasUnseenEntries() const = 0; 68 virtual bool HasUnseenEntries() const = 0;
64 virtual void ResetUnseenEntries() = 0; 69 virtual void ResetUnseenEntries() = 0;
65 70
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. 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 72 virtual const ReadingListEntry* GetEntryByURL(const GURL& gurl) const = 0;
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 73
84 // Adds |url| at the top of the unread entries, and removes entries with the 74 // 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 75 // same |url| from everywhere else if they exist. The entry title will be a
86 // trimmed copy of |title. 76 // trimmed copy of |title.
87 // The addition may be asynchronous, and the data will be available only once 77 // The addition may be asynchronous, and the data will be available only once
88 // the observers are notified. 78 // the observers are notified.
89 virtual const ReadingListEntry& AddEntry(const GURL& url, 79 virtual const ReadingListEntry& AddEntry(const GURL& url,
90 const std::string& title) = 0; 80 const std::string& title) = 0;
91 81
92 // Removes an entry. The removal may be asynchronous, and not happen 82 // Removes an entry. The removal may be asynchronous, and not happen
93 // immediately. 83 // immediately.
94 virtual void RemoveEntryByURL(const GURL& url) = 0; 84 virtual void RemoveEntryByURL(const GURL& url) = 0;
95 85
96 // If the |url| is in the reading list and unread, mark it read. If it is in 86 // If the |url| is in the reading list and entry(|url|).read != |read|, sets
97 // the reading list and read, move it to the top of unread if it is not here 87 // the read state of the URL to read. This will also update the update time of
98 // already. This may trigger deletion of old read entries. 88 // the entry.
99 virtual void MarkReadByURL(const GURL& url) = 0; 89 virtual void SetReadStatus(const GURL& url, bool read) = 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 90
105 // Methods to mutate an entry. Will locate the relevant entry by URL. Does 91 // Methods to mutate an entry. Will locate the relevant entry by URL. Does
106 // nothing if the entry is not found. 92 // nothing if the entry is not found.
107 virtual void SetEntryTitle(const GURL& url, const std::string& title) = 0; 93 virtual void SetEntryTitle(const GURL& url, const std::string& title) = 0;
108 virtual void SetEntryDistilledPath(const GURL& url, 94 virtual void SetEntryDistilledPath(const GURL& url,
109 const base::FilePath& distilled_path) = 0; 95 const base::FilePath& distilled_path) = 0;
110 virtual void SetEntryDistilledState( 96 virtual void SetEntryDistilledState(
111 const GURL& url, 97 const GURL& url,
112 ReadingListEntry::DistillationState state) = 0; 98 ReadingListEntry::DistillationState state) = 0;
113 99
114 // Observer registration methods. The model will remove all observers upon 100 // Observer registration methods. The model will remove all observers upon
115 // destruction automatically. 101 // destruction automatically.
116 void AddObserver(ReadingListModelObserver* observer); 102 void AddObserver(ReadingListModelObserver* observer);
117 void RemoveObserver(ReadingListModelObserver* observer); 103 void RemoveObserver(ReadingListModelObserver* observer);
118 104
119 // Helper class that is used to scope batch updates. 105 // Helper class that is used to scope batch updates.
120 class ScopedReadingListBatchUpdate { 106 class ScopedReadingListBatchUpdate {
121 public: 107 public:
122 explicit ScopedReadingListBatchUpdate(ReadingListModel* model) 108 explicit ScopedReadingListBatchUpdate(ReadingListModel* model)
123 : model_(model) {} 109 : model_(model) {}
124 110
125 virtual ~ScopedReadingListBatchUpdate(); 111 virtual ~ScopedReadingListBatchUpdate();
126 112
127 private: 113 private:
128 ReadingListModel* model_; 114 ReadingListModel* model_;
129 115
130 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate); 116 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate);
131 }; 117 };
132 118
119 // TODO(crbug.com/664924): Remove temporary methods for transition.
120
121 // Allows iterating through read entries in the model. Must be called on a
122 // singlerunloop to ensure no entry is returned twice and all entries are
123 // returned
124 virtual const ReadingListEntry& GetReadEntryAtIndex(size_t index) const = 0;
125 virtual const ReadingListEntry& GetUnreadEntryAtIndex(size_t index) const = 0;
126 virtual void MarkReadByURL(const GURL& url) = 0;
127 virtual void MarkUnreadByURL(const GURL& url) = 0;
128 virtual size_t read_size() const = 0;
129
133 protected: 130 protected:
134 ReadingListModel(); 131 ReadingListModel();
135 virtual ~ReadingListModel(); 132 virtual ~ReadingListModel();
136 133
137 // The observers. 134 // The observers.
138 base::ObserverList<ReadingListModelObserver> observers_; 135 base::ObserverList<ReadingListModelObserver> observers_;
139 136
140 // Tells model that batch updates have completed. Called from 137 // Tells model that batch updates have completed. Called from
141 // ReadingListBatchUpdateToken dtor. 138 // ReadingListBatchUpdateToken dtor.
142 virtual void EndBatchUpdates(); 139 virtual void EndBatchUpdates();
143 140
144 // Called when model is entering batch update mode. 141 // Called when model is entering batch update mode.
145 virtual void EnteringBatchUpdates(); 142 virtual void EnteringBatchUpdates();
146 143
147 // Called when model is leaving batch update mode. 144 // Called when model is leaving batch update mode.
148 virtual void LeavingBatchUpdates(); 145 virtual void LeavingBatchUpdates();
149 146
150 private: 147 private:
151 unsigned int current_batch_updates_count_; 148 unsigned int current_batch_updates_count_;
152 149
153 DISALLOW_COPY_AND_ASSIGN(ReadingListModel); 150 DISALLOW_COPY_AND_ASSIGN(ReadingListModel);
154 }; 151 };
155 152
156 #endif // COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_H_ 153 #endif // COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698