OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "chrome/browser/offline_pages/offline_page_metadata_store_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/location.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/sequenced_task_runner.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "chrome/browser/offline_pages/offline_pages.pb.h" | |
15 #include "components/leveldb_proto/proto_database.h" | |
16 #include "components/offline_pages/offline_page_item.h" | |
17 #include "third_party/leveldatabase/env_chromium.h" | |
18 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
19 #include "url/gurl.h" | |
20 | |
21 using leveldb_proto::ProtoDatabase; | |
22 | |
23 namespace offline_pages { | |
24 namespace { | |
25 | |
26 typedef std::vector<OfflinePageEntry> OfflinePageEntryVector; | |
27 | |
28 void OfflinePageItemToEntry(const OfflinePageItem& item, | |
29 offline_pages::OfflinePageEntry* item_proto) { | |
30 DCHECK(item_proto); | |
31 item_proto->set_url(item.url.spec()); | |
32 item_proto->set_title(base::UTF16ToUTF8(item.title)); | |
33 item_proto->set_version(item.version); | |
34 std::string path_string; | |
35 #if defined(OS_POSIX) | |
36 path_string = item.file_path.value(); | |
37 #elif defined(OS_WIN) | |
38 path_string = base::WideToUTF8(item.file_path.value()); | |
39 #endif | |
40 item_proto->set_file_path(path_string); | |
41 item_proto->set_file_size(item.file_size); | |
42 item_proto->set_creation_time(item.creation_time.ToInternalValue()); | |
43 item_proto->set_last_access_time(item.last_access_time.ToInternalValue()); | |
44 } | |
45 | |
46 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto, | |
47 OfflinePageItem* item) { | |
48 DCHECK(item); | |
49 if (!item_proto.has_url() || !item_proto.has_title() || | |
50 !item_proto.has_version() || !item_proto.has_file_path()) { | |
51 return false; | |
52 } | |
53 item->url = GURL(item_proto.url()); | |
54 item->title = base::UTF8ToUTF16(item_proto.title()); | |
55 item->version = item_proto.version(); | |
56 #if defined(OS_POSIX) | |
57 item->file_path = base::FilePath(item_proto.file_path()); | |
58 #elif defined(OS_WIN) | |
59 item->file_path = base::FilePath(base::UTF8ToWide(item_proto.file_path())); | |
60 #endif | |
61 if (item_proto.has_file_size()) { | |
62 item->file_size = item_proto.file_size(); | |
63 } | |
64 if (item_proto.has_creation_time()) { | |
65 item->creation_time = | |
66 base::Time::FromInternalValue(item_proto.creation_time()); | |
67 } | |
68 if (item_proto.has_last_access_time()) { | |
69 item->last_access_time = | |
70 base::Time::FromInternalValue(item_proto.last_access_time()); | |
71 } | |
72 return true; | |
73 } | |
74 | |
75 void OnLoadDone(const OfflinePageMetadataStore::LoadCallback& callback, | |
76 const base::Callback<void()>& failure_callback, | |
77 bool success, | |
78 scoped_ptr<OfflinePageEntryVector> entries) { | |
79 if (!success) { | |
80 // TODO(fgorski): Add UMA for this case. | |
81 DVLOG(1) << "Offline pages database load failed."; | |
82 failure_callback.Run(); | |
83 base::MessageLoop::current()->PostTask( | |
84 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); | |
85 return; | |
86 } | |
87 | |
88 std::vector<OfflinePageItem> result; | |
89 for (OfflinePageEntryVector::iterator it = entries->begin(); | |
90 it != entries->end(); ++it) { | |
91 OfflinePageItem item; | |
92 if (OfflinePageItemFromEntry(*it, &item)) | |
93 result.push_back(item); | |
94 else | |
95 DVLOG(1) << "Failed to create offline page item from proto."; | |
96 } | |
97 | |
98 base::MessageLoop::current()->PostTask(FROM_HERE, | |
99 base::Bind(callback, true, result)); | |
100 } | |
101 | |
102 void OnUpdateDone(const OfflinePageMetadataStore::UpdateCallback& callback, | |
103 const base::Callback<void()>& failure_callback, | |
104 bool success) { | |
105 if (!success) { | |
106 // TODO(fgorski): Add UMA for this case. | |
107 DVLOG(1) << "Offline pages database update failed."; | |
108 failure_callback.Run(); | |
109 } | |
110 | |
111 base::MessageLoop::current()->PostTask(FROM_HERE, | |
112 base::Bind(callback, success)); | |
113 } | |
114 | |
115 } // namespace | |
116 | |
117 OfflinePageMetadataStoreImpl::OfflinePageMetadataStoreImpl( | |
118 scoped_ptr<ProtoDatabase<OfflinePageEntry>> database, | |
119 const base::FilePath& database_dir) | |
120 : database_(database.Pass()), weak_ptr_factory_(this) { | |
121 database_->Init(database_dir, | |
122 base::Bind(&OfflinePageMetadataStoreImpl::OnInitDone, | |
123 weak_ptr_factory_.GetWeakPtr())); | |
124 } | |
125 | |
126 OfflinePageMetadataStoreImpl::~OfflinePageMetadataStoreImpl() { | |
127 } | |
128 | |
129 void OfflinePageMetadataStoreImpl::OnInitDone(bool success) { | |
130 if (!success) { | |
131 // TODO(fgorski): Add UMA for this case. | |
132 DVLOG(1) << "Offline pages database init failed."; | |
133 ResetDB(); | |
134 return; | |
135 } | |
136 } | |
137 | |
138 void OfflinePageMetadataStoreImpl::Load(const LoadCallback& callback) { | |
139 if (!database_.get()) { | |
140 // Failing fast here, because DB is not initialized, and there is nothing | |
141 // that can be done about it. | |
142 // Callback is invoked through message loop to avoid improper retry and | |
143 // simplify testing. | |
144 DVLOG(1) << "Offline pages database not available in Load."; | |
145 base::MessageLoop::current()->PostTask( | |
146 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); | |
147 return; | |
148 } | |
149 | |
150 database_->LoadEntries(base::Bind( | |
151 &OnLoadDone, callback, base::Bind(&OfflinePageMetadataStoreImpl::ResetDB, | |
152 weak_ptr_factory_.GetWeakPtr()))); | |
153 } | |
154 | |
155 void OfflinePageMetadataStoreImpl::AddOfflinePage( | |
156 const OfflinePageItem& offline_page_item, | |
157 const UpdateCallback& callback) { | |
158 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( | |
159 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | |
160 scoped_ptr<std::vector<std::string>> keys_to_remove( | |
161 new std::vector<std::string>()); | |
162 | |
163 OfflinePageEntry offline_page_proto; | |
164 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); | |
165 entries_to_save->push_back( | |
166 std::make_pair(offline_page_proto.url(), offline_page_proto)); | |
167 | |
168 UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), callback); | |
169 } | |
170 | |
171 void OfflinePageMetadataStoreImpl::RemoveOfflinePage( | |
172 const GURL& page_url, | |
173 const UpdateCallback& callback) { | |
174 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( | |
175 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | |
176 scoped_ptr<std::vector<std::string>> keys_to_remove( | |
177 new std::vector<std::string>()); | |
178 | |
179 keys_to_remove->push_back(page_url.spec()); | |
180 | |
181 UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), callback); | |
182 } | |
183 | |
184 void OfflinePageMetadataStoreImpl::UpdateEntries( | |
185 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save, | |
186 scoped_ptr<std::vector<std::string>> keys_to_remove, | |
187 const UpdateCallback& callback) { | |
188 if (!database_.get()) { | |
189 // Failing fast here, because DB is not initialized, and there is nothing | |
190 // that can be done about it. | |
191 // Callback is invoked through message loop to avoid improper retry and | |
192 // simplify testing. | |
193 DVLOG(1) << "Offline pages database not available in UpdateEntries."; | |
194 base::MessageLoop::current()->PostTask(FROM_HERE, | |
195 base::Bind(callback, false)); | |
196 return; | |
197 } | |
198 | |
199 database_->UpdateEntries( | |
200 entries_to_save.Pass(), keys_to_remove.Pass(), | |
201 base::Bind(&OnUpdateDone, callback, | |
202 base::Bind(&OfflinePageMetadataStoreImpl::ResetDB, | |
203 weak_ptr_factory_.GetWeakPtr()))); | |
204 } | |
205 | |
206 void OfflinePageMetadataStoreImpl::ResetDB() { | |
207 database_.reset(); | |
208 weak_ptr_factory_.InvalidateWeakPtrs(); | |
209 } | |
210 | |
211 } // namespace offline_pages | |
OLD | NEW |