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

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: Resync with master Created 5 years, 2 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 // The handler is responsible for routing the call through the appropriate
28 // thread.
29 virtual void OnMessage(const BlimpMessage& message) = 0;
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698