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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "content/common/content_export.h"
16 #include "content/common/websocket.h"
17 #include "content/public/browser/browser_message_filter.h"
20 class URLRequestContext;
27 // Creates a WebSocketHost object for each WebSocket channel, and dispatches
28 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost.
29 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter {
31 typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback;
33 // Given a routing_id, WebSocketHostFactory returns a new instance of
34 // WebSocketHost or its subclass.
35 typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory;
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
41 enum WebSocketHostState {
43 WEBSOCKET_HOST_DELETED
46 explicit WebSocketDispatcherHost(
47 const GetRequestContextCallback& get_context_callback);
49 // For testing. Specify a factory method that creates mock version of
51 WebSocketDispatcherHost(
52 const GetRequestContextCallback& get_context_callback,
53 const WebSocketHostFactory& websocket_host_factory);
55 // BrowserMessageFilter:
56 virtual bool OnMessageReceived(const IPC::Message& message,
57 bool* message_was_ok) OVERRIDE;
59 // The following methods are used by WebSocketHost::EventInterface to send
60 // IPCs from the browser to the renderer or child process. Any of them may
61 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure,
62 // leading to the WebSocketChannel and EventInterface also being deleted.
64 // Sends a WebSocketMsg_AddChannelResponse IPC, and then deletes and
65 // unregisters the WebSocketHost if |fail| is true.
66 WebSocketHostState SendAddChannelResponse(
69 const std::string& selected_protocol,
70 const std::string& extensions) WARN_UNUSED_RESULT;
72 // Sends a WebSocketMsg_SendFrame IPC.
73 WebSocketHostState SendFrame(int routing_id,
75 WebSocketMessageType type,
76 const std::vector<char>& data);
78 // Sends a WebSocketMsg_FlowControl IPC.
79 WebSocketHostState SendFlowControl(int routing_id,
80 int64 quota) WARN_UNUSED_RESULT;
82 // Sends a WebSocketMsg_SendClosing IPC
83 WebSocketHostState SendClosing(int routing_id) WARN_UNUSED_RESULT;
85 // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the
87 WebSocketHostState DoDropChannel(
90 const std::string& reason) WARN_UNUSED_RESULT;
93 typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable;
95 virtual ~WebSocketDispatcherHost();
97 WebSocketHost* CreateWebSocketHost(int routing_id);
99 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one
100 // is found, or NULL otherwise.
101 WebSocketHost* GetHost(int routing_id) const;
103 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send()
104 // method. If sending the IPC fails, assumes that this connection is no
105 // longer useable, calls DeleteWebSocketHost(), and returns
106 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types.
107 WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT;
109 // Deletes the WebSocketHost object associated with the given |routing_id| and
110 // removes it from the |hosts_| table.
111 void DeleteWebSocketHost(int routing_id);
113 // Table of WebSocketHost objects, owned by this object, indexed by
115 WebSocketHostTable hosts_;
117 // A callback which returns the appropriate net::URLRequestContext for us to
119 GetRequestContextCallback get_context_callback_;
121 WebSocketHostFactory websocket_host_factory_;
123 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost);
126 } // namespace content
128 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_