owner_settings_service.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
  5. #define COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "components/keyed_service/core/keyed_service.h"
  15. #include "components/ownership/ownership_export.h"
  16. #include "components/policy/proto/device_management_backend.pb.h"
  17. namespace base {
  18. class TaskRunner;
  19. class Value;
  20. }
  21. namespace ownership {
  22. class OwnerKeyUtil;
  23. class PrivateKey;
  24. class PublicKey;
  25. // This class is a common interface for platform-specific classes
  26. // which deal with ownership, keypairs and owner-related settings.
  27. class OWNERSHIP_EXPORT OwnerSettingsService : public KeyedService {
  28. public:
  29. class Observer {
  30. public:
  31. virtual ~Observer() {}
  32. // Called when signed policy was stored, or when an error happed during
  33. // policy storage..
  34. virtual void OnSignedPolicyStored(bool success) {}
  35. // Called when tentative changes were made to policy, but the
  36. // policy is not signed and stored yet.
  37. //
  38. // TODO (ygorshenin@, crbug.com/230018): get rid of the method
  39. // since it creates DeviceSettingsService's dependency on
  40. // OwnerSettingsService.
  41. virtual void OnTentativeChangesInPolicy(
  42. const enterprise_management::PolicyData& policy_data) {}
  43. };
  44. typedef base::OnceCallback<void(
  45. std::unique_ptr<enterprise_management::PolicyFetchResponse>
  46. policy_response)>
  47. AssembleAndSignPolicyAsyncCallback;
  48. using IsOwnerCallback = base::OnceCallback<void(bool is_owner)>;
  49. explicit OwnerSettingsService(
  50. const scoped_refptr<ownership::OwnerKeyUtil>& owner_key_util);
  51. OwnerSettingsService(const OwnerSettingsService&) = delete;
  52. OwnerSettingsService& operator=(const OwnerSettingsService&) = delete;
  53. ~OwnerSettingsService() override;
  54. base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
  55. return weak_factory_.GetWeakPtr();
  56. }
  57. void AddObserver(Observer* observer);
  58. void RemoveObserver(Observer* observer);
  59. // Returns whether this OwnerSettingsService has finished loading keys, and so
  60. // we are able to confirm whether the current user is the owner or not.
  61. virtual bool IsReady();
  62. // Returns whether current user is owner or not - as long as IsReady()
  63. // returns true. When IsReady() is false, we don't yet know if the current
  64. // user is the owner or not. In that case this method returns false.
  65. virtual bool IsOwner();
  66. // Determines whether current user is owner or not, responds via |callback|.
  67. // Reliably returns the correct value, but will not respond on the callback
  68. // until IsReady() returns true.
  69. virtual void IsOwnerAsync(IsOwnerCallback callback);
  70. // Assembles and signs |policy| on the |task_runner|, responds on
  71. // the original thread via |callback|.
  72. bool AssembleAndSignPolicyAsync(
  73. base::TaskRunner* task_runner,
  74. std::unique_ptr<enterprise_management::PolicyData> policy,
  75. AssembleAndSignPolicyAsyncCallback callback);
  76. // Checks whether |setting| is handled by OwnerSettingsService.
  77. virtual bool HandlesSetting(const std::string& setting) = 0;
  78. // Sets |setting| value to |value|.
  79. virtual bool Set(const std::string& setting, const base::Value& value) = 0;
  80. // Convenience functions for manipulating lists. Note that the following
  81. // functions employs a read, modify and write pattern. If there're
  82. // pending updates to |setting|, value cache they read from might not
  83. // be fresh and multiple calls to those function would lose data.
  84. virtual bool AppendToList(const std::string& setting,
  85. const base::Value& value) = 0;
  86. virtual bool RemoveFromList(const std::string& setting,
  87. const base::Value& value) = 0;
  88. // Sets a bunch of device settings accumulated before ownership gets
  89. // established.
  90. //
  91. // TODO (ygorshenin@, crbug.com/230018): that this is a temporary
  92. // solution and should be removed.
  93. virtual bool CommitTentativeDeviceSettings(
  94. std::unique_ptr<enterprise_management::PolicyData> policy) = 0;
  95. bool SetBoolean(const std::string& setting, bool value);
  96. bool SetInteger(const std::string& setting, int value);
  97. bool SetDouble(const std::string& setting, double value);
  98. bool SetString(const std::string& setting, const std::string& value);
  99. // Run callbacks in test setting. Mocks ownership when full device setup is
  100. // not needed.
  101. void RunPendingIsOwnerCallbacksForTesting(bool is_owner);
  102. protected:
  103. void ReloadKeypair();
  104. void OnKeypairLoaded(const scoped_refptr<PublicKey>& public_key,
  105. const scoped_refptr<PrivateKey>& private_key);
  106. // Platform-specific keypair loading algorithm.
  107. virtual void ReloadKeypairImpl(
  108. base::OnceCallback<void(const scoped_refptr<PublicKey>& public_key,
  109. const scoped_refptr<PrivateKey>& private_key)>
  110. callback) = 0;
  111. // Plafrom-specific actions which should be performed when keypair is loaded.
  112. virtual void OnPostKeypairLoadedActions() = 0;
  113. scoped_refptr<ownership::PublicKey> public_key_;
  114. scoped_refptr<ownership::PrivateKey> private_key_;
  115. scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
  116. std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
  117. base::ObserverList<Observer>::Unchecked observers_;
  118. base::ThreadChecker thread_checker_;
  119. private:
  120. base::WeakPtrFactory<OwnerSettingsService> weak_factory_{this};
  121. };
  122. } // namespace ownership
  123. #endif // COMPONENTS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_