| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 std::string path_string; | 36 std::string path_string; |
| 37 #if defined(OS_POSIX) | 37 #if defined(OS_POSIX) |
| 38 path_string = item.file_path.value(); | 38 path_string = item.file_path.value(); |
| 39 #elif defined(OS_WIN) | 39 #elif defined(OS_WIN) |
| 40 path_string = base::WideToUTF8(item.file_path.value()); | 40 path_string = base::WideToUTF8(item.file_path.value()); |
| 41 #endif | 41 #endif |
| 42 item_proto->set_file_path(path_string); | 42 item_proto->set_file_path(path_string); |
| 43 item_proto->set_file_size(item.file_size); | 43 item_proto->set_file_size(item.file_size); |
| 44 item_proto->set_creation_time(item.creation_time.ToInternalValue()); | 44 item_proto->set_creation_time(item.creation_time.ToInternalValue()); |
| 45 item_proto->set_last_access_time(item.last_access_time.ToInternalValue()); | 45 item_proto->set_last_access_time(item.last_access_time.ToInternalValue()); |
| 46 item_proto->set_access_count(item.access_count); |
| 46 } | 47 } |
| 47 | 48 |
| 48 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto, | 49 bool OfflinePageItemFromEntry(const offline_pages::OfflinePageEntry& item_proto, |
| 49 OfflinePageItem* item) { | 50 OfflinePageItem* item) { |
| 50 DCHECK(item); | 51 DCHECK(item); |
| 51 if (!item_proto.has_url() || !item_proto.has_bookmark_id() || | 52 if (!item_proto.has_url() || !item_proto.has_bookmark_id() || |
| 52 !item_proto.has_version() || !item_proto.has_file_path()) { | 53 !item_proto.has_version() || !item_proto.has_file_path()) { |
| 53 return false; | 54 return false; |
| 54 } | 55 } |
| 55 item->url = GURL(item_proto.url()); | 56 item->url = GURL(item_proto.url()); |
| 56 item->bookmark_id = item_proto.bookmark_id(); | 57 item->bookmark_id = item_proto.bookmark_id(); |
| 57 item->version = item_proto.version(); | 58 item->version = item_proto.version(); |
| 58 #if defined(OS_POSIX) | 59 #if defined(OS_POSIX) |
| 59 item->file_path = base::FilePath(item_proto.file_path()); | 60 item->file_path = base::FilePath(item_proto.file_path()); |
| 60 #elif defined(OS_WIN) | 61 #elif defined(OS_WIN) |
| 61 item->file_path = base::FilePath(base::UTF8ToWide(item_proto.file_path())); | 62 item->file_path = base::FilePath(base::UTF8ToWide(item_proto.file_path())); |
| 62 #endif | 63 #endif |
| 63 if (item_proto.has_file_size()) { | 64 if (item_proto.has_file_size()) { |
| 64 item->file_size = item_proto.file_size(); | 65 item->file_size = item_proto.file_size(); |
| 65 } | 66 } |
| 66 if (item_proto.has_creation_time()) { | 67 if (item_proto.has_creation_time()) { |
| 67 item->creation_time = | 68 item->creation_time = |
| 68 base::Time::FromInternalValue(item_proto.creation_time()); | 69 base::Time::FromInternalValue(item_proto.creation_time()); |
| 69 } | 70 } |
| 70 if (item_proto.has_last_access_time()) { | 71 if (item_proto.has_last_access_time()) { |
| 71 item->last_access_time = | 72 item->last_access_time = |
| 72 base::Time::FromInternalValue(item_proto.last_access_time()); | 73 base::Time::FromInternalValue(item_proto.last_access_time()); |
| 73 } | 74 } |
| 75 if (item_proto.has_access_count()) { |
| 76 item->access_count = item_proto.access_count(); |
| 77 } |
| 74 return true; | 78 return true; |
| 75 } | 79 } |
| 76 | 80 |
| 77 void OnLoadDone(const OfflinePageMetadataStore::LoadCallback& callback, | 81 void OnLoadDone(const OfflinePageMetadataStore::LoadCallback& callback, |
| 78 const base::Callback<void()>& failure_callback, | 82 const base::Callback<void()>& failure_callback, |
| 79 bool success, | 83 bool success, |
| 80 scoped_ptr<OfflinePageEntryVector> entries) { | 84 scoped_ptr<OfflinePageEntryVector> entries) { |
| 81 UMA_HISTOGRAM_BOOLEAN("OfflinePages.LoadSuccess", success); | 85 UMA_HISTOGRAM_BOOLEAN("OfflinePages.LoadSuccess", success); |
| 82 if (!success) { | 86 if (!success) { |
| 83 DVLOG(1) << "Offline pages database load failed."; | 87 DVLOG(1) << "Offline pages database load failed."; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 base::MessageLoop::current()->PostTask( | 152 base::MessageLoop::current()->PostTask( |
| 149 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); | 153 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); |
| 150 return; | 154 return; |
| 151 } | 155 } |
| 152 | 156 |
| 153 database_->LoadEntries(base::Bind( | 157 database_->LoadEntries(base::Bind( |
| 154 &OnLoadDone, callback, base::Bind(&OfflinePageMetadataStoreImpl::ResetDB, | 158 &OnLoadDone, callback, base::Bind(&OfflinePageMetadataStoreImpl::ResetDB, |
| 155 weak_ptr_factory_.GetWeakPtr()))); | 159 weak_ptr_factory_.GetWeakPtr()))); |
| 156 } | 160 } |
| 157 | 161 |
| 158 void OfflinePageMetadataStoreImpl::AddOfflinePage( | 162 void OfflinePageMetadataStoreImpl::AddOrUpdateOfflinePage( |
| 159 const OfflinePageItem& offline_page_item, | 163 const OfflinePageItem& offline_page_item, |
| 160 const UpdateCallback& callback) { | 164 const UpdateCallback& callback) { |
| 161 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( | 165 scoped_ptr<ProtoDatabase<OfflinePageEntry>::KeyEntryVector> entries_to_save( |
| 162 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); | 166 new ProtoDatabase<OfflinePageEntry>::KeyEntryVector()); |
| 163 scoped_ptr<std::vector<std::string>> keys_to_remove( | 167 scoped_ptr<std::vector<std::string>> keys_to_remove( |
| 164 new std::vector<std::string>()); | 168 new std::vector<std::string>()); |
| 165 | 169 |
| 166 OfflinePageEntry offline_page_proto; | 170 OfflinePageEntry offline_page_proto; |
| 167 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); | 171 OfflinePageItemToEntry(offline_page_item, &offline_page_proto); |
| 168 entries_to_save->push_back( | 172 entries_to_save->push_back( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 base::Bind(&OfflinePageMetadataStoreImpl::ResetDB, | 211 base::Bind(&OfflinePageMetadataStoreImpl::ResetDB, |
| 208 weak_ptr_factory_.GetWeakPtr()))); | 212 weak_ptr_factory_.GetWeakPtr()))); |
| 209 } | 213 } |
| 210 | 214 |
| 211 void OfflinePageMetadataStoreImpl::ResetDB() { | 215 void OfflinePageMetadataStoreImpl::ResetDB() { |
| 212 database_.reset(); | 216 database_.reset(); |
| 213 weak_ptr_factory_.InvalidateWeakPtrs(); | 217 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 214 } | 218 } |
| 215 | 219 |
| 216 } // namespace offline_pages | 220 } // namespace offline_pages |
| OLD | NEW |