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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
(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 {
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;
24
25 // Receives |message| for processing.
26 // Validate() is guaranteed to be called prior to this call to OnMessage().
27 virtual void OnMessage(const BlimpMessage& message) = 0;
28 };
29
30 MessageDispatcher();
31 ~MessageDispatcher();
32
33 // Registers a message dispatcher handler which will process all messages
34 // with the type |type|. Only one handler may be added per type.
35 //
36 // The caller must ensure that the lifetime of |handler| exceeds the lifetime
37 // of |this|.
38 void AddHandler(BlimpMessage::Type type, Handler* handler);
39
40 // Takes a BlimpMessage and dispatches it to the appropriate feature-specific
41 // handler, based on the value of |message.type|.
42 //
43 // Logs an error if there is no Handler registered for |message.type|, or if
44 // the Handler failed to validate the message.
45 void Dispatch(const BlimpMessage& message) const;
46
47 private:
48 std::map<BlimpMessage::Type, Handler*> feature_handler_map_;
49 base::ThreadChecker thread_checker_;
50
51 DISALLOW_COPY_AND_ASSIGN(MessageDispatcher);
52 };
53
54 } // namespace blimp
55
56 #endif // BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698