Chromium Code Reviews| 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/persistent_pref_store_impl.h" | 5 #include "services/preferences/persistent_pref_store_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 | 63 |
| 64 // If true then a write is in progress and any update notifications should be | 64 // If true then a write is in progress and any update notifications should be |
| 65 // ignored, as those updates would originate from ourselves. | 65 // ignored, as those updates would originate from ourselves. |
| 66 bool write_in_progress_ = false; | 66 bool write_in_progress_ = false; |
| 67 | 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Connection); | 68 DISALLOW_COPY_AND_ASSIGN(Connection); |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( | 71 PersistentPrefStoreImpl::PersistentPrefStoreImpl( |
| 72 scoped_refptr<PersistentPrefStore> backing_pref_store, | 72 scoped_refptr<PersistentPrefStore> backing_pref_store, |
| 73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) | 73 mojom::TrackedPreferenceValidationDelegatePtr validation_delegate, |
| 74 base::OnceClosure on_initialized) | |
| 74 : backing_pref_store_(backing_pref_store), | 75 : backing_pref_store_(backing_pref_store), |
| 75 validation_delegate_(std::move(validation_delegate)) { | 76 validation_delegate_(std::move(validation_delegate)) { |
| 76 backing_pref_store_->AddObserver(this); | 77 backing_pref_store_->AddObserver(this); |
| 77 if (!backing_pref_store_->IsInitializationComplete()) { | 78 if (!backing_pref_store_->IsInitializationComplete()) { |
| 79 on_initialized_ = std::move(on_initialized); | |
| 78 initializing_ = true; | 80 initializing_ = true; |
| 79 backing_pref_store_->ReadPrefsAsync(nullptr); | 81 backing_pref_store_->ReadPrefsAsync(nullptr); |
| 80 } | 82 } |
| 81 } | 83 } |
| 82 | 84 |
| 83 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { | 85 PersistentPrefStoreImpl::~PersistentPrefStoreImpl() { |
| 84 backing_pref_store_->RemoveObserver(this); | 86 backing_pref_store_->RemoveObserver(this); |
| 85 } | 87 } |
| 86 | 88 |
| 87 // mojom::PersistentPrefStoreConnector override: | 89 // mojom::PersistentPrefStoreConnector override: |
| 88 void PersistentPrefStoreImpl::Connect(const ConnectCallback& callback) { | 90 mojom::PersistentPrefStoreConnectionPtr PersistentPrefStoreImpl::Connect() { |
|
tibell
2017/03/23 02:22:04
Perhaps call it CreateConnection.
Sam McNally
2017/03/23 02:36:26
Done.
| |
| 89 if (initializing_) { | 91 DCHECK(!initializing_); |
| 90 pending_connect_callbacks_.push_back(callback); | 92 if (!backing_pref_store_->IsInitializationComplete()) { |
| 91 return; | 93 return mojom::PersistentPrefStoreConnection::New( |
|
tibell
2017/03/23 02:22:04
Add a comment saying this is the init failure bran
Sam McNally
2017/03/23 02:36:26
Done.
| |
| 94 nullptr, nullptr, backing_pref_store_->GetReadError(), | |
| 95 backing_pref_store_->ReadOnly()); | |
| 92 } | 96 } |
| 93 CallConnectCallback(callback); | 97 mojom::PersistentPrefStorePtr pref_store_ptr; |
| 98 mojom::PrefStoreObserverPtr observer; | |
| 99 mojom::PrefStoreObserverRequest observer_request = | |
| 100 mojo::MakeRequest(&observer); | |
| 101 auto connection = base::MakeUnique<Connection>( | |
| 102 this, mojo::MakeRequest(&pref_store_ptr), std::move(observer)); | |
| 103 auto* connection_ptr = connection.get(); | |
| 104 connections_.insert(std::make_pair(connection_ptr, std::move(connection))); | |
| 105 return mojom::PersistentPrefStoreConnection::New( | |
| 106 mojom::PrefStoreConnection::New(std::move(observer_request), | |
| 107 backing_pref_store_->GetValues(), true), | |
| 108 std::move(pref_store_ptr), backing_pref_store_->GetReadError(), | |
| 109 backing_pref_store_->ReadOnly()); | |
| 94 } | 110 } |
| 95 | 111 |
| 96 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { | 112 void PersistentPrefStoreImpl::OnPrefValueChanged(const std::string& key) { |
| 97 // All mutations are triggered by a client. Updates are only sent to clients | 113 // 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 | 114 // other than the instigator so if there is only one client, it will ignore |
| 99 // the update. | 115 // the update. |
| 100 if (connections_.size() == 1) | 116 if (connections_.size() == 1) |
| 101 return; | 117 return; |
| 102 | 118 |
| 103 const base::Value* value = nullptr; | 119 const base::Value* value = nullptr; |
| 104 backing_pref_store_->GetValue(key, &value); | 120 backing_pref_store_->GetValue(key, &value); |
| 105 for (auto& entry : connections_) | 121 for (auto& entry : connections_) |
| 106 entry.first->OnPrefValueChanged(key, value); | 122 entry.first->OnPrefValueChanged(key, value); |
| 107 } | 123 } |
| 108 | 124 |
| 109 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) { | 125 void PersistentPrefStoreImpl::OnInitializationCompleted(bool succeeded) { |
| 110 DCHECK(initializing_); | 126 DCHECK(initializing_); |
| 111 initializing_ = false; | 127 initializing_ = false; |
| 112 for (const auto& callback : pending_connect_callbacks_) { | 128 std::move(on_initialized_).Run(); |
| 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 } | 129 } |
| 139 | 130 |
| 140 void PersistentPrefStoreImpl::SetValue(const std::string& key, | 131 void PersistentPrefStoreImpl::SetValue(const std::string& key, |
| 141 std::unique_ptr<base::Value> value, | 132 std::unique_ptr<base::Value> value, |
| 142 uint32_t flags) { | 133 uint32_t flags) { |
| 143 if (value) | 134 if (value) |
| 144 backing_pref_store_->SetValue(key, std::move(value), flags); | 135 backing_pref_store_->SetValue(key, std::move(value), flags); |
| 145 else | 136 else |
| 146 backing_pref_store_->RemoveValue(key, flags); | 137 backing_pref_store_->RemoveValue(key, flags); |
| 147 } | 138 } |
| 148 | 139 |
| 149 void PersistentPrefStoreImpl::CommitPendingWrite() { | 140 void PersistentPrefStoreImpl::CommitPendingWrite() { |
| 150 backing_pref_store_->CommitPendingWrite(); | 141 backing_pref_store_->CommitPendingWrite(); |
| 151 } | 142 } |
| 152 | 143 |
| 153 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() { | 144 void PersistentPrefStoreImpl::SchedulePendingLossyWrites() { |
| 154 backing_pref_store_->SchedulePendingLossyWrites(); | 145 backing_pref_store_->SchedulePendingLossyWrites(); |
| 155 } | 146 } |
| 156 | 147 |
| 157 void PersistentPrefStoreImpl::ClearMutableValues() { | 148 void PersistentPrefStoreImpl::ClearMutableValues() { |
| 158 backing_pref_store_->ClearMutableValues(); | 149 backing_pref_store_->ClearMutableValues(); |
| 159 } | 150 } |
| 160 | 151 |
| 161 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { | 152 void PersistentPrefStoreImpl::OnConnectionError(Connection* connection) { |
| 162 connections_.erase(connection); | 153 connections_.erase(connection); |
| 163 } | 154 } |
| 164 | 155 |
| 165 } // namespace prefs | 156 } // namespace prefs |
| OLD | NEW |