From 5fa0ee22683b92d67e07c060e8c7a3c4cced51ee Mon Sep 17 00:00:00 2001 From: ygorshenin Date: Fri, 24 Oct 2014 10:23:11 -0700 Subject: Implemented OwnerSettingsService::Set() method. BUG=230018 TEST=unit_tests:OwnerSettingsServiceChromeOSTest.* Review URL: https://codereview.chromium.org/654263003 Cr-Commit-Position: refs/heads/master@{#301132} --- components/ownership/owner_settings_service.cc | 57 +++++++++++++++++++++----- components/ownership/owner_settings_service.h | 55 +++++++++++++++++++++---- 2 files changed, 94 insertions(+), 18 deletions(-) (limited to 'components/ownership') diff --git a/components/ownership/owner_settings_service.cc b/components/ownership/owner_settings_service.cc index 56bcbe0..204cdd1 100644 --- a/components/ownership/owner_settings_service.cc +++ b/components/ownership/owner_settings_service.cc @@ -12,6 +12,7 @@ #include "base/message_loop/message_loop.h" #include "base/task_runner.h" #include "base/task_runner_util.h" +#include "base/values.h" #include "components/ownership/owner_key_util.h" #include "crypto/signature_creator.h" @@ -21,13 +22,15 @@ namespace ownership { namespace { -std::string AssembleAndSignPolicy(scoped_ptr policy, - crypto::RSAPrivateKey* private_key) { +scoped_ptr AssembleAndSignPolicy( + scoped_ptr policy, + crypto::RSAPrivateKey* private_key) { // Assemble the policy. - em::PolicyFetchResponse policy_response; - if (!policy->SerializeToString(policy_response.mutable_policy_data())) { + scoped_ptr policy_response( + new em::PolicyFetchResponse()); + if (!policy->SerializeToString(policy_response->mutable_policy_data())) { LOG(ERROR) << "Failed to encode policy payload."; - return std::string(); + return scoped_ptr(nullptr).Pass(); } // Generate the signature. @@ -35,19 +38,19 @@ std::string AssembleAndSignPolicy(scoped_ptr policy, crypto::SignatureCreator::Create(private_key, crypto::SignatureCreator::SHA1)); signature_creator->Update( - reinterpret_cast(policy_response.policy_data().c_str()), - policy_response.policy_data().size()); + reinterpret_cast(policy_response->policy_data().c_str()), + policy_response->policy_data().size()); std::vector signature_bytes; std::string policy_blob; if (!signature_creator->Final(&signature_bytes)) { LOG(ERROR) << "Failed to create policy signature."; - return std::string(); + return scoped_ptr(nullptr).Pass(); } - policy_response.mutable_policy_data_signature()->assign( + policy_response->mutable_policy_data_signature()->assign( reinterpret_cast(vector_as_array(&signature_bytes)), signature_bytes.size()); - return policy_response.SerializeAsString(); + return policy_response.Pass(); } } // namepace @@ -61,6 +64,15 @@ OwnerSettingsService::~OwnerSettingsService() { DCHECK(thread_checker_.CalledOnValidThread()); } +void OwnerSettingsService::AddObserver(Observer* observer) { + if (observer && !observers_.HasObserver(observer)) + observers_.AddObserver(observer); +} + +void OwnerSettingsService::RemoveObserver(Observer* observer) { + observers_.RemoveObserver(observer); +} + bool OwnerSettingsService::IsOwner() { DCHECK(thread_checker_.CalledOnValidThread()); return private_key_.get() && private_key_->key(); @@ -91,6 +103,31 @@ bool OwnerSettingsService::AssembleAndSignPolicyAsync( callback); } +bool OwnerSettingsService::SetBoolean(const std::string& setting, bool value) { + DCHECK(thread_checker_.CalledOnValidThread()); + base::FundamentalValue in_value(value); + return Set(setting, in_value); +} + +bool OwnerSettingsService::SetInteger(const std::string& setting, int value) { + DCHECK(thread_checker_.CalledOnValidThread()); + base::FundamentalValue in_value(value); + return Set(setting, in_value); +} + +bool OwnerSettingsService::SetDouble(const std::string& setting, double value) { + DCHECK(thread_checker_.CalledOnValidThread()); + base::FundamentalValue in_value(value); + return Set(setting, in_value); +} + +bool OwnerSettingsService::SetString(const std::string& setting, + const std::string& value) { + DCHECK(thread_checker_.CalledOnValidThread()); + base::StringValue in_value(value); + return Set(setting, in_value); +} + void OwnerSettingsService::ReloadKeypair() { ReloadKeypairImpl( base::Bind(&OwnerSettingsService::OnKeypairLoaded, as_weak_ptr())); diff --git a/components/ownership/owner_settings_service.h b/components/ownership/owner_settings_service.h index 1961975..db36595 100644 --- a/components/ownership/owner_settings_service.h +++ b/components/ownership/owner_settings_service.h @@ -13,6 +13,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" +#include "base/observer_list.h" #include "base/threading/thread_checker.h" #include "components/keyed_service/core/keyed_service.h" #include "components/ownership/ownership_export.h" @@ -20,6 +21,7 @@ namespace base { class TaskRunner; +class Value; } namespace ownership { @@ -31,19 +33,42 @@ class PublicKey; // which deal with ownership, keypairs and owner-related settings. class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService { public: - typedef base::Callback + class Observer { + public: + virtual ~Observer() {} + + // Called when signed policy was stored, or when an error happed during + // policy storage.. + virtual void OnSignedPolicyStored(bool success) {} + + // Called when tentative changes were made to policy, but the policy still + // not signed and stored. + // + // TODO (ygorshenin@, crbug.com/230018): get rid of the method + // since it creates DeviceSettingsService's dependency on + // OwnerSettingsService. + virtual void OnTentativeChangesInPolicy( + const enterprise_management::PolicyData& policy_data) {} + }; + + typedef base::Callback policy_response)> AssembleAndSignPolicyAsyncCallback; typedef base::Callback IsOwnerCallback; explicit OwnerSettingsService( const scoped_refptr& owner_key_util); - ~OwnerSettingsService() override; + virtual ~OwnerSettingsService(); base::WeakPtr as_weak_ptr() { return weak_factory_.GetWeakPtr(); } + void AddObserver(Observer* observer); + + void RemoveObserver(Observer* observer); + // Returns whether current user is owner or not. When this method // is called too early, incorrect result can be returned because // private key loading may be in progress. @@ -60,12 +85,24 @@ class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService { scoped_ptr policy, const AssembleAndSignPolicyAsyncCallback& callback); - // Signs |settings| with the private half of the owner key and sends - // the resulting policy blob for storage. The - // result of the operation is reported through |callback|. - virtual void SignAndStorePolicyAsync( - scoped_ptr policy, - const base::Closure& callback) = 0; + // Checks whether |setting| is handled by OwnerSettingsService. + virtual bool HandlesSetting(const std::string& setting) = 0; + + // Sets |setting| value to |value|. + virtual bool Set(const std::string& setting, const base::Value& value) = 0; + + // Sets a bunch of device settings accumulated before ownership gets + // established. + // + // TODO (ygorshenin@, crbug.com/230018): that this is a temporary + // solution and should be removed. + virtual bool CommitTentativeDeviceSettings( + scoped_ptr policy) = 0; + + bool SetBoolean(const std::string& setting, bool value); + bool SetInteger(const std::string& setting, int value); + bool SetDouble(const std::string& setting, double value); + bool SetString(const std::string& setting, const std::string& value); protected: void ReloadKeypair(); @@ -89,6 +126,8 @@ class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService { std::vector pending_is_owner_callbacks_; + ObserverList observers_; + base::ThreadChecker thread_checker_; private: -- cgit v1.1