Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / ownership / owner_settings_service.h
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.
4
5 #ifndef COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
6 #define COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
7
8 #include <memory>
9 #include <string>
10 #include <vector>
11
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"
21
22 namespace base {
23 class TaskRunner;
24 class Value;
25 }  // namespace base
26
27 namespace ownership {
28 class OwnerKeyUtil;
29 class PrivateKey;
30 class PublicKey;
31
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).
37 OWNERSHIP_EXPORT
38 BASE_DECLARE_FEATURE(kChromeSideOwnerKeyGeneration);
39
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 {
43  public:
44   class Observer {
45    public:
46     virtual ~Observer() {}
47
48     // Called when signed policy was stored, or when an error happed during
49     // policy storage..
50     virtual void OnSignedPolicyStored(bool success) {}
51
52     // Called when tentative changes were made to policy, but the
53     // policy is not signed and stored yet.
54     //
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) {}
60   };
61
62   typedef base::OnceCallback<void(
63       scoped_refptr<ownership::PublicKey>,
64       std::unique_ptr<enterprise_management::PolicyFetchResponse>)>
65       AssembleAndSignPolicyAsyncCallback;
66
67   using IsOwnerCallback = base::OnceCallback<void(bool is_owner)>;
68
69   explicit OwnerSettingsService(
70       const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
71
72   OwnerSettingsService(const OwnerSettingsService&) = delete;
73   OwnerSettingsService& operator=(const OwnerSettingsService&) = delete;
74
75   ~OwnerSettingsService() override;
76
77   base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
78     return weak_factory_.GetWeakPtr();
79   }
80
81   void AddObserver(Observer* observer);
82
83   void RemoveObserver(Observer* observer);
84
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();
88
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();
93
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);
98
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);
105
106   // Checks whether |setting| is handled by OwnerSettingsService.
107   virtual bool HandlesSetting(const std::string& setting) = 0;
108
109   // Sets |setting| value to |value|.
110   virtual bool Set(const std::string& setting, const base::Value& value) = 0;
111
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;
120
121   // Sets a bunch of device settings accumulated before ownership gets
122   // established.
123   //
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;
128
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);
133
134   // Run callbacks in test setting. Mocks ownership when full device setup is
135   // not needed.
136   void RunPendingIsOwnerCallbacksForTesting(bool is_owner);
137
138  protected:
139   void ReloadKeypair();
140
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);
146
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)>
151           callback) = 0;
152
153   // Plafrom-specific actions which should be performed when keypair is loaded.
154   virtual void OnPostKeypairLoadedActions() = 0;
155
156   scoped_refptr<ownership::PublicKey> public_key_;
157
158   scoped_refptr<ownership::PrivateKey> private_key_;
159
160   scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
161
162   std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
163
164   base::ObserverList<Observer>::Unchecked observers_;
165
166   base::ThreadChecker thread_checker_;
167
168  private:
169   base::WeakPtrFactory<OwnerSettingsService> weak_factory_{this};
170 };
171
172 }  // namespace ownership
173
174 #endif  // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_