[Sync] Componentize UIModelWorker.
[chromium-blink-merge.git] / components / offline_pages / offline_page_metadata_store_impl.cc
blob90b7b45f556cf39967ec57ab84261f8a11ddf337
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/metrics/histogram_macros.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "components/leveldb_proto/proto_database.h"
17 #include "components/offline_pages/offline_page_item.h"
18 #include "components/offline_pages/proto/offline_pages.pb.h"
19 #include "third_party/leveldatabase/env_chromium.h"
20 #include "third_party/leveldatabase/src/include/leveldb/db.h"
21 #include "url/gurl.h"
23 using leveldb_proto::ProtoDatabase;
25 namespace offline_pages {
26 namespace {
28 typedef std::vector<OfflinePageEntry> OfflinePageEntryVector;
30 void OfflinePageItemToEntry(const OfflinePageItem& item,
31 offline_pages::OfflinePageEntry* item_proto) {
32 DCHECK(item_proto);
33 item_proto->set_url(item.url.spec());
34 item_proto->set_bookmark_id(item.bookmark_id);
35 item_proto->set_version(item.version);
36 std::string path_string;
37 #if defined(OS_POSIX)
38 path_string = item.file_path.value();
39 #elif defined(OS_WIN)
40 path_string = base::WideToUTF8(item.file_path.value());
41 #endif
42 item_proto->set_file_path(path_string);
43 item_proto->set_file_size(item.file_size);
44 item_proto->set_creation_time(item.creation_time.ToInternalValue());
45 item_proto->set_last_access_time(item.last_access_time.ToInternalValue());
48 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto,
49 OfflinePageItem* item) {
50 DCHECK(item);
51 if (!item_proto.has_url() || !item_proto.has_bookmark_id() ||
52 !item_proto.has_version() || !item_proto.has_file_path()) {
53 return false;
55 item->url = GURL(item_proto.url());
56 item->bookmark_id = item_proto.bookmark_id();
57 item->version = item_proto.version();
58 #if defined(OS_POSIX)
59 item->file_path = base::FilePath(item_proto.file_path());
60 #elif defined(OS_WIN)
61 item->file_path = base::FilePath(base::UTF8ToWide(item_proto.file_path()));
62 #endif
63 if (item_proto.has_file_size()) {
64 item->file_size = item_proto.file_size();
66 if (item_proto.has_creation_time()) {
67 item->creation_time =
68 base::Time::FromInternalValue(item_proto.creation_time());
70 if (item_proto.has_last_access_time()) {
71 item->last_access_time =
72 base::Time::FromInternalValue(item_proto.last_access_time());
74 return true;
77 void OnLoadDone(const OfflinePageMetadataStore::LoadCallback& callback,
78 const base::Callback<void()>& failure_callback,
79 bool success,
80 scoped_ptr<OfflinePageEntryVector> entries) {
81 UMA_HISTOGRAM_BOOLEAN("OfflinePages.LoadSuccess", success);
82 if (!success) {
83 DVLOG(1) << "Offline pages database load failed.";
84 failure_callback.Run();
85 base::MessageLoop::current()->PostTask(
86 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>()));
87 return;
90 std::vector<OfflinePageItem> result;
91 for (OfflinePageEntryVector::iterator it = entries->begin();
92 it != entries->end(); ++it) {
93 OfflinePageItem item;
94 if (OfflinePageItemFromEntry(*it, &item))
95 result.push_back(item);
96 else
97 DVLOG(1) << "Failed to create offline page item from proto.";
99 UMA_HISTOGRAM_COUNTS("OfflinePages.SavedPageCount", result.size());
101 base::MessageLoop::current()->PostTask(FROM_HERE,
102 base::Bind(callback, true, result));
105 void OnUpdateDone(const OfflinePageMetadataStore::UpdateCallback& callback,
106 const base::Callback<void()>& failure_callback,
107 bool success) {
108 if (!success) {
109 // TODO(fgorski): Add UMA for this case.
110 DVLOG(1) << "Offline pages database update failed.";
111 failure_callback.Run();
114 base::MessageLoop::current()->PostTask(FROM_HERE,
115 base::Bind(callback, success));
118 } // namespace
120 OfflinePageMetadataStoreImpl::OfflinePageMetadataStoreImpl(
121 scoped_ptr<ProtoDatabase<OfflinePageEntry>> database,
122 const base::FilePath& database_dir)
123 : database_(database.Pass()), weak_ptr_factory_(this) {
124 database_->Init(database_dir,
125 base::Bind(&OfflinePageMetadataStoreImpl::OnInitDone,
126 weak_ptr_factory_.GetWeakPtr()));
129 OfflinePageMetadataStoreImpl::~OfflinePageMetadataStoreImpl() {
132 void OfflinePageMetadataStoreImpl::OnInitDone(bool success) {
133 if (!success) {
134 // TODO(fgorski): Add UMA for this case.
135 DVLOG(1) << "Offline pages database init failed.";
136 ResetDB();
137 return;
141 void OfflinePageMetadataStoreImpl::Load(const LoadCallback& callback) {
142 if (!database_.get()) {
143 // Failing fast here, because DB is not initialized, and there is nothing
144 // that can be done about it.
145 // Callback is invoked through message loop to avoid improper retry and
146 // simplify testing.
147 DVLOG(1) << "Offline pages database not available in Load.";
148 base::MessageLoop::current()->PostTask(
149 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>()));
150 return;
153 database_->LoadEntries(base::Bind(
154 &OnLoadDone, callback, base::Bind(&OfflinePageMetadataStoreImpl::ResetDB,
155 weak_ptr_factory_.GetWeakPtr())));
158 void OfflinePageMetadataStoreImpl::AddOfflinePage(
159 const OfflinePageItem& offline_page_item,
160 const UpdateCallback& callback) {
161 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save(
162 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector());
163 scoped_ptr<std::vector<std::string>> keys_to_remove(
164 new std::vector<std::string>());
166 OfflinePageEntry offline_page_proto;
167 OfflinePageItemToEntry(offline_page_item, &offline_page_proto);
168 entries_to_save->push_back(
169 std::make_pair(base::Int64ToString(offline_page_item.bookmark_id),
170 offline_page_proto));
172 UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), callback);
175 void OfflinePageMetadataStoreImpl::RemoveOfflinePages(
176 const std::vector<int64>& bookmark_ids,
177 const UpdateCallback& callback) {
178 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save(
179 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector());
180 scoped_ptr<std::vector<std::string>> keys_to_remove(
181 new std::vector<std::string>());
183 for (int64 id : bookmark_ids)
184 keys_to_remove->push_back(base::Int64ToString(id));
186 UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), callback);
189 void OfflinePageMetadataStoreImpl::UpdateEntries(
190 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save,
191 scoped_ptr<std::vector<std::string>> keys_to_remove,
192 const UpdateCallback& callback) {
193 if (!database_.get()) {
194 // Failing fast here, because DB is not initialized, and there is nothing
195 // that can be done about it.
196 // Callback is invoked through message loop to avoid improper retry and
197 // simplify testing.
198 DVLOG(1) << "Offline pages database not available in UpdateEntries.";
199 base::MessageLoop::current()->PostTask(FROM_HERE,
200 base::Bind(callback, false));
201 return;
204 database_->UpdateEntries(
205 entries_to_save.Pass(), keys_to_remove.Pass(),
206 base::Bind(&OnUpdateDone, callback,
207 base::Bind(&OfflinePageMetadataStoreImpl::ResetDB,
208 weak_ptr_factory_.GetWeakPtr())));
211 void OfflinePageMetadataStoreImpl::ResetDB() {
212 database_.reset();
213 weak_ptr_factory_.InvalidateWeakPtrs();
216 } // namespace offline_pages