Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 DLCSERVICE_DLC_MANAGER_H_ |
| 6 | #define DLCSERVICE_DLC_MANAGER_H_ |
| 7 | |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 10 | #include <base/time/time.h> |
Amin Hassani | a69f32e | 2020-03-30 15:20:42 -0700 | [diff] [blame] | 11 | #include <brillo/errors/error.h> |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 12 | #include <brillo/message_loops/message_loop.h> |
| 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 14 | |
Amin Hassani | 8664998 | 2020-03-31 16:03:37 -0700 | [diff] [blame] | 15 | #include "dlcservice/dlc.h" |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 16 | |
| 17 | namespace dlcservice { |
| 18 | |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 19 | class DlcManagerInterface { |
| 20 | public: |
| 21 | virtual ~DlcManagerInterface() = default; |
| 22 | |
| 23 | virtual DlcBase* GetDlc(const DlcId& id, brillo::ErrorPtr* err) = 0; |
| 24 | virtual void Initialize() = 0; |
| 25 | virtual DlcIdList GetInstalled() = 0; |
| 26 | virtual DlcIdList GetExistingDlcs() = 0; |
| 27 | virtual DlcIdList GetDlcsToUpdate() = 0; |
| 28 | virtual DlcIdList GetSupported() = 0; |
| 29 | virtual bool InstallCompleted(const DlcIdList& ids, |
| 30 | brillo::ErrorPtr* err) = 0; |
| 31 | virtual bool UpdateCompleted(const DlcIdList& ids, brillo::ErrorPtr* err) = 0; |
| 32 | virtual bool Install(const DlcId& id, |
| 33 | bool* external_install_needed, |
| 34 | brillo::ErrorPtr* err) = 0; |
| 35 | virtual bool FinishInstall(const DlcId& id, brillo::ErrorPtr* err) = 0; |
| 36 | virtual bool CancelInstall(const DlcId& id, |
| 37 | const brillo::ErrorPtr& err_in, |
| 38 | brillo::ErrorPtr* err) = 0; |
| 39 | virtual bool Uninstall(const DlcId& id, brillo::ErrorPtr* err) = 0; |
| 40 | virtual bool Purge(const DlcId& id, brillo::ErrorPtr* err) = 0; |
| 41 | virtual void ChangeProgress(double progress) = 0; |
| 42 | }; |
| 43 | |
| 44 | class DlcManager : public DlcManagerInterface { |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 45 | public: |
Amin Hassani | ca3cbb7 | 2020-04-10 12:26:50 -0700 | [diff] [blame] | 46 | DlcManager() = default; |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 47 | ~DlcManager(); |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 48 | |
Amin Hassani | 1ac2831 | 2020-06-04 18:16:30 -0700 | [diff] [blame] | 49 | // Returns a reference to a DLC object given a DLC ID. If the ID is not |
| 50 | // supported, it will set the error and return |nullptr|. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 51 | DlcBase* GetDlc(const DlcId& id, brillo::ErrorPtr* err) override; |
Amin Hassani | 8664998 | 2020-03-31 16:03:37 -0700 | [diff] [blame] | 52 | |
Amin Hassani | ca3cbb7 | 2020-04-10 12:26:50 -0700 | [diff] [blame] | 53 | // Initializes the state of DlcManager. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 54 | void Initialize() override; |
Amin Hassani | ca3cbb7 | 2020-04-10 12:26:50 -0700 | [diff] [blame] | 55 | |
Amin Hassani | f27aac0 | 2020-04-23 21:56:26 -0700 | [diff] [blame] | 56 | // Returns the list of installed DLCs. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 57 | DlcIdList GetInstalled() override; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 58 | |
Amin Hassani | d5fc8b2 | 2020-04-29 12:44:52 -0700 | [diff] [blame] | 59 | // Returns the list of DLCs with installed content. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 60 | DlcIdList GetExistingDlcs() override; |
Amin Hassani | d5fc8b2 | 2020-04-29 12:44:52 -0700 | [diff] [blame] | 61 | |
Amin Hassani | 38f3679 | 2020-04-17 11:47:08 -0700 | [diff] [blame] | 62 | // Returns the list of DLCs that need to be updated. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 63 | DlcIdList GetDlcsToUpdate() override; |
Amin Hassani | 38f3679 | 2020-04-17 11:47:08 -0700 | [diff] [blame] | 64 | |
Jae Hoon Kim | 6cb439b | 2020-03-23 14:02:44 -0700 | [diff] [blame] | 65 | // Returns the list of all supported DLC(s). |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 66 | DlcIdList GetSupported() override; |
Jae Hoon Kim | 6cb439b | 2020-03-23 14:02:44 -0700 | [diff] [blame] | 67 | |
Amin Hassani | f27aac0 | 2020-04-23 21:56:26 -0700 | [diff] [blame] | 68 | // Persists the verified pref for given DLC(s) on install completion. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 69 | bool InstallCompleted(const DlcIdList& ids, brillo::ErrorPtr* err) override; |
Jae Hoon Kim | 9a27d15 | 2020-04-10 12:50:14 -0700 | [diff] [blame] | 70 | |
Amin Hassani | f27aac0 | 2020-04-23 21:56:26 -0700 | [diff] [blame] | 71 | // Persists the verified pref for given DLC(s) on update completion. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 72 | bool UpdateCompleted(const DlcIdList& ids, brillo::ErrorPtr* err) override; |
Jae Hoon Kim | 9a27d15 | 2020-04-10 12:50:14 -0700 | [diff] [blame] | 73 | |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 74 | // DLC Installation Flow |
| 75 | |
| 76 | // Install Step 1: |
| 77 | // To start an install, the initial requirement is to call this function. |
| 78 | // During this phase, all necessary setup for update_engine to successfully |
| 79 | // install DLC(s) and other files that require creation are handled. |
| 80 | // Args: |
Amin Hassani | 6d0367d | 2020-05-10 18:07:03 -0700 | [diff] [blame] | 81 | // id: The DLC ID that needs to be installed. |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 82 | // external_install_needed: It is set to true if we need to actually install |
| 83 | // the DLC through update_engine. |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 84 | // err: The error that's set when returned false. |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 85 | // Return: |
| 86 | // True on success, otherwise false. |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 87 | bool Install(const DlcId& id, |
| 88 | bool* external_install_needed, |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 89 | brillo::ErrorPtr* err) override; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 90 | |
Jae Hoon Kim | 4a7e06b | 2020-05-13 09:14:14 -0700 | [diff] [blame] | 91 | // Install Step 2a: |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 92 | // Once the missing DLC(s) are installed or there were no missing DLC(s), this |
| 93 | // call is still required to finish the installation. |
| 94 | // If there were missing DLC(s) that were newly installed, this call will go |
| 95 | // ahead and mount those DLC(s) to be ready for use. |
| 96 | // Args: |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 97 | // id: The DLC to finish the installation for. |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 98 | // err: The error that's set when returned false. |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 99 | // Return: |
| 100 | // True on success, otherwise false. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 101 | bool FinishInstall(const DlcId& id, brillo::ErrorPtr* err) override; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 102 | |
Jae Hoon Kim | 4a7e06b | 2020-05-13 09:14:14 -0700 | [diff] [blame] | 103 | // Install Step 2b: |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 104 | // If for any reason, the init'ed DLC(s) should not follow through with |
| 105 | // mounting it can be cancelled by invoking this. The call may fail, in |
| 106 | // which case the errors will reflect the causes and provide insight in ways |
| 107 | // dlcservice can be put into a valid state again. |
| 108 | // Args: |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 109 | // id: The DLC to cancel the installation for. |
Andrew | bcc4bd8 | 2020-06-11 14:23:55 -0700 | [diff] [blame] | 110 | // err_in: The error that caused the install to be cancelled. |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 111 | // err: The error that's set when returned false. |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 112 | // Return: |
| 113 | // True on success, otherwise false. |
Andrew | bcc4bd8 | 2020-06-11 14:23:55 -0700 | [diff] [blame] | 114 | bool CancelInstall(const DlcId& id, |
| 115 | const brillo::ErrorPtr& err_in, |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 116 | brillo::ErrorPtr* err) override; |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 117 | |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 118 | // DLC Deletion Flow |
| 119 | |
| 120 | // Delete Step 1: |
| 121 | // To delete the DLC this can be invoked, no prior step is required. |
| 122 | // Args: |
| 123 | // id: The DLC ID that is to be uninstalled. |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 124 | // err: The error that's set when returned false. |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 125 | // Return: |
| 126 | // True if the DLC with the ID passed in is successfully uninstalled, |
| 127 | // otherwise false. Deleting a valid DLC that's not installed is considered |
| 128 | // successfully uninstalled, however uninstalling a DLC that's not supported |
Jae Hoon Kim | fdaa53a | 2020-03-13 11:28:36 -0700 | [diff] [blame] | 129 | // is a failure. Uninstalling a DLC that is installing is also a failure. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 130 | bool Uninstall(const DlcId& id, brillo::ErrorPtr* err) override; |
| 131 | bool Purge(const DlcId& id, brillo::ErrorPtr* err) override; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 132 | |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 133 | // Changes the progress on all DLCs being installed to |progress|. |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 134 | void ChangeProgress(double progress) override; |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 135 | |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 136 | private: |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 137 | FRIEND_TEST(DlcManagerTest, CleanupDanglingDlcs); |
| 138 | |
Jae Hoon Kim | 0d4ff62 | 2020-05-14 14:47:40 -0700 | [diff] [blame] | 139 | // Removes all unsupported/deprecated DLC files and images. |
| 140 | void CleanupUnsupportedDlcs(); |
Amin Hassani | ca3cbb7 | 2020-04-10 12:26:50 -0700 | [diff] [blame] | 141 | |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 142 | // Cleans up all DLCs that are dangling based on the ref count. |
| 143 | void CleanupDanglingDlcs(); |
| 144 | |
| 145 | // Posts the |CleanuupDanglingDlcs| as a delayed task with timeout |timeout|. |
| 146 | void PostCleanupDanglingDlcs(const base::TimeDelta& timeout); |
| 147 | |
Amin Hassani | 1fd2082 | 2020-03-27 11:52:23 -0700 | [diff] [blame] | 148 | DlcMap supported_; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 149 | |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 150 | brillo::MessageLoop::TaskId cleanup_dangling_task_id_; |
| 151 | |
Amin Hassani | 6b010bf | 2020-06-04 17:26:58 -0700 | [diff] [blame] | 152 | DlcManager(const DlcManager&) = delete; |
| 153 | DlcManager& operator=(const DlcManager&) = delete; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | } // namespace dlcservice |
| 157 | |
| 158 | #endif // DLCSERVICE_DLC_MANAGER_H_ |