- add sources.
[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/jingle_glue/signal_strategy.h"
16 #include "remoting/proto/internal.pb.h"
17 #include "remoting/protocol/clipboard_filter.h"
18 #include "remoting/protocol/errors.h"
19 #include "remoting/protocol/input_filter.h"
20 #include "remoting/protocol/message_reader.h"
21 #include "remoting/protocol/session.h"
22 #include "remoting/protocol/session_manager.h"
23
24 namespace pp {
25 class Instance;
26 }  // namespace pp
27
28 namespace remoting {
29
30 class XmppProxy;
31 class VideoPacket;
32
33 namespace protocol {
34
35 class AudioReader;
36 class AudioStub;
37 class Authenticator;
38 class ClientControlDispatcher;
39 class ClientEventDispatcher;
40 class ClientStub;
41 class ClipboardStub;
42 class HostStub;
43 class InputStub;
44 class SessionConfig;
45 class TransportFactory;
46 class VideoReader;
47 class VideoStub;
48
49 class ConnectionToHost : public SignalStrategy::Listener,
50                          public SessionManager::Listener,
51                          public Session::EventHandler,
52                          public base::NonThreadSafe {
53  public:
54   // The UI implementations maintain corresponding definitions of this
55   // enumeration in webapp/client_session.js and
56   // android/java/res/values/strings.xml. The Android app also includes a
57   // constant in android/java/src/org/chromium/chromoting/jni/JniInterface.java
58   // that tracks the numeric value of the CONNECTED state. Be sure to update
59   // these locations to match this one 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
83   ConnectionToHost(bool allow_nat_traversal);
84   virtual ~ConnectionToHost();
85
86   // |signal_strategy| must outlive connection. |audio_stub| may be
87   // null, in which case audio will not be requested.
88   virtual void Connect(SignalStrategy* signal_strategy,
89                        const std::string& host_jid,
90                        const std::string& host_public_key,
91                        scoped_ptr<TransportFactory> transport_factory,
92                        scoped_ptr<Authenticator> authenticator,
93                        HostEventCallback* event_callback,
94                        ClientStub* client_stub,
95                        ClipboardStub* clipboard_stub,
96                        VideoStub* video_stub,
97                        AudioStub* audio_stub);
98
99   virtual const SessionConfig& config();
100
101   // Stubs for sending data to the host.
102   virtual ClipboardStub* clipboard_stub();
103   virtual HostStub* host_stub();
104   virtual InputStub* input_stub();
105
106   // SignalStrategy::StatusObserver interface.
107   virtual void OnSignalStrategyStateChange(
108       SignalStrategy::State state) OVERRIDE;
109   virtual bool OnSignalStrategyIncomingStanza(
110       const buzz::XmlElement* stanza) OVERRIDE;
111
112   // SessionManager::Listener interface.
113   virtual void OnSessionManagerReady() OVERRIDE;
114   virtual void OnIncomingSession(
115       Session* session,
116       SessionManager::IncomingSessionResponse* response) OVERRIDE;
117
118   // Session::EventHandler interface.
119   virtual void OnSessionStateChange(Session::State state) OVERRIDE;
120   virtual void OnSessionRouteChange(const std::string& channel_name,
121                                     const TransportRoute& route) OVERRIDE;
122   virtual void OnSessionChannelReady(const std::string& channel_name,
123                                      bool ready) OVERRIDE;
124
125   // Return the current state of ConnectionToHost.
126   State state() const;
127
128  private:
129   // Callbacks for channel initialization
130   void OnChannelInitialized(bool successful);
131
132   void NotifyIfChannelsReady();
133
134   void CloseOnError(ErrorCode error);
135
136   // Stops writing in the channels.
137   void CloseChannels();
138
139   void SetState(State state, ErrorCode error);
140
141   bool allow_nat_traversal_;
142
143   std::string host_jid_;
144   std::string host_public_key_;
145   scoped_ptr<Authenticator> authenticator_;
146
147   HostEventCallback* event_callback_;
148
149   // Stub for incoming messages.
150   ClientStub* client_stub_;
151   ClipboardStub* clipboard_stub_;
152   VideoStub* video_stub_;
153   AudioStub* audio_stub_;
154
155   SignalStrategy* signal_strategy_;
156   scoped_ptr<SessionManager> session_manager_;
157   scoped_ptr<Session> session_;
158
159   scoped_ptr<VideoReader> video_reader_;
160   scoped_ptr<AudioReader> audio_reader_;
161   scoped_ptr<ClientControlDispatcher> control_dispatcher_;
162   scoped_ptr<ClientEventDispatcher> event_dispatcher_;
163   ClipboardFilter clipboard_forwarder_;
164   InputFilter event_forwarder_;
165
166   // Internal state of the connection.
167   State state_;
168   ErrorCode error_;
169
170   // List of channels that are not currently ready.
171   std::set<std::string> not_ready_channels_;
172
173  private:
174   DISALLOW_COPY_AND_ASSIGN(ConnectionToHost);
175 };
176
177 }  // namespace protocol
178 }  // namespace remoting
179
180 #endif  // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_