File: host_starter_base.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,756 bytes parent folder | download | duplicates (5)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_SETUP_HOST_STARTER_BASE_H_
#define REMOTING_HOST_SETUP_HOST_STARTER_BASE_H_

#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "remoting/base/http_status.h"
#include "remoting/base/rsa_key_pair.h"
#include "remoting/host/setup/daemon_controller.h"
#include "remoting/host/setup/host_starter.h"
#include "remoting/host/setup/host_starter_oauth_helper.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace network {
class SharedURLLoaderFactory;
}

namespace remoting {

// Base class used to provide common functionality needed when registering a
// new remote access host instance in the CRD backend. Subclasses should
// override the methods which handle the steps which differ however the flow
// is generally the same for all workflows.
//
// Overview of the steps in the registration flow once StartHost() is called:
// 1.) Check for an existing host instance
// 2.) <optional> Retrieve an access token
// 3.) Register the new host instance in the CRD backend
// 4.) Exchange the service account authorization_code for a refresh token
// 5.) Remove the existing host from the CRD backend
// 6.) Stop the existing host (if one is running)
// 7.) Write the host configuration file to disk
// 8.) Start the host service using the new configuration file
//
class HostStarterBase : public HostStarter {
 public:
  explicit HostStarterBase(
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  HostStarterBase(const HostStarterBase&) = delete;
  HostStarterBase& operator=(const HostStarterBase&) = delete;
  ~HostStarterBase() override;

  // HostStarter implementation.
  void StartHost(Params params, CompletionCallback on_done) override;

 protected:
  Params& params() { return start_host_params_; }
  RsaKeyPair& key_pair() { return *key_pair_; }
  std::optional<std::string>& existing_host_id() { return existing_host_id_; }

  // Methods used to implement the registration process described in the class
  // comment. They are listed in the order in which they are called.
  void OnExistingConfigLoaded(std::optional<base::Value::Dict> config);
  virtual void RetrieveApiAccessToken();
  virtual void RegisterNewHost(std::optional<std::string> access_token) = 0;
  void OnNewHostRegistered(const std::string& directory_id,
                           const std::string& owner_account_email,
                           const std::string& service_account_email,
                           const std::string& authorization_code);
  void OnServiceAccountTokensRetrieved(const std::string& service_account_email,
                                       const std::string& access_token,
                                       const std::string& refresh_token,
                                       const std::string& scopes);
  virtual void RemoveOldHostFromDirectory(
      base::OnceClosure on_host_removed) = 0;
  void StopOldHost();
  void OnOldHostStopped(DaemonController::AsyncResult result);
  void GenerateConfigFile();
  virtual void ApplyConfigValues(base::Value::Dict& config) = 0;
  void OnNewHostStarted(DaemonController::AsyncResult result);

  // |HandleError| will cause |on_done_| to be executed.
  void HandleError(const std::string& error_message, Result error_result);
  // Converts |status| into a HostStarter error and logs error information.
  void HandleHttpStatusError(const HttpStatus& status);
  // Overiddable to allow for reporting errors to a service backend.
  // |on_error_reported| will be run whether the error is reported successfully
  // or not.
  virtual void ReportError(const std::string& error_message,
                           base::OnceClosure on_error_reported);

  void SetDaemonControllerForTest(
      scoped_refptr<DaemonController> daemon_controller);

 private:
  Params start_host_params_;
  scoped_refptr<RsaKeyPair> key_pair_{RsaKeyPair::Generate()};
  std::optional<std::string> existing_host_id_;

  std::string service_account_email_;
  std::string service_account_refresh_token_;

  HostStarterOAuthHelper oauth_helper_;

  scoped_refptr<DaemonController> daemon_controller_ =
      DaemonController::Create();

  CompletionCallback on_done_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtr<HostStarterBase> weak_ptr_;
  base::WeakPtrFactory<HostStarterBase> weak_ptr_factory_{this};
};

}  // namespace remoting

#endif  // REMOTING_HOST_SETUP_HOST_STARTER_BASE_H_