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 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/compiler_specific.h" // for WARN_UNUSED_RESULT | |
jochen (gone - plz use gerrit)
2013/10/23 14:01:28
nit. comment not required
Adam Rice
2013/10/24 02:22:23
Removed.
| |
13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
14 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
15 #include "content/common/websocket.h" | 16 #include "content/common/websocket.h" |
16 #include "content/public/browser/browser_message_filter.h" | 17 #include "content/public/browser/browser_message_filter.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 class URLRequestContext; | 20 class URLRequestContext; |
20 } // namespace net | 21 } // namespace net |
21 | 22 |
22 namespace content { | 23 namespace content { |
23 | 24 |
24 class WebSocketHost; | 25 class WebSocketHost; |
25 | 26 |
26 // Creates a WebSocketHost object for each WebSocket channel, and dispatches | 27 // Creates a WebSocketHost object for each WebSocket channel, and dispatches |
27 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. | 28 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost. |
28 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { | 29 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter { |
29 public: | 30 public: |
30 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; | 31 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback; |
31 | 32 |
32 // Given a routing_id, WebSocketHostFactory returns a new instance of | 33 // Given a routing_id, WebSocketHostFactory returns a new instance of |
33 // WebSocketHost or its subclass. | 34 // WebSocketHost or its subclass. |
34 typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory; | 35 typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory; |
35 | 36 |
37 // Return value for methods that may delete the WebSocketHost. This enum is | |
38 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make | |
39 // conversion cheap. By using a separate enum including net/ header files can | |
40 // be avoided. | |
41 enum WebSocketHostState { | |
42 WEBSOCKET_HOST_ALIVE, | |
43 WEBSOCKET_HOST_DELETED | |
44 }; | |
45 | |
36 explicit WebSocketDispatcherHost( | 46 explicit WebSocketDispatcherHost( |
37 const GetRequestContextCallback& get_context_callback); | 47 const GetRequestContextCallback& get_context_callback); |
38 | 48 |
39 // For testing. Specify a factory method that creates mock version of | 49 // For testing. Specify a factory method that creates mock version of |
40 // WebSocketHost. | 50 // WebSocketHost. |
41 WebSocketDispatcherHost( | 51 WebSocketDispatcherHost( |
42 const GetRequestContextCallback& get_context_callback, | 52 const GetRequestContextCallback& get_context_callback, |
43 const WebSocketHostFactory& websocket_host_factory); | 53 const WebSocketHostFactory& websocket_host_factory); |
44 | 54 |
45 // BrowserMessageFilter: | 55 // BrowserMessageFilter: |
46 virtual bool OnMessageReceived(const IPC::Message& message, | 56 virtual bool OnMessageReceived(const IPC::Message& message, |
47 bool* message_was_ok) OVERRIDE; | 57 bool* message_was_ok) OVERRIDE; |
48 | 58 |
49 // The following methods are used by WebSocketHost::EventInterface to send | 59 // The following methods are used by WebSocketHost::EventInterface to send |
50 // IPCs from the browser to the renderer or child process. Any of them may | 60 // IPCs from the browser to the renderer or child process. Any of them may |
51 // delete the WebSocketHost on failure, leading to the WebSocketChannel and | 61 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure, |
52 // EventInterface also being deleted. | 62 // leading to the WebSocketChannel and EventInterface also being deleted. |
53 | 63 |
54 // Sends a WebSocketMsg_AddChannelResponse IPC, and then deletes and | 64 // Sends a WebSocketMsg_AddChannelResponse IPC, and then deletes and |
55 // unregisters the WebSocketHost if |fail| is true. | 65 // unregisters the WebSocketHost if |fail| is true. |
56 void SendAddChannelResponse(int routing_id, | 66 WebSocketHostState SendAddChannelResponse( |
57 bool fail, | 67 int routing_id, |
58 const std::string& selected_protocol, | 68 bool fail, |
59 const std::string& extensions); | 69 const std::string& selected_protocol, |
70 const std::string& extensions) WARN_UNUSED_RESULT; | |
60 | 71 |
61 // Sends a WebSocketMsg_SendFrame IPC. | 72 // Sends a WebSocketMsg_SendFrame IPC. |
62 void SendFrame(int routing_id, | 73 WebSocketHostState SendFrame(int routing_id, |
63 bool fin, | 74 bool fin, |
64 WebSocketMessageType type, | 75 WebSocketMessageType type, |
65 const std::vector<char>& data); | 76 const std::vector<char>& data); |
66 | 77 |
67 // Sends a WebSocketMsg_FlowControl IPC. | 78 // Sends a WebSocketMsg_FlowControl IPC. |
68 void SendFlowControl(int routing_id, int64 quota); | 79 WebSocketHostState SendFlowControl(int routing_id, |
80 int64 quota) WARN_UNUSED_RESULT; | |
69 | 81 |
70 // Sends a WebSocketMsg_SendClosing IPC | 82 // Sends a WebSocketMsg_SendClosing IPC |
71 void SendClosing(int routing_id); | 83 WebSocketHostState SendClosing(int routing_id) WARN_UNUSED_RESULT; |
72 | 84 |
73 // Sends a WebSocketMsg_DropChannel IPC and delete and unregister the channel. | 85 // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the |
74 void DoDropChannel(int routing_id, uint16 code, const std::string& reason); | 86 // channel. |
87 WebSocketHostState DoDropChannel( | |
88 int routing_id, | |
89 uint16 code, | |
90 const std::string& reason) WARN_UNUSED_RESULT; | |
75 | 91 |
76 private: | 92 private: |
77 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; | 93 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable; |
78 | 94 |
79 virtual ~WebSocketDispatcherHost(); | 95 virtual ~WebSocketDispatcherHost(); |
80 | 96 |
81 WebSocketHost* CreateWebSocketHost(int routing_id); | 97 WebSocketHost* CreateWebSocketHost(int routing_id); |
82 | 98 |
83 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one | 99 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one |
84 // is found, or NULL otherwise. | 100 // is found, or NULL otherwise. |
85 WebSocketHost* GetHost(int routing_id) const; | 101 WebSocketHost* GetHost(int routing_id) const; |
86 | 102 |
87 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() | 103 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send() |
88 // method. If the Send() fails, logs it and deletes the corresponding | 104 // method. If sending the IPC fails, assumes that this connection is no |
89 // WebSocketHost object (the client is probably dead). | 105 // longer useable, calls DeleteWebSocketHost(), and returns |
90 void SendOrDrop(IPC::Message* message); | 106 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types. |
107 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT; | |
91 | 108 |
92 // Deletes the WebSocketHost object associated with the given |routing_id| and | 109 // Deletes the WebSocketHost object associated with the given |routing_id| and |
93 // removes it from the |hosts_| table. Does nothing if the |routing_id| is | 110 // removes it from the |hosts_| table. |
94 // unknown, since SendAddChannelResponse() may call this method twice. | |
95 void DeleteWebSocketHost(int routing_id); | 111 void DeleteWebSocketHost(int routing_id); |
96 | 112 |
97 // Table of WebSocketHost objects, owned by this object, indexed by | 113 // Table of WebSocketHost objects, owned by this object, indexed by |
98 // routing_id. | 114 // routing_id. |
99 WebSocketHostTable hosts_; | 115 WebSocketHostTable hosts_; |
100 | 116 |
101 // A callback which returns the appropriate net::URLRequestContext for us to | 117 // A callback which returns the appropriate net::URLRequestContext for us to |
102 // use. | 118 // use. |
103 GetRequestContextCallback get_context_callback_; | 119 GetRequestContextCallback get_context_callback_; |
104 | 120 |
105 WebSocketHostFactory websocket_host_factory_; | 121 WebSocketHostFactory websocket_host_factory_; |
106 | 122 |
107 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); | 123 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost); |
108 }; | 124 }; |
109 | 125 |
110 } // namespace content | 126 } // namespace content |
111 | 127 |
112 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ | 128 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_ |
OLD | NEW |