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

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

Issue 1324263003: Blimp: create MessageDispatcher class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp-protos
Patch Set: Resync with master 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
Index: blimp/common/net/message_dispatcher.h
diff --git a/blimp/common/net/message_dispatcher.h b/blimp/common/net/message_dispatcher.h
new file mode 100644
index 0000000000000000000000000000000000000000..167e9d68ed2a8d8aa48ddd38a4e5be7e91abf0c0
--- /dev/null
+++ b/blimp/common/net/message_dispatcher.h
@@ -0,0 +1,59 @@
+// 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.
+
+#ifndef BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_
+#define BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_
+
+#include <map>
+
+#include "base/threading/thread_checker.h"
+#include "blimp/common/blimp_common_export.h"
+#include "blimp/common/proto/blimp_message.pb.h"
+
+namespace blimp {
+
+// Routes BlimpMessages to feature-specific handlers according to the messages'
+// types.
+class BLIMP_COMMON_EXPORT MessageDispatcher {
+ public:
+ class Handler {
+ public:
+ // Returns true if |this| is capable of handling |message|.
+ virtual bool Validate(const BlimpMessage& message) const = 0;
+
+ // Receives |message| for processing.
+ // Validate() is guaranteed to be called prior to this call to OnMessage().
+ // The handler is responsible for routing the call through the appropriate
+ // thread.
+ virtual void OnMessage(const BlimpMessage& message) = 0;
+ };
+
+ MessageDispatcher();
+ ~MessageDispatcher();
+
+ // Registers a message dispatcher handler which will process all messages
+ // with the type |type|.
+ // Only one handler may be added per type.
+ //
+ // The caller must ensure that the lifetime of |handler| exceeds the lifetime
+ // of |this|.
+ void AddHandler(BlimpMessage::Type type, Handler* handler);
+
+ // Takes a BlimpMessage and dispatches it to the appropriate tab and
+ // feature-specific handler, based on the value of |message.type|.
+ //
+ // Logs an error if there is no Handler registered for
+ // |message.type|, or if the Handler failed to validate the message.
+ void Dispatch(const BlimpMessage& message) const;
+
+ private:
+ std::map<BlimpMessage::Type, Handler*> feature_handler_map_;
+ base::ThreadChecker thread_checker_;
+
+ DISALLOW_COPY_AND_ASSIGN(MessageDispatcher);
+};
+
+} // namespace blimp
+
+#endif // BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_

Powered by Google App Engine
This is Rietveld 408576698