Index: remoting/protocol/data_channel_manager.cc |
diff --git a/remoting/protocol/data_channel_manager.cc b/remoting/protocol/data_channel_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0fb3036d9f28afef9d556242eec6b00c1cca33a1 |
--- /dev/null |
+++ b/remoting/protocol/data_channel_manager.cc |
@@ -0,0 +1,57 @@ |
+// 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_manager.h" |
+ |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/location.h" |
+#include "remoting/protocol/data_channel_handler.h" |
+#include "third_party/re2/src/re2/re2.h" |
+ |
+namespace remoting { |
+namespace protocol { |
+ |
+DataChannelManager::DataChannelManager() = default; |
+DataChannelManager::~DataChannelManager() = default; |
+ |
+bool DataChannelManager::RegisterHandlerFactory( |
+ const std::string& regex, |
+ HandlerFactory constructor) { |
+ if (regex.empty() || !constructor) { |
+ return false; |
+ } |
+ |
+ constructors_.push_back(std::make_pair(regex, constructor)); |
+ return true; |
+} |
+ |
+bool DataChannelManager::OnIncomingDataChannel( |
+ const std::string& name, |
+ std::unique_ptr<MessagePipe> pipe) { |
+#if DCHECK_IS_ON() |
+ // Ensures the name matches only one regex. |
+ bool matched = false; |
+ for (auto& constructor : constructors_) { |
+ if (re2::RE2::FullMatch(name, constructor.first)) { |
+ DCHECK(!matched); |
+ matched = true; |
+ } |
+ } |
+ if (!matched) { |
+ return false; |
+ } |
+#endif |
+ for (auto& constructor : constructors_) { |
+ if (re2::RE2::FullMatch(name, constructor.first)) { |
+ constructor.second.Run(name, std::move(pipe)); |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+} // namespace protocol |
+} // namespace remoting |