| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "components/leveldb_proto/proto_database_impl.h" |
| 11 #include "components/ntp_snippets/proto/ntp_snippets.pb.h" |
| 12 |
| 13 using leveldb_proto::ProtoDatabaseImpl; |
| 14 |
| 15 namespace { |
| 16 // Statistics are logged to UMA with this string as part of histogram name. They |
| 17 // can all be found under LevelDB.*.NTPSnippets. Changing this needs to |
| 18 // synchronize with histograms.xml, AND will also become incompatible with older |
| 19 // browsers still reporting the previous values. |
| 20 const char kDatabaseUMAClientName[] = "NTPSnippets"; |
| 21 } |
| 22 |
| 23 namespace ntp_snippets { |
| 24 |
| 25 NTPSnippetsDatabase::NTPSnippetsDatabase( |
| 26 const base::FilePath& database_dir, |
| 27 scoped_refptr<base::SequencedTaskRunner> file_task_runner) |
| 28 : database_( |
| 29 new ProtoDatabaseImpl<SnippetProto>(std::move(file_task_runner))), |
| 30 database_initialized_(false), |
| 31 weak_ptr_factory_(this) { |
| 32 database_->Init(kDatabaseUMAClientName, database_dir, |
| 33 base::Bind(&NTPSnippetsDatabase::OnDatabaseInited, |
| 34 weak_ptr_factory_.GetWeakPtr())); |
| 35 } |
| 36 |
| 37 NTPSnippetsDatabase::~NTPSnippetsDatabase() {} |
| 38 |
| 39 void NTPSnippetsDatabase::Load(const SnippetsLoadedCallback& callback) { |
| 40 DCHECK(callback_.is_null()); |
| 41 DCHECK(!callback.is_null()); |
| 42 callback_ = callback; |
| 43 if (database_ && database_initialized_) |
| 44 LoadImpl(); |
| 45 } |
| 46 |
| 47 void NTPSnippetsDatabase::Save(const NTPSnippet& snippet) { |
| 48 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 49 entries_to_save->emplace_back(snippet.id(), snippet.ToProto()); |
| 50 SaveImpl(std::move(entries_to_save)); |
| 51 } |
| 52 |
| 53 void NTPSnippetsDatabase::Save(const NTPSnippet::PtrVector& snippets) { |
| 54 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 55 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) |
| 56 entries_to_save->emplace_back(snippet->id(), snippet->ToProto()); |
| 57 SaveImpl(std::move(entries_to_save)); |
| 58 } |
| 59 |
| 60 void NTPSnippetsDatabase::Delete(const std::string& snippet_id) { |
| 61 DeleteImpl(base::WrapUnique(new std::vector<std::string>(1, snippet_id))); |
| 62 } |
| 63 |
| 64 void NTPSnippetsDatabase::Delete(const NTPSnippet::PtrVector& snippets) { |
| 65 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 66 new std::vector<std::string>()); |
| 67 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) |
| 68 keys_to_remove->emplace_back(snippet->id()); |
| 69 DeleteImpl(std::move(keys_to_remove)); |
| 70 } |
| 71 |
| 72 void NTPSnippetsDatabase::OnDatabaseInited(bool success) { |
| 73 DCHECK(!database_initialized_); |
| 74 if (!success) { |
| 75 DVLOG(1) << "NTPSnippetsDatabase init failed."; |
| 76 database_.reset(); |
| 77 return; |
| 78 } |
| 79 database_initialized_ = true; |
| 80 if (!callback_.is_null()) |
| 81 LoadImpl(); |
| 82 } |
| 83 |
| 84 void NTPSnippetsDatabase::OnDatabaseLoaded( |
| 85 bool success, |
| 86 std::unique_ptr<std::vector<SnippetProto>> entries) { |
| 87 if (!success) { |
| 88 DVLOG(1) << "NTPSnippetsDatabase load failed."; |
| 89 database_.reset(); |
| 90 return; |
| 91 } |
| 92 |
| 93 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 94 new std::vector<std::string>()); |
| 95 |
| 96 NTPSnippet::PtrVector snippets; |
| 97 for (const SnippetProto& proto : *entries) { |
| 98 std::unique_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromProto(proto); |
| 99 if (snippet) { |
| 100 snippets.emplace_back(std::move(snippet)); |
| 101 } else { |
| 102 LOG(WARNING) << "Invalid proto from DB " << proto.id(); |
| 103 keys_to_remove->emplace_back(proto.id()); |
| 104 } |
| 105 } |
| 106 |
| 107 // We only start loading if we have a non-null callback, but it's possible |
| 108 // that it's been cleared in the meantime. |
| 109 DCHECK(!callback_.is_null()); |
| 110 callback_.Run(std::move(snippets)); |
| 111 callback_.Reset(); |
| 112 |
| 113 // If any of the snippet protos couldn't be converted to actual snippets, |
| 114 // clean them up now. |
| 115 if (!keys_to_remove->empty()) |
| 116 DeleteImpl(std::move(keys_to_remove)); |
| 117 } |
| 118 |
| 119 void NTPSnippetsDatabase::OnDatabaseSaved(bool success) { |
| 120 if (!success) { |
| 121 DVLOG(1) << "NTPSnippetsDatabase save failed."; |
| 122 database_.reset(); |
| 123 } |
| 124 } |
| 125 |
| 126 void NTPSnippetsDatabase::LoadImpl() { |
| 127 DCHECK(database_); |
| 128 DCHECK(database_initialized_); |
| 129 database_->LoadEntries(base::Bind(&NTPSnippetsDatabase::OnDatabaseLoaded, |
| 130 weak_ptr_factory_.GetWeakPtr())); |
| 131 } |
| 132 |
| 133 void NTPSnippetsDatabase::SaveImpl( |
| 134 std::unique_ptr<KeyEntryVector> entries_to_save) { |
| 135 if (!database_ || !database_initialized_) |
| 136 return; |
| 137 |
| 138 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 139 new std::vector<std::string>()); |
| 140 database_->UpdateEntries(std::move(entries_to_save), |
| 141 std::move(keys_to_remove), |
| 142 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 143 weak_ptr_factory_.GetWeakPtr())); |
| 144 } |
| 145 |
| 146 void NTPSnippetsDatabase::DeleteImpl( |
| 147 std::unique_ptr<std::vector<std::string>> keys_to_remove) { |
| 148 if (!database_ || !database_initialized_) |
| 149 return; |
| 150 |
| 151 std::unique_ptr<KeyEntryVector> entries_to_save(new KeyEntryVector()); |
| 152 database_->UpdateEntries(std::move(entries_to_save), |
| 153 std::move(keys_to_remove), |
| 154 base::Bind(&NTPSnippetsDatabase::OnDatabaseSaved, |
| 155 weak_ptr_factory_.GetWeakPtr())); |
| 156 } |
| 157 |
| 158 } // namespace ntp_snippets |
| OLD | NEW |