xref: /aosp_15_r20/system/update_engine/aosp/cleanup_previous_update_action.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
18 #define UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
19 
20 #include <memory>
21 #include <string>
22 #include <string_view>
23 
24 #include <brillo/message_loops/message_loop.h>
25 #include <libsnapshot/snapshot.h>
26 #include <libsnapshot/snapshot_stats.h>
27 
28 #include "update_engine/common/action.h"
29 #include "update_engine/common/boot_control_interface.h"
30 #include "update_engine/common/cleanup_previous_update_action_delegate.h"
31 #include "update_engine/common/error_code.h"
32 #include "update_engine/common/prefs_interface.h"
33 #include "update_engine/common/scoped_task_id.h"
34 
35 namespace chromeos_update_engine {
36 
37 class CleanupPreviousUpdateAction;
38 
39 template <>
40 class ActionTraits<CleanupPreviousUpdateAction> {
41  public:
42   typedef NoneType InputObjectType;
43   typedef NoneType OutputObjectType;
44 };
45 
46 // On Android Virtual A/B devices, clean up snapshots from previous update
47 // attempt. See DynamicPartitionControlAndroid::CleanupSuccessfulUpdate.
48 class CleanupPreviousUpdateAction : public Action<CleanupPreviousUpdateAction> {
49  public:
50   CleanupPreviousUpdateAction(
51       PrefsInterface* prefs,
52       BootControlInterface* boot_control,
53       android::snapshot::ISnapshotManager* snapshot,
54       CleanupPreviousUpdateActionDelegateInterface* delegate);
55   ~CleanupPreviousUpdateAction();
56 
57   void PerformAction() override;
58   void SuspendAction() override;
59   void ResumeAction() override;
60   void TerminateProcessing() override;
61   void ActionCompleted(ErrorCode error_code) override;
62   std::string Type() const override;
63   static std::string StaticType();
64   typedef ActionTraits<CleanupPreviousUpdateAction>::InputObjectType
65       InputObjectType;
66   typedef ActionTraits<CleanupPreviousUpdateAction>::OutputObjectType
67       OutputObjectType;
68 
69  private:
70   PrefsInterface* prefs_;
71   BootControlInterface* boot_control_;
72   android::snapshot::ISnapshotManager* snapshot_;
73   CleanupPreviousUpdateActionDelegateInterface* delegate_;
74   std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
75   bool running_{false};
76   bool cancel_failed_{false};
77   unsigned int last_percentage_{0};
78   android::snapshot::ISnapshotMergeStats* merge_stats_;
79   ScopedTaskId scheduled_task_;
80 
81   // Helpers for task management.
82   void AcknowledgeTaskExecuted();
83   void CheckTaskScheduled(std::string_view name);
84 
85   void StopActionInternal();
86   void StartActionInternal();
87   void ScheduleWaitBootCompleted();
88   void WaitBootCompletedOrSchedule();
89   void ScheduleWaitMarkBootSuccessful();
90   void CheckSlotMarkedSuccessfulOrSchedule();
91   void CheckForMergeDelay();
92   void StartMerge();
93   void ScheduleWaitForMerge();
94   void WaitForMergeOrSchedule();
95   void InitiateMergeAndWait();
96   void ReportMergeStats();
97 
98   // Callbacks to ProcessUpdateState.
99   bool OnMergePercentageUpdate();
100   bool BeforeCancel();
101 };
102 
103 }  // namespace chromeos_update_engine
104 
105 #endif  // UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
106