| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/preferences/public/cpp/persistent_pref_store_client.h" | 5 #include "services/preferences/public/cpp/persistent_pref_store_client.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "components/prefs/pref_registry.h" | 10 #include "components/prefs/pref_registry.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 other_pref_stores) { | 139 other_pref_stores) { |
| 140 connector_.reset(); | 140 connector_.reset(); |
| 141 read_error_ = connection->read_error; | 141 read_error_ = connection->read_error; |
| 142 read_only_ = connection->read_only; | 142 read_only_ = connection->read_only; |
| 143 pref_store_ = std::move(connection->pref_store); | 143 pref_store_ = std::move(connection->pref_store); |
| 144 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 144 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 145 error_delegate_->OnError(read_error_); | 145 error_delegate_->OnError(read_error_); |
| 146 error_delegate_.reset(); | 146 error_delegate_.reset(); |
| 147 | 147 |
| 148 if (connection->pref_store_connection) { | 148 if (connection->pref_store_connection) { |
| 149 Init(std::move(connection->pref_store_connection->initial_prefs), true, | 149 Init(&connection->pref_store_connection->initial_prefs, true, |
| 150 std::move(connection->pref_store_connection->observer)); | 150 std::move(connection->pref_store_connection->observer)); |
| 151 } else { | 151 } else { |
| 152 Init(nullptr, false, nullptr); | 152 Init(nullptr, false, nullptr); |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 | 155 |
| 156 void PersistentPrefStoreClient::QueueWrite(const std::string& key, | 156 void PersistentPrefStoreClient::QueueWrite(const std::string& key, |
| 157 uint32_t flags) { | 157 uint32_t flags) { |
| 158 if (pending_writes_.empty()) { | 158 if (pending_writes_.empty()) { |
| 159 // Use a weak pointer since a pending write should not prolong the life of | 159 // Use a weak pointer since a pending write should not prolong the life of |
| 160 // |this|. Instead, the destruction of |this| will flush any pending writes. | 160 // |this|. Instead, the destruction of |this| will flush any pending writes. |
| 161 base::ThreadTaskRunnerHandle::Get()->PostTask( | 161 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 162 FROM_HERE, base::Bind(&PersistentPrefStoreClient::FlushPendingWrites, | 162 FROM_HERE, base::Bind(&PersistentPrefStoreClient::FlushPendingWrites, |
| 163 weak_factory_.GetWeakPtr())); | 163 weak_factory_.GetWeakPtr())); |
| 164 } | 164 } |
| 165 pending_writes_.insert(std::make_pair(key, flags)); | 165 pending_writes_.insert(std::make_pair(key, flags)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void PersistentPrefStoreClient::FlushPendingWrites() { | 168 void PersistentPrefStoreClient::FlushPendingWrites() { |
| 169 std::vector<mojom::PrefUpdatePtr> updates; | 169 std::vector<mojom::PrefUpdatePtr> updates; |
| 170 for (const auto& pref : pending_writes_) { | 170 for (const auto& pref : pending_writes_) { |
| 171 const base::Value* value = nullptr; | 171 const base::Value* value = nullptr; |
| 172 if (GetValue(pref.first, &value)) { | 172 if (GetValue(pref.first, &value)) { |
| 173 updates.push_back(mojom::PrefUpdate::New( | 173 updates.push_back( |
| 174 pref.first, value->CreateDeepCopy(), pref.second)); | 174 mojom::PrefUpdate::New(pref.first, *value, pref.second)); |
| 175 } else { | 175 } else { |
| 176 updates.push_back( | 176 updates.push_back( |
| 177 mojom::PrefUpdate::New(pref.first, nullptr, pref.second)); | 177 mojom::PrefUpdate::New(pref.first, base::nullopt, pref.second)); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 pref_store_->SetValues(std::move(updates)); | 180 pref_store_->SetValues(std::move(updates)); |
| 181 pending_writes_.clear(); | 181 pending_writes_.clear(); |
| 182 } | 182 } |
| 183 | 183 |
| 184 } // namespace prefs | 184 } // namespace prefs |
| OLD | NEW |