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 CHROME_BROWSER_CHROMEOS_BOREALIS_BOREALIS_TASK_H_
6 #define CHROME_BROWSER_CHROMEOS_BOREALIS_BOREALIS_TASK_H_
7 
8 #include "base/memory/weak_ptr.h"
9 #include "chrome/browser/chromeos/borealis/borealis_context_manager.h"
10 #include "chrome/browser/chromeos/borealis/borealis_launch_watcher.h"
11 #include "chrome/browser/chromeos/borealis/borealis_metrics.h"
12 #include "chromeos/dbus/concierge_client.h"
13 #include "chromeos/dbus/dlcservice/dlcservice_client.h"
14 
15 namespace borealis {
16 
17 class BorealisContext;
18 
19 // BorealisTasks are collections of operations that are run by the
20 // Borealis Context Manager.
21 class BorealisTask {
22  public:
23   // Callback to be run when the task completes. The |result| should reflect
24   // if the task succeeded with kSuccess and an empty string. If it fails, a
25   // different result should be used, and an error string provided.
26   using CompletionResultCallback =
27       base::OnceCallback<void(BorealisStartupResult, std::string)>;
28   BorealisTask();
29   BorealisTask(const BorealisTask&) = delete;
30   BorealisTask& operator=(const BorealisTask&) = delete;
31   virtual ~BorealisTask();
32 
33   void Run(BorealisContext* context, CompletionResultCallback callback);
34 
35  protected:
36   virtual void RunInternal(BorealisContext* context) = 0;
37 
38   void Complete(BorealisStartupResult result, std::string message);
39 
40  private:
41   CompletionResultCallback callback_;
42 };
43 
44 // Mounts the Borealis DLC.
45 class MountDlc : public BorealisTask {
46  public:
47   MountDlc();
48   ~MountDlc() override;
49   void RunInternal(BorealisContext* context) override;
50 
51  private:
52   void OnMountDlc(
53       BorealisContext* context,
54       const chromeos::DlcserviceClient::InstallResult& install_result);
55   base::WeakPtrFactory<MountDlc> weak_factory_{this};
56 };
57 
58 // Creates a disk image for the Borealis VM.
59 class CreateDiskImage : public BorealisTask {
60  public:
61   CreateDiskImage();
62   ~CreateDiskImage() override;
63   void RunInternal(BorealisContext* context) override;
64 
65  private:
66   void OnCreateDiskImage(
67       BorealisContext* context,
68       base::Optional<vm_tools::concierge::CreateDiskImageResponse> response);
69   base::WeakPtrFactory<CreateDiskImage> weak_factory_{this};
70 };
71 
72 // Instructs Concierge to start the Borealis VM.
73 class StartBorealisVm : public BorealisTask {
74  public:
75   StartBorealisVm();
76   ~StartBorealisVm() override;
77   void RunInternal(BorealisContext* context) override;
78 
79  private:
80   void OnStartBorealisVm(
81       BorealisContext* context,
82       base::Optional<vm_tools::concierge::StartVmResponse> response);
83   base::WeakPtrFactory<StartBorealisVm> weak_factory_{this};
84 };
85 
86 // Waits for the startup daemon to signal completion.
87 class AwaitBorealisStartup : public BorealisTask {
88  public:
89   AwaitBorealisStartup(Profile* profile, std::string vm_name);
90   ~AwaitBorealisStartup() override;
91   void RunInternal(BorealisContext* context) override;
92   BorealisLaunchWatcher& GetWatcherForTesting();
93 
94  private:
95   void OnAwaitBorealisStartup(BorealisContext* context,
96                               base::Optional<std::string> container);
97   BorealisLaunchWatcher watcher_;
98   base::WeakPtrFactory<AwaitBorealisStartup> weak_factory_{this};
99 };
100 
101 }  // namespace borealis
102 
103 #endif  // CHROME_BROWSER_CHROMEOS_BOREALIS_BOREALIS_TASK_H_
104