blob: 1d701b16879489b6ddd6cda6620bf5bb44a55831 [file] [log] [blame]
Yifan Hongdad0af82020-02-19 17:19:49 -08001//
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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
18#define UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
Yifan Hongdad0af82020-02-19 17:19:49 -080019
Yifan Hongd976cc52020-02-25 14:51:42 -080020#include <chrono> // NOLINT(build/c++11) -- for merge times
21#include <memory>
Yifan Hongdad0af82020-02-19 17:19:49 -080022#include <string>
Yifan Hongd1d52a02020-09-25 15:09:07 -070023#include <string_view>
Yifan Hongdad0af82020-02-19 17:19:49 -080024
25#include <brillo/message_loops/message_loop.h>
26#include <libsnapshot/snapshot.h>
Yifan Hongd976cc52020-02-25 14:51:42 -080027#include <libsnapshot/snapshot_stats.h>
Yifan Hongdad0af82020-02-19 17:19:49 -080028
29#include "update_engine/common/action.h"
30#include "update_engine/common/boot_control_interface.h"
31#include "update_engine/common/cleanup_previous_update_action_delegate.h"
32#include "update_engine/common/error_code.h"
33#include "update_engine/common/prefs_interface.h"
Kelvin Zhang8bcb2212023-11-16 10:45:29 -080034#include "update_engine/common/scoped_task_id.h"
Yifan Hongdad0af82020-02-19 17:19:49 -080035
36namespace chromeos_update_engine {
37
38class CleanupPreviousUpdateAction;
39
40template <>
41class ActionTraits<CleanupPreviousUpdateAction> {
42 public:
43 typedef NoneType InputObjectType;
44 typedef NoneType OutputObjectType;
45};
46
47// On Android Virtual A/B devices, clean up snapshots from previous update
48// attempt. See DynamicPartitionControlAndroid::CleanupSuccessfulUpdate.
49class CleanupPreviousUpdateAction : public Action<CleanupPreviousUpdateAction> {
50 public:
51 CleanupPreviousUpdateAction(
52 PrefsInterface* prefs,
53 BootControlInterface* boot_control,
Yifan Hongf9cb4492020-04-15 13:00:20 -070054 android::snapshot::ISnapshotManager* snapshot,
Yifan Hongdad0af82020-02-19 17:19:49 -080055 CleanupPreviousUpdateActionDelegateInterface* delegate);
Yifan Hongd1d52a02020-09-25 15:09:07 -070056 ~CleanupPreviousUpdateAction();
Yifan Hongdad0af82020-02-19 17:19:49 -080057
58 void PerformAction() override;
59 void SuspendAction() override;
60 void ResumeAction() override;
61 void TerminateProcessing() override;
62 void ActionCompleted(ErrorCode error_code) override;
63 std::string Type() const override;
64 static std::string StaticType();
65 typedef ActionTraits<CleanupPreviousUpdateAction>::InputObjectType
66 InputObjectType;
67 typedef ActionTraits<CleanupPreviousUpdateAction>::OutputObjectType
68 OutputObjectType;
69
70 private:
71 PrefsInterface* prefs_;
72 BootControlInterface* boot_control_;
Yifan Hongf9cb4492020-04-15 13:00:20 -070073 android::snapshot::ISnapshotManager* snapshot_;
Yifan Hongdad0af82020-02-19 17:19:49 -080074 CleanupPreviousUpdateActionDelegateInterface* delegate_;
Yifan Hong5cd63fa2020-03-16 12:31:16 -070075 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongdad0af82020-02-19 17:19:49 -080076 bool running_{false};
77 bool cancel_failed_{false};
78 unsigned int last_percentage_{0};
Yifan Hongf9cb4492020-04-15 13:00:20 -070079 android::snapshot::ISnapshotMergeStats* merge_stats_;
Kelvin Zhang8bcb2212023-11-16 10:45:29 -080080 ScopedTaskId scheduled_task_;
Yifan Hongd1d52a02020-09-25 15:09:07 -070081
82 // Helpers for task management.
83 void AcknowledgeTaskExecuted();
84 void CheckTaskScheduled(std::string_view name);
Yifan Hongdad0af82020-02-19 17:19:49 -080085
Yifan Hongd506dee2020-09-25 15:08:19 -070086 void StopActionInternal();
Yifan Hongdad0af82020-02-19 17:19:49 -080087 void StartActionInternal();
88 void ScheduleWaitBootCompleted();
89 void WaitBootCompletedOrSchedule();
90 void ScheduleWaitMarkBootSuccessful();
91 void CheckSlotMarkedSuccessfulOrSchedule();
Kelvin Zhang8bcb2212023-11-16 10:45:29 -080092 void CheckForMergeDelay();
93 void StartMerge();
Yifan Hongdad0af82020-02-19 17:19:49 -080094 void ScheduleWaitForMerge();
95 void WaitForMergeOrSchedule();
96 void InitiateMergeAndWait();
Yifan Hongd976cc52020-02-25 14:51:42 -080097 void ReportMergeStats();
Yifan Hongdad0af82020-02-19 17:19:49 -080098
99 // Callbacks to ProcessUpdateState.
100 bool OnMergePercentageUpdate();
101 bool BeforeCancel();
102};
103
104} // namespace chromeos_update_engine
105
Amin Hassaniec7bc112020-10-29 16:47:58 -0700106#endif // UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_