Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / components / ownership / owner_settings_service.h
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.
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/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"
21
22 namespace base {
23 class TaskRunner;
24 class Value;
25 }
26
27 namespace ownership {
28 class OwnerKeyUtil;
29 class PrivateKey;
30 class PublicKey;
31
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 {
35  public:
36   class Observer {
37    public:
38     virtual ~Observer() {}
39
40     // Called when signed policy was stored, or when an error happed during
41     // policy storage..
42     virtual void OnSignedPolicyStored(bool success) {}
43
44     // Called when tentative changes were made to policy, but the
45     // policy is not signed and stored yet.
46     //
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) {}
52   };
53
54   typedef base::OnceCallback<void(
55       std::unique_ptr<enterprise_management::PolicyFetchResponse>
56           policy_response)>
57       AssembleAndSignPolicyAsyncCallback;
58
59   using IsOwnerCallback = base::OnceCallback<void(bool is_owner)>;
60
61   explicit OwnerSettingsService(
62       const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
63   ~OwnerSettingsService() override;
64
65   base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
66     return weak_factory_.GetWeakPtr();
67   }
68
69   void AddObserver(Observer* observer);
70
71   void RemoveObserver(Observer* observer);
72
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();
76
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();
81
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);
86
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);
93
94   // Checks whether |setting| is handled by OwnerSettingsService.
95   virtual bool HandlesSetting(const std::string& setting) = 0;
96
97   // Sets |setting| value to |value|.
98   virtual bool Set(const std::string& setting, const base::Value& value) = 0;
99
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;
108
109   // Sets a bunch of device settings accumulated before ownership gets
110   // established.
111   //
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;
116
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);
121
122   // Run callbacks in test setting. Mocks ownership when full device setup is
123   // not needed.
124   void RunPendingIsOwnerCallbacksForTesting(bool is_owner);
125
126  protected:
127   void ReloadKeypair();
128
129   void OnKeypairLoaded(const scoped_refptr<PublicKey>& public_key,
130                        const scoped_refptr<PrivateKey>& private_key);
131
132   // Platform-specific keypair loading algorithm.
133   virtual void ReloadKeypairImpl(
134       base::OnceCallback<void(const scoped_refptr<PublicKey>& public_key,
135                               const scoped_refptr<PrivateKey>& private_key)>
136           callback) = 0;
137
138   // Plafrom-specific actions which should be performed when keypair is loaded.
139   virtual void OnPostKeypairLoadedActions() = 0;
140
141   scoped_refptr<ownership::PublicKey> public_key_;
142
143   scoped_refptr<ownership::PrivateKey> private_key_;
144
145   scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
146
147   std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
148
149   base::ObserverList<Observer>::Unchecked observers_;
150
151   base::ThreadChecker thread_checker_;
152
153  private:
154   base::WeakPtrFactory<OwnerSettingsService> weak_factory_{this};
155
156   DISALLOW_COPY_AND_ASSIGN(OwnerSettingsService);
157 };
158
159 }  // namespace ownership
160
161 #endif  // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_