1 // Copyright 2020 The Chromium 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 CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_SYSTEM_ROUTINE_CONTROLLER_H_
6 #define CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_SYSTEM_ROUTINE_CONTROLLER_H_
7 
8 #include <memory>
9 
10 #include "chromeos/components/diagnostics_ui/mojom/system_routine_controller.mojom.h"
11 #include "chromeos/services/cros_healthd/public/mojom/cros_healthd.mojom.h"
12 #include "mojo/public/cpp/bindings/receiver.h"
13 #include "mojo/public/cpp/bindings/remote.h"
14 
15 namespace base {
16 class OneShotTimer;
17 }  // namespace base
18 
19 namespace cros_healthd {
20 namespace mojom {
21 class RunRoutineResponsePtr;
22 class RoutineUpdatePtr;
23 }  // namespace mojom
24 }  // namespace cros_healthd
25 
26 namespace chromeos {
27 namespace diagnostics {
28 
29 using RunRoutineCallback =
30     base::OnceCallback<void(cros_healthd::mojom::RunRoutineResponsePtr)>;
31 
32 class SystemRoutineController : public mojom::SystemRoutineController {
33  public:
34   SystemRoutineController();
35   ~SystemRoutineController() override;
36 
37   SystemRoutineController(const SystemRoutineController&) = delete;
38   SystemRoutineController& operator=(const SystemRoutineController&) = delete;
39 
40   // mojom::SystemRoutineController:
41   void RunRoutine(mojom::RoutineType type,
42                   mojo::PendingRemote<mojom::RoutineRunner> runner) override;
43 
44  private:
45   void ExecuteRoutine(mojom::RoutineType routine_type);
46 
47   void OnRoutineStarted(
48       mojom::RoutineType routine_type,
49       cros_healthd::mojom::RunRoutineResponsePtr response_ptr);
50 
51   void CheckRoutineStatus(mojom::RoutineType routine_type, int32_t id);
52 
53   void OnRoutineStatusUpdated(mojom::RoutineType routine_type,
54                               int32_t id,
55                               cros_healthd::mojom::RoutineUpdatePtr update_ptr);
56 
57   bool IsRoutineRunning() const;
58 
59   void ScheduleCheckRoutineStatus(uint32_t duration_in_seconds,
60                                   mojom::RoutineType routine_type,
61                                   int32_t id);
62 
63   void OnStandardRoutineResult(mojom::RoutineType routine_type,
64                                mojom::StandardRoutineResult result);
65 
66   void BindCrosHealthdDiagnosticsServiceIfNeccessary();
67 
68   void OnDiagnosticsServiceDisconnected();
69 
70   void OnInflightRoutineRunnerDisconnected();
71 
72   mojo::Remote<mojom::RoutineRunner> inflight_routine_runner_;
73   std::unique_ptr<base::OneShotTimer> inflight_routine_timer_;
74 
75   mojo::Remote<cros_healthd::mojom::CrosHealthdDiagnosticsService>
76       diagnostics_service_;
77 
78   mojo::Receiver<mojom::SystemRoutineController> receiver_{this};
79 };
80 
81 }  // namespace diagnostics
82 }  // namespace chromeos
83 
84 #endif  // CHROMEOS_COMPONENTS_DIAGNOSTICS_UI_BACKEND_SYSTEM_ROUTINE_CONTROLLER_H_
85