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 #include "ios/chrome/browser/reading_list/reading_list_store.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
| 11 #include "ios/web/public/web_thread.h" |
| 12 |
| 13 ReadingListStore::ReadingListStore(std::unique_ptr<ReadingListDB> database, |
| 14 const base::FilePath& database_dir) |
| 15 : database_(std::move(database)), |
| 16 database_loaded_(false), |
| 17 pending_transaction_(0), |
| 18 weak_ptr_factory_(this) { |
| 19 database_->Init("ReadingList", database_dir, |
| 20 base::Bind(&ReadingListStore::OnDatabaseInit, |
| 21 weak_ptr_factory_.GetWeakPtr())); |
| 22 } |
| 23 |
| 24 ReadingListStore::~ReadingListStore() { |
| 25 DCHECK(pending_transaction_ == 0); |
| 26 } |
| 27 |
| 28 void ReadingListStore::OnDatabaseInit(bool success) { |
| 29 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 30 if (!success) { |
| 31 database_.reset(); |
| 32 } |
| 33 } |
| 34 |
| 35 void ReadingListStore::SetReadingListModel(ReadingListModelImpl* model) { |
| 36 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 37 model_ = model; |
| 38 } |
| 39 |
| 40 void ReadingListStore::LoadPersistentLists() { |
| 41 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 42 DCHECK(model_); |
| 43 database_->LoadEntries(base::Bind(&ReadingListStore::OnDatabaseLoad, |
| 44 weak_ptr_factory_.GetWeakPtr())); |
| 45 } |
| 46 |
| 47 void ReadingListStore::BeginTransaction() { |
| 48 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 49 pending_transaction_++; |
| 50 if (pending_transaction_ == 1) { |
| 51 pending_keys_to_save_ = base::MakeUnique<ReadingListDB::KeyEntryVector>(); |
| 52 pending_keys_to_remove_ = base::MakeUnique<std::vector<std::string>>(); |
| 53 } |
| 54 } |
| 55 |
| 56 void ReadingListStore::CommitTransaction() { |
| 57 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 58 pending_transaction_--; |
| 59 if (pending_transaction_ == 0) { |
| 60 database_->UpdateEntries(std::move(pending_keys_to_save_), |
| 61 std::move(pending_keys_to_remove_), |
| 62 base::Bind(&ReadingListStore::OnDatabaseSave, |
| 63 weak_ptr_factory_.GetWeakPtr())); |
| 64 pending_keys_to_save_ = nullptr; |
| 65 pending_keys_to_remove_ = nullptr; |
| 66 } |
| 67 } |
| 68 |
| 69 void ReadingListStore::SaveEntry(const ReadingListEntry& entry, bool read) { |
| 70 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 71 BeginTransaction(); |
| 72 |
| 73 std::unique_ptr<sync_pb::ReadingListLocal> pb_entry = |
| 74 entry.AsReadingListLocal(read); |
| 75 pending_keys_to_save_->push_back( |
| 76 std::make_pair(entry.URL().spec(), *pb_entry)); |
| 77 |
| 78 CommitTransaction(); |
| 79 } |
| 80 |
| 81 void ReadingListStore::RemoveEntry(const ReadingListEntry& entry) { |
| 82 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 83 BeginTransaction(); |
| 84 pending_keys_to_remove_->push_back(entry.URL().spec()); |
| 85 CommitTransaction(); |
| 86 } |
| 87 |
| 88 void ReadingListStore::OnDatabaseLoad(bool success, |
| 89 std::unique_ptr<EntryVector> entries) { |
| 90 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 91 if (!success) { |
| 92 database_.reset(); |
| 93 return; |
| 94 } |
| 95 database_loaded_ = true; |
| 96 auto read = base::MakeUnique<ReadingListEntries>(); |
| 97 auto unread = base::MakeUnique<ReadingListEntries>(); |
| 98 |
| 99 for (const sync_pb::ReadingListLocal& pb_entry : *entries) { |
| 100 std::unique_ptr<ReadingListEntry> entry( |
| 101 ReadingListEntry::FromReadingListLocal(pb_entry)); |
| 102 if (!entry) { |
| 103 continue; |
| 104 } |
| 105 if (pb_entry.entry().status() == sync_pb::ReadingListSpecifics::READ) { |
| 106 read->push_back(std::move(*entry)); |
| 107 } else { |
| 108 unread->push_back(std::move(*entry)); |
| 109 } |
| 110 } |
| 111 std::sort(read->begin(), read->end(), |
| 112 ReadingListEntry::CompareEntryUpdateTime); |
| 113 std::sort(unread->begin(), unread->end(), |
| 114 ReadingListEntry::CompareEntryUpdateTime); |
| 115 |
| 116 model_->ModelLoaded(std::move(unread), std::move(read)); |
| 117 } |
| 118 |
| 119 void ReadingListStore::OnDatabaseSave(bool success) { |
| 120 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 121 if (!success) { |
| 122 database_.reset(); |
| 123 database_loaded_ = false; |
| 124 } |
| 125 } |
OLD | NEW |