MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / remoting / protocol / channel_dispatcher_base.h
blob3a2fe076e3e4984bfb2ebc73fb104301fa9183fe
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_CHANNEL_DISPATCHER_BASE_H_
6 #define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "remoting/base/buffered_socket_writer.h"
14 #include "remoting/protocol/errors.h"
15 #include "remoting/protocol/message_reader.h"
17 namespace remoting {
18 namespace protocol {
20 struct ChannelConfig;
21 class StreamChannelFactory;
22 class Session;
24 // Base class for channel message dispatchers. It's responsible for
25 // creating the named channel. Derived dispatchers then dispatch
26 // incoming messages on this channel as well as send outgoing
27 // messages.
28 class ChannelDispatcherBase {
29 public:
30 class EventHandler {
31 public:
32 EventHandler() {}
33 virtual ~EventHandler() {}
35 virtual void OnChannelInitialized(
36 ChannelDispatcherBase* channel_dispatcher) = 0;
37 virtual void OnChannelError(ChannelDispatcherBase* channel_dispatcher,
38 ErrorCode error) = 0;
41 // The callback is called when initialization is finished. The
42 // parameter is set to true on success.
43 typedef base::Callback<void(bool)> InitializedCallback;
45 virtual ~ChannelDispatcherBase();
47 // Creates and connects the channel in the specified
48 // |session|. Caller retains ownership of the Session.
49 void Init(Session* session,
50 const ChannelConfig& config,
51 EventHandler* event_handler);
53 const std::string& channel_name() { return channel_name_; }
55 // Returns true if the channel is currently connected.
56 bool is_connected() { return channel_ != nullptr; }
58 protected:
59 explicit ChannelDispatcherBase(const char* channel_name);
61 BufferedSocketWriter* writer() { return &writer_; }
62 MessageReader* reader() { return &reader_; }
64 private:
65 void OnChannelReady(scoped_ptr<P2PStreamSocket> socket);
66 void OnReadWriteFailed(int error);
68 std::string channel_name_;
69 StreamChannelFactory* channel_factory_;
70 EventHandler* event_handler_;
71 scoped_ptr<P2PStreamSocket> channel_;
73 BufferedSocketWriter writer_;
74 MessageReader reader_;
76 DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase);
79 } // namespace protocol
80 } // namespace remoting
82 #endif // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_