#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 {
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;
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_; }
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);
void HandleError(const std::string& error_message, Result error_result);
void HandleHttpStatusError(const HttpStatus& status);
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};
};
}
#endif