OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/websocket_dispatcher.h" | 5 #include "content/child/websocket_dispatcher.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "content/child/websocket_bridge.h" | 11 #include "content/child/websocket_bridge.h" |
12 #include "content/common/websocket_messages.h" | 12 #include "content/common/websocket_messages.h" |
13 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 WebSocketDispatcher::WebSocketDispatcher() : channel_id_max_(0) {} | 18 WebSocketDispatcher::WebSocketDispatcher() |
| 19 : channel_id_max_(0), |
| 20 weak_ptr_factory_(this) {} |
19 | 21 |
20 WebSocketDispatcher::~WebSocketDispatcher() {} | 22 WebSocketDispatcher::~WebSocketDispatcher() {} |
21 | 23 |
| 24 bool WebSocketDispatcher::CanHandleMessage(const IPC::Message& msg) { |
| 25 switch (msg.type()) { |
| 26 case WebSocketMsg_AddChannelResponse::ID: |
| 27 case WebSocketMsg_NotifyStartOpeningHandshake::ID: |
| 28 case WebSocketMsg_NotifyFinishOpeningHandshake::ID: |
| 29 case WebSocketMsg_NotifyFailure::ID: |
| 30 case WebSocketMsg_SendFrame::ID: |
| 31 case WebSocketMsg_FlowControl::ID: |
| 32 case WebSocketMsg_DropChannel::ID: |
| 33 case WebSocketMsg_NotifyClosing::ID: |
| 34 return true; |
| 35 default: |
| 36 return false; |
| 37 } |
| 38 } |
| 39 |
22 int WebSocketDispatcher::AddBridge(WebSocketBridge* bridge) { | 40 int WebSocketDispatcher::AddBridge(WebSocketBridge* bridge) { |
23 ++channel_id_max_; | 41 ++channel_id_max_; |
24 bridges_.insert(std::make_pair(channel_id_max_, bridge)); | 42 bridges_.insert(std::make_pair(channel_id_max_, bridge)); |
25 return channel_id_max_; | 43 return channel_id_max_; |
26 } | 44 } |
27 | 45 |
28 void WebSocketDispatcher::RemoveBridge(int channel_id) { | 46 void WebSocketDispatcher::RemoveBridge(int channel_id) { |
29 std::map<int, WebSocketBridge*>::iterator iter = bridges_.find(channel_id); | 47 std::map<int, WebSocketBridge*>::iterator iter = bridges_.find(channel_id); |
30 if (iter == bridges_.end()) { | 48 if (iter == bridges_.end()) { |
31 DVLOG(1) << "Remove a non-existent bridge(" << channel_id << ")"; | 49 DVLOG(1) << "Remove a non-existent bridge(" << channel_id << ")"; |
32 return; | 50 return; |
33 } | 51 } |
34 bridges_.erase(iter); | 52 bridges_.erase(iter); |
35 } | 53 } |
36 | 54 |
37 bool WebSocketDispatcher::OnMessageReceived(const IPC::Message& msg) { | 55 bool WebSocketDispatcher::OnMessageReceived(const IPC::Message& msg) { |
38 switch (msg.type()) { | 56 if (!CanHandleMessage(msg)) |
39 case WebSocketMsg_AddChannelResponse::ID: | 57 return false; |
40 case WebSocketMsg_NotifyStartOpeningHandshake::ID: | |
41 case WebSocketMsg_NotifyFinishOpeningHandshake::ID: | |
42 case WebSocketMsg_NotifyFailure::ID: | |
43 case WebSocketMsg_SendFrame::ID: | |
44 case WebSocketMsg_FlowControl::ID: | |
45 case WebSocketMsg_DropChannel::ID: | |
46 case WebSocketMsg_NotifyClosing::ID: | |
47 break; | |
48 default: | |
49 return false; | |
50 } | |
51 | |
52 WebSocketBridge* bridge = GetBridge(msg.routing_id(), msg.type()); | 58 WebSocketBridge* bridge = GetBridge(msg.routing_id(), msg.type()); |
53 if (!bridge) | 59 if (!bridge) |
54 return true; | 60 return true; |
55 return bridge->OnMessageReceived(msg); | 61 return bridge->OnMessageReceived(msg); |
56 } | 62 } |
57 | 63 |
58 WebSocketBridge* WebSocketDispatcher::GetBridge(int channel_id, uint32_t type) { | 64 WebSocketBridge* WebSocketDispatcher::GetBridge(int channel_id, uint32_t type) { |
59 std::map<int, WebSocketBridge*>::iterator iter = bridges_.find(channel_id); | 65 std::map<int, WebSocketBridge*>::iterator iter = bridges_.find(channel_id); |
60 if (iter == bridges_.end()) { | 66 if (iter == bridges_.end()) { |
61 DVLOG(1) << "No bridge for channel_id=" << channel_id | 67 DVLOG(1) << "No bridge for channel_id=" << channel_id |
62 << ", type=" << type; | 68 << ", type=" << type; |
63 return NULL; | 69 return NULL; |
64 } | 70 } |
65 return iter->second; | 71 return iter->second; |
66 } | 72 } |
67 | 73 |
68 } // namespace content | 74 } // namespace content |
OLD | NEW |