| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_CHROMOTING_HOST_H_ | 5 #ifndef REMOTING_CHROMOTING_HOST_H_ |
| 6 #define REMOTING_CHROMOTING_HOST_H_ | 6 #define REMOTING_CHROMOTING_HOST_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/thread.h" | 10 #include "base/thread.h" |
| 11 #include "remoting/host/capturer.h" | 11 #include "remoting/host/capturer.h" |
| 12 #include "remoting/host/client_connection.h" | 12 #include "remoting/host/client_connection.h" |
| 13 #include "remoting/host/encoder.h" | 13 #include "remoting/host/encoder.h" |
| 14 #include "remoting/host/event_executor.h" | 14 #include "remoting/host/event_executor.h" |
| 15 #include "remoting/host/heartbeat_sender.h" | 15 #include "remoting/host/heartbeat_sender.h" |
| 16 #include "remoting/host/session_manager.h" | 16 #include "remoting/host/session_manager.h" |
| 17 #include "remoting/jingle_glue/jingle_client.h" | 17 #include "remoting/jingle_glue/jingle_client.h" |
| 18 #include "remoting/jingle_glue/jingle_thread.h" | 18 #include "remoting/jingle_glue/jingle_thread.h" |
| 19 | 19 |
| 20 namespace base { | 20 class Task; |
| 21 class WaitableEvent; | |
| 22 } // namespace base | |
| 23 | 21 |
| 24 namespace remoting { | 22 namespace remoting { |
| 25 | 23 |
| 24 class ChromotingHostContext; |
| 26 class MutableHostConfig; | 25 class MutableHostConfig; |
| 27 | 26 |
| 28 // A class to implement the functionality of a host process. | 27 // A class to implement the functionality of a host process. |
| 29 // | 28 // |
| 30 // Here's the work flow of this class: | 29 // Here's the work flow of this class: |
| 31 // 1. We should load the saved GAIA ID token or if this is the first | 30 // 1. We should load the saved GAIA ID token or if this is the first |
| 32 // time the host process runs we should prompt user for the | 31 // time the host process runs we should prompt user for the |
| 33 // credential. We will use this token or credentials to authenicate | 32 // credential. We will use this token or credentials to authenicate |
| 34 // and register the host. | 33 // and register the host. |
| 35 // | 34 // |
| 36 // 2. We listen for incoming connection using libjingle. We will create | 35 // 2. We listen for incoming connection using libjingle. We will create |
| 37 // a ClientConnection object that wraps around linjingle for transport. Also | 36 // a ClientConnection object that wraps around linjingle for transport. Also |
| 38 // create a SessionManager with appropriate Encoder and Capturer and | 37 // create a SessionManager with appropriate Encoder and Capturer and |
| 39 // add the ClientConnection to this SessionManager for transporting the | 38 // add the ClientConnection to this SessionManager for transporting the |
| 40 // screen captures. A EventExecutor is created and registered with the | 39 // screen captures. A EventExecutor is created and registered with the |
| 41 // ClientConnection to receive mouse / keyboard events from the remote | 40 // ClientConnection to receive mouse / keyboard events from the remote |
| 42 // client. | 41 // client. |
| 43 // This is also the right time to create multiple threads to host | 42 // This is also the right time to create multiple threads to host |
| 44 // the above objects. After we have done all the initialization | 43 // the above objects. After we have done all the initialization |
| 45 // we'll start the SessionManager. We'll then enter the running state | 44 // we'll start the SessionManager. We'll then enter the running state |
| 46 // of the host process. | 45 // of the host process. |
| 47 // | 46 // |
| 48 // 3. When the user is disconencted, we will pause the SessionManager | 47 // 3. When the user is disconencted, we will pause the SessionManager |
| 49 // and try to terminate the threads we have created. This will allow | 48 // and try to terminate the threads we have created. This will allow |
| 50 // all pending tasks to complete. After all of that completed we | 49 // all pending tasks to complete. After all of that completed we |
| 51 // return to the idle state. We then go to step (2) if there a new | 50 // return to the idle state. We then go to step (2) if there a new |
| 52 // incoming connection. | 51 // incoming connection. |
| 53 class ChromotingHost : public base::RefCountedThreadSafe<ChromotingHost>, | 52 class ChromotingHost : public base::RefCountedThreadSafe<ChromotingHost>, |
| 54 public ClientConnection::EventHandler, | 53 public ClientConnection::EventHandler, |
| 55 public JingleClient::Callback { | 54 public JingleClient::Callback { |
| 56 public: | 55 public: |
| 57 ChromotingHost(MutableHostConfig* config, Capturer* capturer, | 56 ChromotingHost(ChromotingHostContext* context, MutableHostConfig* config, |
| 58 Encoder* encoder, EventExecutor* executor, | 57 Capturer* capturer, Encoder* encoder, EventExecutor* executor); |
| 59 base::WaitableEvent* host_done); | |
| 60 virtual ~ChromotingHost(); | 58 virtual ~ChromotingHost(); |
| 61 | 59 |
| 62 // Run the host porcess. This method returns only after the message loop | 60 // Start the host porcess. This methods starts the chromoting host |
| 63 // of the host process exits. | 61 // asynchronously. |
| 64 void Run(); | 62 // |
| 63 // |shutdown_task| is called if Start() has failed ot Shutdown() is called |
| 64 // and all related operations are completed. |
| 65 // |
| 66 // This method can only be called once during the lifetime of this object. |
| 67 void Start(Task* shutdown_task); |
| 65 | 68 |
| 66 // This method is called when we need to the host process. | 69 // This method is called when we need to the host process. |
| 67 void DestroySession(); | 70 void Shutdown(); |
| 68 | |
| 69 // This method talks to the cloud to register the host process. If | |
| 70 // successful we will start listening to network requests. | |
| 71 void RegisterHost(); | |
| 72 | 71 |
| 73 // This method is called if a client is connected to this object. | 72 // This method is called if a client is connected to this object. |
| 74 void OnClientConnected(ClientConnection* client); | 73 void OnClientConnected(ClientConnection* client); |
| 75 | 74 |
| 76 // This method is called if a client is disconnected from the host. | 75 // This method is called if a client is disconnected from the host. |
| 77 void OnClientDisconnected(ClientConnection* client); | 76 void OnClientDisconnected(ClientConnection* client); |
| 78 | 77 |
| 79 //////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////// |
| 80 // ClientConnection::EventHandler implementations | 79 // ClientConnection::EventHandler implementations |
| 81 virtual void HandleMessages(ClientConnection* client, | 80 virtual void HandleMessages(ClientConnection* client, |
| 82 ClientMessageList* messages); | 81 ClientMessageList* messages); |
| 83 virtual void OnConnectionOpened(ClientConnection* client); | 82 virtual void OnConnectionOpened(ClientConnection* client); |
| 84 virtual void OnConnectionClosed(ClientConnection* client); | 83 virtual void OnConnectionClosed(ClientConnection* client); |
| 85 virtual void OnConnectionFailed(ClientConnection* client); | 84 virtual void OnConnectionFailed(ClientConnection* client); |
| 86 | 85 |
| 87 //////////////////////////////////////////////////////////////////////////// | 86 //////////////////////////////////////////////////////////////////////////// |
| 88 // JingleClient::Callback implementations | 87 // JingleClient::Callback implementations |
| 89 virtual void OnStateChange(JingleClient* client, JingleClient::State state); | 88 virtual void OnStateChange(JingleClient* client, JingleClient::State state); |
| 90 virtual bool OnAcceptConnection( | 89 virtual bool OnAcceptConnection( |
| 91 JingleClient* jingle, const std::string& jid, | 90 JingleClient* jingle, const std::string& jid, |
| 92 JingleChannel::Callback** channel_callback); | 91 JingleChannel::Callback** channel_callback); |
| 93 virtual void OnNewConnection( | 92 virtual void OnNewConnection( |
| 94 JingleClient* jingle, | 93 JingleClient* jingle, |
| 95 scoped_refptr<JingleChannel> channel); | 94 scoped_refptr<JingleChannel> channel); |
| 96 | 95 |
| 97 private: | 96 private: |
| 98 // The message loop that this class runs on. | 97 enum State { |
| 99 MessageLoop* message_loop(); | 98 kInitial, |
| 99 kStarted, |
| 100 kStopped, |
| 101 }; |
| 100 | 102 |
| 101 // The main thread that this object runs on. | 103 // This method connects to the talk network and start listening for incoming |
| 102 base::Thread main_thread_; | 104 // connections. |
| 105 void DoStart(Task* shutdown_task); |
| 103 | 106 |
| 104 // Used to handle the Jingle connection. | 107 // This method shuts down the host process. |
| 105 JingleThread network_thread_; | 108 void DoShutdown(); |
| 106 | 109 |
| 107 // A thread that hosts capture operations. | 110 // The context that the chromoting host runs on. |
| 108 base::Thread capture_thread_; | 111 ChromotingHostContext* context_; |
| 109 | |
| 110 // A thread that hosts encode operations. | |
| 111 base::Thread encode_thread_; | |
| 112 | 112 |
| 113 scoped_refptr<MutableHostConfig> config_; | 113 scoped_refptr<MutableHostConfig> config_; |
| 114 | 114 |
| 115 // Capturer to be used by SessionManager. Once the SessionManager is | 115 // Capturer to be used by SessionManager. Once the SessionManager is |
| 116 // constructed this is set to NULL. | 116 // constructed this is set to NULL. |
| 117 scoped_ptr<Capturer> capturer_; | 117 scoped_ptr<Capturer> capturer_; |
| 118 | 118 |
| 119 // Encoder to be used by the SessionManager. Once the SessionManager is | 119 // Encoder to be used by the SessionManager. Once the SessionManager is |
| 120 // constructed this is set to NULL. | 120 // constructed this is set to NULL. |
| 121 scoped_ptr<Encoder> encoder_; | 121 scoped_ptr<Encoder> encoder_; |
| 122 | 122 |
| 123 // EventExecutor executes input events received from the client. | 123 // EventExecutor executes input events received from the client. |
| 124 scoped_ptr<EventExecutor> executor_; | 124 scoped_ptr<EventExecutor> executor_; |
| 125 | 125 |
| 126 // The libjingle client. This is used to connect to the talk network to | 126 // The libjingle client. This is used to connect to the talk network to |
| 127 // receive connection requests from chromoting client. | 127 // receive connection requests from chromoting client. |
| 128 scoped_refptr<JingleClient> jingle_client_; | 128 scoped_refptr<JingleClient> jingle_client_; |
| 129 | 129 |
| 130 // Objects that takes care of sending heartbeats to the chromoting bot. | 130 // Objects that takes care of sending heartbeats to the chromoting bot. |
| 131 scoped_refptr<HeartbeatSender> heartbeat_sender_; | 131 scoped_refptr<HeartbeatSender> heartbeat_sender_; |
| 132 | 132 |
| 133 // A ClientConnection manages the connectino to a remote client. | 133 // A ClientConnection manages the connectino to a remote client. |
| 134 // TODO(hclam): Expand this to a list of clients. | 134 // TODO(hclam): Expand this to a list of clients. |
| 135 scoped_refptr<ClientConnection> client_; | 135 scoped_refptr<ClientConnection> client_; |
| 136 | 136 |
| 137 // Session manager for the host process. | 137 // Session manager for the host process. |
| 138 scoped_refptr<SessionManager> session_; | 138 scoped_refptr<SessionManager> session_; |
| 139 | 139 |
| 140 // Signals the host is ready to be destroyed. | 140 // This task gets executed when this object fails to connect to the |
| 141 base::WaitableEvent* host_done_; | 141 // talk network or Shutdown() is called. |
| 142 scoped_ptr<Task> shutdown_task_; |
| 143 |
| 144 // Tracks the internal state of the host. |
| 145 // This variable is written on the main thread of ChromotingHostContext |
| 146 // and read by jingle thread. |
| 147 State state_; |
| 148 |
| 149 // Lock is to lock the access to |state_|. |
| 150 Lock lock_; |
| 142 | 151 |
| 143 DISALLOW_COPY_AND_ASSIGN(ChromotingHost); | 152 DISALLOW_COPY_AND_ASSIGN(ChromotingHost); |
| 144 }; | 153 }; |
| 145 | 154 |
| 146 } // namespace remoting | 155 } // namespace remoting |
| 147 | 156 |
| 148 #endif // REMOTING_HOST_CHROMOTING_HOST_H_ | 157 #endif // REMOTING_HOST_CHROMOTING_HOST_H_ |
| OLD | NEW |