host_list_service.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 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. #if !defined(__has_feature) || !__has_feature(objc_arc)
  5. #error "This file requires ARC support."
  6. #endif
  7. #import "remoting/ios/facade/host_list_service.h"
  8. #import <CoreFoundation/CoreFoundation.h>
  9. #include <algorithm>
  10. #import "remoting/ios/domain/user_info.h"
  11. #import "remoting/ios/facade/remoting_authentication.h"
  12. #import "remoting/ios/facade/remoting_service.h"
  13. #include "base/bind.h"
  14. #include "base/logging.h"
  15. #include "base/no_destructor.h"
  16. #include "remoting/base/directory_service_client.h"
  17. #include "remoting/base/protobuf_http_status.h"
  18. #include "remoting/base/string_resources.h"
  19. #include "remoting/base/task_util.h"
  20. #include "remoting/client/chromoting_client_runtime.h"
  21. #include "services/network/public/cpp/shared_url_loader_factory.h"
  22. #include "ui/base/l10n/l10n_util.h"
  23. namespace remoting {
  24. namespace {
  25. HostListService::FetchFailureReason MapError(
  26. ProtobufHttpStatus::Code status_code) {
  27. switch (status_code) {
  28. case ProtobufHttpStatus::Code::UNAVAILABLE:
  29. case ProtobufHttpStatus::Code::DEADLINE_EXCEEDED:
  30. return HostListService::FetchFailureReason::NETWORK_ERROR;
  31. case ProtobufHttpStatus::Code::PERMISSION_DENIED:
  32. case ProtobufHttpStatus::Code::UNAUTHENTICATED:
  33. return HostListService::FetchFailureReason::AUTH_ERROR;
  34. default:
  35. return HostListService::FetchFailureReason::UNKNOWN_ERROR;
  36. }
  37. }
  38. // Returns true if |h1| should sort before |h2|.
  39. bool CompareHost(const apis::v1::HostInfo& h1, const apis::v1::HostInfo& h2) {
  40. // Online hosts always sort before offline hosts.
  41. if (h1.status() != h2.status()) {
  42. return h1.status() == apis::v1::HostInfo_Status_ONLINE;
  43. }
  44. // Sort by host name.
  45. int name_compare = h1.host_name().compare(h2.host_name());
  46. if (name_compare != 0) {
  47. return name_compare < 0;
  48. }
  49. // Sort by last seen time if names are identical.
  50. return h1.last_seen_time() < h2.last_seen_time();
  51. }
  52. } // namespace
  53. HostListService* HostListService::GetInstance() {
  54. static base::NoDestructor<HostListService> instance;
  55. return instance.get();
  56. }
  57. HostListService::HostListService()
  58. : HostListService(ChromotingClientRuntime::GetInstance()
  59. ->CreateDirectoryServiceClient()) {}
  60. HostListService::HostListService(
  61. base::SequenceBound<DirectoryServiceClient> directory_client) {
  62. directory_client_ = std::move(directory_client);
  63. Init();
  64. }
  65. HostListService::~HostListService() {
  66. [NSNotificationCenter.defaultCenter removeObserver:user_update_observer_];
  67. }
  68. void HostListService::Init() {
  69. auto weak_this = weak_factory_.GetWeakPtr();
  70. user_update_observer_ = [NSNotificationCenter.defaultCenter
  71. addObserverForName:kUserDidUpdate
  72. object:nil
  73. queue:nil
  74. usingBlock:^(NSNotification* notification) {
  75. UserInfo* user = notification.userInfo[kUserInfo];
  76. if (weak_this) {
  77. weak_this->OnUserUpdated(user != nil);
  78. }
  79. }];
  80. }
  81. base::CallbackListSubscription HostListService::RegisterHostListStateCallback(
  82. const base::RepeatingClosure& callback) {
  83. return host_list_state_callbacks_.Add(callback);
  84. }
  85. base::CallbackListSubscription HostListService::RegisterFetchFailureCallback(
  86. const base::RepeatingClosure& callback) {
  87. return fetch_failure_callbacks_.Add(callback);
  88. }
  89. void HostListService::RequestFetch() {
  90. if (state_ == State::FETCHING) {
  91. return;
  92. }
  93. SetState(State::FETCHING);
  94. PostWithCallback(FROM_HERE, &directory_client_,
  95. &DirectoryServiceClient::GetHostList,
  96. base::BindOnce(&HostListService::HandleHostListResult,
  97. weak_factory_.GetWeakPtr()));
  98. }
  99. void HostListService::SetState(State state) {
  100. if (state == state_) {
  101. return;
  102. }
  103. if (state == State::NOT_FETCHED) {
  104. hosts_ = {};
  105. } else if (state == State::FETCHING || state == State::FETCHED) {
  106. last_fetch_failure_.reset();
  107. }
  108. state_ = state;
  109. host_list_state_callbacks_.Notify();
  110. }
  111. void HostListService::HandleHostListResult(
  112. const ProtobufHttpStatus& status,
  113. std::unique_ptr<apis::v1::GetHostListResponse> response) {
  114. if (!status.ok()) {
  115. HandleFetchFailure(status);
  116. return;
  117. }
  118. hosts_.clear();
  119. for (const auto& host : response->hosts()) {
  120. hosts_.push_back(host);
  121. }
  122. std::sort(hosts_.begin(), hosts_.end(), &CompareHost);
  123. SetState(State::FETCHED);
  124. }
  125. void HostListService::HandleFetchFailure(const ProtobufHttpStatus& status) {
  126. SetState(State::NOT_FETCHED);
  127. if (status.error_code() == ProtobufHttpStatus::Code::CANCELLED) {
  128. return;
  129. }
  130. last_fetch_failure_ = std::make_unique<FetchFailureInfo>();
  131. last_fetch_failure_->reason = MapError(status.error_code());
  132. switch (last_fetch_failure_->reason) {
  133. case FetchFailureReason::NETWORK_ERROR:
  134. last_fetch_failure_->localized_description =
  135. l10n_util::GetStringUTF8(IDS_ERROR_NETWORK_ERROR);
  136. break;
  137. case FetchFailureReason::AUTH_ERROR:
  138. last_fetch_failure_->localized_description =
  139. l10n_util::GetStringUTF8(IDS_ERROR_OAUTH_TOKEN_INVALID);
  140. break;
  141. default:
  142. last_fetch_failure_->localized_description = status.error_message();
  143. }
  144. LOG(WARNING) << "Failed to fetch host list: "
  145. << last_fetch_failure_->localized_description
  146. << " reason: " << static_cast<int>(last_fetch_failure_->reason);
  147. fetch_failure_callbacks_.Notify();
  148. if (last_fetch_failure_->reason == FetchFailureReason::AUTH_ERROR) {
  149. [RemotingService.instance.authentication logout];
  150. }
  151. }
  152. void HostListService::OnUserUpdated(bool is_user_signed_in) {
  153. directory_client_.AsyncCall(&DirectoryServiceClient::CancelPendingRequests);
  154. SetState(State::NOT_FETCHED);
  155. if (is_user_signed_in) {
  156. RequestFetch();
  157. }
  158. }
  159. } // namespace remoting