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

Side by Side Diff: chrome/browser/chromeos/settings/device_settings_provider.cc

Issue 666363002: DeviceSettingsProvider's write path uses OwnerSettingsService now. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes. Created 6 years, 2 months 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/settings/device_settings_provider.h" 5 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 // Revert UI change. 472 // Revert UI change.
473 NotifyObservers(path); 473 NotifyObservers(path);
474 return; 474 return;
475 } 475 }
476 476
477 if (!IsDeviceSetting(path)) { 477 if (!IsDeviceSetting(path)) {
478 NOTREACHED() << "Try to set unhandled cros setting " << path; 478 NOTREACHED() << "Try to set unhandled cros setting " << path;
479 return; 479 return;
480 } 480 }
481 481
482 // TODO (ygorshenin@, crbug.com/230018)): use OwnerSettingsService::Set() 482 if (device_settings_service_->HasPrivateOwnerKey()) {
483 // here. 483 // Directly set setting through OwnerSettingsService.
484 pending_changes_.push_back(PendingQueueElement(path, in_value.DeepCopy())); 484 ownership::OwnerSettingsService* service =
485 if (!store_callback_factory_.HasWeakPtrs()) 485 device_settings_service_->GetOwnerSettingsService();
486 SetInPolicy(); 486 if (!service->Set(path, in_value)) {
487 NotifyObservers(path);
488 return;
489 }
490 } else {
491 // Temporary store new setting in
492 // |device_settings_|. |device_settings_| will be stored on a disk
493 // as soon as an ownership of device the will be taken.
494 OwnerSettingsServiceChromeOS::UpdateDeviceSettings(
495 path, in_value, device_settings_);
496 em::PolicyData data;
497 data.set_username(device_settings_service_->GetUsername());
498 CHECK(device_settings_.SerializeToString(data.mutable_policy_value()));
499
500 // Set the cache to the updated value.
501 UpdateValuesCache(data, device_settings_, TEMPORARILY_UNTRUSTED);
502
503 if (!device_settings_cache::Store(data, g_browser_process->local_state())) {
504 LOG(ERROR) << "Couldn't store to the temp storage.";
505 NotifyObservers(path);
506 return;
507 }
508 }
509
510 bool metrics_value;
511 if (path == kStatsReportingPref && in_value.GetAsBoolean(&metrics_value))
512 ApplyMetricsSetting(false, metrics_value);
487 } 513 }
488 514
489 void DeviceSettingsProvider::OwnershipStatusChanged() { 515 void DeviceSettingsProvider::OwnershipStatusChanged() {
490 DeviceSettingsService::OwnershipStatus new_ownership_status = 516 DeviceSettingsService::OwnershipStatus new_ownership_status =
491 device_settings_service_->GetOwnershipStatus(); 517 device_settings_service_->GetOwnershipStatus();
492 518
493 if (device_settings_service_->GetOwnerSettingsService()) 519 if (device_settings_service_->GetOwnerSettingsService())
494 device_settings_service_->GetOwnerSettingsService()->AddObserver(this); 520 device_settings_service_->GetOwnerSettingsService()->AddObserver(this);
495 521
496 // If the device just became owned, write the settings accumulated in the 522 // If the device just became owned, write the settings accumulated in the
497 // cache to device settings proper. It is important that writing only happens 523 // cache to device settings proper. It is important that writing only happens
498 // in this case, as during normal operation, the contents of the cache should 524 // in this case, as during normal operation, the contents of the cache should
499 // never overwrite actual device settings. 525 // never overwrite actual device settings.
500 if (new_ownership_status == DeviceSettingsService::OWNERSHIP_TAKEN && 526 if (new_ownership_status == DeviceSettingsService::OWNERSHIP_TAKEN &&
501 ownership_status_ == DeviceSettingsService::OWNERSHIP_NONE && 527 ownership_status_ == DeviceSettingsService::OWNERSHIP_NONE &&
502 device_settings_service_->HasPrivateOwnerKey()) { 528 device_settings_service_->HasPrivateOwnerKey()) {
503 // There shouldn't be any pending writes, since the cache writes are all 529 // There shouldn't be any pending writes, since the cache writes are all
504 // immediate. 530 // immediate.
505 DCHECK(!store_callback_factory_.HasWeakPtrs()); 531 DCHECK(!store_callback_factory_.HasWeakPtrs());
506 532
507 trusted_status_ = TEMPORARILY_UNTRUSTED; 533 trusted_status_ = TEMPORARILY_UNTRUSTED;
508 // Apply the locally-accumulated device settings on top of the initial 534 // Apply the locally-accumulated device settings on top of the initial
509 // settings from the service and write back the result. 535 // settings from the service and write back the result.
510 if (device_settings_service_->device_settings()) { 536 if (device_settings_service_->device_settings()) {
511 em::ChromeDeviceSettingsProto new_settings( 537 em::ChromeDeviceSettingsProto new_settings(
512 *device_settings_service_->device_settings()); 538 *device_settings_service_->device_settings());
513 new_settings.MergeFrom(device_settings_); 539 new_settings.MergeFrom(device_settings_);
514 device_settings_.Swap(&new_settings); 540 device_settings_.Swap(&new_settings);
515 } 541 }
516 StoreDeviceSettings(); 542
543 scoped_ptr<em::PolicyData> policy(new em::PolicyData());
544 policy->set_username(device_settings_service_->GetUsername());
545 CHECK(device_settings_.SerializeToString(policy->mutable_policy_value()));
546 if (!device_settings_service_->GetOwnerSettingsService()
547 ->CommitTentativeDeviceSettings(policy.Pass())) {
548 LOG(ERROR) << "Can't store policy";
549 }
517 } 550 }
518 551
519 // The owner key might have become available, allowing migration to happen. 552 // The owner key might have become available, allowing migration to happen.
520 AttemptMigration(); 553 AttemptMigration();
521 554
522 ownership_status_ = new_ownership_status; 555 ownership_status_ = new_ownership_status;
523 } 556 }
524 557
525 void DeviceSettingsProvider::DeviceSettingsUpdated() { 558 void DeviceSettingsProvider::DeviceSettingsUpdated() {
526 if (!store_callback_factory_.HasWeakPtrs()) 559 if (!store_callback_factory_.HasWeakPtrs())
(...skipping 11 matching lines...) Expand all
538 em::PolicyData policy_data; 571 em::PolicyData policy_data;
539 if (!device_settings_cache::Retrieve(&policy_data, 572 if (!device_settings_cache::Retrieve(&policy_data,
540 g_browser_process->local_state()) || 573 g_browser_process->local_state()) ||
541 !device_settings_.ParseFromString(policy_data.policy_value())) { 574 !device_settings_.ParseFromString(policy_data.policy_value())) {
542 VLOG(1) << "Can't retrieve temp store, possibly not created yet."; 575 VLOG(1) << "Can't retrieve temp store, possibly not created yet.";
543 } 576 }
544 577
545 UpdateValuesCache(policy_data, device_settings_, trusted_status_); 578 UpdateValuesCache(policy_data, device_settings_, trusted_status_);
546 } 579 }
547 580
548 void DeviceSettingsProvider::SetInPolicy() {
549 if (pending_changes_.empty()) {
550 NOTREACHED();
551 return;
552 }
553
554 if (RequestTrustedEntity() != TRUSTED) {
555 // Re-sync device settings before proceeding.
556 device_settings_service_->Load();
557 return;
558 }
559
560 std::string prop(pending_changes_.front().first);
561 scoped_ptr<base::Value> value(pending_changes_.front().second);
562 pending_changes_.pop_front();
563
564 trusted_status_ = TEMPORARILY_UNTRUSTED;
565 OwnerSettingsServiceChromeOS::UpdateDeviceSettings(
566 prop, *value, device_settings_);
567
568 bool metrics_value;
569 if (prop == kStatsReportingPref && value->GetAsBoolean(&metrics_value))
570 ApplyMetricsSetting(false, metrics_value);
571
572 em::PolicyData data;
573 data.set_username(device_settings_service_->GetUsername());
574 CHECK(device_settings_.SerializeToString(data.mutable_policy_value()));
575
576 // Set the cache to the updated value.
577 UpdateValuesCache(data, device_settings_, trusted_status_);
578
579 if (ownership_status_ == DeviceSettingsService::OWNERSHIP_TAKEN) {
580 StoreDeviceSettings();
581 } else {
582 if (!device_settings_cache::Store(data, g_browser_process->local_state()))
583 LOG(ERROR) << "Couldn't store to the temp storage.";
584
585 // OnStorePolicyCompleted won't get called in this case so proceed with any
586 // pending operations immediately.
587 if (!pending_changes_.empty())
588 SetInPolicy();
589 }
590 }
591
592 void DeviceSettingsProvider::UpdateValuesCache( 581 void DeviceSettingsProvider::UpdateValuesCache(
593 const em::PolicyData& policy_data, 582 const em::PolicyData& policy_data,
594 const em::ChromeDeviceSettingsProto& settings, 583 const em::ChromeDeviceSettingsProto& settings,
595 TrustedStatus trusted_status) { 584 TrustedStatus trusted_status) {
596 PrefValueMap new_values_cache; 585 PrefValueMap new_values_cache;
597 586
598 // If the device is not managed, or is consumer-managed, we set the device 587 // If the device is not managed, or is consumer-managed, we set the device
599 // owner value. 588 // owner value.
600 if (policy_data.has_username() && 589 if (policy_data.has_username() &&
601 (!policy_data.has_request_token() || 590 (!policy_data.has_request_token() ||
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 DeviceSettingsProvider::TrustedStatus 714 DeviceSettingsProvider::TrustedStatus
726 DeviceSettingsProvider::RequestTrustedEntity() { 715 DeviceSettingsProvider::RequestTrustedEntity() {
727 if (ownership_status_ == DeviceSettingsService::OWNERSHIP_NONE) 716 if (ownership_status_ == DeviceSettingsService::OWNERSHIP_NONE)
728 return TRUSTED; 717 return TRUSTED;
729 return trusted_status_; 718 return trusted_status_;
730 } 719 }
731 720
732 void DeviceSettingsProvider::UpdateAndProceedStoring() { 721 void DeviceSettingsProvider::UpdateAndProceedStoring() {
733 // Re-sync the cache from the service. 722 // Re-sync the cache from the service.
734 UpdateFromService(); 723 UpdateFromService();
735
736 // Trigger the next change if necessary.
737 if (trusted_status_ == TRUSTED && !pending_changes_.empty())
738 SetInPolicy();
739 } 724 }
740 725
741 bool DeviceSettingsProvider::UpdateFromService() { 726 bool DeviceSettingsProvider::UpdateFromService() {
742 bool settings_loaded = false; 727 bool settings_loaded = false;
743 switch (device_settings_service_->status()) { 728 switch (device_settings_service_->status()) {
744 case DeviceSettingsService::STORE_SUCCESS: { 729 case DeviceSettingsService::STORE_SUCCESS: {
745 const em::PolicyData* policy_data = 730 const em::PolicyData* policy_data =
746 device_settings_service_->policy_data(); 731 device_settings_service_->policy_data();
747 const em::ChromeDeviceSettingsProto* device_settings = 732 const em::ChromeDeviceSettingsProto* device_settings =
748 device_settings_service_->device_settings(); 733 device_settings_service_->device_settings();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
792 777
793 // Notify the observers we are done. 778 // Notify the observers we are done.
794 std::vector<base::Closure> callbacks; 779 std::vector<base::Closure> callbacks;
795 callbacks.swap(callbacks_); 780 callbacks.swap(callbacks_);
796 for (size_t i = 0; i < callbacks.size(); ++i) 781 for (size_t i = 0; i < callbacks.size(); ++i)
797 callbacks[i].Run(); 782 callbacks[i].Run();
798 783
799 return settings_loaded; 784 return settings_loaded;
800 } 785 }
801 786
802 void DeviceSettingsProvider::StoreDeviceSettings() {
803 // Mute all previous callbacks to guarantee the |pending_changes_| queue is
804 // processed serially.
805 store_callback_factory_.InvalidateWeakPtrs();
806
807 device_settings_service_->SignAndStore(
808 scoped_ptr<em::ChromeDeviceSettingsProto>(
809 new em::ChromeDeviceSettingsProto(device_settings_)),
810 base::Bind(&DeviceSettingsProvider::UpdateAndProceedStoring,
811 store_callback_factory_.GetWeakPtr()));
812 }
813
814 void DeviceSettingsProvider::AttemptMigration() { 787 void DeviceSettingsProvider::AttemptMigration() {
815 if (device_settings_service_->HasPrivateOwnerKey()) { 788 if (device_settings_service_->HasPrivateOwnerKey()) {
816 PrefValueMap::const_iterator i; 789 PrefValueMap::const_iterator i;
817 for (i = migration_values_.begin(); i != migration_values_.end(); ++i) 790 for (i = migration_values_.begin(); i != migration_values_.end(); ++i)
818 DoSet(i->first, *i->second); 791 DoSet(i->first, *i->second);
819 migration_values_.Clear(); 792 migration_values_.Clear();
820 } 793 }
821 } 794 }
822 795
823 } // namespace chromeos 796 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/settings/device_settings_provider.h ('k') | components/ownership/owner_settings_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698