Roll src/third_party/WebKit e2f1087:c936ac9 (svn 200537:200538)
[chromium-blink-merge.git] / components / offline_pages / offline_page_metadata_store_impl.cc
blobc68c67641a1b04b1f25d35f488a1466187d4204a
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"
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 "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"
19 #include "url/gurl.h"
21 using leveldb_proto::ProtoDatabase;
23 namespace offline_pages {
24 namespace {
26 typedef std::vector<OfflinePageEntry> OfflinePageEntryVector;
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_bookmark_id(item.bookmark_id);
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());
46 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto,
47 OfflinePageItem* item) {
48 DCHECK(item);
49 if (!item_proto.has_url() || !item_proto.has_bookmark_id() ||
50 !item_proto.has_version() || !item_proto.has_file_path()) {
51 return false;
53 item->url = GURL(item_proto.url());
54 item->bookmark_id = item_proto.bookmark_id();
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();
64 if (item_proto.has_creation_time()) {
65 item->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());
72 return true;
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;
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.";
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,
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();
111 base::MessageLoop::current()->PostTask(FROM_HERE,
112 base::Bind(callback, success));
115 } // namespace
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) {
130 if (!success) {
131 // TODO(fgorski): Add UMA for this case.
132 DVLOG(1) << "Offline pages database init failed.";
133 ResetDB();
134 return;
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;
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
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;
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() {
207 database_.reset();
208 weak_ptr_factory_.InvalidateWeakPtrs();
211 } // namespace offline_pages