blob: fe65e60dc5727f93ce2d22b849a8f2141fd34cf6 [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
17#ifndef UPDATE_ENGINE_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
18#define UPDATE_ENGINE_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
19
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"
34
35namespace chromeos_update_engine {
36
37class CleanupPreviousUpdateAction;
38
39template <>
40class 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.
48class CleanupPreviousUpdateAction : public Action<CleanupPreviousUpdateAction> {
49 public:
50 CleanupPreviousUpdateAction(
51 PrefsInterface* prefs,
52 BootControlInterface* boot_control,
Yifan Hongf9cb4492020-04-15 13:00:20 -070053 android::snapshot::ISnapshotManager* snapshot,
Yifan Hongdad0af82020-02-19 17:19:49 -080054 CleanupPreviousUpdateActionDelegateInterface* delegate);
Yifan Hongd1d52a02020-09-25 15:09:07 -070055 ~CleanupPreviousUpdateAction();
Yifan Hongdad0af82020-02-19 17:19:49 -080056
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_;
Yifan Hongf9cb4492020-04-15 13:00:20 -070072 android::snapshot::ISnapshotManager* snapshot_;
Yifan Hongdad0af82020-02-19 17:19:49 -080073 CleanupPreviousUpdateActionDelegateInterface* delegate_;
Yifan Hong5cd63fa2020-03-16 12:31:16 -070074 std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
Yifan Hongdad0af82020-02-19 17:19:49 -080075 bool running_{false};
76 bool cancel_failed_{false};
77 unsigned int last_percentage_{0};
Yifan Hongf9cb4492020-04-15 13:00:20 -070078 android::snapshot::ISnapshotMergeStats* merge_stats_;
Yifan Hongd1d52a02020-09-25 15:09:07 -070079 brillo::MessageLoop::TaskId scheduled_task_{brillo::MessageLoop::kTaskIdNull};
80
81 // Helpers for task management.
82 void AcknowledgeTaskExecuted();
83 void CheckTaskScheduled(std::string_view name);
Yifan Hongdad0af82020-02-19 17:19:49 -080084
Yifan Hongd506dee2020-09-25 15:08:19 -070085 void StopActionInternal();
Yifan Hongdad0af82020-02-19 17:19:49 -080086 void StartActionInternal();
87 void ScheduleWaitBootCompleted();
88 void WaitBootCompletedOrSchedule();
89 void ScheduleWaitMarkBootSuccessful();
90 void CheckSlotMarkedSuccessfulOrSchedule();
91 void ScheduleWaitForMerge();
92 void WaitForMergeOrSchedule();
93 void InitiateMergeAndWait();
Yifan Hongd976cc52020-02-25 14:51:42 -080094 void ReportMergeStats();
Yifan Hongdad0af82020-02-19 17:19:49 -080095
96 // Callbacks to ProcessUpdateState.
97 bool OnMergePercentageUpdate();
98 bool BeforeCancel();
99};
100
101} // namespace chromeos_update_engine
102
103#endif // UPDATE_ENGINE_CLEANUP_PREVIOUS_UPDATE_ACTION_H_