Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Unified Diff: blimp/net/blimp_message_dispatcher.cc

Issue 1324263003: Blimp: create MessageDispatcher class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp-protos
Patch Set: Addressed wez's feedback. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/net/blimp_message_dispatcher.cc
diff --git a/blimp/net/blimp_message_dispatcher.cc b/blimp/net/blimp_message_dispatcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..864284fe7b6abbd08d7ac73d643bdcf82aec83d6
--- /dev/null
+++ b/blimp/net/blimp_message_dispatcher.cc
@@ -0,0 +1,48 @@
+// Copyright 2015 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 "blimp/net/blimp_message_dispatcher.h"
+
+#include <string>
+
+#include "base/strings/stringprintf.h"
+
+namespace blimp {
+namespace {
+
+std::string BlimpMessageToDebugString(const BlimpMessage& message) {
+ return base::StringPrintf("<message type=%d>", message.type());
+}
+
+} // namespace
+
+BlimpMessageDispatcher::BlimpMessageDispatcher() {}
+
+BlimpMessageDispatcher::~BlimpMessageDispatcher() {}
+
+void BlimpMessageDispatcher::AddReceiver(BlimpMessage::Type type,
+ BlimpMessageReceiver* receiver) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(receiver);
+ if (feature_receiver_map_.find(type) == feature_receiver_map_.end()) {
+ feature_receiver_map_.insert(std::make_pair(type, receiver));
+ } else {
+ DCHECK(false) << "Handler already registered for type=" << type << ".";
Wez 2015/10/09 23:15:34 DLOG(FATAL)
Kevin M 2015/10/10 00:42:34 Done.
+ }
+}
+
+bool BlimpMessageDispatcher::OnMessage(const BlimpMessage& message) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ auto receiver_iter = feature_receiver_map_.find(message.type());
+ if (receiver_iter == feature_receiver_map_.end()) {
+ VLOG(0) << "No registered receiver for "
+ << BlimpMessageToDebugString(message) << ".";
Wez 2015/10/09 23:15:34 As previously noted, this indicates a coding error
Kevin M 2015/10/10 00:42:34 Done.
+ return false;
+ }
+
+ return receiver_iter->second->OnMessage(message);
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698