Chromium Code Reviews| 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 return; | |
|
gambard
2016/09/28 09:19:08
Is this return needed?
Olivier
2016/09/28 11:20:06
Done.
| |
| 30 } | |
| 31 } | |
| 32 | |
| 33 void ReadingListStore::SetReadingListModel(ReadingListModelImpl* model) { | |
| 34 model_ = model; | |
| 35 } | |
| 36 | |
| 37 void ReadingListStore::LoadPersistentLists() { | |
| 38 DCHECK(model_); | |
| 39 database_->LoadEntries(base::Bind(&ReadingListStore::OnDatabaseLoad, | |
| 40 weak_ptr_factory_.GetWeakPtr())); | |
| 41 } | |
| 42 | |
| 43 void ReadingListStore::BeginTransaction() { | |
| 44 pending_transaction_++; | |
| 45 if (pending_transaction_ == 1) { | |
| 46 pending_keys_to_save_ = base::MakeUnique<ReadingListDB::KeyEntryVector>(); | |
| 47 pending_keys_to_remove_ = base::MakeUnique<std::vector<std::string>>(); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 void ReadingListStore::CommitTransaction() { | |
| 52 pending_transaction_--; | |
| 53 if (pending_transaction_ == 0) { | |
| 54 database_->UpdateEntries(std::move(pending_keys_to_save_), | |
| 55 std::move(pending_keys_to_remove_), | |
| 56 base::Bind(&ReadingListStore::OnDatabaseSave, | |
| 57 weak_ptr_factory_.GetWeakPtr())); | |
| 58 pending_keys_to_save_ = nullptr; | |
| 59 pending_keys_to_remove_ = nullptr; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void ReadingListStore::SaveEntry(const ReadingListEntry& entry, bool read) { | |
| 64 BeginTransaction(); | |
| 65 | |
| 66 std::unique_ptr<sync_pb::ReadingListLocal> pb_entry = | |
| 67 entry.AsReadingListLocal(read); | |
| 68 pending_keys_to_save_->push_back( | |
| 69 std::make_pair(entry.URL().spec(), *pb_entry)); | |
| 70 | |
| 71 CommitTransaction(); | |
| 72 } | |
| 73 | |
| 74 void ReadingListStore::RemoveEntry(const ReadingListEntry& entry) { | |
| 75 BeginTransaction(); | |
| 76 pending_keys_to_remove_->push_back(entry.URL().spec()); | |
| 77 CommitTransaction(); | |
| 78 } | |
| 79 | |
| 80 void ReadingListStore::OnDatabaseLoad(bool success, | |
| 81 std::unique_ptr<EntryVector> entries) { | |
| 82 if (!success) { | |
| 83 database_.reset(); | |
| 84 return; | |
| 85 } | |
| 86 database_loaded_ = true; | |
| 87 auto read = base::MakeUnique<ReadingListEntries>(); | |
| 88 auto unread = base::MakeUnique<ReadingListEntries>(); | |
| 89 | |
| 90 for (sync_pb::ReadingListLocal& pb_entry : *entries) { | |
|
jif-google
2016/09/27 16:39:03
might as well make |pb_entry| be const.
Olivier
2016/09/28 11:20:06
Done.
| |
| 91 std::unique_ptr<ReadingListEntry> entry( | |
| 92 ReadingListEntry::FromReadingListLocal(pb_entry)); | |
| 93 if (!entry) { | |
| 94 continue; | |
| 95 } | |
| 96 if (pb_entry.entry().status() == sync_pb::ReadingListSpecifics::READ) { | |
| 97 read->push_back(std::move(*entry)); | |
| 98 } else { | |
| 99 unread->push_back(std::move(*entry)); | |
| 100 } | |
| 101 } | |
| 102 std::sort(read->begin(), read->end(), | |
| 103 ReadingListEntry::CompareEntryUpdateTime); | |
| 104 std::sort(unread->begin(), unread->end(), | |
| 105 ReadingListEntry::CompareEntryUpdateTime); | |
| 106 model_->ModelLoaded(std::move(unread), std::move(read)); | |
| 107 } | |
| 108 | |
| 109 void ReadingListStore::OnDatabaseSave(bool success) { | |
| 110 if (!success) { | |
| 111 database_.reset(); | |
| 112 database_loaded_ = false; | |
| 113 } | |
| 114 } | |
| OLD | NEW |