daemon_process.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2012 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_HOST_DAEMON_PROCESS_H_
  5. #define REMOTING_HOST_DAEMON_PROCESS_H_
  6. #include <stdint.h>
  7. #include <list>
  8. #include <memory>
  9. #include <string>
  10. #include "base/compiler_specific.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/process/process.h"
  13. #include "base/time/time.h"
  14. #include "ipc/ipc_message.h"
  15. #include "mojo/public/cpp/bindings/associated_receiver.h"
  16. #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
  17. #include "mojo/public/cpp/system/message_pipe.h"
  18. #include "remoting/host/config_watcher.h"
  19. #include "remoting/host/host_status_monitor.h"
  20. #include "remoting/host/host_status_observer.h"
  21. #include "remoting/host/mojom/desktop_session.mojom.h"
  22. #include "remoting/host/worker_process_ipc_delegate.h"
  23. namespace base {
  24. class Location;
  25. } // namespace base
  26. namespace remoting {
  27. class AutoThreadTaskRunner;
  28. class DesktopSession;
  29. class HostEventLogger;
  30. class ScreenResolution;
  31. // This class implements core of the daemon process. It manages the networking
  32. // process running at lower privileges and maintains the list of desktop
  33. // sessions.
  34. class DaemonProcess : public ConfigWatcher::Delegate,
  35. public WorkerProcessIpcDelegate,
  36. public HostStatusObserver,
  37. public mojom::DesktopSessionManager {
  38. public:
  39. typedef std::list<DesktopSession*> DesktopSessionList;
  40. DaemonProcess(const DaemonProcess&) = delete;
  41. DaemonProcess& operator=(const DaemonProcess&) = delete;
  42. ~DaemonProcess() override;
  43. // Creates a platform-specific implementation of the daemon process object
  44. // passing relevant task runners. Public methods of this class must be called
  45. // on the |caller_task_runner| thread. |io_task_runner| is used to handle IPC
  46. // and background I/O tasks.
  47. static std::unique_ptr<DaemonProcess> Create(
  48. scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
  49. scoped_refptr<AutoThreadTaskRunner> io_task_runner,
  50. const base::OnceClosure stopped_callback);
  51. // ConfigWatcher::Delegate
  52. void OnConfigUpdated(const std::string& serialized_config) override;
  53. void OnConfigWatcherError() override;
  54. scoped_refptr<HostStatusMonitor> status_monitor() { return status_monitor_; }
  55. // WorkerProcessIpcDelegate implementation.
  56. void OnChannelConnected(int32_t peer_pid) override;
  57. void OnPermanentError(int exit_code) override;
  58. void OnWorkerProcessStopped() override;
  59. void OnAssociatedInterfaceRequest(
  60. const std::string& interface_name,
  61. mojo::ScopedInterfaceEndpointHandle handle) override;
  62. // mojom::DesktopSessionManager implementation.
  63. void CreateDesktopSession(int terminal_id,
  64. const ScreenResolution& resolution,
  65. bool virtual_terminal) override;
  66. void CloseDesktopSession(int terminal_id) override;
  67. void SetScreenResolution(int terminal_id,
  68. const ScreenResolution& resolution) override;
  69. // Called when a desktop integration process attaches to |terminal_id|.
  70. // |session_id| is the id of the desktop session being attached.
  71. // |desktop_pipe| specifies the client end of the desktop pipe. Returns true
  72. // on success, false otherwise.
  73. virtual bool OnDesktopSessionAgentAttached(
  74. int terminal_id,
  75. int session_id,
  76. mojo::ScopedMessagePipeHandle desktop_pipe) = 0;
  77. // Requests the network process to crash.
  78. void CrashNetworkProcess(const base::Location& location);
  79. protected:
  80. DaemonProcess(scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
  81. scoped_refptr<AutoThreadTaskRunner> io_task_runner,
  82. base::OnceClosure stopped_callback);
  83. // Reads the host configuration and launches the network process.
  84. void Initialize();
  85. // Invokes |stopped_callback_| to ask the owner to delete |this|.
  86. void Stop();
  87. // Returns true if |terminal_id| is in the range of allocated IDs. I.e. it is
  88. // less or equal to the highest ID we have seen so far.
  89. bool WasTerminalIdAllocated(int terminal_id);
  90. // HostStatusObserver overrides.
  91. void OnClientAccessDenied(const std::string& signaling_id) override;
  92. void OnClientAuthenticated(const std::string& signaling_id) override;
  93. void OnClientConnected(const std::string& signaling_id) override;
  94. void OnClientDisconnected(const std::string& signaling_id) override;
  95. void OnClientRouteChange(const std::string& signaling_id,
  96. const std::string& channel_name,
  97. const protocol::TransportRoute& route) override;
  98. void OnHostStarted(const std::string& owner_email) override;
  99. void OnHostShutdown() override;
  100. // Creates a platform-specific desktop session and assigns a unique ID to it.
  101. // An implementation should validate |params| as they are received via IPC.
  102. virtual std::unique_ptr<DesktopSession> DoCreateDesktopSession(
  103. int terminal_id,
  104. const ScreenResolution& resolution,
  105. bool virtual_terminal) = 0;
  106. // Requests the network process to crash.
  107. virtual void DoCrashNetworkProcess(const base::Location& location) = 0;
  108. // Launches the network process and establishes an IPC channel with it.
  109. virtual void LaunchNetworkProcess() = 0;
  110. // Sends |serialized_config| to the network process. The config includes
  111. // details such as the host owner email and robot account refresh token which
  112. // are required to start the host and get online.
  113. virtual void SendHostConfigToNetworkProcess(
  114. const std::string& serialized_config) = 0;
  115. // Notifies the network process that the daemon has disconnected the desktop
  116. // session from the associated desktop environment.
  117. virtual void SendTerminalDisconnected(int terminal_id) = 0;
  118. scoped_refptr<AutoThreadTaskRunner> caller_task_runner() {
  119. return caller_task_runner_;
  120. }
  121. scoped_refptr<AutoThreadTaskRunner> io_task_runner() {
  122. return io_task_runner_;
  123. }
  124. // Let the test code analyze the list of desktop sessions.
  125. friend class DaemonProcessTest;
  126. const DesktopSessionList& desktop_sessions() const {
  127. return desktop_sessions_;
  128. }
  129. private:
  130. // Deletes all desktop sessions.
  131. void DeleteAllDesktopSessions();
  132. // Gets the location of the config file.
  133. base::FilePath GetConfigPath();
  134. // Task runner on which public methods of this class must be called.
  135. scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
  136. // Handles IPC and background I/O tasks.
  137. scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
  138. std::unique_ptr<ConfigWatcher> config_watcher_;
  139. // The configuration file contents.
  140. std::string serialized_config_;
  141. // The list of active desktop sessions.
  142. DesktopSessionList desktop_sessions_;
  143. // The highest desktop session ID that has been seen so far.
  144. int next_terminal_id_;
  145. // Invoked to ask the owner to delete |this|.
  146. base::OnceClosure stopped_callback_;
  147. // Writes host status updates to the system event log.
  148. std::unique_ptr<HostEventLogger> host_event_logger_;
  149. mojo::AssociatedReceiver<mojom::DesktopSessionManager>
  150. desktop_session_manager_{this};
  151. mojo::AssociatedReceiver<mojom::HostStatusObserver> host_status_observer_{
  152. this};
  153. scoped_refptr<HostStatusMonitor> status_monitor_;
  154. };
  155. } // namespace remoting
  156. #endif // REMOTING_HOST_DAEMON_PROCESS_H_