| 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 | 10 |
| 11 namespace prefs { | 11 namespace prefs { |
| 12 | 12 |
| 13 PersistentPrefStoreClient::PersistentPrefStoreClient( | 13 PersistentPrefStoreClient::PersistentPrefStoreClient( |
| 14 mojom::PersistentPrefStoreConnectorPtr connector) | 14 mojom::PrefStoreConnectorPtr connector) |
| 15 : connector_(std::move(connector)) {} | 15 : connector_(std::move(connector)) { |
| 16 DCHECK(connector_); |
| 17 } |
| 18 |
| 19 PersistentPrefStoreClient::PersistentPrefStoreClient( |
| 20 mojom::PersistentPrefStoreConnectionPtr connection) { |
| 21 OnConnect(std::move(connection), |
| 22 std::unordered_map<PrefValueStore::PrefStoreType, |
| 23 prefs::mojom::PrefStoreConnectionPtr>()); |
| 24 } |
| 16 | 25 |
| 17 void PersistentPrefStoreClient::SetValue(const std::string& key, | 26 void PersistentPrefStoreClient::SetValue(const std::string& key, |
| 18 std::unique_ptr<base::Value> value, | 27 std::unique_ptr<base::Value> value, |
| 19 uint32_t flags) { | 28 uint32_t flags) { |
| 20 base::Value* old_value = nullptr; | 29 base::Value* old_value = nullptr; |
| 21 GetMutableValues().Get(key, &old_value); | 30 GetMutableValues().Get(key, &old_value); |
| 22 if (!old_value || !value->Equals(old_value)) { | 31 if (!old_value || !value->Equals(old_value)) { |
| 23 GetMutableValues().Set(key, std::move(value)); | 32 GetMutableValues().Set(key, std::move(value)); |
| 24 ReportValueChanged(key, flags); | 33 ReportValueChanged(key, flags); |
| 25 } | 34 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 bool PersistentPrefStoreClient::ReadOnly() const { | 67 bool PersistentPrefStoreClient::ReadOnly() const { |
| 59 return read_only_; | 68 return read_only_; |
| 60 } | 69 } |
| 61 | 70 |
| 62 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError() | 71 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::GetReadError() |
| 63 const { | 72 const { |
| 64 return read_error_; | 73 return read_error_; |
| 65 } | 74 } |
| 66 | 75 |
| 67 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::ReadPrefs() { | 76 PersistentPrefStore::PrefReadError PersistentPrefStoreClient::ReadPrefs() { |
| 68 PrefReadError read_error = PrefReadError::PREF_READ_ERROR_NONE; | 77 mojom::PersistentPrefStoreConnectionPtr connection; |
| 69 bool read_only = false; | 78 std::unordered_map<PrefValueStore::PrefStoreType, |
| 70 std::unique_ptr<base::DictionaryValue> local_prefs; | 79 prefs::mojom::PrefStoreConnectionPtr> |
| 71 mojom::PersistentPrefStorePtr pref_store; | 80 other_pref_stores; |
| 72 mojom::PrefStoreObserverRequest observer_request; | 81 if (!connector_->Connect(&connection, &other_pref_stores)) { |
| 73 if (!connector_->Connect(&read_error, &read_only, &local_prefs, &pref_store, | |
| 74 &observer_request)) { | |
| 75 NOTREACHED(); | 82 NOTREACHED(); |
| 76 } | 83 } |
| 77 | 84 |
| 78 OnCreateComplete(read_error, read_only, std::move(local_prefs), | 85 OnConnect(std::move(connection), std::move(other_pref_stores)); |
| 79 std::move(pref_store), std::move(observer_request)); | |
| 80 return read_error_; | 86 return read_error_; |
| 81 } | 87 } |
| 82 | 88 |
| 83 void PersistentPrefStoreClient::ReadPrefsAsync( | 89 void PersistentPrefStoreClient::ReadPrefsAsync( |
| 84 ReadErrorDelegate* error_delegate) { | 90 ReadErrorDelegate* error_delegate) { |
| 85 error_delegate_.reset(error_delegate); | 91 error_delegate_.reset(error_delegate); |
| 86 connector_->Connect(base::Bind(&PersistentPrefStoreClient::OnCreateComplete, | 92 connector_->Connect(base::Bind(&PersistentPrefStoreClient::OnConnect, |
| 87 base::Unretained(this))); | 93 base::Unretained(this))); |
| 88 } | 94 } |
| 89 | 95 |
| 90 void PersistentPrefStoreClient::CommitPendingWrite() { | 96 void PersistentPrefStoreClient::CommitPendingWrite() { |
| 91 DCHECK(pref_store_); | 97 DCHECK(pref_store_); |
| 92 pref_store_->CommitPendingWrite(); | 98 pref_store_->CommitPendingWrite(); |
| 93 } | 99 } |
| 94 | 100 |
| 95 void PersistentPrefStoreClient::SchedulePendingLossyWrites() { | 101 void PersistentPrefStoreClient::SchedulePendingLossyWrites() { |
| 96 DCHECK(pref_store_); | 102 DCHECK(pref_store_); |
| 97 return pref_store_->SchedulePendingLossyWrites(); | 103 return pref_store_->SchedulePendingLossyWrites(); |
| 98 } | 104 } |
| 99 | 105 |
| 100 void PersistentPrefStoreClient::ClearMutableValues() { | 106 void PersistentPrefStoreClient::ClearMutableValues() { |
| 101 DCHECK(pref_store_); | 107 DCHECK(pref_store_); |
| 102 return pref_store_->ClearMutableValues(); | 108 return pref_store_->ClearMutableValues(); |
| 103 } | 109 } |
| 104 | 110 |
| 105 PersistentPrefStoreClient::~PersistentPrefStoreClient() { | 111 PersistentPrefStoreClient::~PersistentPrefStoreClient() { |
| 106 if (!pref_store_) | 112 if (!pref_store_) |
| 107 return; | 113 return; |
| 108 | 114 |
| 109 pref_store_->CommitPendingWrite(); | 115 pref_store_->CommitPendingWrite(); |
| 110 } | 116 } |
| 111 | 117 |
| 112 void PersistentPrefStoreClient::OnCreateComplete( | 118 void PersistentPrefStoreClient::OnConnect( |
| 113 PrefReadError read_error, | 119 mojom::PersistentPrefStoreConnectionPtr connection, |
| 114 bool read_only, | 120 std::unordered_map<PrefValueStore::PrefStoreType, |
| 115 std::unique_ptr<base::DictionaryValue> cached_prefs, | 121 prefs::mojom::PrefStoreConnectionPtr> |
| 116 mojom::PersistentPrefStorePtr pref_store, | 122 other_pref_stores) { |
| 117 mojom::PrefStoreObserverRequest observer_request) { | |
| 118 connector_.reset(); | 123 connector_.reset(); |
| 119 read_error_ = read_error; | 124 read_error_ = connection->read_error; |
| 120 read_only_ = read_only; | 125 read_only_ = connection->read_only; |
| 121 pref_store_ = std::move(pref_store); | 126 pref_store_ = std::move(connection->pref_store); |
| 122 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 127 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 123 error_delegate_->OnError(read_error_); | 128 error_delegate_->OnError(read_error_); |
| 124 error_delegate_.reset(); | 129 error_delegate_.reset(); |
| 125 | 130 |
| 126 Init(std::move(cached_prefs), true, std::move(observer_request)); | 131 if (connection->pref_store_connection) { |
| 132 Init(std::move(connection->pref_store_connection->initial_prefs), true, |
| 133 std::move(connection->pref_store_connection->observer)); |
| 134 } else { |
| 135 Init(nullptr, false, nullptr); |
| 136 } |
| 127 } | 137 } |
| 128 | 138 |
| 129 } // namespace prefs | 139 } // namespace prefs |
| OLD | NEW |