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