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/containers/small_map.h" | |
11 #include "base/threading/thread_checker.h" | |
12 #include "blimp/common/blimp_common_export.h" | |
13 #include "blimp/common/proto/blimp_message.pb.h" | |
14 | |
15 namespace blimp { | |
16 | |
17 // Routes BlimpMessages to feature-specific handlers according to the messages' | |
18 // types. | |
19 class BLIMP_COMMON_EXPORT MessageDispatcher { | |
Wez
2015/10/07 00:20:15
Since this dispatches BlimpMessages, let's call it
Kevin M
2015/10/07 00:58:51
Done.
| |
20 public: | |
21 class Handler { | |
22 public: | |
23 // Returns true if |this| is capable of handling |message|. | |
24 virtual bool Validate(const BlimpMessage& message) const = 0; | |
Wez
2015/10/07 00:20:15
As previously noted, what's the value of the Valid
Kevin M
2015/10/07 00:58:51
I guess it doesn't make a lot of sense to validate
Wez
2015/10/09 23:15:34
bool or net::Error? net::Error means you have to p
Kevin M
2015/10/10 00:42:34
Done.
| |
25 | |
26 // Receives |message| for processing. | |
27 // Validate() is guaranteed to be called prior to this call to OnMessage(). | |
28 // The handler is responsible for routing the call through the appropriate | |
29 // thread. | |
30 virtual void OnMessage(const BlimpMessage& message) = 0; | |
31 }; | |
32 | |
33 MessageDispatcher(); | |
34 ~MessageDispatcher(); | |
35 | |
36 // Registers a message dispatcher handler which will process all messages | |
37 // with the type |type|. | |
38 // Only one handler may be added per type. | |
39 // | |
40 // The caller must ensure that the lifetime of |handler| exceeds the lifetime | |
41 // of |this|. | |
42 void AddHandler(BlimpMessage::Type type, Handler* handler); | |
43 | |
44 // Takes a BlimpMessage and dispatches it to the appropriate tab and | |
45 // feature-specific handler, based on the value of |message.type|. | |
46 // | |
47 // Logs an error if there is no Handler registered for | |
48 // |message.type|, or if the Handler failed to validate the message. | |
49 // | |
50 // Returns true if |message| was dispatched successfully. | |
51 // Returns false if no handler exists for |message|, or if the handler | |
52 // rejected |message|. | |
53 bool Dispatch(const BlimpMessage& message) const; | |
Wez
2015/10/07 00:20:15
Why do we want this to have a separate message-han
Kevin M
2015/10/07 00:58:52
Good idea, done.
See comments on BlimpMessageRece
| |
54 | |
55 private: | |
56 base::SmallMap<std::map<BlimpMessage::Type, Handler*>> feature_handler_map_; | |
57 base::ThreadChecker thread_checker_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(MessageDispatcher); | |
60 }; | |
61 | |
62 } // namespace blimp | |
63 | |
64 #endif // BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ | |
OLD | NEW |