remoting_service.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2021 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/host/chromeos/remoting_service.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/no_destructor.h"
  8. #include "base/sequence_checker.h"
  9. #include "base/task/task_traits.h"
  10. #include "base/task/thread_pool.h"
  11. #include "chrome/browser/browser_process.h"
  12. #include "content/public/browser/browser_task_traits.h"
  13. #include "content/public/browser/browser_thread.h"
  14. #include "content/public/browser/storage_partition.h"
  15. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  16. #include "remoting/base/auto_thread_task_runner.h"
  17. #include "remoting/host/chromeos/remote_support_host_ash.h"
  18. #include "remoting/host/chromoting_host_context.h"
  19. #include "remoting/host/policy_watcher.h"
  20. namespace remoting {
  21. namespace {
  22. class RemotingServiceImpl : public RemotingService {
  23. public:
  24. RemotingServiceImpl();
  25. RemotingServiceImpl(const RemotingServiceImpl&) = delete;
  26. RemotingServiceImpl& operator=(const RemotingServiceImpl&) = delete;
  27. ~RemotingServiceImpl() override;
  28. // RemotingService implementation.
  29. RemoteSupportHostAsh& GetSupportHost() override;
  30. std::unique_ptr<ChromotingHostContext> CreateHostContext() override;
  31. std::unique_ptr<PolicyWatcher> CreatePolicyWatcher() override;
  32. private:
  33. void ReleaseSupportHost();
  34. SEQUENCE_CHECKER(sequence_checker_);
  35. std::unique_ptr<RemoteSupportHostAsh> remote_support_host_
  36. GUARDED_BY_CONTEXT(sequence_checker_);
  37. };
  38. RemotingServiceImpl::RemotingServiceImpl() = default;
  39. RemotingServiceImpl::~RemotingServiceImpl() = default;
  40. RemoteSupportHostAsh& RemotingServiceImpl::GetSupportHost() {
  41. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  42. if (!remote_support_host_) {
  43. remote_support_host_ =
  44. std::make_unique<RemoteSupportHostAsh>(base::BindOnce(
  45. &RemotingServiceImpl::ReleaseSupportHost, base::Unretained(this)));
  46. }
  47. return *remote_support_host_;
  48. }
  49. std::unique_ptr<ChromotingHostContext>
  50. RemotingServiceImpl::CreateHostContext() {
  51. return ChromotingHostContext::CreateForChromeOS(
  52. content::GetIOThreadTaskRunner({}), content::GetUIThreadTaskRunner({}),
  53. base::ThreadPool::CreateSingleThreadTaskRunner(
  54. {base::MayBlock(), base::TaskPriority::BEST_EFFORT}));
  55. }
  56. std::unique_ptr<PolicyWatcher> RemotingServiceImpl::CreatePolicyWatcher() {
  57. return PolicyWatcher::CreateWithPolicyService(
  58. g_browser_process->policy_service());
  59. }
  60. void RemotingServiceImpl::ReleaseSupportHost() {
  61. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  62. remote_support_host_.reset();
  63. }
  64. } // namespace
  65. RemotingService& RemotingService::Get() {
  66. static base::NoDestructor<RemotingServiceImpl> instance;
  67. return *instance;
  68. }
  69. } // namespace remoting