Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/browser/chromeos/ownership/owner_settings_service_chromeos.cc

Issue 695193004: Added key-value storage for pending changes to OwnerSettingsServiceChromeOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h" 5 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 OwnerSettingsServiceChromeOS::OwnerSettingsServiceChromeOS( 151 OwnerSettingsServiceChromeOS::OwnerSettingsServiceChromeOS(
152 DeviceSettingsService* device_settings_service, 152 DeviceSettingsService* device_settings_service,
153 Profile* profile, 153 Profile* profile,
154 const scoped_refptr<OwnerKeyUtil>& owner_key_util) 154 const scoped_refptr<OwnerKeyUtil>& owner_key_util)
155 : ownership::OwnerSettingsService(owner_key_util), 155 : ownership::OwnerSettingsService(owner_key_util),
156 device_settings_service_(device_settings_service), 156 device_settings_service_(device_settings_service),
157 profile_(profile), 157 profile_(profile),
158 waiting_for_profile_creation_(true), 158 waiting_for_profile_creation_(true),
159 waiting_for_tpm_token_(true), 159 waiting_for_tpm_token_(true),
160 has_pending_changes_(false),
161 weak_factory_(this), 160 weak_factory_(this),
162 store_settings_factory_(this) { 161 store_settings_factory_(this) {
163 if (TPMTokenLoader::IsInitialized()) { 162 if (TPMTokenLoader::IsInitialized()) {
164 TPMTokenLoader::TPMTokenStatus tpm_token_status = 163 TPMTokenLoader::TPMTokenStatus tpm_token_status =
165 TPMTokenLoader::Get()->IsTPMTokenEnabled( 164 TPMTokenLoader::Get()->IsTPMTokenEnabled(
166 base::Bind(&OwnerSettingsServiceChromeOS::OnTPMTokenReady, 165 base::Bind(&OwnerSettingsServiceChromeOS::OnTPMTokenReady,
167 weak_factory_.GetWeakPtr())); 166 weak_factory_.GetWeakPtr()));
168 waiting_for_tpm_token_ = 167 waiting_for_tpm_token_ =
169 tpm_token_status == TPMTokenLoader::TPM_TOKEN_STATUS_UNDETERMINED; 168 tpm_token_status == TPMTokenLoader::TPM_TOKEN_STATUS_UNDETERMINED;
170 } 169 }
171 170
172 if (DBusThreadManager::IsInitialized() && 171 if (DBusThreadManager::IsInitialized() &&
173 DBusThreadManager::Get()->GetSessionManagerClient()) { 172 DBusThreadManager::Get()->GetSessionManagerClient()) {
174 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); 173 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this);
175 } 174 }
176 175
177 if (device_settings_service_) 176 if (device_settings_service_)
178 device_settings_service_->AddObserver(this); 177 device_settings_service_->AddObserver(this);
179 178
180 registrar_.Add(this, 179 registrar_.Add(this,
181 chrome::NOTIFICATION_PROFILE_CREATED, 180 chrome::NOTIFICATION_PROFILE_CREATED,
182 content::Source<Profile>(profile_)); 181 content::Source<Profile>(profile_));
183
184 UpdateFromService();
185 } 182 }
186 183
187 OwnerSettingsServiceChromeOS::~OwnerSettingsServiceChromeOS() { 184 OwnerSettingsServiceChromeOS::~OwnerSettingsServiceChromeOS() {
188 DCHECK(thread_checker_.CalledOnValidThread()); 185 DCHECK(thread_checker_.CalledOnValidThread());
189 186
190 if (device_settings_service_) 187 if (device_settings_service_)
191 device_settings_service_->RemoveObserver(this); 188 device_settings_service_->RemoveObserver(this);
192 189
193 if (DBusThreadManager::IsInitialized() && 190 if (DBusThreadManager::IsInitialized() &&
194 DBusThreadManager::Get()->GetSessionManagerClient()) { 191 DBusThreadManager::Get()->GetSessionManagerClient()) {
(...skipping 13 matching lines...) Expand all
208 205
209 bool OwnerSettingsServiceChromeOS::HandlesSetting(const std::string& setting) { 206 bool OwnerSettingsServiceChromeOS::HandlesSetting(const std::string& setting) {
210 return DeviceSettingsProvider::IsDeviceSetting(setting); 207 return DeviceSettingsProvider::IsDeviceSetting(setting);
211 } 208 }
212 209
213 bool OwnerSettingsServiceChromeOS::Set(const std::string& setting, 210 bool OwnerSettingsServiceChromeOS::Set(const std::string& setting,
214 const base::Value& value) { 211 const base::Value& value) {
215 if (!IsOwner() && !IsOwnerInTests(user_id_)) 212 if (!IsOwner() && !IsOwnerInTests(user_id_))
216 return false; 213 return false;
217 214
218 UpdateDeviceSettings(setting, value, device_settings_); 215 pending_changes_.add(setting, make_scoped_ptr(value.DeepCopy()));
219 em::PolicyData policy_data; 216 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, observers_,
220 policy_data.set_username(user_id_); 217 OnTentativeChangeInSettings(setting, value));
221 CHECK(device_settings_.SerializeToString(policy_data.mutable_policy_value())); 218 StorePendingChanges();
222 FOR_EACH_OBSERVER(OwnerSettingsService::Observer,
223 observers_,
224 OnTentativeChangesInPolicy(policy_data));
225 has_pending_changes_ = true;
226 StoreDeviceSettings();
227 return true; 219 return true;
228 } 220 }
229 221
230 bool OwnerSettingsServiceChromeOS::CommitTentativeDeviceSettings( 222 bool OwnerSettingsServiceChromeOS::CommitTentativeDeviceSettings(
231 scoped_ptr<enterprise_management::PolicyData> policy) { 223 scoped_ptr<enterprise_management::PolicyData> policy) {
232 if (!IsOwner() && !IsOwnerInTests(user_id_)) 224 if (!IsOwner() && !IsOwnerInTests(user_id_))
233 return false; 225 return false;
234 if (policy->username() != user_id_) { 226 if (policy->username() != user_id_) {
235 LOG(ERROR) << "Username mismatch: " << policy->username() << " vs. " 227 LOG(ERROR) << "Username mismatch: " << policy->username() << " vs. "
236 << user_id_; 228 << user_id_;
237 return false; 229 return false;
238 } 230 }
239 CHECK(device_settings_.ParseFromString(policy->policy_value())); 231 tentative_settings_.reset(new em::ChromeDeviceSettingsProto);
240 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, 232 CHECK(tentative_settings_->ParseFromString(policy->policy_value()));
241 observers_, 233 StorePendingChanges();
242 OnTentativeChangesInPolicy(*policy));
243 has_pending_changes_ = true;
244 StoreDeviceSettings();
245 return true; 234 return true;
246 } 235 }
247 236
248 void OwnerSettingsServiceChromeOS::Observe( 237 void OwnerSettingsServiceChromeOS::Observe(
249 int type, 238 int type,
250 const content::NotificationSource& source, 239 const content::NotificationSource& source,
251 const content::NotificationDetails& details) { 240 const content::NotificationDetails& details) {
252 DCHECK(thread_checker_.CalledOnValidThread()); 241 DCHECK(thread_checker_.CalledOnValidThread());
253 if (type != chrome::NOTIFICATION_PROFILE_CREATED) { 242 if (type != chrome::NOTIFICATION_PROFILE_CREATED) {
254 NOTREACHED(); 243 NOTREACHED();
(...skipping 11 matching lines...) Expand all
266 } 255 }
267 256
268 void OwnerSettingsServiceChromeOS::OwnerKeySet(bool success) { 257 void OwnerSettingsServiceChromeOS::OwnerKeySet(bool success) {
269 DCHECK(thread_checker_.CalledOnValidThread()); 258 DCHECK(thread_checker_.CalledOnValidThread());
270 if (success) 259 if (success)
271 ReloadKeypair(); 260 ReloadKeypair();
272 } 261 }
273 262
274 void OwnerSettingsServiceChromeOS::OwnershipStatusChanged() { 263 void OwnerSettingsServiceChromeOS::OwnershipStatusChanged() {
275 DCHECK(thread_checker_.CalledOnValidThread()); 264 DCHECK(thread_checker_.CalledOnValidThread());
276 StoreDeviceSettings(); 265 StorePendingChanges();
277 } 266 }
278 267
279 void OwnerSettingsServiceChromeOS::DeviceSettingsUpdated() { 268 void OwnerSettingsServiceChromeOS::DeviceSettingsUpdated() {
280 DCHECK(thread_checker_.CalledOnValidThread()); 269 DCHECK(thread_checker_.CalledOnValidThread());
281 StoreDeviceSettings(); 270 StorePendingChanges();
282 } 271 }
283 272
284 void OwnerSettingsServiceChromeOS::OnDeviceSettingsServiceShutdown() { 273 void OwnerSettingsServiceChromeOS::OnDeviceSettingsServiceShutdown() {
285 device_settings_service_ = nullptr; 274 device_settings_service_ = nullptr;
286 } 275 }
287 276
288 // static 277 // static
289 void OwnerSettingsServiceChromeOS::IsOwnerForSafeModeAsync( 278 void OwnerSettingsServiceChromeOS::IsOwnerForSafeModeAsync(
290 const std::string& user_hash, 279 const std::string& user_hash,
291 const scoped_refptr<OwnerKeyUtil>& owner_key_util, 280 const scoped_refptr<OwnerKeyUtil>& owner_key_util,
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( 568 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
580 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 569 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
581 task_runner->PostTask( 570 task_runner->PostTask(
582 FROM_HERE, 571 FROM_HERE,
583 base::Bind(&LoadPrivateKey, 572 base::Bind(&LoadPrivateKey,
584 owner_key_util_, 573 owner_key_util_,
585 ProfileHelper::GetUserIdHashFromProfile(profile_), 574 ProfileHelper::GetUserIdHashFromProfile(profile_),
586 callback)); 575 callback));
587 } 576 }
588 577
589 void OwnerSettingsServiceChromeOS::StoreDeviceSettings() { 578 void OwnerSettingsServiceChromeOS::StorePendingChanges() {
590 if (!has_pending_changes_ || store_settings_factory_.HasWeakPtrs()) 579 if (!has_pending_changes() || store_settings_factory_.HasWeakPtrs() ||
580 !device_settings_service_) {
591 return; 581 return;
592 if (!UpdateFromService()) 582 }
583
584 em::ChromeDeviceSettingsProto settings;
585 if (tentative_settings_.get()) {
586 settings.Swap(tentative_settings_.get());
587 tentative_settings_.reset();
588 } else if (device_settings_service_->status() ==
589 DeviceSettingsService::STORE_SUCCESS &&
590 device_settings_service_->device_settings()) {
591 settings = *device_settings_service_->device_settings();
592 } else {
593 return; 593 return;
594 }
595
596 for (const auto& change : pending_changes_)
597 UpdateDeviceSettings(change.first, *change.second, settings);
598 pending_changes_.clear();
599
594 scoped_ptr<em::PolicyData> policy = AssemblePolicy( 600 scoped_ptr<em::PolicyData> policy = AssemblePolicy(
595 user_id_, device_settings_service_->policy_data(), &device_settings_); 601 user_id_, device_settings_service_->policy_data(), &settings);
596 has_pending_changes_ = false;
597 bool rv = AssembleAndSignPolicyAsync( 602 bool rv = AssembleAndSignPolicyAsync(
598 content::BrowserThread::GetBlockingPool(), 603 content::BrowserThread::GetBlockingPool(), policy.Pass(),
599 policy.Pass(),
600 base::Bind(&OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned, 604 base::Bind(&OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned,
601 store_settings_factory_.GetWeakPtr())); 605 store_settings_factory_.GetWeakPtr()));
602 if (!rv) 606 if (!rv)
603 OnSignedPolicyStored(false /* success */); 607 OnSignedPolicyStored(false /* success */);
604 } 608 }
605 609
606 void OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned( 610 void OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned(
607 scoped_ptr<em::PolicyFetchResponse> policy_response) { 611 scoped_ptr<em::PolicyFetchResponse> policy_response) {
608 if (!policy_response.get() || !device_settings_service_) { 612 if (!policy_response.get() || !device_settings_service_) {
609 OnSignedPolicyStored(false /* success */); 613 OnSignedPolicyStored(false /* success */);
610 return; 614 return;
611 } 615 }
612 device_settings_service_->Store( 616 device_settings_service_->Store(
613 policy_response.Pass(), 617 policy_response.Pass(),
614 base::Bind(&OwnerSettingsServiceChromeOS::OnSignedPolicyStored, 618 base::Bind(&OwnerSettingsServiceChromeOS::OnSignedPolicyStored,
615 store_settings_factory_.GetWeakPtr(), 619 store_settings_factory_.GetWeakPtr(),
616 true /* success */)); 620 true /* success */));
617 } 621 }
618 622
619 void OwnerSettingsServiceChromeOS::OnSignedPolicyStored(bool success) { 623 void OwnerSettingsServiceChromeOS::OnSignedPolicyStored(bool success) {
620 store_settings_factory_.InvalidateWeakPtrs(); 624 store_settings_factory_.InvalidateWeakPtrs();
621 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, 625 FOR_EACH_OBSERVER(OwnerSettingsService::Observer,
622 observers_, 626 observers_,
623 OnSignedPolicyStored(success)); 627 OnSignedPolicyStored(success));
624 StoreDeviceSettings(); 628 StorePendingChanges();
625 if (!success)
626 has_pending_changes_ = true;
627 }
628
629 bool OwnerSettingsServiceChromeOS::UpdateFromService() {
630 if (!device_settings_service_ ||
631 device_settings_service_->status() !=
632 DeviceSettingsService::STORE_SUCCESS ||
633 !device_settings_service_->device_settings()) {
634 return false;
635 }
636 enterprise_management::ChromeDeviceSettingsProto settings =
637 *device_settings_service_->device_settings();
638 settings.MergeFrom(device_settings_);
639 device_settings_.Swap(&settings);
640 return true;
641 } 629 }
642 630
643 } // namespace chromeos 631 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698