blob: 2c3081e8bdfb05115524a93c1e8789e66c5eed86 [file] [log] [blame]
Jae Hoon Kim823ead32019-12-13 09:30:09 -08001// 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 Kim823ead32019-12-13 09:30:09 -08008#include <string>
9
Amin Hassani40c0f112020-04-15 09:55:00 -070010#include <base/time/time.h>
Amin Hassania69f32e2020-03-30 15:20:42 -070011#include <brillo/errors/error.h>
Amin Hassani40c0f112020-04-15 09:55:00 -070012#include <brillo/message_loops/message_loop.h>
13#include <gtest/gtest_prod.h> // for FRIEND_TEST
Jae Hoon Kim823ead32019-12-13 09:30:09 -080014
Amin Hassani86649982020-03-31 16:03:37 -070015#include "dlcservice/dlc.h"
Jae Hoon Kim823ead32019-12-13 09:30:09 -080016
17namespace dlcservice {
18
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070019class 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
44class DlcManager : public DlcManagerInterface {
Jae Hoon Kim823ead32019-12-13 09:30:09 -080045 public:
Amin Hassanica3cbb72020-04-10 12:26:50 -070046 DlcManager() = default;
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070047 ~DlcManager();
Jae Hoon Kim823ead32019-12-13 09:30:09 -080048
Amin Hassani1ac28312020-06-04 18:16:30 -070049 // 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 Kim7d0001e2021-07-07 13:04:40 -070051 DlcBase* GetDlc(const DlcId& id, brillo::ErrorPtr* err) override;
Amin Hassani86649982020-03-31 16:03:37 -070052
Amin Hassanica3cbb72020-04-10 12:26:50 -070053 // Initializes the state of DlcManager.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070054 void Initialize() override;
Amin Hassanica3cbb72020-04-10 12:26:50 -070055
Amin Hassanif27aac02020-04-23 21:56:26 -070056 // Returns the list of installed DLCs.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070057 DlcIdList GetInstalled() override;
Jae Hoon Kim823ead32019-12-13 09:30:09 -080058
Amin Hassanid5fc8b22020-04-29 12:44:52 -070059 // Returns the list of DLCs with installed content.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070060 DlcIdList GetExistingDlcs() override;
Amin Hassanid5fc8b22020-04-29 12:44:52 -070061
Amin Hassani38f36792020-04-17 11:47:08 -070062 // Returns the list of DLCs that need to be updated.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070063 DlcIdList GetDlcsToUpdate() override;
Amin Hassani38f36792020-04-17 11:47:08 -070064
Jae Hoon Kim6cb439b2020-03-23 14:02:44 -070065 // Returns the list of all supported DLC(s).
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070066 DlcIdList GetSupported() override;
Jae Hoon Kim6cb439b2020-03-23 14:02:44 -070067
Amin Hassanif27aac02020-04-23 21:56:26 -070068 // Persists the verified pref for given DLC(s) on install completion.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070069 bool InstallCompleted(const DlcIdList& ids, brillo::ErrorPtr* err) override;
Jae Hoon Kim9a27d152020-04-10 12:50:14 -070070
Amin Hassanif27aac02020-04-23 21:56:26 -070071 // Persists the verified pref for given DLC(s) on update completion.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070072 bool UpdateCompleted(const DlcIdList& ids, brillo::ErrorPtr* err) override;
Jae Hoon Kim9a27d152020-04-10 12:50:14 -070073
Jae Hoon Kim823ead32019-12-13 09:30:09 -080074 // 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 Hassani6d0367d2020-05-10 18:07:03 -070081 // id: The DLC ID that needs to be installed.
Amin Hassani9a3f20c2020-05-25 16:38:33 -070082 // external_install_needed: It is set to true if we need to actually install
83 // the DLC through update_engine.
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -070084 // err: The error that's set when returned false.
Jae Hoon Kim823ead32019-12-13 09:30:09 -080085 // Return:
86 // True on success, otherwise false.
Amin Hassani9a3f20c2020-05-25 16:38:33 -070087 bool Install(const DlcId& id,
88 bool* external_install_needed,
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -070089 brillo::ErrorPtr* err) override;
Jae Hoon Kim823ead32019-12-13 09:30:09 -080090
Jae Hoon Kim4a7e06b2020-05-13 09:14:14 -070091 // Install Step 2a:
Jae Hoon Kim823ead32019-12-13 09:30:09 -080092 // 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 Kim53514af2020-07-27 11:50:26 -070097 // id: The DLC to finish the installation for.
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -070098 // err: The error that's set when returned false.
Jae Hoon Kim823ead32019-12-13 09:30:09 -080099 // Return:
100 // True on success, otherwise false.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -0700101 bool FinishInstall(const DlcId& id, brillo::ErrorPtr* err) override;
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800102
Jae Hoon Kim4a7e06b2020-05-13 09:14:14 -0700103 // Install Step 2b:
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800104 // 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 Kim53514af2020-07-27 11:50:26 -0700109 // id: The DLC to cancel the installation for.
Andrewbcc4bd82020-06-11 14:23:55 -0700110 // err_in: The error that caused the install to be cancelled.
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -0700111 // err: The error that's set when returned false.
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800112 // Return:
113 // True on success, otherwise false.
Andrewbcc4bd82020-06-11 14:23:55 -0700114 bool CancelInstall(const DlcId& id,
115 const brillo::ErrorPtr& err_in,
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -0700116 brillo::ErrorPtr* err) override;
Amin Hassani9a3f20c2020-05-25 16:38:33 -0700117
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800118 // 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 Kimc98e3a32020-03-13 09:40:48 -0700124 // err: The error that's set when returned false.
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800125 // 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 Kimfdaa53a2020-03-13 11:28:36 -0700129 // is a failure. Uninstalling a DLC that is installing is also a failure.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -0700130 bool Uninstall(const DlcId& id, brillo::ErrorPtr* err) override;
131 bool Purge(const DlcId& id, brillo::ErrorPtr* err) override;
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800132
Amin Hassani78a5ec82020-05-19 09:47:49 -0700133 // Changes the progress on all DLCs being installed to |progress|.
Jae Hoon Kim7d0001e2021-07-07 13:04:40 -0700134 void ChangeProgress(double progress) override;
Amin Hassani78a5ec82020-05-19 09:47:49 -0700135
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800136 private:
Amin Hassani40c0f112020-04-15 09:55:00 -0700137 FRIEND_TEST(DlcManagerTest, CleanupDanglingDlcs);
138
Jae Hoon Kim0d4ff622020-05-14 14:47:40 -0700139 // Removes all unsupported/deprecated DLC files and images.
140 void CleanupUnsupportedDlcs();
Amin Hassanica3cbb72020-04-10 12:26:50 -0700141
Amin Hassani40c0f112020-04-15 09:55:00 -0700142 // 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 Hassani1fd20822020-03-27 11:52:23 -0700148 DlcMap supported_;
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800149
Amin Hassani40c0f112020-04-15 09:55:00 -0700150 brillo::MessageLoop::TaskId cleanup_dangling_task_id_;
151
Amin Hassani6b010bf2020-06-04 17:26:58 -0700152 DlcManager(const DlcManager&) = delete;
153 DlcManager& operator=(const DlcManager&) = delete;
Jae Hoon Kim823ead32019-12-13 09:30:09 -0800154};
155
156} // namespace dlcservice
157
158#endif // DLCSERVICE_DLC_MANAGER_H_