dlcservice_client.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2019 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 CHROMEOS_DBUS_DLCSERVICE_DLCSERVICE_CLIENT_H_
  5. #define CHROMEOS_DBUS_DLCSERVICE_DLCSERVICE_CLIENT_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/component_export.h"
  11. #include "base/observer_list_types.h"
  12. #include "chromeos/dbus/common/dbus_client.h"
  13. #include "chromeos/dbus/dlcservice/dlcservice.pb.h"
  14. #include "dbus/message.h"
  15. #include "third_party/cros_system_api/dbus/dlcservice/dbus-constants.h"
  16. namespace chromeos {
  17. // This class is a singleton and should be accessed using |Get()|.
  18. // DlcserviceClient is used to communicate with the dlcservice daemon which
  19. // manages DLC (Downloadable Content) modules. DlcserviceClient will allow for
  20. // CrOS features to be installed and uninstalled at runtime of the system. If
  21. // more details about dlcservice are required, please consult
  22. // https://chromium.git.corp.google.com/chromiumos/platform2/+/HEAD/dlcservice
  23. class COMPONENT_EXPORT(DLCSERVICE_CLIENT) DlcserviceClient {
  24. public:
  25. // Observer class for objects that need to know the change in the state of
  26. // DLCs like UI, etc.
  27. class Observer : public base::CheckedObserver {
  28. public:
  29. ~Observer() override = default;
  30. // Is called whenever the state of a DLC is changed. Changing the
  31. // installation progress of the DLC constitues as a state change.
  32. virtual void OnDlcStateChanged(const dlcservice::DlcState& dlc_state) = 0;
  33. protected:
  34. Observer() = default;
  35. };
  36. // This object is returned as the result of DLC install success or failure.
  37. struct InstallResult {
  38. // The error associated with the install. |dlcservice::kErrorNone| indicates
  39. // a success. Any other error code, indicates a failure.
  40. std::string error;
  41. // The unique DLC ID which was requested to be installed.
  42. std::string dlc_id;
  43. // The path where the DLC is available for users to use.
  44. std::string root_path;
  45. };
  46. // The callback used for |Install()|. For large DLC(s) to install, there may
  47. // be a delay between the time of this call and the callback being invoked.
  48. using InstallCallback =
  49. base::OnceCallback<void(const InstallResult& install_result)>;
  50. // The callback used for |Install()|, if the caller wants to listen in on the
  51. // progress of their download/install. If the caller only cares for whether
  52. // the install is complete or not, the caller can pass in |RepeatingCallback|
  53. // that is a no-op.
  54. using ProgressCallback = base::RepeatingCallback<void(double progress)>;
  55. // The callback used for |Uninstall()|, if the error is something other than
  56. // |dlcservice::kErrorNone| the call has failed.
  57. using UninstallCallback = base::OnceCallback<void(const std::string& err)>;
  58. // The callback used for |Purge()|, if the error is something other than
  59. // |dlcservice::kErrorNone| the call has failed.
  60. using PurgeCallback = base::OnceCallback<void(const std::string& err)>;
  61. // The callback used for |GetDlcState()|, if the error is something other
  62. // than |dlcservice::kErrorNone| the call has failed.
  63. using GetDlcStateCallback =
  64. base::OnceCallback<void(const std::string& err,
  65. const dlcservice::DlcState& dlc_state)>;
  66. // The callback used for |GetExistingDlcs()|, if the error is something other
  67. // than |dlcservice::kErrorNone| the call has failed. It is a very rare case
  68. // for |GetExistingDlcs()| call to fail.
  69. using GetExistingDlcsCallback = base::OnceCallback<void(
  70. const std::string& err,
  71. const dlcservice::DlcsWithContent& dlcs_with_content)>;
  72. // Installs the DLC passed in while reporting progress through the progress
  73. // callback and only calls install callback on install success/failure.
  74. virtual void Install(const dlcservice::InstallRequest& install_request,
  75. InstallCallback callback,
  76. ProgressCallback progress_callback) = 0;
  77. // Uninstalls a single DLC and calls the callback with indication of
  78. // success/failure. Uninstalling disables the DLC but does not remove the DLC
  79. // from disk. After each uninstallation, a refcount to the DLC is decremented.
  80. // Once the refcount reaches 0, the DLC will remain in cache. However, if
  81. // the DLC is not installed within a window of time after reaching a
  82. // refcount of 0, the DLC will be purged automatically.
  83. virtual void Uninstall(const std::string& dlc_id,
  84. UninstallCallback callback) = 0;
  85. // Purges a single DLC and calls the callback with indication of
  86. // success/failure. Purging removes the DLC entirely from disk, regardless if
  87. // the DLC has been uninstalled or if there is a nonzero installed refcount.
  88. virtual void Purge(const std::string& dlc_id,
  89. PurgeCallback purge_callback) = 0;
  90. // Returns the state of a single DLC. Including information
  91. // such as installation state, id, and verification state.
  92. virtual void GetDlcState(const std::string& dlc_id,
  93. GetDlcStateCallback callback) = 0;
  94. // Provides the DLC(s) information such as:
  95. // id, name, description, used_bytes_on_disk. (reference
  96. // |dlcservice::DlcsWithContent| proto for complete details)
  97. virtual void GetExistingDlcs(GetExistingDlcsCallback callback) = 0;
  98. // During testing, can be used to mimic signals received back from dlcservice.
  99. virtual void DlcStateChangedForTest(dbus::Signal* signal) = 0;
  100. // Adds an observer instance to the observers list to listen on changes like
  101. // DLC state change, etc.
  102. virtual void AddObserver(Observer* observer) = 0;
  103. // Removes an observer from observers list.
  104. virtual void RemoveObserver(Observer* observer) = 0;
  105. // Creates and initializes the global instance. |bus| must not be nullptr.
  106. static void Initialize(dbus::Bus* bus);
  107. // Creates and initializes a fake global instance if not already created.
  108. static void InitializeFake();
  109. // Destroys the global instance.
  110. static void Shutdown();
  111. // Returns the global instance which may be nullptr if not initialized.
  112. static DlcserviceClient* Get();
  113. DlcserviceClient(const DlcserviceClient&) = delete;
  114. DlcserviceClient& operator=(const DlcserviceClient&) = delete;
  115. protected:
  116. friend class DlcserviceClientTest;
  117. // Initialize/Shutdown should be used instead.
  118. DlcserviceClient();
  119. virtual ~DlcserviceClient();
  120. };
  121. } // namespace chromeos
  122. // TODO(https://crbug.com/1164001): remove when moved to ash.
  123. namespace ash {
  124. using ::chromeos::DlcserviceClient;
  125. } // namespace ash
  126. #endif // CHROMEOS_DBUS_DLCSERVICE_DLCSERVICE_CLIENT_H_