1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
6 #define COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
12 #include "base/feature_list.h"
13 #include "base/functional/callback_forward.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/threading/thread_checker.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "components/ownership/ownership_export.h"
20 #include "components/policy/proto/device_management_backend.pb.h"
32 // Feature flag to toggle Chrome-side owner key generation (see
33 // go/generate-owner-key-in-chrome). If enabled, Chrome will take the
34 // responsibility of generating the owner key from session_manager. If disabled,
35 // Chrome will still load/generate the owner key using the new code in parallel,
36 // but the result will be discarded (see OwnerKeyDarkLaunchTracker).
38 BASE_DECLARE_FEATURE(kChromeSideOwnerKeyGeneration);
40 // This class is a common interface for platform-specific classes
41 // which deal with ownership, keypairs and owner-related settings.
42 class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService {
46 virtual ~Observer() {}
48 // Called when signed policy was stored, or when an error happed during
50 virtual void OnSignedPolicyStored(bool success) {}
52 // Called when tentative changes were made to policy, but the
53 // policy is not signed and stored yet.
55 // TODO (ygorshenin@, crbug.com/230018): get rid of the method
56 // since it creates DeviceSettingsService's dependency on
57 // OwnerSettingsService.
58 virtual void OnTentativeChangesInPolicy(
59 const enterprise_management::PolicyData& policy_data) {}
62 typedef base::OnceCallback<void(
63 scoped_refptr<ownership::PublicKey>,
64 std::unique_ptr<enterprise_management::PolicyFetchResponse>)>
65 AssembleAndSignPolicyAsyncCallback;
67 using IsOwnerCallback = base::OnceCallback<void(bool is_owner)>;
69 explicit OwnerSettingsService(
70 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
72 OwnerSettingsService(const OwnerSettingsService&) = delete;
73 OwnerSettingsService& operator=(const OwnerSettingsService&) = delete;
75 ~OwnerSettingsService() override;
77 base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
78 return weak_factory_.GetWeakPtr();
81 void AddObserver(Observer* observer);
83 void RemoveObserver(Observer* observer);
85 // Returns whether this OwnerSettingsService has finished loading keys, and so
86 // we are able to confirm whether the current user is the owner or not.
87 virtual bool IsReady();
89 // Returns whether current user is owner or not - as long as IsReady()
90 // returns true. When IsReady() is false, we don't yet know if the current
91 // user is the owner or not. In that case this method returns false.
92 virtual bool IsOwner();
94 // Determines whether current user is owner or not, responds via |callback|.
95 // Reliably returns the correct value, but will not respond on the callback
96 // until IsReady() returns true.
97 virtual void IsOwnerAsync(IsOwnerCallback callback);
99 // Assembles and signs |policy| on the |task_runner|, responds on
100 // the original thread via |callback|.
101 bool AssembleAndSignPolicyAsync(
102 base::TaskRunner* task_runner,
103 std::unique_ptr<enterprise_management::PolicyData> policy,
104 AssembleAndSignPolicyAsyncCallback callback);
106 // Checks whether |setting| is handled by OwnerSettingsService.
107 virtual bool HandlesSetting(const std::string& setting) = 0;
109 // Sets |setting| value to |value|.
110 virtual bool Set(const std::string& setting, const base::Value& value) = 0;
112 // Convenience functions for manipulating lists. Note that the following
113 // functions employs a read, modify and write pattern. If there're
114 // pending updates to |setting|, value cache they read from might not
115 // be fresh and multiple calls to those function would lose data.
116 virtual bool AppendToList(const std::string& setting,
117 const base::Value& value) = 0;
118 virtual bool RemoveFromList(const std::string& setting,
119 const base::Value& value) = 0;
121 // Sets a bunch of device settings accumulated before ownership gets
124 // TODO (ygorshenin@, crbug.com/230018): that this is a temporary
125 // solution and should be removed.
126 virtual bool CommitTentativeDeviceSettings(
127 std::unique_ptr<enterprise_management::PolicyData> policy) = 0;
129 bool SetBoolean(const std::string& setting, bool value);
130 bool SetInteger(const std::string& setting, int value);
131 bool SetDouble(const std::string& setting, double value);
132 bool SetString(const std::string& setting, const std::string& value);
134 // Run callbacks in test setting. Mocks ownership when full device setup is
136 void RunPendingIsOwnerCallbacksForTesting(bool is_owner);
139 void ReloadKeypair();
141 // Stores the provided keys. Ensures that |public_key_| and |private_key_| are
142 // not null (even if the key objects themself are empty) to indicate that the
143 // key loading finished.
144 void OnKeypairLoaded(scoped_refptr<PublicKey> public_key,
145 scoped_refptr<PrivateKey> private_key);
147 // Platform-specific keypair loading algorithm.
148 virtual void ReloadKeypairImpl(
149 base::OnceCallback<void(scoped_refptr<PublicKey> public_key,
150 scoped_refptr<PrivateKey> private_key)>
153 // Plafrom-specific actions which should be performed when keypair is loaded.
154 virtual void OnPostKeypairLoadedActions() = 0;
156 scoped_refptr<ownership::PublicKey> public_key_;
158 scoped_refptr<ownership::PrivateKey> private_key_;
160 scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
162 std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
164 base::ObserverList<Observer>::Unchecked observers_;
166 base::ThreadChecker thread_checker_;
169 base::WeakPtrFactory<OwnerSettingsService> weak_factory_{this};
172 } // namespace ownership
174 #endif // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_