Chromium Code Reviews| Index: remoting/protocol/data_channel_handler.cc |
| diff --git a/remoting/protocol/data_channel_handler.cc b/remoting/protocol/data_channel_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cf525cdf5d72f8bfa834d1113934a09c7122927d |
| --- /dev/null |
| +++ b/remoting/protocol/data_channel_handler.cc |
| @@ -0,0 +1,74 @@ |
| +// 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. |
| + |
| +#include "remoting/protocol/data_channel_handler.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "remoting/base/compound_buffer.h" |
| + |
| +namespace remoting { |
| +namespace protocol { |
| + |
| +DataChannelHandler::DataChannelHandler(const std::string& name, |
| + std::unique_ptr<MessagePipe> pipe) |
| + : name_(name), |
| + pipe_(std::move(pipe)) { |
| + DCHECK(pipe_); |
| + pipe_->Start(this); |
| +} |
| + |
| +DataChannelHandler::~DataChannelHandler() = default; |
|
joedow
2017/05/30 16:24:17
Don't you want to call Close() in the d'tor?
Hzj_jie
2017/05/31 00:11:51
No, instead, destructor is called by Close() funct
Sergey Ulanov
2017/05/31 19:49:02
This is a rather bug-prone approach, we should be
Hzj_jie
2017/06/01 15:43:34
only
The public methods are only public for tests.
|
| + |
| +void DataChannelHandler::Close() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (connected()) { |
| + OnDisconnected(); |
|
joedow
2017/05/30 16:24:17
Should this be called 'OnDisconnecting()'? You ar
Hzj_jie
2017/05/31 00:11:51
Since this is the only callback regarding to the d
joedow
2017/06/01 17:25:25
It is important as they have very different meanin
Hzj_jie
2017/06/01 19:32:08
Done.
|
| + is_connected_ = false; |
| + } |
| + if (!closed()) { |
| + pipe_.reset(); |
| + base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
|
joedow
2017/05/30 16:24:17
This feels weird, I'm not sure why the owner shoul
Hzj_jie
2017/05/31 00:11:51
Because the handler should be deleted once the Mes
Sergey Ulanov
2017/05/31 19:49:02
In order to send outgoing messages some other clas
Hzj_jie
2017/06/01 15:43:34
Yes, Send() is not intentionally used by any other
|
| + } |
| +} |
| + |
| +bool DataChannelHandler::Send(google::protobuf::MessageLite* message, |
| + const base::Closure& done) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (is_connected_) { |
|
joedow
2017/05/30 16:24:17
You use your helper method above (connected()), it
Hzj_jie
2017/05/31 00:11:51
Done.
|
| + pipe_->Send(message, done); |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void DataChannelHandler::OnIncomingMessage( |
| + std::unique_ptr<CompoundBuffer> message) {} |
|
joedow
2017/05/30 16:24:17
Do these need to be implemented? If they are opti
Hzj_jie
2017/05/31 00:11:51
Observer pattern seems not fitting this class: the
|
| + |
| +void DataChannelHandler::OnConnected() {} |
| + |
| +void DataChannelHandler::OnDisconnected() {} |
| + |
| +void DataChannelHandler::OnMessagePipeOpen() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + is_connected_ = true; |
|
joedow
2017/05/30 16:24:17
DCHECK(!is_connected); to protect against multiple
Hzj_jie
2017/05/31 00:11:51
Done.
|
| + OnConnected(); |
| +} |
| + |
| +void DataChannelHandler::OnMessageReceived( |
| + std::unique_ptr<CompoundBuffer> message) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + OnIncomingMessage(std::move(message)); |
| +} |
| + |
| +void DataChannelHandler::OnMessagePipeClosed() { |
| + Close(); |
| +} |
| + |
| +} // namespace protocol |
| +} // namespace remoting |