jni_directory_service.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 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/client/jni/jni_directory_service.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/android/jni_android.h"
  9. #include "base/android/jni_array.h"
  10. #include "base/android/jni_string.h"
  11. #include "base/bind.h"
  12. #include "base/threading/sequenced_task_runner_handle.h"
  13. #include "remoting/android/jni_headers/DirectoryService_jni.h"
  14. #include "remoting/base/oauth_token_getter.h"
  15. #include "remoting/base/protobuf_http_status.h"
  16. #include "remoting/base/service_urls.h"
  17. #include "remoting/base/task_util.h"
  18. #include "remoting/client/chromoting_client_runtime.h"
  19. #include "remoting/proto/remoting/v1/directory_messages.pb.h"
  20. #include "services/network/public/cpp/shared_url_loader_factory.h"
  21. #include "base/logging.h"
  22. namespace remoting {
  23. namespace {
  24. JniDirectoryService::RequestError MapError(
  25. ProtobufHttpStatus::Code status_code) {
  26. switch (status_code) {
  27. case ProtobufHttpStatus::Code::UNAVAILABLE:
  28. return JniDirectoryService::RequestError::SERVICE_UNAVAILABLE;
  29. case ProtobufHttpStatus::Code::PERMISSION_DENIED:
  30. case ProtobufHttpStatus::Code::UNAUTHENTICATED:
  31. return JniDirectoryService::RequestError::AUTH_FAILED;
  32. default:
  33. return JniDirectoryService::RequestError::UNKNOWN;
  34. }
  35. }
  36. } // namespace
  37. JniDirectoryService::JniDirectoryService()
  38. : client_(ChromotingClientRuntime::GetInstance()
  39. ->CreateDirectoryServiceClient()),
  40. sequence_(base::SequencedTaskRunnerHandle::Get()) {}
  41. JniDirectoryService::~JniDirectoryService() {
  42. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  43. }
  44. void JniDirectoryService::RetrieveHostList(
  45. JNIEnv* env,
  46. const base::android::JavaParamRef<jobject>& callback) {
  47. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  48. PostWithCallback(
  49. FROM_HERE, &client_, &DirectoryServiceClient::GetHostList,
  50. base::BindOnce(&JniDirectoryService::OnHostListRetrieved,
  51. weak_factory_.GetWeakPtr(),
  52. base::android::ScopedJavaGlobalRef<jobject>(callback)));
  53. }
  54. void JniDirectoryService::DeleteHost(
  55. JNIEnv* env,
  56. const base::android::JavaParamRef<jstring>& host_id,
  57. const base::android::JavaParamRef<jobject>& callback) {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. PostWithCallback(
  60. FROM_HERE, &client_, &DirectoryServiceClient::DeleteHost,
  61. base::BindOnce(&JniDirectoryService::OnHostDeleted,
  62. weak_factory_.GetWeakPtr(),
  63. base::android::ScopedJavaGlobalRef<jobject>(callback)),
  64. base::android::ConvertJavaStringToUTF8(env, host_id));
  65. }
  66. void JniDirectoryService::Destroy(JNIEnv* env) {
  67. if (sequence_->RunsTasksInCurrentSequence()) {
  68. delete this;
  69. } else {
  70. sequence_->DeleteSoon(FROM_HERE, this);
  71. }
  72. }
  73. void JniDirectoryService::OnHostListRetrieved(
  74. base::android::ScopedJavaGlobalRef<jobject> callback,
  75. const ProtobufHttpStatus& status,
  76. std::unique_ptr<apis::v1::GetHostListResponse> response) {
  77. JNIEnv* env = base::android::AttachCurrentThread();
  78. if (status.ok()) {
  79. Java_DirectoryService_onHostListRetrieved(
  80. env, callback,
  81. base::android::ToJavaByteArray(env, response->SerializeAsString()));
  82. } else {
  83. LOG(ERROR) << "Retrieving host list failed: " << status.error_message();
  84. Java_DirectoryService_onError(
  85. env, callback, static_cast<jint>(MapError(status.error_code())));
  86. }
  87. }
  88. void JniDirectoryService::OnHostDeleted(
  89. base::android::ScopedJavaGlobalRef<jobject> callback,
  90. const ProtobufHttpStatus& status,
  91. std::unique_ptr<apis::v1::DeleteHostResponse> response) {
  92. JNIEnv* env = base::android::AttachCurrentThread();
  93. if (status.ok()) {
  94. Java_DirectoryService_onHostDeleted(env, callback);
  95. } else {
  96. LOG(ERROR) << "Deleting host failed: " << status.error_message();
  97. // TODO(rkjnsn): Translate error code from status.
  98. Java_DirectoryService_onError(
  99. env, callback, static_cast<jint>(MapError(status.error_code())));
  100. }
  101. }
  102. static jlong JNI_DirectoryService_Init(JNIEnv* env) {
  103. return reinterpret_cast<intptr_t>(new JniDirectoryService());
  104. }
  105. } // namespace remoting