Chromium Code Reviews| Index: ios/chrome/browser/reading_list/reading_list_store.cc |
| diff --git a/ios/chrome/browser/reading_list/reading_list_store.cc b/ios/chrome/browser/reading_list/reading_list_store.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..88746456bd40fbeaca7dd06c6fedc618ca6c8d7f |
| --- /dev/null |
| +++ b/ios/chrome/browser/reading_list/reading_list_store.cc |
| @@ -0,0 +1,128 @@ |
| +// 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. |
| + |
| +#include "ios/chrome/browser/reading_list/reading_list_store.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "ios/chrome/browser/reading_list/proto/reading_list.pb.h" |
| +#include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
| +#include "ios/web/public/web_thread.h" |
| + |
| +ReadingListStore::ReadingListStore(std::unique_ptr<ReadingListDB> database, |
| + const base::FilePath& database_dir) |
| + : database_(std::move(database)), |
| + database_loaded_(false), |
| + pending_transaction_(0), |
| + weak_ptr_factory_(this) { |
| + database_->Init("ReadingList", database_dir, |
|
Olivier
2016/10/06 11:58:15
Convert database to ModelTypeStore.
|
| + base::Bind(&ReadingListStore::OnDatabaseInit, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +ReadingListStore::~ReadingListStore() { |
| + DCHECK(pending_transaction_ == 0); |
| +} |
| + |
| +void ReadingListStore::OnDatabaseInit(bool success) { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + if (!success) { |
| + database_.reset(); |
| + } |
| +} |
| + |
| +void ReadingListStore::SetReadingListModel(ReadingListModelImpl* model) { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + model_ = model; |
| +} |
| + |
| +void ReadingListStore::LoadPersistentLists() { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + DCHECK(model_); |
| + database_->LoadEntries(base::Bind(&ReadingListStore::OnDatabaseLoad, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void ReadingListStore::BeginTransaction() { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + 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() { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + 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) { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + BeginTransaction(); |
| + |
| + std::unique_ptr<reading_list::ReadingListLocal> pb_entry = |
| + entry.AsReadingListLocal(read); |
| + // Unref the URL before making asynchronous call. |
| + std::string local_key = entry.URL().spec(); |
| + pending_keys_to_save_->push_back(std::make_pair(local_key, *pb_entry)); |
| + |
| + CommitTransaction(); |
| +} |
| + |
| +void ReadingListStore::RemoveEntry(const ReadingListEntry& entry) { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + BeginTransaction(); |
| + pending_keys_to_remove_->push_back(entry.URL().spec()); |
| + CommitTransaction(); |
| +} |
| + |
| +void ReadingListStore::OnDatabaseLoad(bool success, |
| + std::unique_ptr<EntryVector> entries) { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + if (!success) { |
| + database_.reset(); |
| + return; |
| + } |
| + database_loaded_ = true; |
| + auto read = base::MakeUnique<ReadingListEntries>(); |
| + auto unread = base::MakeUnique<ReadingListEntries>(); |
| + |
| + for (const reading_list::ReadingListLocal& pb_entry : *entries) { |
| + 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) { |
| + DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| + if (!success) { |
| + database_.reset(); |
| + database_loaded_ = false; |
| + } |
| +} |