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