Chromium Code Reviews| 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 { |
|
Wez
2015/10/02 01:10:29
nit: BlimpMessageDispatcher, for consistency w/ Bl
|
| + public: |
| + class Handler { |
| + public: |
| + // Returns true if |this| is capable of handling |message|. |
| + virtual bool Validate(const BlimpMessage& message) const = 0; |
|
Wez
2015/10/02 01:10:29
What's the value in keeping this and OnMessage sep
|
| + |
| + // 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; |
|
Wez
2015/10/02 01:10:29
nit: OnBlimpMessage?
|
| + }; |
| + |
| + 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_ |