Chromium Code Reviews| Index: remoting/protocol/data_channel_handler.h |
| diff --git a/remoting/protocol/data_channel_handler.h b/remoting/protocol/data_channel_handler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7a75a386b7d411ff5d03ccaf4182ecc079df00c3 |
| --- /dev/null |
| +++ b/remoting/protocol/data_channel_handler.h |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2017 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_DATA_CHANNEL_HANDLER_H_ |
| +#define REMOTING_PROTOCOL_DATA_CHANNEL_HANDLER_H_ |
| + |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/sequenced_task_runner_helpers.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "remoting/protocol/message_pipe.h" |
| + |
| +namespace google { |
| +namespace protobuf { |
| +class MessageLite; |
| +} // namespace protobuf |
| +} // namespace google |
| + |
| +namespace remoting { |
| + |
| +class CompoundBuffer; |
| + |
| +namespace protocol { |
| + |
| +// A base class to handle data from a named data channel. This class manages the |
|
joedow
2017/06/06 23:33:25
nit: Replace 'data channel' with 'MessagePipe'. O
Hzj_jie
2017/06/07 17:31:24
Done.
|
| +// lifetime itself: it deletes itself once the data channel is closed or the |
| +// derived instance actively calls Close() function. |
| +class DataChannelHandler : public MessagePipe::EventHandler { |
|
joedow
2017/06/06 23:33:25
Consider renaming to something like MessagePipeHan
Hzj_jie
2017/06/07 17:31:24
I think NamedMessagePipeHandler would be better, a
|
| + protected: |
| + // Creates a DataChannelHandler to handle data from |pipe|. |
| + // This class is not virtual, but it is an no-op instance. So hide the |
|
joedow
2017/06/06 23:33:25
This class isn't a no-op instance though, it does
Hzj_jie
2017/06/07 17:31:24
Done.
|
| + // constructor. |
| + DataChannelHandler(const std::string& name, |
| + std::unique_ptr<MessagePipe> pipe); |
|
joedow
2017/06/06 23:33:25
Would it makes sense to use a zero-param c'tor and
Hzj_jie
2017/06/07 17:31:24
Keeping a copy of |name| in derived classes is not
|
| + |
| + ~DataChannelHandler() override; |
| + |
| + // Closes the channel and eventually destructs this instance. No operations |
| + // should be performed after executing this function. |
| + void Close(); |
| + |
| + const std::string& pipe_name() const { return name_; } |
| + |
| + // Returns true if |this| object has been scheduled to be deleted. This |
| + // function always indicates the lifetime of |this| even the |pipe_| has never |
| + // been opened. |
| + bool closing() const { return pipe_ == nullptr; } |
| + |
| + // Whether |pipe_| has been connected. |
| + bool connected() const { return is_connected_; } |
| + |
| + // Returns false if the message is not sent, usually it happens before the |
| + // |pipe_| is opened. |
| + bool Send(google::protobuf::MessageLite* message, const base::Closure& done); |
| + |
| + // Derived classes can override these functions to receive data from the |
| + // connection or observe the connection state. These functions will not be |
| + // called unless |pipe_| has been opened. |
| + |
|
joedow
2017/06/06 23:33:25
nit: remove newline
Hzj_jie
2017/06/07 17:31:24
Done.
|
| + virtual void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message); |
| + virtual void OnConnected(); |
| + virtual void OnDisconnecting(); |
|
joedow
2017/06/06 23:33:25
Can these be pure virtual? Is the only benefit th
Hzj_jie
2017/06/07 17:31:24
I believe in most the case, only one or two of the
|
| + |
| + private: |
| + friend class base::DeleteHelper<DataChannelHandler>; |
|
joedow
2017/06/06 23:33:25
Why is DeleteHelper needed here? I haven't used i
Hzj_jie
2017/06/07 17:31:24
Because the destructor is protected (to avoid unex
|
| + |
| + // MessagePipe::EventHandler implementation. |
| + void OnMessagePipeOpen() override; |
| + void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override; |
| + void OnMessagePipeClosed() override; |
| + |
| + const std::string name_; |
| + std::unique_ptr<MessagePipe> pipe_; |
| + base::ThreadChecker thread_checker_; |
| + bool is_connected_ = false; |
| +}; |
| + |
| +} // namespace protocol |
| +} // namespace remoting |
| + |
| +#endif // REMOTING_PROTOCOL_DATA_CHANNEL_HANDLER_H_ |