Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / protocol / connection_to_host.h
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_HOST_H_
6 #define REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_
7
8 #include <set>
9 #include <string>
10
11 #include "base/callback_forward.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "remoting/proto/internal.pb.h"
16 #include "remoting/protocol/clipboard_filter.h"
17 #include "remoting/protocol/errors.h"
18 #include "remoting/protocol/input_filter.h"
19 #include "remoting/protocol/message_reader.h"
20 #include "remoting/protocol/monitored_video_stub.h"
21 #include "remoting/protocol/session.h"
22 #include "remoting/protocol/session_config.h"
23 #include "remoting/protocol/session_manager.h"
24 #include "remoting/signaling/signal_strategy.h"
25
26 namespace remoting {
27
28 class XmppProxy;
29 class VideoPacket;
30
31 namespace protocol {
32
33 class AudioReader;
34 class AudioStub;
35 class Authenticator;
36 class ClientControlDispatcher;
37 class ClientEventDispatcher;
38 class ClientStub;
39 class ClipboardStub;
40 class HostStub;
41 class InputStub;
42 class SessionConfig;
43 class TransportFactory;
44 class ClientVideoDispatcher;
45 class VideoStub;
46
47 class ConnectionToHost : public SignalStrategy::Listener,
48                          public SessionManager::Listener,
49                          public Session::EventHandler,
50                          public base::NonThreadSafe {
51  public:
52   // The UI implementations maintain corresponding definitions of this
53   // enumeration in webapp/client_session.js and
54   // android/java/src/org/chromium/chromoting/jni/JniInterface.java. Be sure to
55   // update these locations if you make any changes to the ordering.
56   enum State {
57     INITIALIZING,
58     CONNECTING,
59     AUTHENTICATED,
60     CONNECTED,
61     FAILED,
62     CLOSED,
63   };
64
65   class HostEventCallback {
66    public:
67     virtual ~HostEventCallback() {}
68
69     // Called when state of the connection changes.
70     virtual void OnConnectionState(State state, ErrorCode error) = 0;
71
72     // Called when ready state of the connection changes. When |ready|
73     // is set to false some data sent by the peers may be
74     // delayed. This is used to indicate in the UI when connection is
75     // temporarily broken.
76     virtual void OnConnectionReady(bool ready) = 0;
77
78     // Called when the route type (direct vs. STUN vs. proxied) changes.
79     virtual void OnRouteChanged(const std::string& channel_name,
80                                 const protocol::TransportRoute& route) = 0;
81   };
82
83   ConnectionToHost();
84   ~ConnectionToHost() override;
85
86   // Allows to set a custom protocol configuration (e.g. for tests). Cannot be
87   // called after Connect().
88   void set_candidate_config(scoped_ptr<CandidateSessionConfig> config);
89
90   // Set the stubs which will handle messages from the host.
91   // The caller must ensure that stubs out-live the connection.
92   // Unless otherwise specified, all stubs must be set before Connect()
93   // is called.
94   void set_client_stub(ClientStub* client_stub);
95   void set_clipboard_stub(ClipboardStub* clipboard_stub);
96   void set_video_stub(VideoStub* video_stub);
97   // If no audio stub is specified then audio will not be requested.
98   void set_audio_stub(AudioStub* audio_stub);
99
100   // Initiates a connection to the host specified by |host_jid|.
101   // |signal_strategy| is used to signal to the host, and must outlive the
102   // connection. Data channels will be negotiated over |transport_factory|.
103   // |authenticator| will be used to authenticate the session and data channels.
104   // |event_callback| will be notified of changes in the state of the connection
105   // and must outlive the ConnectionToHost.
106   // Caller must set stubs (see below) before calling Connect.
107   virtual void Connect(SignalStrategy* signal_strategy,
108                        scoped_ptr<TransportFactory> transport_factory,
109                        scoped_ptr<Authenticator> authenticator,
110                        const std::string& host_jid,
111                        HostEventCallback* event_callback);
112
113   // Returns the session configuration that was negotiated with the host.
114   virtual const SessionConfig& config();
115
116   // Stubs for sending data to the host.
117   virtual ClipboardStub* clipboard_forwarder();
118   virtual HostStub* host_stub();
119   virtual InputStub* input_stub();
120
121   // SignalStrategy::StatusObserver interface.
122   void OnSignalStrategyStateChange(SignalStrategy::State state) override;
123   bool OnSignalStrategyIncomingStanza(const buzz::XmlElement* stanza) override;
124
125   // SessionManager::Listener interface.
126   void OnSessionManagerReady() override;
127   void OnIncomingSession(
128       Session* session,
129       SessionManager::IncomingSessionResponse* response) override;
130
131   // Session::EventHandler interface.
132   void OnSessionStateChange(Session::State state) override;
133   void OnSessionRouteChange(const std::string& channel_name,
134                             const TransportRoute& route) override;
135
136   // MonitoredVideoStub::EventHandler interface.
137   virtual void OnVideoChannelStatus(bool active);
138
139   // Return the current state of ConnectionToHost.
140   State state() const;
141
142  private:
143   // Callbacks for channel initialization
144   void OnChannelInitialized(bool successful);
145
146   void NotifyIfChannelsReady();
147
148   void CloseOnError(ErrorCode error);
149
150   // Stops writing in the channels.
151   void CloseChannels();
152
153   void SetState(State state, ErrorCode error);
154
155   std::string host_jid_;
156   std::string host_public_key_;
157   scoped_ptr<Authenticator> authenticator_;
158
159   HostEventCallback* event_callback_;
160
161   scoped_ptr<CandidateSessionConfig> candidate_config_;
162
163   // Stub for incoming messages.
164   ClientStub* client_stub_;
165   ClipboardStub* clipboard_stub_;
166   AudioStub* audio_stub_;
167
168   SignalStrategy* signal_strategy_;
169   scoped_ptr<SessionManager> session_manager_;
170   scoped_ptr<Session> session_;
171   scoped_ptr<MonitoredVideoStub> monitored_video_stub_;
172
173   scoped_ptr<ClientVideoDispatcher> video_dispatcher_;
174   scoped_ptr<AudioReader> audio_reader_;
175   scoped_ptr<ClientControlDispatcher> control_dispatcher_;
176   scoped_ptr<ClientEventDispatcher> event_dispatcher_;
177   ClipboardFilter clipboard_forwarder_;
178   InputFilter event_forwarder_;
179
180   // Internal state of the connection.
181   State state_;
182   ErrorCode error_;
183
184  private:
185   DISALLOW_COPY_AND_ASSIGN(ConnectionToHost);
186 };
187
188 }  // namespace protocol
189 }  // namespace remoting
190
191 #endif  // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_