| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/offline_pages/offline_page_metadata_store_impl.h" | 5 #include "components/offline_pages/offline_page_metadata_store_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "components/leveldb_proto/proto_database_impl.h" | 20 #include "components/leveldb_proto/proto_database_impl.h" |
| 21 #include "components/offline_pages/offline_page_item.h" | 21 #include "components/offline_pages/offline_page_item.h" |
| 22 #include "components/offline_pages/offline_page_model.h" |
| 22 #include "components/offline_pages/proto/offline_pages.pb.h" | 23 #include "components/offline_pages/proto/offline_pages.pb.h" |
| 23 #include "third_party/leveldatabase/env_chromium.h" | 24 #include "third_party/leveldatabase/env_chromium.h" |
| 24 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 | 27 |
| 27 using leveldb_proto::ProtoDatabase; | 28 using leveldb_proto::ProtoDatabase; |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 // Statistics are logged to UMA with this string as part of histogram name. They | 31 // Statistics are logged to UMA with this string as part of histogram name. They |
| 31 // can all be found under LevelDB.*.OfflinePageMetadataStore. Changing this | 32 // can all be found under LevelDB.*.OfflinePageMetadataStore. Changing this |
| 32 // needs to synchronize with histograms.xml, AND will also become incompatible | 33 // needs to synchronize with histograms.xml, AND will also become incompatible |
| 33 // with older browsers still reporting the previous values. | 34 // with older browsers still reporting the previous values. |
| 34 const char kDatabaseUMAClientName[] = "OfflinePageMetadataStore"; | 35 const char kDatabaseUMAClientName[] = "OfflinePageMetadataStore"; |
| 35 } | 36 } |
| 36 | 37 |
| 37 namespace offline_pages { | 38 namespace offline_pages { |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 void OfflinePageItemToEntry(const OfflinePageItem& item, | 41 void OfflinePageItemToEntry(const OfflinePageItem& item, |
| 41 offline_pages::OfflinePageEntry* item_proto) { | 42 offline_pages::OfflinePageEntry* item_proto) { |
| 42 DCHECK(item_proto); | 43 DCHECK(item_proto); |
| 43 item_proto->set_url(item.url.spec()); | 44 item_proto->set_url(item.url.spec()); |
| 44 item_proto->set_bookmark_id(item.bookmark_id); | 45 // TODO(bburns): switch this to offline id when we stop passing bookmark |
| 46 // id down. |
| 47 item_proto->set_deprecated_bookmark_id(item.offline_id); |
| 45 item_proto->set_version(item.version); | 48 item_proto->set_version(item.version); |
| 46 std::string path_string; | 49 std::string path_string; |
| 47 #if defined(OS_POSIX) | 50 #if defined(OS_POSIX) |
| 48 path_string = item.file_path.value(); | 51 path_string = item.file_path.value(); |
| 49 #elif defined(OS_WIN) | 52 #elif defined(OS_WIN) |
| 50 path_string = base::WideToUTF8(item.file_path.value()); | 53 path_string = base::WideToUTF8(item.file_path.value()); |
| 51 #endif | 54 #endif |
| 52 item_proto->set_file_path(path_string); | 55 item_proto->set_file_path(path_string); |
| 53 item_proto->set_file_size(item.file_size); | 56 item_proto->set_file_size(item.file_size); |
| 54 item_proto->set_creation_time(item.creation_time.ToInternalValue()); | 57 item_proto->set_creation_time(item.creation_time.ToInternalValue()); |
| 55 item_proto->set_last_access_time(item.last_access_time.ToInternalValue()); | 58 item_proto->set_last_access_time(item.last_access_time.ToInternalValue()); |
| 56 item_proto->set_access_count(item.access_count); | 59 item_proto->set_access_count(item.access_count); |
| 57 item_proto->set_flags( | 60 item_proto->set_flags( |
| 58 static_cast<::offline_pages::OfflinePageEntry_Flags>(item.flags)); | 61 static_cast<::offline_pages::OfflinePageEntry_Flags>(item.flags)); |
| 62 item_proto->set_client_id_name_space(item.client_id.name_space); |
| 63 item_proto->set_client_id(item.client_id.id); |
| 59 } | 64 } |
| 60 | 65 |
| 61 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto, | 66 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto, |
| 62 OfflinePageItem* item) { | 67 OfflinePageItem* item) { |
| 63 DCHECK(item); | 68 DCHECK(item); |
| 64 if (!item_proto.has_url() || !item_proto.has_bookmark_id() || | 69 if (!item_proto.has_url() || !item_proto.has_offline_id() || |
| 65 !item_proto.has_version() || !item_proto.has_file_path()) { | 70 !item_proto.has_version() || !item_proto.has_file_path()) { |
| 66 return false; | 71 return false; |
| 67 } | 72 } |
| 68 item->url = GURL(item_proto.url()); | 73 item->url = GURL(item_proto.url()); |
| 69 item->bookmark_id = item_proto.bookmark_id(); | 74 item->offline_id = item_proto.offline_id(); |
| 70 item->version = item_proto.version(); | 75 item->version = item_proto.version(); |
| 71 #if defined(OS_POSIX) | 76 #if defined(OS_POSIX) |
| 72 item->file_path = base::FilePath(item_proto.file_path()); | 77 item->file_path = base::FilePath(item_proto.file_path()); |
| 73 #elif defined(OS_WIN) | 78 #elif defined(OS_WIN) |
| 74 item->file_path = base::FilePath(base::UTF8ToWide(item_proto.file_path())); | 79 item->file_path = base::FilePath(base::UTF8ToWide(item_proto.file_path())); |
| 75 #endif | 80 #endif |
| 76 if (item_proto.has_file_size()) { | 81 if (item_proto.has_file_size()) { |
| 77 item->file_size = item_proto.file_size(); | 82 item->file_size = item_proto.file_size(); |
| 78 } | 83 } |
| 79 if (item_proto.has_creation_time()) { | 84 if (item_proto.has_creation_time()) { |
| 80 item->creation_time = | 85 item->creation_time = |
| 81 base::Time::FromInternalValue(item_proto.creation_time()); | 86 base::Time::FromInternalValue(item_proto.creation_time()); |
| 82 } | 87 } |
| 83 if (item_proto.has_last_access_time()) { | 88 if (item_proto.has_last_access_time()) { |
| 84 item->last_access_time = | 89 item->last_access_time = |
| 85 base::Time::FromInternalValue(item_proto.last_access_time()); | 90 base::Time::FromInternalValue(item_proto.last_access_time()); |
| 86 } | 91 } |
| 87 if (item_proto.has_access_count()) { | 92 if (item_proto.has_access_count()) { |
| 88 item->access_count = item_proto.access_count(); | 93 item->access_count = item_proto.access_count(); |
| 89 } | 94 } |
| 90 if (item_proto.has_flags()) { | 95 if (item_proto.has_flags()) { |
| 91 item->flags = static_cast<OfflinePageItem::Flags>(item_proto.flags()); | 96 item->flags = static_cast<OfflinePageItem::Flags>(item_proto.flags()); |
| 92 } | 97 } |
| 98 item->client_id.name_space = item_proto.client_id_name_space(); |
| 99 item->client_id.id = item_proto.client_id(); |
| 100 |
| 101 // Legacy storage. |
| 102 if (item->client_id.name_space == "" && |
| 103 item_proto.has_deprecated_bookmark_id()) { |
| 104 item->client_id.name_space = offline_pages::BOOKMARK_NAMESPACE; |
| 105 item->client_id.id = |
| 106 base::Int64ToString(item_proto.deprecated_bookmark_id()); |
| 107 if (item->offline_id == 0) { |
| 108 item->offline_id = item_proto.deprecated_bookmark_id(); |
| 109 } |
| 110 } |
| 93 return true; | 111 return true; |
| 94 } | 112 } |
| 95 | 113 |
| 96 } // namespace | 114 } // namespace |
| 97 | 115 |
| 98 OfflinePageMetadataStoreImpl::OfflinePageMetadataStoreImpl( | 116 OfflinePageMetadataStoreImpl::OfflinePageMetadataStoreImpl( |
| 99 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | 117 scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
| 100 const base::FilePath& database_dir) | 118 const base::FilePath& database_dir) |
| 101 : background_task_runner_(background_task_runner), | 119 : background_task_runner_(background_task_runner), |
| 102 database_dir_(database_dir), | 120 database_dir_(database_dir), |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 void OfflinePageMetadataStoreImpl::AddOrUpdateOfflinePage( | 197 void OfflinePageMetadataStoreImpl::AddOrUpdateOfflinePage( |
| 180 const OfflinePageItem& offline_page_item, | 198 const OfflinePageItem& offline_page_item, |
| 181 const UpdateCallback& callback) { | 199 const UpdateCallback& callback) { |
| 182 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( | 200 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( |
| 183 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | 201 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); |
| 184 scoped_ptr<std::vector<std::string>> keys_to_remove( | 202 scoped_ptr<std::vector<std::string>> keys_to_remove( |
| 185 new std::vector<std::string>()); | 203 new std::vector<std::string>()); |
| 186 | 204 |
| 187 OfflinePageEntry offline_page_proto; | 205 OfflinePageEntry offline_page_proto; |
| 188 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); | 206 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); |
| 189 entries_to_save->push_back( | 207 entries_to_save->push_back(std::make_pair( |
| 190 std::make_pair(base::Int64ToString(offline_page_item.bookmark_id), | 208 base::Int64ToString(offline_page_item.offline_id), offline_page_proto)); |
| 191 offline_page_proto)); | |
| 192 | 209 |
| 193 UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove), | 210 UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove), |
| 194 callback); | 211 callback); |
| 195 } | 212 } |
| 196 | 213 |
| 197 void OfflinePageMetadataStoreImpl::RemoveOfflinePages( | 214 void OfflinePageMetadataStoreImpl::RemoveOfflinePages( |
| 198 const std::vector<int64_t>& bookmark_ids, | 215 const std::vector<int64_t>& offline_ids, |
| 199 const UpdateCallback& callback) { | 216 const UpdateCallback& callback) { |
| 200 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( | 217 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( |
| 201 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | 218 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); |
| 202 scoped_ptr<std::vector<std::string>> keys_to_remove( | 219 scoped_ptr<std::vector<std::string>> keys_to_remove( |
| 203 new std::vector<std::string>()); | 220 new std::vector<std::string>()); |
| 204 | 221 |
| 205 for (int64_t id : bookmark_ids) | 222 for (int64_t id : offline_ids) |
| 206 keys_to_remove->push_back(base::Int64ToString(id)); | 223 keys_to_remove->push_back(base::Int64ToString(id)); |
| 207 | 224 |
| 208 UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove), | 225 UpdateEntries(std::move(entries_to_save), std::move(keys_to_remove), |
| 209 callback); | 226 callback); |
| 210 } | 227 } |
| 211 | 228 |
| 212 void OfflinePageMetadataStoreImpl::UpdateEntries( | 229 void OfflinePageMetadataStoreImpl::UpdateEntries( |
| 213 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save, | 230 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save, |
| 214 scoped_ptr<std::vector<std::string>> keys_to_remove, | 231 scoped_ptr<std::vector<std::string>> keys_to_remove, |
| 215 const UpdateCallback& callback) { | 232 const UpdateCallback& callback) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 267 |
| 251 void OfflinePageMetadataStoreImpl::ResetDone( | 268 void OfflinePageMetadataStoreImpl::ResetDone( |
| 252 const ResetCallback& callback, | 269 const ResetCallback& callback, |
| 253 bool success) { | 270 bool success) { |
| 254 database_.reset(); | 271 database_.reset(); |
| 255 weak_ptr_factory_.InvalidateWeakPtrs(); | 272 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 256 callback.Run(success); | 273 callback.Run(success); |
| 257 } | 274 } |
| 258 | 275 |
| 259 } // namespace offline_pages | 276 } // namespace offline_pages |
| OLD | NEW |