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

Side by Side Diff: blimp/common/net/message_dispatcher.cc

Issue 1324263003: Blimp: create MessageDispatcher class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp-protos
Patch Set: Remove testonly annotation 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 #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, 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()) {
30 feature_handler_map_.insert(std::make_pair(type, handler));
31 } else {
32 LOG(ERROR) << "Handler already registered for type " << type << ".";
33 }
34 }
35
36 void MessageDispatcher::Dispatch(const BlimpMessage& message) const {
37 DCHECK(thread_checker_.CalledOnValidThread());
38
39 auto handler_iter = feature_handler_map_.find(message.type());
40 if (handler_iter == feature_handler_map_.end()) {
41 LOG(ERROR) << "No registered handler for "
42 << BlimpMessageToDebugString(message) << ".";
43 return;
44 }
45
46 DCHECK(handler_iter->second);
47 if (!handler_iter->second->Validate(message)) {
48 LOG(WARNING) << BlimpMessageToDebugString(message)
49 << " rejected by handler.";
50 return;
51 }
52
53 handler_iter->second->OnMessage(message);
54 }
55
56 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698