File: cloud_service_client.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 6,548 bytes parent folder | download | duplicates (3)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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_BASE_CLOUD_SERVICE_CLIENT_H_
#define REMOTING_BASE_CLOUD_SERVICE_CLIENT_H_

#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "remoting/base/protobuf_http_client.h"
#include "remoting/base/protobuf_http_request_config.h"

namespace google::internal::remoting::cloud::v1alpha {
class Empty;
class GenerateHostTokenResponse;
class GenerateIceConfigResponse;
class ReauthorizeHostResponse;
class RemoteAccessHost;
class VerifySessionTokenResponse;
}  // namespace google::internal::remoting::cloud::v1alpha

namespace google::remoting::cloud::v1 {
class ProvisionGceInstanceResponse;
}  // namespace google::remoting::cloud::v1

namespace google::protobuf {
class MessageLite;
}  // namespace google::protobuf

namespace net {
struct NetworkTrafficAnnotationTag;
}  // namespace net

namespace remoting {

class HttpStatus;

// A service client that communicates with the directory service.
class CloudServiceClient {
 public:
  using GenerateHostTokenCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<::google::internal::remoting::cloud::v1alpha::
                          GenerateHostTokenResponse>)>;
  using GenerateIceConfigCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<::google::internal::remoting::cloud::v1alpha::
                          GenerateIceConfigResponse>)>;
  using ProvisionGceInstanceCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<
          ::google::remoting::cloud::v1::ProvisionGceInstanceResponse>)>;
  using ReauthorizeHostCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<::google::internal::remoting::cloud::v1alpha::
                          ReauthorizeHostResponse>)>;
  using SendHeartbeatCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<::google::internal::remoting::cloud::v1alpha::Empty>)>;
  using UpdateRemoteAccessHostCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<
          ::google::internal::remoting::cloud::v1alpha::RemoteAccessHost>)>;
  using VerifySessionTokenCallback = base::OnceCallback<void(
      const HttpStatus&,
      std::unique_ptr<::google::internal::remoting::cloud::v1alpha::
                          VerifySessionTokenResponse>)>;

  // Used for creating a service client to call the Remoting Cloud Private API
  // using a scoped OAuth access token generated for the device robot account.
  static std::unique_ptr<CloudServiceClient> CreateForChromotingRobotAccount(
      OAuthTokenGetter* oauth_token_getter,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  // Used for creating a service client to call the Remoting Cloud API using
  // the |api_key| provided which associates the request with a GCP project.
  static std::unique_ptr<CloudServiceClient> CreateForGcpProject(
      const std::string& api_key,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  // Used for creating a service client to call the Remoting Cloud API using
  // an access token associated with the default service account for the
  // Compute Engine Instance the code is running on.
  static std::unique_ptr<CloudServiceClient> CreateForGceDefaultServiceAccount(
      OAuthTokenGetter* oauth_token_getter,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);

  virtual ~CloudServiceClient();

  CloudServiceClient(const CloudServiceClient&) = delete;
  CloudServiceClient& operator=(const CloudServiceClient&) = delete;

  void ProvisionGceInstance(
      const std::string& owner_email,
      const std::string& display_name,
      const std::string& public_key,
      const std::optional<std::string>& existing_directory_id,
      const std::optional<std::string>& instance_identity_token,
      ProvisionGceInstanceCallback callback);

  void SendHeartbeat(const std::string& directory_id,
                     std::string_view instance_identity_token,
                     SendHeartbeatCallback callback);

  void UpdateRemoteAccessHost(const std::string& directory_id,
                              std::optional<std::string> host_version,
                              std::optional<std::string> signaling_id,
                              std::optional<std::string> offline_reason,
                              std::optional<std::string> os_name,
                              std::optional<std::string> os_version,
                              std::string_view instance_identity_token,
                              UpdateRemoteAccessHostCallback callback);

  void GenerateIceConfig(std::string_view instance_identity_token,
                         GenerateIceConfigCallback callback);

  void GenerateHostToken(std::string_view instance_identity_token,
                         GenerateHostTokenCallback callback);

  void VerifySessionToken(const std::string& session_token,
                          std::string_view instance_identity_token,
                          VerifySessionTokenCallback callback);

  void ReauthorizeHost(
      const std::string& session_reauth_token,
      const std::string& session_id,
      std::string_view instance_identity_token,
      scoped_refptr<const ProtobufHttpRequestConfig::RetryPolicy> retry_policy,
      ReauthorizeHostCallback callback);

  void CancelPendingRequests();

 private:
  CloudServiceClient(
      const std::string& api_key,
      OAuthTokenGetter* oauth_token_getter,
      const std::string& base_service_url,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);

  template <typename CallbackType>
  void ExecuteRequest(
      const net::NetworkTrafficAnnotationTag& traffic_annotation,
      const std::string& path,
      const std::string& api_key,
      const std::string& method,
      std::unique_ptr<google::protobuf::MessageLite> request_message,
      CallbackType callback,
      scoped_refptr<const ProtobufHttpRequestConfig::RetryPolicy> retry_policy =
          ProtobufHttpRequestConfig::GetSimpleRetryPolicy());

  // The customer API_KEY to use for calling the Remoting Cloud API.
  std::string api_key_;

  ProtobufHttpClient http_client_;
};

}  // namespace remoting

#endif  // REMOTING_BASE_CLOUD_SERVICE_CLIENT_H_