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