Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 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 | |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 8 | #include <memory> |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 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" |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 16 | #include "base/observer_list.h" |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 17 | #include "base/threading/thread_checker.h" |
| 18 | #include "components/keyed_service/core/keyed_service.h" |
| 19 | #include "components/ownership/ownership_export.h" |
brettw | 39d6ba4 | 2016-08-24 16:56:38 | [diff] [blame] | 20 | #include "components/policy/proto/device_management_backend.pb.h" |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | class TaskRunner; |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 24 | class Value; |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 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: |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 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 | |
ygorshenin | 45f512bd | 2014-11-10 15:13:45 | [diff] [blame] | 44 | // Called when tentative changes were made to policy, but the |
| 45 | // policy is not signed and stored yet. |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 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::Callback<void( |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 55 | std::unique_ptr<enterprise_management::PolicyFetchResponse> |
| 56 | policy_response)> |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 57 | AssembleAndSignPolicyAsyncCallback; |
| 58 | |
| 59 | typedef base::Callback<void(bool is_owner)> IsOwnerCallback; |
| 60 | |
| 61 | explicit OwnerSettingsService( |
| 62 | const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 63 | ~OwnerSettingsService() override; |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 64 | |
| 65 | base::WeakPtr<OwnerSettingsService> as_weak_ptr() { |
| 66 | return weak_factory_.GetWeakPtr(); |
| 67 | } |
| 68 | |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 69 | void AddObserver(Observer* observer); |
| 70 | |
| 71 | void RemoveObserver(Observer* observer); |
| 72 | |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 73 | // Returns whether current user is owner or not. When this method |
| 74 | // is called too early, incorrect result can be returned because |
| 75 | // private key loading may be in progress. |
Roman Sorokin | d6ffafb | 2018-05-11 10:31:38 | [diff] [blame] | 76 | virtual bool IsOwner(); |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 77 | |
| 78 | // Determines whether current user is owner or not, responds via |
| 79 | // |callback|. |
Roman Sorokin | d6ffafb | 2018-05-11 10:31:38 | [diff] [blame] | 80 | virtual void IsOwnerAsync(const IsOwnerCallback& callback); |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 81 | |
| 82 | // Assembles and signs |policy| on the |task_runner|, responds on |
| 83 | // the original thread via |callback|. |
| 84 | bool AssembleAndSignPolicyAsync( |
| 85 | base::TaskRunner* task_runner, |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 86 | std::unique_ptr<enterprise_management::PolicyData> policy, |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 87 | const AssembleAndSignPolicyAsyncCallback& callback); |
| 88 | |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 89 | // Checks whether |setting| is handled by OwnerSettingsService. |
| 90 | virtual bool HandlesSetting(const std::string& setting) = 0; |
| 91 | |
| 92 | // Sets |setting| value to |value|. |
| 93 | virtual bool Set(const std::string& setting, const base::Value& value) = 0; |
| 94 | |
ygorshenin | 84b2ec0 | 2014-12-25 18:37:11 | [diff] [blame] | 95 | // Convenience functions for manipulating lists. Note that the following |
| 96 | // functions employs a read, modify and write pattern. If there're |
| 97 | // pending updates to |setting|, value cache they read from might not |
| 98 | // be fresh and multiple calls to those function would lose data. |
| 99 | virtual bool AppendToList(const std::string& setting, |
| 100 | const base::Value& value) = 0; |
| 101 | virtual bool RemoveFromList(const std::string& setting, |
| 102 | const base::Value& value) = 0; |
| 103 | |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 104 | // Sets a bunch of device settings accumulated before ownership gets |
| 105 | // established. |
| 106 | // |
| 107 | // TODO (ygorshenin@, crbug.com/230018): that this is a temporary |
| 108 | // solution and should be removed. |
| 109 | virtual bool CommitTentativeDeviceSettings( |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 110 | std::unique_ptr<enterprise_management::PolicyData> policy) = 0; |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 111 | |
| 112 | bool SetBoolean(const std::string& setting, bool value); |
| 113 | bool SetInteger(const std::string& setting, int value); |
| 114 | bool SetDouble(const std::string& setting, double value); |
| 115 | bool SetString(const std::string& setting, const std::string& value); |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 116 | |
| 117 | protected: |
| 118 | void ReloadKeypair(); |
| 119 | |
| 120 | void OnKeypairLoaded(const scoped_refptr<PublicKey>& public_key, |
| 121 | const scoped_refptr<PrivateKey>& private_key); |
| 122 | |
| 123 | // Platform-specific keypair loading algorithm. |
| 124 | virtual void ReloadKeypairImpl(const base::Callback< |
| 125 | void(const scoped_refptr<PublicKey>& public_key, |
| 126 | const scoped_refptr<PrivateKey>& private_key)>& callback) = 0; |
| 127 | |
| 128 | // Plafrom-specific actions which should be performed when keypair is loaded. |
| 129 | virtual void OnPostKeypairLoadedActions() = 0; |
| 130 | |
| 131 | scoped_refptr<ownership::PublicKey> public_key_; |
| 132 | |
| 133 | scoped_refptr<ownership::PrivateKey> private_key_; |
| 134 | |
| 135 | scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_; |
| 136 | |
| 137 | std::vector<IsOwnerCallback> pending_is_owner_callbacks_; |
| 138 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 139 | base::ObserverList<Observer>::Unchecked observers_; |
ygorshenin | 0e5fbf43 | 2014-10-27 09:10:56 | [diff] [blame] | 140 | |
Yuri Gorshenin | c7bf410d | 2014-09-15 13:07:49 | [diff] [blame] | 141 | base::ThreadChecker thread_checker_; |
| 142 | |
| 143 | private: |
| 144 | base::WeakPtrFactory<OwnerSettingsService> weak_factory_; |
| 145 | |
| 146 | DISALLOW_COPY_AND_ASSIGN(OwnerSettingsService); |
| 147 | }; |
| 148 | |
| 149 | } // namespace ownership |
| 150 | |
| 151 | #endif // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_ |