#ifndef CHROME_BROWSER_ASH_OWNERSHIP_OWNER_SETTINGS_SERVICE_ASH_H_
#define CHROME_BROWSER_ASH_OWNERSHIP_OWNER_SETTINGS_SERVICE_ASH_H_
#include <string>
#include <unordered_map>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/values.h"
#include "chrome/browser/ash/settings/device_settings_service.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/ownership/owner_key_util.h"
#include "components/ownership/owner_settings_service.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
class Profile;
namespace content {
class WebUI;
}
namespace ownership {
class OwnerKeyUtil;
}
namespace ash {
class OwnerKeyLoader;
class OwnerSettingsServiceAsh : public ownership::OwnerSettingsService,
public ProfileManagerObserver,
public SessionManagerClient::Observer,
public DeviceSettingsService::Observer {
public:
struct ManagementSettings {
ManagementSettings();
~ManagementSettings();
std::string request_token;
std::string device_id;
};
OwnerSettingsServiceAsh(
DeviceSettingsService* device_settings_service,
Profile* profile,
const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
OwnerSettingsServiceAsh(const OwnerSettingsServiceAsh&) = delete;
OwnerSettingsServiceAsh& operator=(const OwnerSettingsServiceAsh&) = delete;
~OwnerSettingsServiceAsh() override;
static OwnerSettingsServiceAsh* FromWebUI(content::WebUI* web_ui);
void OnTPMTokenReady();
void OnEasyUnlockKeyOpsFinished();
bool HasPendingChanges() const;
bool IsOwner() override;
void IsOwnerAsync(IsOwnerCallback callback) override;
bool HandlesSetting(const std::string& setting) override;
bool Set(const std::string& setting, const base::Value& value) override;
bool AppendToList(const std::string& setting,
const base::Value& value) override;
bool RemoveFromList(const std::string& setting,
const base::Value& value) override;
bool CommitTentativeDeviceSettings(
std::unique_ptr<enterprise_management::PolicyData> policy) override;
void OnProfileAdded(Profile* profile) override;
void OnProfileManagerDestroying() override;
void OwnerKeySet(bool success) override;
void OwnershipStatusChanged() override;
void DeviceSettingsUpdated() override;
void OnDeviceSettingsServiceShutdown() override;
static void IsOwnerForSafeModeAsync(
const std::string& user_hash,
const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util,
IsOwnerCallback callback);
static std::unique_ptr<enterprise_management::PolicyData> AssemblePolicy(
const std::string& user_id,
const enterprise_management::PolicyData* policy_data,
enterprise_management::ChromeDeviceSettingsProto* settings);
static void UpdateDeviceSettings(
const std::string& path,
const base::Value& value,
enterprise_management::ChromeDeviceSettingsProto& settings);
void SetPrivateKeyForTesting(
scoped_refptr<ownership::PrivateKey> private_key);
private:
friend class OwnerSettingsServiceAshFactory;
static void FixupLocalOwnerPolicy(
const std::string& user_id,
enterprise_management::ChromeDeviceSettingsProto* settings);
void ReloadKeypairImpl(
base::OnceCallback<void(scoped_refptr<ownership::PublicKey> public_key,
scoped_refptr<ownership::PrivateKey> private_key)>
callback) override;
void OnReloadedKeypairImpl(
base::OnceCallback<void(scoped_refptr<ownership::PublicKey>,
scoped_refptr<ownership::PrivateKey>)> callback,
scoped_refptr<ownership::PublicKey> public_key,
scoped_refptr<ownership::PrivateKey> private_key);
void OnPostKeypairLoadedActions() override;
void StorePendingChanges();
base::Value::List GetListForSetting(const std::string& setting) const;
void OnPolicyAssembledAndSigned(
scoped_refptr<ownership::PublicKey> public_key,
std::unique_ptr<enterprise_management::PolicyFetchResponse>
policy_response);
void OnSignedPolicyStored(scoped_refptr<ownership::PublicKey> public_key,
bool success);
void ReportStatusAndContinueStoring(bool success);
void MigrateFeatureFlags(
enterprise_management::ChromeDeviceSettingsProto* settings);
raw_ptr<DeviceSettingsService> device_settings_service_;
raw_ptr<Profile> profile_;
std::string user_id_;
bool waiting_for_tpm_token_ = true;
bool has_pending_fixups_ = false;
std::unordered_map<std::string, std::unique_ptr<base::Value>>
pending_changes_;
std::unique_ptr<enterprise_management::ChromeDeviceSettingsProto>
tentative_settings_;
std::unique_ptr<OwnerKeyLoader> owner_key_loader_;
crypto::ScopedSECKEYPrivateKey old_owner_key_;
base::ScopedObservation<ProfileManager, ProfileManagerObserver>
profile_manager_observation_{this};
base::WeakPtrFactory<OwnerSettingsServiceAsh> weak_factory_{this};
base::WeakPtrFactory<OwnerSettingsServiceAsh> store_settings_factory_{this};
};
}
#endif