• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
6 #define REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
7 
8 #include <deque>
9 #include <string>
10 #include <vector>
11 
12 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "remoting/protocol/audio_writer.h"
16 #include "remoting/protocol/session.h"
17 #include "remoting/protocol/video_writer.h"
18 
19 namespace remoting {
20 namespace protocol {
21 
22 class ClientStub;
23 class ClipboardStub;
24 class HostStub;
25 class InputStub;
26 class HostControlDispatcher;
27 class HostEventDispatcher;
28 
29 // This class represents a remote viewer connection to the chromoting
30 // host. It sets up all protocol channels and connects them to the
31 // stubs.
32 class ConnectionToClient : public base::NonThreadSafe,
33                            public Session::EventHandler {
34  public:
35   class EventHandler {
36    public:
37     // Called when the network connection is authenticating
38     virtual void OnConnectionAuthenticating(ConnectionToClient* connection) = 0;
39 
40     // Called when the network connection is authenticated.
41     virtual void OnConnectionAuthenticated(ConnectionToClient* connection) = 0;
42 
43     // Called when the network connection is authenticated and all
44     // channels are connected.
45     virtual void OnConnectionChannelsConnected(
46         ConnectionToClient* connection) = 0;
47 
48     // Called when the network connection is closed or failed.
49     virtual void OnConnectionClosed(ConnectionToClient* connection,
50                                     ErrorCode error) = 0;
51 
52     // Called when sequence number is updated.
53     virtual void OnSequenceNumberUpdated(ConnectionToClient* connection,
54                                          int64 sequence_number) = 0;
55 
56     // Called on notification of a route change event, which happens when a
57     // channel is connected.
58     virtual void OnRouteChange(ConnectionToClient* connection,
59                                const std::string& channel_name,
60                                const TransportRoute& route) = 0;
61 
62    protected:
~EventHandler()63     virtual ~EventHandler() {}
64   };
65 
66   // Constructs a ConnectionToClient object for the |session|. Takes
67   // ownership of |session|.
68   explicit ConnectionToClient(Session* session);
69   virtual ~ConnectionToClient();
70 
71   // Set |event_handler| for connection events. Must be called once when this
72   // object is created.
73   void SetEventHandler(EventHandler* event_handler);
74 
75   // Returns the connection in use.
76   virtual Session* session();
77 
78   // Disconnect the client connection.
79   virtual void Disconnect();
80 
81   // Update the sequence number when received from the client. EventHandler
82   // will be called.
83   virtual void UpdateSequenceNumber(int64 sequence_number);
84 
85   // Get the stubs used by the host to transmit messages to the client.
86   // The stubs must not be accessed before OnConnectionAuthenticated(), or
87   // after OnConnectionClosed().
88   // Note that the audio stub will be NULL if audio is not enabled.
89   virtual VideoStub* video_stub();
90   virtual AudioStub* audio_stub();
91   virtual ClientStub* client_stub();
92 
93   // Set/get the stubs which will handle messages we receive from the client.
94   // All stubs MUST be set before the session's channels become connected.
95   virtual void set_clipboard_stub(ClipboardStub* clipboard_stub);
96   virtual ClipboardStub* clipboard_stub();
97   virtual void set_host_stub(HostStub* host_stub);
98   virtual HostStub* host_stub();
99   virtual void set_input_stub(InputStub* input_stub);
100   virtual InputStub* input_stub();
101 
102   // Session::EventHandler interface.
103   virtual void OnSessionStateChange(Session::State state) OVERRIDE;
104   virtual void OnSessionRouteChange(const std::string& channel_name,
105                                     const TransportRoute& route) OVERRIDE;
106 
107  private:
108   // Callback for channel initialization.
109   void OnChannelInitialized(bool successful);
110 
111   void NotifyIfChannelsReady();
112 
113   void Close(ErrorCode error);
114 
115   // Stops writing in the channels.
116   void CloseChannels();
117 
118   // Event handler for handling events sent from this object.
119   EventHandler* handler_;
120 
121   // Stubs that are called for incoming messages.
122   ClipboardStub* clipboard_stub_;
123   HostStub* host_stub_;
124   InputStub* input_stub_;
125 
126   // The libjingle channel used to send and receive data from the remote client.
127   scoped_ptr<Session> session_;
128 
129   scoped_ptr<HostControlDispatcher> control_dispatcher_;
130   scoped_ptr<HostEventDispatcher> event_dispatcher_;
131   scoped_ptr<VideoWriter> video_writer_;
132   scoped_ptr<AudioWriter> audio_writer_;
133 
134   DISALLOW_COPY_AND_ASSIGN(ConnectionToClient);
135 };
136 
137 }  // namespace protocol
138 }  // namespace remoting
139 
140 #endif  // REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
141