summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/channel_dispatcher_base.h
blob: 0a1e29cfbc2833bda73196b33dc63478c3771a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_
#define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_

#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "remoting/protocol/errors.h"

namespace remoting {

class CompoundBuffer;

namespace protocol {

class MessageChannelFactory;
class MessagePipe;

// Base class for channel message dispatchers. It's responsible for
// creating the named channel. Derived dispatchers then dispatch
// incoming messages on this channel as well as send outgoing
// messages.
class ChannelDispatcherBase {
 public:
  class EventHandler {
   public:
    EventHandler() {}
    virtual ~EventHandler() {}

    virtual void OnChannelInitialized(
        ChannelDispatcherBase* channel_dispatcher) = 0;
  };

  // The callback is called when initialization is finished. The
  // parameter is set to true on success.
  typedef base::Callback<void(bool)> InitializedCallback;

  virtual ~ChannelDispatcherBase();

  // Creates and connects the channel in the specified
  // |session|. Caller retains ownership of the Session.
  void Init(MessageChannelFactory* channel_factory,
            EventHandler* event_handler);

  const std::string& channel_name() { return channel_name_; }

  // Returns true if the channel is currently connected.
  bool is_connected() { return message_pipe() != nullptr; }

 protected:
  explicit ChannelDispatcherBase(const char* channel_name);

  MessagePipe* message_pipe() { return message_pipe_.get(); }

  // Child classes must override this method to handle incoming messages.
  virtual void OnIncomingMessage(scoped_ptr<CompoundBuffer> message) = 0;

 private:
  void OnChannelReady(scoped_ptr<MessagePipe> message_pipe);
  void OnPipeError(int error);

  std::string channel_name_;
  MessageChannelFactory* channel_factory_ = nullptr;
  EventHandler* event_handler_ = nullptr;

  scoped_ptr<MessagePipe> message_pipe_;

  DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_