| 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_IMPL_H_ | 5 #ifndef IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_IMPL_H_ |
| 6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_IMPL_H_ | 6 #define IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_IMPL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "components/keyed_service/core/keyed_service.h" | 10 #include "components/keyed_service/core/keyed_service.h" |
| 11 #include "ios/chrome/browser/reading_list/reading_list_entry.h" | 11 #include "ios/chrome/browser/reading_list/reading_list_entry.h" |
| 12 #include "ios/chrome/browser/reading_list/reading_list_model.h" | 12 #include "ios/chrome/browser/reading_list/reading_list_model.h" |
| 13 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" |
| 14 #include "ios/chrome/browser/reading_list/reading_list_store_delegate.h" |
| 13 | 15 |
| 14 class ReadingListModelStorage; | |
| 15 class PrefService; | 16 class PrefService; |
| 16 | 17 |
| 17 // Concrete implementation of a reading list model using in memory lists. | 18 // Concrete implementation of a reading list model using in memory lists. |
| 18 class ReadingListModelImpl : public ReadingListModel, public KeyedService { | 19 class ReadingListModelImpl : public ReadingListModel, |
| 20 public ReadingListStoreDelegate, |
| 21 public KeyedService { |
| 19 public: | 22 public: |
| 20 // Initialize a ReadingListModelImpl to load and save data in | 23 // Initialize a ReadingListModelImpl to load and save data in |
| 21 // |persistence_layer|. | 24 // |persistence_layer|. |
| 22 ReadingListModelImpl(std::unique_ptr<ReadingListModelStorage> storage, | 25 ReadingListModelImpl(std::unique_ptr<ReadingListModelStorage> storage_layer, |
| 23 PrefService* pref_service); | 26 PrefService* pref_service); |
| 24 | 27 |
| 25 // Initialize a ReadingListModelImpl without persistence. Data will not be | 28 // Initialize a ReadingListModelImpl without persistence. Data will not be |
| 26 // persistent across sessions. | 29 // persistent across sessions. |
| 27 ReadingListModelImpl(); | 30 ReadingListModelImpl(); |
| 28 | 31 |
| 32 syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() override; |
| 33 |
| 29 ~ReadingListModelImpl() override; | 34 ~ReadingListModelImpl() override; |
| 30 | 35 |
| 36 void StoreLoaded(std::unique_ptr<ReadingListEntries> unread, |
| 37 std::unique_ptr<ReadingListEntries> read) override; |
| 38 |
| 31 // KeyedService implementation. | 39 // KeyedService implementation. |
| 32 void Shutdown() override; | 40 void Shutdown() override; |
| 33 | 41 |
| 34 // ReadingListModel implementation. | 42 // ReadingListModel implementation. |
| 35 bool loaded() const override; | 43 bool loaded() const override; |
| 36 | 44 |
| 37 size_t unread_size() const override; | 45 size_t unread_size() const override; |
| 38 size_t read_size() const override; | 46 size_t read_size() const override; |
| 39 | 47 |
| 40 bool HasUnseenEntries() const override; | 48 bool HasUnseenEntries() const override; |
| 41 void ResetUnseenEntries() override; | 49 void ResetUnseenEntries() override; |
| 42 | 50 |
| 43 const ReadingListEntry& GetUnreadEntryAtIndex(size_t index) const override; | 51 const ReadingListEntry& GetUnreadEntryAtIndex(size_t index) const override; |
| 44 const ReadingListEntry& GetReadEntryAtIndex(size_t index) const override; | 52 const ReadingListEntry& GetReadEntryAtIndex(size_t index) const override; |
| 45 | 53 |
| 46 const ReadingListEntry* GetEntryFromURL(const GURL& gurl) const override; | 54 const ReadingListEntry* GetEntryFromURL(const GURL& gurl, |
| 55 bool* read) const override; |
| 47 | 56 |
| 48 bool CallbackEntryURL( | 57 bool CallbackEntryURL( |
| 49 const GURL& url, | 58 const GURL& url, |
| 50 base::Callback<void(const ReadingListEntry&)> callback) const override; | 59 base::Callback<void(const ReadingListEntry&)> callback) const override; |
| 51 | 60 |
| 52 void RemoveEntryByURL(const GURL& url) override; | 61 void RemoveEntryByURL(const GURL& url) override; |
| 53 | 62 |
| 54 const ReadingListEntry& AddEntry(const GURL& url, | 63 const ReadingListEntry& AddEntry(const GURL& url, |
| 55 const std::string& title) override; | 64 const std::string& title) override; |
| 56 | 65 |
| 57 void MarkReadByURL(const GURL& url) override; | 66 void MarkReadByURL(const GURL& url) override; |
| 58 void MarkUnreadByURL(const GURL& url) override; | 67 void MarkUnreadByURL(const GURL& url) override; |
| 59 | 68 |
| 60 void SetEntryTitle(const GURL& url, const std::string& title) override; | 69 void SetEntryTitle(const GURL& url, const std::string& title) override; |
| 61 void SetEntryDistilledURL(const GURL& url, | 70 void SetEntryDistilledURL(const GURL& url, |
| 62 const GURL& distilled_url) override; | 71 const GURL& distilled_url) override; |
| 63 void SetEntryDistilledState( | 72 void SetEntryDistilledState( |
| 64 const GURL& url, | 73 const GURL& url, |
| 65 ReadingListEntry::DistillationState state) override; | 74 ReadingListEntry::DistillationState state) override; |
| 66 | 75 |
| 76 void SyncAddEntry(std::unique_ptr<ReadingListEntry> entry, |
| 77 bool read) override; |
| 78 void SyncRemoveEntry(const GURL& gurl) override; |
| 79 |
| 80 std::unique_ptr<ReadingListModel::ScopedReadingListBatchUpdate> |
| 81 CreateBatchToken() override; |
| 82 |
| 83 // Helper class that is used to scope batch updates. |
| 84 class ScopedReadingListBatchUpdate |
| 85 : public ReadingListModel::ScopedReadingListBatchUpdate { |
| 86 public: |
| 87 explicit ScopedReadingListBatchUpdate(ReadingListModelImpl* model); |
| 88 |
| 89 ~ScopedReadingListBatchUpdate() override; |
| 90 |
| 91 private: |
| 92 std::unique_ptr<ReadingListModelStorage::ScopedBatchUpdate> storage_token_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(ScopedReadingListBatchUpdate); |
| 95 }; |
| 96 |
| 67 protected: | 97 protected: |
| 68 void EndBatchUpdates() override; | 98 void EnteringBatchUpdates() override; |
| 99 void LeavingBatchUpdates() override; |
| 69 | 100 |
| 70 private: | 101 private: |
| 102 // Sets/Loads the pref flag that indicate if some entries have never been seen |
| 103 // since being added to the store. |
| 71 void SetPersistentHasUnseen(bool has_unseen); | 104 void SetPersistentHasUnseen(bool has_unseen); |
| 72 bool GetPersistentHasUnseen(); | 105 bool GetPersistentHasUnseen(); |
| 73 | 106 |
| 74 typedef std::vector<ReadingListEntry> ReadingListEntries; | 107 // Returns a mutable pointer to the entry with URL |gurl|. Return nullptr if |
| 108 // no entry is found. If an entry is found, |read| is set to the read status |
| 109 // of the entry. |
| 110 ReadingListEntry* GetMutableEntryFromURL(const GURL& gurl, bool* read) const; |
| 75 | 111 |
| 76 ReadingListEntries unread_; | 112 // Sorts the entries in |read_| and |unread_| according to their |UpdateTime|. |
| 77 ReadingListEntries read_; | 113 void SortEntries(); |
| 114 |
| 115 // Returns the |storage_layer_| of the model. |
| 116 ReadingListModelStorage* StorageLayer(); |
| 117 |
| 118 // Remove |entry| from the |entries| vector and calls the Move notifications |
| 119 // on observers. |
| 120 void MoveEntryFrom(ReadingListEntries* entries, |
| 121 const ReadingListEntry& entry, |
| 122 bool read); |
| 123 |
| 124 std::unique_ptr<ReadingListEntries> unread_; |
| 125 std::unique_ptr<ReadingListEntries> read_; |
| 78 std::unique_ptr<ReadingListModelStorage> storage_layer_; | 126 std::unique_ptr<ReadingListModelStorage> storage_layer_; |
| 79 PrefService* pref_service_; | 127 PrefService* pref_service_; |
| 80 bool has_unseen_; | 128 bool has_unseen_; |
| 81 bool loaded_; | 129 bool loaded_; |
| 82 | 130 base::WeakPtrFactory<ReadingListModelImpl> weak_ptr_factory_; |
| 83 DISALLOW_COPY_AND_ASSIGN(ReadingListModelImpl); | 131 DISALLOW_COPY_AND_ASSIGN(ReadingListModelImpl); |
| 84 }; | 132 }; |
| 85 | 133 |
| 86 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_MEMORY_H_ | 134 #endif // IOS_CHROME_BROWSER_READING_LIST_READING_LIST_MODEL_MEMORY_H_ |
| OLD | NEW |