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 #include "blimp/common/net/message_dispatcher.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/strings/stringprintf.h" | |
10 | |
11 namespace blimp { | |
12 namespace { | |
13 | |
14 std::string BlimpMessageToString(const BlimpMessage& message) { | |
nyquist
2015/09/08 17:30:12
Nit: Could we make it clear that this is meant to
Kevin M
2015/09/08 20:50:19
Done.
| |
15 return base::StringPrintf("<message type=%d, tab=%d>", message.type(), | |
16 message.target_tab_id()); | |
17 } | |
18 | |
19 } // namespace | |
20 | |
21 MessageDispatcher::MessageDispatcher() {} | |
22 | |
23 MessageDispatcher::~MessageDispatcher() { | |
24 DCHECK(thread_checker_.CalledOnValidThread()); | |
25 } | |
26 | |
27 void MessageDispatcher::AddHandler(BlimpMessage::Type type, Handler* handler) { | |
28 DCHECK(thread_checker_.CalledOnValidThread()); | |
29 if (feature_handler_map_.find(type) == feature_handler_map_.end()) { | |
nyquist
2015/09/08 17:30:12
Nit: Should we log an error if there already exist
Kevin M
2015/09/08 20:50:19
Done.
| |
30 feature_handler_map_.insert(std::make_pair(type, handler)); | |
31 } | |
32 } | |
33 | |
34 void MessageDispatcher::Dispatch(const BlimpMessage& message) const { | |
35 DCHECK(thread_checker_.CalledOnValidThread()); | |
36 | |
37 auto handler_iter = feature_handler_map_.find(message.type()); | |
38 if (handler_iter == feature_handler_map_.end()) { | |
39 LOG(ERROR) << "No registered handler for " << BlimpMessageToString(message) | |
40 << "."; | |
41 return; | |
42 } | |
43 | |
44 DCHECK(handler_iter->second); | |
45 if (!handler_iter->second->Validate(message)) { | |
46 LOG(WARNING) << BlimpMessageToString(message) << " rejected by handler."; | |
47 return; | |
48 } | |
49 | |
50 handler_iter->second->OnMessage(message); | |
51 } | |
52 | |
53 } // namespace blimp | |
OLD | NEW |