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

Unified Diff: blimp/common/net/message_dispatcher.cc

Issue 1324263003: Blimp: create MessageDispatcher class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp-protos
Patch Set: Add unit tests; refactor BUILD.gns Created 5 years, 3 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
« no previous file with comments | « blimp/common/net/message_dispatcher.h ('k') | blimp/common/net/message_dispatcher_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: blimp/common/net/message_dispatcher.cc
diff --git a/blimp/common/net/message_dispatcher.cc b/blimp/common/net/message_dispatcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5a90d510e6a34051850415cb9b932fe1959983c1
--- /dev/null
+++ b/blimp/common/net/message_dispatcher.cc
@@ -0,0 +1,53 @@
+// 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/common/net/message_dispatcher.h"
+
+#include <string>
+
+#include "base/strings/stringprintf.h"
+
+namespace blimp {
+namespace {
+
+std::string BlimpMessageToString(const BlimpMessage& message) {
nyquist 2015/09/08 17:30:12 Nit: Could we make it clear that this is meant to
Kevin M 2015/09/08 20:50:19 Done.
+ return base::StringPrintf("<message type=%d, tab=%d>", message.type(),
+ message.target_tab_id());
+}
+
+} // namespace
+
+MessageDispatcher::MessageDispatcher() {}
+
+MessageDispatcher::~MessageDispatcher() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+void MessageDispatcher::AddHandler(BlimpMessage::Type type, Handler* handler) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (feature_handler_map_.find(type) == feature_handler_map_.end()) {
nyquist 2015/09/08 17:30:12 Nit: Should we log an error if there already exist
Kevin M 2015/09/08 20:50:19 Done.
+ feature_handler_map_.insert(std::make_pair(type, handler));
+ }
+}
+
+void MessageDispatcher::Dispatch(const BlimpMessage& message) const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ auto handler_iter = feature_handler_map_.find(message.type());
+ if (handler_iter == feature_handler_map_.end()) {
+ LOG(ERROR) << "No registered handler for " << BlimpMessageToString(message)
+ << ".";
+ return;
+ }
+
+ DCHECK(handler_iter->second);
+ if (!handler_iter->second->Validate(message)) {
+ LOG(WARNING) << BlimpMessageToString(message) << " rejected by handler.";
+ return;
+ }
+
+ handler_iter->second->OnMessage(message);
+}
+
+} // namespace blimp
« no previous file with comments | « blimp/common/net/message_dispatcher.h ('k') | blimp/common/net/message_dispatcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698