OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ | |
6 #define BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/threading/thread_checker.h" | |
11 #include "blimp/common/blimp_common_export.h" | |
12 #include "blimp/common/proto/blimp_message.pb.h" | |
13 | |
14 namespace blimp { | |
15 | |
16 // Routes BlimpMessages to feature-specific handlers according to the messages' | |
17 // types. | |
18 class BLIMP_COMMON_EXPORT MessageDispatcher { | |
Wez
2015/10/02 01:10:29
nit: BlimpMessageDispatcher, for consistency w/ Bl
| |
19 public: | |
20 class Handler { | |
21 public: | |
22 // Returns true if |this| is capable of handling |message|. | |
23 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
| |
24 | |
25 // Receives |message| for processing. | |
26 // Validate() is guaranteed to be called prior to this call to OnMessage(). | |
27 // The handler is responsible for routing the call through the appropriate | |
28 // thread. | |
29 virtual void OnMessage(const BlimpMessage& message) = 0; | |
Wez
2015/10/02 01:10:29
nit: OnBlimpMessage?
| |
30 }; | |
31 | |
32 MessageDispatcher(); | |
33 ~MessageDispatcher(); | |
34 | |
35 // Registers a message dispatcher handler which will process all messages | |
36 // with the type |type|. | |
37 // Only one handler may be added per type. | |
38 // | |
39 // The caller must ensure that the lifetime of |handler| exceeds the lifetime | |
40 // of |this|. | |
41 void AddHandler(BlimpMessage::Type type, Handler* handler); | |
42 | |
43 // Takes a BlimpMessage and dispatches it to the appropriate tab and | |
44 // feature-specific handler, based on the value of |message.type|. | |
45 // | |
46 // Logs an error if there is no Handler registered for | |
47 // |message.type|, or if the Handler failed to validate the message. | |
48 void Dispatch(const BlimpMessage& message) const; | |
49 | |
50 private: | |
51 std::map<BlimpMessage::Type, Handler*> feature_handler_map_; | |
52 base::ThreadChecker thread_checker_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(MessageDispatcher); | |
55 }; | |
56 | |
57 } // namespace blimp | |
58 | |
59 #endif // BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ | |
OLD | NEW |