summaryrefslogtreecommitdiff
path: root/aosp/cleanup_previous_update_action.h
blob: b93c55758cfad5bcf55672d99da5d83d493372de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_
#define UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_

#include <chrono>  // NOLINT(build/c++11) -- for merge times
#include <memory>
#include <string>
#include <string_view>

#include <brillo/message_loops/message_loop.h>
#include <libsnapshot/snapshot.h>
#include <libsnapshot/snapshot_stats.h>

#include "update_engine/common/action.h"
#include "update_engine/common/boot_control_interface.h"
#include "update_engine/common/cleanup_previous_update_action_delegate.h"
#include "update_engine/common/error_code.h"
#include "update_engine/common/prefs_interface.h"

namespace chromeos_update_engine {

class CleanupPreviousUpdateAction;

template <>
class ActionTraits<CleanupPreviousUpdateAction> {
 public:
  typedef NoneType InputObjectType;
  typedef NoneType OutputObjectType;
};

// On Android Virtual A/B devices, clean up snapshots from previous update
// attempt. See DynamicPartitionControlAndroid::CleanupSuccessfulUpdate.
class CleanupPreviousUpdateAction : public Action<CleanupPreviousUpdateAction> {
 public:
  CleanupPreviousUpdateAction(
      PrefsInterface* prefs,
      BootControlInterface* boot_control,
      android::snapshot::ISnapshotManager* snapshot,
      CleanupPreviousUpdateActionDelegateInterface* delegate);
  ~CleanupPreviousUpdateAction();

  void PerformAction() override;
  void SuspendAction() override;
  void ResumeAction() override;
  void TerminateProcessing() override;
  void ActionCompleted(ErrorCode error_code) override;
  std::string Type() const override;
  static std::string StaticType();
  typedef ActionTraits<CleanupPreviousUpdateAction>::InputObjectType
      InputObjectType;
  typedef ActionTraits<CleanupPreviousUpdateAction>::OutputObjectType
      OutputObjectType;

 private:
  PrefsInterface* prefs_;
  BootControlInterface* boot_control_;
  android::snapshot::ISnapshotManager* snapshot_;
  CleanupPreviousUpdateActionDelegateInterface* delegate_;
  std::unique_ptr<android::snapshot::AutoDevice> metadata_device_;
  bool running_{false};
  bool cancel_failed_{false};
  unsigned int last_percentage_{0};
  android::snapshot::ISnapshotMergeStats* merge_stats_;
  brillo::MessageLoop::TaskId scheduled_task_{brillo::MessageLoop::kTaskIdNull};

  // Helpers for task management.
  void AcknowledgeTaskExecuted();
  void CheckTaskScheduled(std::string_view name);

  void StopActionInternal();
  void StartActionInternal();
  void ScheduleWaitBootCompleted();
  void WaitBootCompletedOrSchedule();
  void ScheduleWaitMarkBootSuccessful();
  void CheckSlotMarkedSuccessfulOrSchedule();
  void ScheduleWaitForMerge();
  void WaitForMergeOrSchedule();
  void InitiateMergeAndWait();
  void ReportMergeStats();

  // Callbacks to ProcessUpdateState.
  bool OnMergePercentageUpdate();
  bool BeforeCancel();
};

}  // namespace chromeos_update_engine

#endif  // UPDATE_ENGINE_AOSP_CLEANUP_PREVIOUS_UPDATE_ACTION_H_