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

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: 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
« no previous file with comments | « blimp/common/net/message_dispatcher.h ('k') | blimp/common/net/message_dispatcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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
OLDNEW
« no previous file with comments | « blimp/common/net/message_dispatcher.h ('k') | blimp/common/net/message_dispatcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698