| 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 BlimpMessageToDebugString(const BlimpMessage& message) { |
| 15 return base::StringPrintf("<message type=%d>", message.type()); |
| 16 } |
| 17 |
| 18 } // namespace |
| 19 |
| 20 MessageDispatcher::MessageDispatcher() {} |
| 21 |
| 22 MessageDispatcher::~MessageDispatcher() {} |
| 23 |
| 24 void MessageDispatcher::AddHandler(BlimpMessage::Type type, Handler* handler) { |
| 25 DCHECK(thread_checker_.CalledOnValidThread()); |
| 26 DCHECK(handler); |
| 27 if (feature_handler_map_.find(type) == feature_handler_map_.end()) { |
| 28 feature_handler_map_.insert(std::make_pair(type, handler)); |
| 29 } else { |
| 30 DCHECK(false) << "Handler already registered for type=" << type << "."; |
| 31 } |
| 32 } |
| 33 |
| 34 bool 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 VLOG(0) << "No registered handler for " |
| 40 << BlimpMessageToDebugString(message) << "."; |
| 41 return false; |
| 42 } |
| 43 |
| 44 if (!handler_iter->second->Validate(message)) { |
| 45 VLOG(0) << BlimpMessageToDebugString(message) << " rejected by handler."; |
| 46 return false; |
| 47 } |
| 48 |
| 49 handler_iter->second->OnMessage(message); |
| 50 return true; |
| 51 } |
| 52 |
| 53 } // namespace blimp |
| OLD | NEW |