Add MetricsService::WasLastShutdownClean()
[chromium-blink-merge.git] / remoting / protocol / connection_to_client.h
blob43c0b1afa0ccc793ff5a98261366188a84054200
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.
5 #ifndef REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
6 #define REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
8 #include <deque>
9 #include <string>
10 #include <vector>
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"
18 namespace remoting {
19 namespace protocol {
21 class ClientStub;
22 class ClipboardStub;
23 class HostControlDispatcher;
24 class HostEventDispatcher;
25 class HostStub;
26 class HostVideoDispatcher;
27 class InputStub;
28 class VideoFeedbackStub;
29 class VideoStub;
31 // This class represents a remote viewer connection to the chromoting
32 // host. It sets up all protocol channels and connects them to the
33 // stubs.
34 class ConnectionToClient : public base::NonThreadSafe,
35 public Session::EventHandler,
36 public ChannelDispatcherBase::EventHandler {
37 public:
38 class EventHandler {
39 public:
40 // Called when the network connection is authenticating
41 virtual void OnConnectionAuthenticating(ConnectionToClient* connection) = 0;
43 // Called when the network connection is authenticated.
44 virtual void OnConnectionAuthenticated(ConnectionToClient* connection) = 0;
46 // Called when the network connection is authenticated and all
47 // channels are connected.
48 virtual void OnConnectionChannelsConnected(
49 ConnectionToClient* connection) = 0;
51 // Called when the network connection is closed or failed.
52 virtual void OnConnectionClosed(ConnectionToClient* connection,
53 ErrorCode error) = 0;
55 // Called when sequence number is updated.
56 virtual void OnEventTimestamp(ConnectionToClient* connection,
57 int64 timestamp) = 0;
59 // Called on notification of a route change event, which happens when a
60 // channel is connected.
61 virtual void OnRouteChange(ConnectionToClient* connection,
62 const std::string& channel_name,
63 const TransportRoute& route) = 0;
65 protected:
66 virtual ~EventHandler() {}
69 // Constructs a ConnectionToClient object for the |session|. Takes
70 // ownership of |session|.
71 explicit ConnectionToClient(Session* session);
72 ~ConnectionToClient() override;
74 // Set |event_handler| for connection events. Must be called once when this
75 // object is created.
76 void SetEventHandler(EventHandler* event_handler);
78 // Returns the connection in use.
79 virtual Session* session();
81 // Disconnect the client connection.
82 virtual void Disconnect();
84 // Callback for HostEventDispatcher to be called with a timestamp for each
85 // received event.
86 virtual void OnEventTimestamp(int64 timestamp);
88 // Get the stubs used by the host to transmit messages to the client.
89 // The stubs must not be accessed before OnConnectionAuthenticated(), or
90 // after OnConnectionClosed().
91 // Note that the audio stub will be nullptr if audio is not enabled.
92 virtual VideoStub* video_stub();
93 virtual AudioStub* audio_stub();
94 virtual ClientStub* client_stub();
96 // Set the stubs which will handle messages we receive from the client. These
97 // must be called in EventHandler::OnConnectionAuthenticated().
98 virtual void set_clipboard_stub(ClipboardStub* clipboard_stub);
99 virtual void set_host_stub(HostStub* host_stub);
100 virtual void set_input_stub(InputStub* input_stub);
102 // Sets video feedback stub. Can be called at any time after connection is
103 // authenticated.
104 virtual void set_video_feedback_stub(VideoFeedbackStub* video_feedback_stub);
106 // Session::EventHandler interface.
107 void OnSessionStateChange(Session::State state) override;
108 void OnSessionRouteChange(const std::string& channel_name,
109 const TransportRoute& route) override;
111 // ChannelDispatcherBase::EventHandler interface.
112 void OnChannelInitialized(ChannelDispatcherBase* channel_dispatcher) override;
113 void OnChannelError(ChannelDispatcherBase* channel_dispatcher,
114 ErrorCode error) override;
116 private:
117 void NotifyIfChannelsReady();
119 void Close(ErrorCode error);
121 // Stops writing in the channels.
122 void CloseChannels();
124 // Event handler for handling events sent from this object.
125 EventHandler* handler_;
127 // The libjingle channel used to send and receive data from the remote client.
128 scoped_ptr<Session> session_;
130 scoped_ptr<HostControlDispatcher> control_dispatcher_;
131 scoped_ptr<HostEventDispatcher> event_dispatcher_;
132 scoped_ptr<HostVideoDispatcher> video_dispatcher_;
133 scoped_ptr<AudioWriter> audio_writer_;
135 DISALLOW_COPY_AND_ASSIGN(ConnectionToClient);
138 } // namespace protocol
139 } // namespace remoting
141 #endif // REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_