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