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
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/callback_forward.h"
13 #include "base/macros.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 // This class is a common interface for platform-specific classes
33 // which deal with ownership, keypairs and owner-related settings.
34 class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService {
38 virtual ~Observer() {}
40 // Called when signed policy was stored, or when an error happed during
42 virtual void OnSignedPolicyStored(bool success) {}
44 // Called when tentative changes were made to policy, but the
45 // policy is not signed and stored yet.
47 // TODO (ygorshenin@, crbug.com/230018): get rid of the method
48 // since it creates DeviceSettingsService's dependency on
49 // OwnerSettingsService.
50 virtual void OnTentativeChangesInPolicy(
51 const enterprise_management::PolicyData& policy_data) {}
54 typedef base::OnceCallback<void(
55 std::unique_ptr<enterprise_management::PolicyFetchResponse>
57 AssembleAndSignPolicyAsyncCallback;
59 using IsOwnerCallback = base::OnceCallback<void(bool is_owner)>;
61 explicit OwnerSettingsService(
62 const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
63 ~OwnerSettingsService() override;
65 base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
66 return weak_factory_.GetWeakPtr();
69 void AddObserver(Observer* observer);
71 void RemoveObserver(Observer* observer);
73 // Returns whether this OwnerSettingsService has finished loading keys, and so
74 // we are able to confirm whether the current user is the owner or not.
75 virtual bool IsReady();
77 // Returns whether current user is owner or not - as long as IsReady()
78 // returns true. When IsReady() is false, we don't yet know if the current
79 // user is the owner or not. In that case this method returns false.
80 virtual bool IsOwner();
82 // Determines whether current user is owner or not, responds via |callback|.
83 // Reliably returns the correct value, but will not respond on the callback
84 // until IsReady() returns true.
85 virtual void IsOwnerAsync(IsOwnerCallback callback);
87 // Assembles and signs |policy| on the |task_runner|, responds on
88 // the original thread via |callback|.
89 bool AssembleAndSignPolicyAsync(
90 base::TaskRunner* task_runner,
91 std::unique_ptr<enterprise_management::PolicyData> policy,
92 AssembleAndSignPolicyAsyncCallback callback);
94 // Checks whether |setting| is handled by OwnerSettingsService.
95 virtual bool HandlesSetting(const std::string& setting) = 0;
97 // Sets |setting| value to |value|.
98 virtual bool Set(const std::string& setting, const base::Value& value) = 0;
100 // Convenience functions for manipulating lists. Note that the following
101 // functions employs a read, modify and write pattern. If there're
102 // pending updates to |setting|, value cache they read from might not
103 // be fresh and multiple calls to those function would lose data.
104 virtual bool AppendToList(const std::string& setting,
105 const base::Value& value) = 0;
106 virtual bool RemoveFromList(const std::string& setting,
107 const base::Value& value) = 0;
109 // Sets a bunch of device settings accumulated before ownership gets
112 // TODO (ygorshenin@, crbug.com/230018): that this is a temporary
113 // solution and should be removed.
114 virtual bool CommitTentativeDeviceSettings(
115 std::unique_ptr<enterprise_management::PolicyData> policy) = 0;
117 bool SetBoolean(const std::string& setting, bool value);
118 bool SetInteger(const std::string& setting, int value);
119 bool SetDouble(const std::string& setting, double value);
120 bool SetString(const std::string& setting, const std::string& value);
123 void ReloadKeypair();
125 void OnKeypairLoaded(const scoped_refptr<PublicKey>& public_key,
126 const scoped_refptr<PrivateKey>& private_key);
128 // Platform-specific keypair loading algorithm.
129 virtual void ReloadKeypairImpl(const base::Callback<
130 void(const scoped_refptr<PublicKey>& public_key,
131 const scoped_refptr<PrivateKey>& private_key)>& callback) = 0;
133 // Plafrom-specific actions which should be performed when keypair is loaded.
134 virtual void OnPostKeypairLoadedActions() = 0;
136 scoped_refptr<ownership::PublicKey> public_key_;
138 scoped_refptr<ownership::PrivateKey> private_key_;
140 scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
142 std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
144 base::ObserverList<Observer>::Unchecked observers_;
146 base::ThreadChecker thread_checker_;
149 base::WeakPtrFactory<OwnerSettingsService> weak_factory_{this};
151 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsService);
154 } // namespace ownership
156 #endif // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_