OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/preferences/persistent_pref_store_impl.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/auto_reset.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/values.h" |
| 12 #include "components/prefs/persistent_pref_store.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 |
| 15 namespace prefs { |
| 16 |
| 17 class PersistentPrefStoreImpl::Connection : public mojom::PersistentPrefStore { |
| 18 public: |
| 19 Connection(PersistentPrefStoreImpl* pref_store, |
| 20 mojom::PersistentPrefStoreRequest request, |
| 21 mojom::PrefStoreObserverPtr observer) |
| 22 : pref_store_(pref_store), |
| 23 binding_(this, std::move(request)), |
| 24 observer_(std::move(observer)) { |
| 25 auto error_callback = |
| 26 base::Bind(&PersistentPrefStoreImpl::Connection::OnConnectionError, |
| 27 base::Unretained(this)); |
| 28 binding_.set_connection_error_handler(error_callback); |
| 29 observer_.set_connection_error_handler(error_callback); |
| 30 } |
| 31 |
| 32 ~Connection() override = default; |
| 33 |
| 34 void OnPrefValueChanged(const std::string& key, const base::Value* value) { |
| 35 if (write_in_progress_) |
| 36 return; |
| 37 |
| 38 observer_->OnPrefChanged(key, value ? value->CreateDeepCopy() : nullptr); |
| 39 } |
| 40 |
| 41 private: |
| 42 // mojom::PersistentPrefStore: |
| 43 void SetValue(const std::string& key, |
| 44 std::unique_ptr<base::Value> value, |
| 45 uint32_t flags) override { |
| 46 base::AutoReset<bool> scoped_call_in_progress(&write_in_progress_, true); |
| 47 pref_store_->SetValue(key, std::move(value), flags); |
| 48 } |
| 49 |
| 50 void CommitPendingWrite() override { pref_store_->CommitPendingWrite(); } |
| 51 void SchedulePendingLossyWrites() override { |
| 52 pref_store_->SchedulePendingLossyWrites(); |
| 53 } |
| 54 void ClearMutableValues() override { pref_store_->ClearMutableValues(); } |
| 55 |
| 56 void OnConnectionError() { pref_store_->OnConnectionError(this); } |
| 57 |
| 58 // Owns |this|. |
| 59 PersistentPrefStoreImpl* pref_store_; |
| 60 |
| 61 mojo::Binding<mojom::PersistentPrefStore> binding_; |
| 62 mojom::PrefStoreObserverPtr observer_; |
| 63 |
| 64 // If true then a write is in progress and any update notifications should be |
| 65 // ignored, as those updates would originate from ourselves. |
| 66 bool write_in_progress_ = false; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 69 }; |
| 70 |
| 71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( |
| 72 scoped_refptr<PersistentPrefStore> backing_pref_store, |
| 73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) |
| 74 : backing_pref_store_(backing_pref_store), |
| 75 validation_delegate_(std::move(validation_delegate)) { |
| 76 backing_pref_store_->AddObserver(this); |
| 77 if (!backing_pref_store_->IsInitializationComplete()) { |
| 78 initializing_ = true; |
| 79 backing_pref_store_->ReadPrefsAsync(nullptr); |
| 80 } |
| 81 } |
| 82 |
| 83 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { |
| 84 backing_pref_store_->RemoveObserver(this); |
| 85 } |
| 86 |
| 87 // mojom::PersistentPrefStoreConnector override: |
| 88 void PersistentPrefStoreImpl::Connect(const ConnectCallback& callback) { |
| 89 if (initializing_) { |
| 90 pending_connect_callbacks_.push_back(callback); |
| 91 return; |
| 92 } |
| 93 CallConnectCallback(callback); |
| 94 } |
| 95 |
| 96 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { |
| 97 // All mutations are triggered by a client. Updates are only sent to clients |
| 98 // other than the instigator so if there is only one client, it will ignore |
| 99 // the update. |
| 100 if (connections_.size() == 1) |
| 101 return; |
| 102 |
| 103 const base::Value* value = nullptr; |
| 104 backing_pref_store_->GetValue(key, &value); |
| 105 for (auto& entry : connections_) |
| 106 entry.first->OnPrefValueChanged(key, value); |
| 107 } |
| 108 |
| 109 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) { |
| 110 DCHECK(initializing_); |
| 111 initializing_ = false; |
| 112 for (const auto& callback : pending_connect_callbacks_) { |
| 113 CallConnectCallback(callback); |
| 114 } |
| 115 pending_connect_callbacks_.clear(); |
| 116 } |
| 117 |
| 118 void PersistentPrefStoreImpl::CallConnectCallback( |
| 119 const mojom::PersistentPrefStoreConnector::ConnectCallback& callback) { |
| 120 DCHECK(!initializing_); |
| 121 if (!backing_pref_store_->IsInitializationComplete()) { |
| 122 callback.Run(backing_pref_store_->GetReadError(), |
| 123 backing_pref_store_->ReadOnly(), nullptr, nullptr, nullptr); |
| 124 return; |
| 125 } |
| 126 mojom::PersistentPrefStorePtr pref_store_ptr; |
| 127 mojom::PrefStoreObserverPtr observer; |
| 128 mojom::PrefStoreObserverRequest observer_request = |
| 129 mojo::MakeRequest(&observer); |
| 130 auto connection = base::MakeUnique<Connection>( |
| 131 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer)); |
| 132 auto* connection_ptr = connection.get(); |
| 133 connections_.insert(std::make_pair(connection_ptr, std::move(connection))); |
| 134 callback.Run(backing_pref_store_->GetReadError(), |
| 135 backing_pref_store_->ReadOnly(), |
| 136 backing_pref_store_->GetValues(), std::move(pref_store_ptr), |
| 137 std::move(observer_request)); |
| 138 } |
| 139 |
| 140 void PersistentPrefStoreImpl::SetValue(const std::string& key, |
| 141 std::unique_ptr<base::Value> value, |
| 142 uint32_t flags) { |
| 143 if (value) |
| 144 backing_pref_store_->SetValue(key, std::move(value), flags); |
| 145 else |
| 146 backing_pref_store_->RemoveValue(key, flags); |
| 147 } |
| 148 |
| 149 void PersistentPrefStoreImpl::CommitPendingWrite() { |
| 150 backing_pref_store_->CommitPendingWrite(); |
| 151 } |
| 152 |
| 153 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() { |
| 154 backing_pref_store_->SchedulePendingLossyWrites(); |
| 155 } |
| 156 |
| 157 void PersistentPrefStoreImpl::ClearMutableValues() { |
| 158 backing_pref_store_->ClearMutableValues(); |
| 159 } |
| 160 |
| 161 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { |
| 162 connections_.erase(connection); |
| 163 } |
| 164 |
| 165 } // namespace prefs |
OLD | NEW |