Upstream version 7.36.149.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 "base/timer/timer.h"
16 #include "remoting/jingle_glue/signal_strategy.h"
17 #include "remoting/proto/internal.pb.h"
18 #include "remoting/protocol/clipboard_filter.h"
19 #include "remoting/protocol/errors.h"
20 #include "remoting/protocol/input_filter.h"
21 #include "remoting/protocol/message_reader.h"
22 #include "remoting/protocol/monitored_video_stub.h"
23 #include "remoting/protocol/session.h"
24 #include "remoting/protocol/session_manager.h"
25
26 namespace pp {
27 class Instance;
28 }  // namespace pp
29
30 namespace remoting {
31
32 class XmppProxy;
33 class VideoPacket;
34
35 namespace protocol {
36
37 class AudioReader;
38 class AudioStub;
39 class Authenticator;
40 class ClientControlDispatcher;
41 class ClientEventDispatcher;
42 class ClientStub;
43 class ClipboardStub;
44 class HostStub;
45 class InputStub;
46 class SessionConfig;
47 class TransportFactory;
48 class VideoReader;
49 class VideoStub;
50
51 class ConnectionToHost : public SignalStrategy::Listener,
52                          public SessionManager::Listener,
53                          public Session::EventHandler,
54                          public base::NonThreadSafe {
55  public:
56   // The UI implementations maintain corresponding definitions of this
57   // enumeration in webapp/client_session.js and
58   // android/java/src/org/chromium/chromoting/jni/JniInterface.java. Be sure to
59   // update these locations if you make any changes to the ordering.
60   enum State {
61     INITIALIZING,
62     CONNECTING,
63     AUTHENTICATED,
64     CONNECTED,
65     FAILED,
66     CLOSED,
67   };
68
69   class HostEventCallback {
70    public:
71     virtual ~HostEventCallback() {}
72
73     // Called when state of the connection changes.
74     virtual void OnConnectionState(State state, ErrorCode error) = 0;
75
76     // Called when ready state of the connection changes. When |ready|
77     // is set to false some data sent by the peers may be
78     // delayed. This is used to indicate in the UI when connection is
79     // temporarily broken.
80     virtual void OnConnectionReady(bool ready) = 0;
81
82     // Called when the route type (direct vs. STUN vs. proxied) changes.
83     virtual void OnRouteChanged(const std::string& channel_name,
84                                 const protocol::TransportRoute& route) = 0;
85   };
86
87   ConnectionToHost(bool allow_nat_traversal);
88   virtual ~ConnectionToHost();
89
90   // |signal_strategy| must outlive connection. |audio_stub| may be
91   // null, in which case audio will not be requested.
92   virtual void Connect(SignalStrategy* signal_strategy,
93                        const std::string& host_jid,
94                        const std::string& host_public_key,
95                        scoped_ptr<TransportFactory> transport_factory,
96                        scoped_ptr<Authenticator> authenticator,
97                        HostEventCallback* event_callback,
98                        ClientStub* client_stub,
99                        ClipboardStub* clipboard_stub,
100                        VideoStub* video_stub,
101                        AudioStub* audio_stub);
102
103   virtual const SessionConfig& config();
104
105   // Stubs for sending data to the host.
106   virtual ClipboardStub* clipboard_stub();
107   virtual HostStub* host_stub();
108   virtual InputStub* input_stub();
109
110   // SignalStrategy::StatusObserver interface.
111   virtual void OnSignalStrategyStateChange(
112       SignalStrategy::State state) OVERRIDE;
113   virtual bool OnSignalStrategyIncomingStanza(
114       const buzz::XmlElement* stanza) OVERRIDE;
115
116   // SessionManager::Listener interface.
117   virtual void OnSessionManagerReady() OVERRIDE;
118   virtual void OnIncomingSession(
119       Session* session,
120       SessionManager::IncomingSessionResponse* response) OVERRIDE;
121
122   // Session::EventHandler interface.
123   virtual void OnSessionStateChange(Session::State state) OVERRIDE;
124   virtual void OnSessionRouteChange(const std::string& channel_name,
125                                     const TransportRoute& route) OVERRIDE;
126
127   // MonitoredVideoStub::EventHandler interface.
128   virtual void OnVideoChannelStatus(bool active);
129
130   // Return the current state of ConnectionToHost.
131   State state() const;
132
133  private:
134   // Callbacks for channel initialization
135   void OnChannelInitialized(bool successful);
136
137   void NotifyIfChannelsReady();
138
139   void CloseOnError(ErrorCode error);
140
141   // Stops writing in the channels.
142   void CloseChannels();
143
144   void SetState(State state, ErrorCode error);
145
146   bool allow_nat_traversal_;
147
148   std::string host_jid_;
149   std::string host_public_key_;
150   scoped_ptr<Authenticator> authenticator_;
151
152   HostEventCallback* event_callback_;
153
154   // Stub for incoming messages.
155   ClientStub* client_stub_;
156   ClipboardStub* clipboard_stub_;
157   AudioStub* audio_stub_;
158
159   SignalStrategy* signal_strategy_;
160   scoped_ptr<SessionManager> session_manager_;
161   scoped_ptr<Session> session_;
162   scoped_ptr<MonitoredVideoStub> monitored_video_stub_;
163
164   scoped_ptr<VideoReader> video_reader_;
165   scoped_ptr<AudioReader> audio_reader_;
166   scoped_ptr<ClientControlDispatcher> control_dispatcher_;
167   scoped_ptr<ClientEventDispatcher> event_dispatcher_;
168   ClipboardFilter clipboard_forwarder_;
169   InputFilter event_forwarder_;
170
171   // Internal state of the connection.
172   State state_;
173   ErrorCode error_;
174
175  private:
176   DISALLOW_COPY_AND_ASSIGN(ConnectionToHost);
177 };
178
179 }  // namespace protocol
180 }  // namespace remoting
181
182 #endif  // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_