Chromium Code Reviews| Index: ios/chrome/browser/reading_list/reading_list_store.mm |
| diff --git a/ios/chrome/browser/reading_list/reading_list_store.mm b/ios/chrome/browser/reading_list/reading_list_store.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b48d6d43bebdb8cbc963a8cadf6668ef9f6493cc |
| --- /dev/null |
| +++ b/ios/chrome/browser/reading_list/reading_list_store.mm |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import <Foundation/Foundation.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
| +#include "ios/chrome/browser/reading_list/reading_list_store.h" |
| + |
| +ReadingListStore::ReadingListStore(std::unique_ptr<ReadingListDB> database, |
| + const base::FilePath& database_dir) |
| + : database_(std::move(database)), |
| + database_loaded_(false), |
| + weak_ptr_factory_(this) { |
| + database_->Init("ReadingList", database_dir, |
| + base::Bind(&ReadingListStore::OnDatabaseInit, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +ReadingListStore::~ReadingListStore() { |
| + DCHECK(pending_transaction_ == 0); |
| +} |
| + |
| +void ReadingListStore::OnDatabaseInit(bool success) { |
| + if (!success) { |
| + database_.reset(); |
| + return; |
|
gambard
2016/09/28 09:19:08
Is this return needed?
Olivier
2016/09/28 11:20:06
Done.
|
| + } |
| +} |
| + |
| +void ReadingListStore::SetReadingListModel(ReadingListModelImpl* model) { |
| + model_ = model; |
| +} |
| + |
| +void ReadingListStore::LoadPersistentLists() { |
| + DCHECK(model_); |
| + database_->LoadEntries(base::Bind(&ReadingListStore::OnDatabaseLoad, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void ReadingListStore::BeginTransaction() { |
| + pending_transaction_++; |
| + if (pending_transaction_ == 1) { |
| + pending_keys_to_save_ = base::MakeUnique<ReadingListDB::KeyEntryVector>(); |
| + pending_keys_to_remove_ = base::MakeUnique<std::vector<std::string>>(); |
| + } |
| +} |
| + |
| +void ReadingListStore::CommitTransaction() { |
| + pending_transaction_--; |
| + if (pending_transaction_ == 0) { |
| + database_->UpdateEntries(std::move(pending_keys_to_save_), |
| + std::move(pending_keys_to_remove_), |
| + base::Bind(&ReadingListStore::OnDatabaseSave, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + pending_keys_to_save_ = nullptr; |
| + pending_keys_to_remove_ = nullptr; |
| + } |
| +} |
| + |
| +void ReadingListStore::SaveEntry(const ReadingListEntry& entry, bool read) { |
| + BeginTransaction(); |
| + |
| + std::unique_ptr<sync_pb::ReadingListLocal> pb_entry = |
| + entry.AsReadingListLocal(read); |
| + pending_keys_to_save_->push_back( |
| + std::make_pair(entry.URL().spec(), *pb_entry)); |
| + |
| + CommitTransaction(); |
| +} |
| + |
| +void ReadingListStore::RemoveEntry(const ReadingListEntry& entry) { |
| + BeginTransaction(); |
| + pending_keys_to_remove_->push_back(entry.URL().spec()); |
| + CommitTransaction(); |
| +} |
| + |
| +void ReadingListStore::OnDatabaseLoad(bool success, |
| + std::unique_ptr<EntryVector> entries) { |
| + if (!success) { |
| + database_.reset(); |
| + return; |
| + } |
| + database_loaded_ = true; |
| + auto read = base::MakeUnique<ReadingListEntries>(); |
| + auto unread = base::MakeUnique<ReadingListEntries>(); |
| + |
| + 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.
|
| + std::unique_ptr<ReadingListEntry> entry( |
| + ReadingListEntry::FromReadingListLocal(pb_entry)); |
| + if (!entry) { |
| + continue; |
| + } |
| + if (pb_entry.entry().status() == sync_pb::ReadingListSpecifics::READ) { |
| + read->push_back(std::move(*entry)); |
| + } else { |
| + unread->push_back(std::move(*entry)); |
| + } |
| + } |
| + std::sort(read->begin(), read->end(), |
| + ReadingListEntry::CompareEntryUpdateTime); |
| + std::sort(unread->begin(), unread->end(), |
| + ReadingListEntry::CompareEntryUpdateTime); |
| + model_->ModelLoaded(std::move(unread), std::move(read)); |
| +} |
| + |
| +void ReadingListStore::OnDatabaseSave(bool success) { |
| + if (!success) { |
| + database_.reset(); |
| + database_loaded_ = false; |
| + } |
| +} |