chromoting_client_runtime.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2016 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. #ifndef REMOTING_CLIENT_CHROMOTING_CLIENT_RUNTIME_H_
  5. #define REMOTING_CLIENT_CHROMOTING_CLIENT_RUNTIME_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/threading/sequence_bound.h"
  11. #include "net/url_request/url_request_context_getter.h"
  12. #include "remoting/base/auto_thread.h"
  13. #include "remoting/base/oauth_token_getter.h"
  14. #include "remoting/base/telemetry_log_writer.h"
  15. namespace base {
  16. class SingleThreadTaskExecutor;
  17. template <typename T>
  18. struct DefaultSingletonTraits;
  19. } // namespace base
  20. namespace network {
  21. class SharedURLLoaderFactory;
  22. class TransitionalURLLoaderFactoryOwner;
  23. } // namespace network
  24. // Houses the global resources on which the Chromoting components run
  25. // (e.g. message loops and task runners).
  26. namespace remoting {
  27. class DirectoryServiceClient;
  28. class ChromotingClientRuntime {
  29. public:
  30. class Delegate {
  31. public:
  32. virtual ~Delegate() {}
  33. // RuntimeWillShutdown will be called on the delegate when the runtime
  34. // enters into the destructor. This is a good time for the delegate to
  35. // start shutting down on threads while they exist.
  36. virtual void RuntimeWillShutdown() = 0;
  37. // RuntimeDidShutdown will be called after task managers and threads
  38. // have been stopped.
  39. virtual void RuntimeDidShutdown() = 0;
  40. // For fetching auth token. Called on the UI thread.
  41. virtual base::WeakPtr<OAuthTokenGetter> oauth_token_getter() = 0;
  42. };
  43. static ChromotingClientRuntime* GetInstance();
  44. ChromotingClientRuntime(const ChromotingClientRuntime&) = delete;
  45. ChromotingClientRuntime& operator=(const ChromotingClientRuntime&) = delete;
  46. // Must be called before calling any other methods on this object.
  47. void Init(ChromotingClientRuntime::Delegate* delegate);
  48. std::unique_ptr<OAuthTokenGetter> CreateOAuthTokenGetter();
  49. base::SequenceBound<DirectoryServiceClient> CreateDirectoryServiceClient();
  50. scoped_refptr<AutoThreadTaskRunner> network_task_runner() {
  51. return network_task_runner_;
  52. }
  53. scoped_refptr<AutoThreadTaskRunner> audio_task_runner() {
  54. return audio_task_runner_;
  55. }
  56. scoped_refptr<AutoThreadTaskRunner> ui_task_runner() {
  57. return ui_task_runner_;
  58. }
  59. scoped_refptr<AutoThreadTaskRunner> display_task_runner() {
  60. return display_task_runner_;
  61. }
  62. scoped_refptr<net::URLRequestContextGetter> url_requester() {
  63. return url_requester_;
  64. }
  65. // Must be called from the network thread.
  66. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory();
  67. ChromotingEventLogWriter* log_writer() { return log_writer_.get(); }
  68. private:
  69. ChromotingClientRuntime();
  70. virtual ~ChromotingClientRuntime();
  71. // Initializes URL loader factory owner, log writer, and other resources on
  72. // the network thread.
  73. void InitializeOnNetworkThread();
  74. // Chromium code's connection to the app message loop. Once created the
  75. // SingleThreadTaskExecutor will live for the life of the program.
  76. std::unique_ptr<base::SingleThreadTaskExecutor> ui_task_executor_;
  77. // References to native threads.
  78. scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
  79. // TODO(nicholss): AutoThreads will be leaked because they depend on the main
  80. // thread. We should update this class to use regular threads like the client
  81. // plugin does.
  82. // Longer term we should migrate most of these to background tasks except the
  83. // network thread to ThreadPool, removing the need for threads.
  84. scoped_refptr<AutoThreadTaskRunner> audio_task_runner_;
  85. scoped_refptr<AutoThreadTaskRunner> display_task_runner_;
  86. scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
  87. scoped_refptr<net::URLRequestContextGetter> url_requester_;
  88. std::unique_ptr<network::TransitionalURLLoaderFactoryOwner>
  89. url_loader_factory_owner_;
  90. // For logging session stage changes and stats.
  91. std::unique_ptr<TelemetryLogWriter> log_writer_;
  92. raw_ptr<ChromotingClientRuntime::Delegate> delegate_ = nullptr;
  93. friend struct base::DefaultSingletonTraits<ChromotingClientRuntime>;
  94. };
  95. } // namespace remoting
  96. #endif // REMOTING_CLIENT_CHROMOTING_CLIENT_RUNTIME_H_