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_IMPL_H_ | |
6 #define COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 | |
11 #include "components/keyed_service/core/keyed_service.h" | |
12 #include "components/reading_list/ios/reading_list_entry.h" | |
13 #include "components/reading_list/ios/reading_list_model.h" | |
14 #include "components/reading_list/ios/reading_list_model_storage.h" | |
15 #include "components/reading_list/ios/reading_list_store_delegate.h" | |
16 | |
17 namespace base { | |
18 class Clock; | |
19 } | |
20 | |
21 class PrefService; | |
22 | |
23 // Concrete implementation of a reading list model using in memory lists. | |
24 class ReadingListModelImpl : public ReadingListModel, | |
25 public ReadingListStoreDelegate, | |
26 public KeyedService { | |
27 public: | |
28 using ReadingListEntries = std::map<GURL, ReadingListEntry>; | |
29 | |
30 // Initialize a ReadingListModelImpl to load and save data in | |
31 // |storage_layer|. Passing null to |storage_layer| will create a | |
32 // ReadingListModelImpl without persistence. Data will not be persistent | |
33 // across sessions. | |
34 // |clock| will be used to timestamp all the operations. | |
35 ReadingListModelImpl(std::unique_ptr<ReadingListModelStorage> storage_layer, | |
36 PrefService* pref_service, | |
37 std::unique_ptr<base::Clock> clock_); | |
38 | |
39 ReadingListModelImpl(); | |
40 | |
41 syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() override; | |
42 | |
43 ~ReadingListModelImpl() override; | |
44 | |
45 void StoreLoaded(std::unique_ptr<ReadingListEntries> entries) override; | |
46 | |
47 // KeyedService implementation. | |
48 void Shutdown() override; | |
49 | |
50 // ReadingListModel implementation. | |
51 bool loaded() const override; | |
52 | |
53 size_t size() const override; | |
54 size_t unread_size() const override; | |
55 size_t unseen_size() const override; | |
56 | |
57 void MarkAllSeen() override; | |
58 bool GetLocalUnseenFlag() const override; | |
59 void ResetLocalUnseenFlag() override; | |
60 | |
61 const std::vector<GURL> Keys() const override; | |
62 | |
63 const ReadingListEntry* GetEntryByURL(const GURL& gurl) const override; | |
64 const ReadingListEntry* GetFirstUnreadEntry(bool distilled) const override; | |
65 | |
66 void RemoveEntryByURL(const GURL& url) override; | |
67 | |
68 const ReadingListEntry& AddEntry(const GURL& url, | |
69 const std::string& title, | |
70 reading_list::EntrySource source) override; | |
71 | |
72 void SetReadStatus(const GURL& url, bool read) override; | |
73 | |
74 void SetEntryTitle(const GURL& url, const std::string& title) override; | |
75 void SetEntryDistilledState( | |
76 const GURL& url, | |
77 ReadingListEntry::DistillationState state) override; | |
78 void SetEntryDistilledInfo(const GURL& url, | |
79 const base::FilePath& distilled_path, | |
80 const GURL& distilled_url, | |
81 int64_t distillation_size, | |
82 const base::Time& distillation_date) override; | |
83 | |
84 void SyncAddEntry(std::unique_ptr<ReadingListEntry> entry) override; | |
85 ReadingListEntry* SyncMergeEntry( | |
86 std::unique_ptr<ReadingListEntry> entry) override; | |
87 void SyncRemoveEntry(const GURL& url) override; | |
88 bool DeleteAllEntries() override; | |
89 | |
90 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> | |
91 CreateBatchToken() override; | |
92 | |
93 // Helper class that is used to scope batch updates. | |
94 class ScopedReadingListBatchUpdate | |
95 : public ReadingListModel::ScopedReadingListBatchUpdate { | |
96 public: | |
97 explicit ScopedReadingListBatchUpdate(ReadingListModelImpl* model); | |
98 | |
99 ~ScopedReadingListBatchUpdate() override; | |
100 | |
101 private: | |
102 std::unique_ptr<ReadingListModelStorage::ScopedBatchUpdate> storage_token_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate); | |
105 }; | |
106 | |
107 protected: | |
108 void EnteringBatchUpdates() override; | |
109 void LeavingBatchUpdates() override; | |
110 | |
111 private: | |
112 // Sets/Loads the pref flag that indicate if some entries have never been seen | |
113 // since being added to the store. | |
114 void SetPersistentHasUnseen(bool has_unseen); | |
115 bool GetPersistentHasUnseen(); | |
116 | |
117 // Returns a mutable pointer to the entry with URL |url|. Return nullptr if | |
118 // no entry is found. | |
119 ReadingListEntry* GetMutableEntryFromURL(const GURL& url) const; | |
120 | |
121 // Returns the |storage_layer_| of the model. | |
122 ReadingListModelStorage* StorageLayer(); | |
123 | |
124 // Remove entry |url| and propagate to store if |from_sync| is false. | |
125 void RemoveEntryByURLImpl(const GURL& url, bool from_sync); | |
126 | |
127 void RebuildIndex() const; | |
128 | |
129 std::unique_ptr<ReadingListEntries> entries_; | |
130 size_t unread_entry_count_; | |
131 size_t read_entry_count_; | |
132 size_t unseen_entry_count_; | |
133 | |
134 // Update the 3 counts above considering addition/removal of |entry|. | |
135 void UpdateEntryStateCountersOnEntryRemoval(const ReadingListEntry& entry); | |
136 void UpdateEntryStateCountersOnEntryInsertion(const ReadingListEntry& entry); | |
137 | |
138 // Set the unseen flag to true. | |
139 void SetUnseenFlag(); | |
140 | |
141 // |storage_layer_| depends on |clock_| so keep the order. | |
142 std::unique_ptr<base::Clock> clock_; | |
143 std::unique_ptr<ReadingListModelStorage> storage_layer_; | |
144 PrefService* pref_service_; | |
145 bool has_unseen_; | |
146 bool loaded_; | |
147 | |
148 base::WeakPtrFactory<ReadingListModelImpl> weak_ptr_factory_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(ReadingListModelImpl); | |
151 }; | |
152 | |
153 #endif // COMPONENTS_READING_LIST_IOS_READING_LIST_MODEL_IMPL_H_ | |
OLD | NEW |