directory_service_client.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include "remoting/base/directory_service_client.h"
  5. #include "base/bind.h"
  6. #include "remoting/base/protobuf_http_request.h"
  7. #include "remoting/base/protobuf_http_request_config.h"
  8. #include "remoting/base/service_urls.h"
  9. #include "remoting/proto/remoting/v1/directory_messages.pb.h"
  10. #include "services/network/public/cpp/shared_url_loader_factory.h"
  11. namespace remoting {
  12. DirectoryServiceClient::DirectoryServiceClient(
  13. OAuthTokenGetter* token_getter,
  14. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
  15. : http_client_(ServiceUrls::GetInstance()->remoting_server_endpoint(),
  16. token_getter,
  17. url_loader_factory) {}
  18. DirectoryServiceClient::~DirectoryServiceClient() = default;
  19. void DirectoryServiceClient::DeleteHost(const std::string& host_id,
  20. DeleteHostCallback callback) {
  21. constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
  22. net::DefineNetworkTrafficAnnotation("remoting_directory_delete_host",
  23. R"(
  24. semantics {
  25. sender: "Chrome Remote Desktop"
  26. description:
  27. "Deletes a Chrome Remote Desktop host from the user's account."
  28. trigger:
  29. "User deletes a Chrome Remote Desktop host from the host list."
  30. data:
  31. "User's Chrome Remote Desktop credentials and the Chrome Remote "
  32. "Desktop host ID that identifies the host."
  33. destination: GOOGLE_OWNED_SERVICE
  34. }
  35. policy {
  36. cookies_allowed: NO
  37. setting:
  38. "This request cannot be stopped in settings, but will not be sent "
  39. "if the user does not use Chrome Remote Desktop."
  40. policy_exception_justification:
  41. "Not implemented."
  42. })");
  43. constexpr char path[] = "/v1/directory:deletehost";
  44. auto delete_host_request = std::make_unique<apis::v1::DeleteHostRequest>();
  45. delete_host_request->set_host_id(host_id);
  46. ExecuteRequest(traffic_annotation, path, std::move(delete_host_request),
  47. std::move(callback));
  48. }
  49. void DirectoryServiceClient::GetHostList(GetHostListCallback callback) {
  50. constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
  51. net::DefineNetworkTrafficAnnotation("remoting_directory_get_host_list",
  52. R"(
  53. semantics {
  54. sender: "Chrome Remote Desktop"
  55. description:
  56. "Retrieves information about Chrome Remote Desktop hosts that are "
  57. "registered under the user's account."
  58. trigger:
  59. "User opens the Chrome Remote Desktop app."
  60. data:
  61. "User's Chrome Remote Desktop credentials."
  62. destination: GOOGLE_OWNED_SERVICE
  63. }
  64. policy {
  65. cookies_allowed: NO
  66. setting:
  67. "This request cannot be stopped in settings, but will not be sent "
  68. "if the user does not use Chrome Remote Desktop."
  69. policy_exception_justification:
  70. "Not implemented."
  71. })");
  72. constexpr char path[] = "/v1/directory:gethostlist";
  73. ExecuteRequest(traffic_annotation, path,
  74. std::make_unique<apis::v1::GetHostListRequest>(),
  75. std::move(callback));
  76. }
  77. void DirectoryServiceClient::RegisterHost(const std::string& host_id,
  78. const std::string& host_name,
  79. const std::string& public_key,
  80. const std::string& host_client_id,
  81. RegisterHostCallback callback) {
  82. constexpr net::NetworkTrafficAnnotationTag traffic_annotation =
  83. net::DefineNetworkTrafficAnnotation("remoting_directory_register_host",
  84. R"(
  85. semantics {
  86. sender: "Chrome Remote Desktop"
  87. description:
  88. "Registers a new Chrome Remote Desktop host under the user's "
  89. "account."
  90. trigger:
  91. "User runs the remoting_start_host command by typing it on the "
  92. "terminal. Third party administrators might implement scripts to "
  93. "run this automatically, but the Chrome Remote Desktop product "
  94. "does not come with such scripts."
  95. data:
  96. "User's Chrome Remote Desktop credentials and information about "
  97. "the new Chrome Remote Desktop host such as host ID and host name."
  98. destination: GOOGLE_OWNED_SERVICE
  99. }
  100. policy {
  101. cookies_allowed: NO
  102. setting:
  103. "This request cannot be stopped in settings, but will not be sent "
  104. "if the user does not use Chrome Remote Desktop."
  105. policy_exception_justification:
  106. "Not implemented."
  107. })");
  108. constexpr char path[] = "/v1/directory:registerhost";
  109. auto register_host_request =
  110. std::make_unique<apis::v1::RegisterHostRequest>();
  111. register_host_request->set_host_id(host_id);
  112. register_host_request->set_host_name(host_name);
  113. register_host_request->set_public_key(public_key);
  114. register_host_request->set_host_client_id(host_client_id);
  115. ExecuteRequest(traffic_annotation, path, std::move(register_host_request),
  116. std::move(callback));
  117. }
  118. void DirectoryServiceClient::CancelPendingRequests() {
  119. http_client_.CancelPendingRequests();
  120. }
  121. template <typename CallbackType>
  122. void DirectoryServiceClient::ExecuteRequest(
  123. const net::NetworkTrafficAnnotationTag& traffic_annotation,
  124. const std::string& path,
  125. std::unique_ptr<google::protobuf::MessageLite> request_message,
  126. CallbackType callback) {
  127. auto request_config =
  128. std::make_unique<ProtobufHttpRequestConfig>(traffic_annotation);
  129. request_config->path = path;
  130. request_config->request_message = std::move(request_message);
  131. auto request =
  132. std::make_unique<ProtobufHttpRequest>(std::move(request_config));
  133. request->SetResponseCallback(std::move(callback));
  134. http_client_.ExecuteRequest(std::move(request));
  135. }
  136. } // namespace remoting