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.
5 #include "components/offline_pages/offline_page_metadata_store_impl.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 "components/leveldb_proto/proto_database.h"
15 #include "components/offline_pages/offline_page_item.h"
16 #include "components/offline_pages/proto/offline_pages.pb.h"
17 #include "third_party/leveldatabase/env_chromium.h"
18 #include "third_party/leveldatabase/src/include/leveldb/db.h"
21 using leveldb_proto::ProtoDatabase
;
23 namespace offline_pages
{
26 typedef std::vector
<OfflinePageEntry
> OfflinePageEntryVector
;
28 void OfflinePageItemToEntry(const OfflinePageItem
& item
,
29 offline_pages::OfflinePageEntry
* item_proto
) {
31 item_proto
->set_url(item
.url
.spec());
32 item_proto
->set_bookmark_id(item
.bookmark_id
);
33 item_proto
->set_version(item
.version
);
34 std::string path_string
;
36 path_string
= item
.file_path
.value();
38 path_string
= base::WideToUTF8(item
.file_path
.value());
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());
46 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry
& item_proto
,
47 OfflinePageItem
* item
) {
49 if (!item_proto
.has_url() || !item_proto
.has_bookmark_id() ||
50 !item_proto
.has_version() || !item_proto
.has_file_path()) {
53 item
->url
= GURL(item_proto
.url());
54 item
->bookmark_id
= item_proto
.bookmark_id();
55 item
->version
= item_proto
.version();
57 item
->file_path
= base::FilePath(item_proto
.file_path());
59 item
->file_path
= base::FilePath(base::UTF8ToWide(item_proto
.file_path()));
61 if (item_proto
.has_file_size()) {
62 item
->file_size
= item_proto
.file_size();
64 if (item_proto
.has_creation_time()) {
66 base::Time::FromInternalValue(item_proto
.creation_time());
68 if (item_proto
.has_last_access_time()) {
69 item
->last_access_time
=
70 base::Time::FromInternalValue(item_proto
.last_access_time());
75 void OnLoadDone(const OfflinePageMetadataStore::LoadCallback
& callback
,
76 const base::Callback
<void()>& failure_callback
,
78 scoped_ptr
<OfflinePageEntryVector
> entries
) {
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
>()));
88 std::vector
<OfflinePageItem
> result
;
89 for (OfflinePageEntryVector::iterator it
= entries
->begin();
90 it
!= entries
->end(); ++it
) {
92 if (OfflinePageItemFromEntry(*it
, &item
))
93 result
.push_back(item
);
95 DVLOG(1) << "Failed to create offline page item from proto.";
98 base::MessageLoop::current()->PostTask(FROM_HERE
,
99 base::Bind(callback
, true, result
));
102 void OnUpdateDone(const OfflinePageMetadataStore::UpdateCallback
& callback
,
103 const base::Callback
<void()>& failure_callback
,
106 // TODO(fgorski): Add UMA for this case.
107 DVLOG(1) << "Offline pages database update failed.";
108 failure_callback
.Run();
111 base::MessageLoop::current()->PostTask(FROM_HERE
,
112 base::Bind(callback
, success
));
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()));
126 OfflinePageMetadataStoreImpl::~OfflinePageMetadataStoreImpl() {
129 void OfflinePageMetadataStoreImpl::OnInitDone(bool success
) {
131 // TODO(fgorski): Add UMA for this case.
132 DVLOG(1) << "Offline pages database init failed.";
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
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
>()));
150 database_
->LoadEntries(base::Bind(
151 &OnLoadDone
, callback
, base::Bind(&OfflinePageMetadataStoreImpl::ResetDB
,
152 weak_ptr_factory_
.GetWeakPtr())));
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
>());
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
));
168 UpdateEntries(entries_to_save
.Pass(), keys_to_remove
.Pass(), callback
);
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
>());
179 keys_to_remove
->push_back(page_url
.spec());
181 UpdateEntries(entries_to_save
.Pass(), keys_to_remove
.Pass(), callback
);
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
193 DVLOG(1) << "Offline pages database not available in UpdateEntries.";
194 base::MessageLoop::current()->PostTask(FROM_HERE
,
195 base::Bind(callback
, false));
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())));
206 void OfflinePageMetadataStoreImpl::ResetDB() {
208 weak_ptr_factory_
.InvalidateWeakPtrs();
211 } // namespace offline_pages