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