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
;
25 struct WebSocketHandshakeRequest
;
26 struct WebSocketHandshakeResponse
;
29 // Creates a WebSocketHost object for each WebSocket channel, and dispatches
30 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost.
31 class CONTENT_EXPORT WebSocketDispatcherHost
: public BrowserMessageFilter
{
33 typedef base::Callback
<net::URLRequestContext
*()> GetRequestContextCallback
;
35 // Given a routing_id, WebSocketHostFactory returns a new instance of
36 // WebSocketHost or its subclass.
37 typedef base::Callback
<WebSocketHost
*(int)> WebSocketHostFactory
; // NOLINT
39 // Return value for methods that may delete the WebSocketHost. This enum is
40 // binary-compatible with net::WebSocketEventInterface::ChannelState, to make
41 // conversion cheap. By using a separate enum including net/ header files can
43 enum WebSocketHostState
{
45 WEBSOCKET_HOST_DELETED
48 WebSocketDispatcherHost(
50 const GetRequestContextCallback
& get_context_callback
);
52 // For testing. Specify a factory method that creates mock version of
54 WebSocketDispatcherHost(int process_id
,
55 const GetRequestContextCallback
& get_context_callback
,
56 const WebSocketHostFactory
& websocket_host_factory
);
58 // BrowserMessageFilter:
59 virtual bool OnMessageReceived(const IPC::Message
& message
,
60 bool* message_was_ok
) OVERRIDE
;
62 // The following methods are used by WebSocketHost::EventInterface to send
63 // IPCs from the browser to the renderer or child process. Any of them may
64 // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure,
65 // leading to the WebSocketChannel and EventInterface also being deleted.
67 // Sends a WebSocketMsg_AddChannelResponse IPC, and then deletes and
68 // unregisters the WebSocketHost if |fail| is true.
69 WebSocketHostState
SendAddChannelResponse(
72 const std::string
& selected_protocol
,
73 const std::string
& extensions
) WARN_UNUSED_RESULT
;
75 // Sends a WebSocketMsg_SendFrame IPC.
76 WebSocketHostState
SendFrame(int routing_id
,
78 WebSocketMessageType type
,
79 const std::vector
<char>& data
);
81 // Sends a WebSocketMsg_FlowControl IPC.
82 WebSocketHostState
SendFlowControl(int routing_id
,
83 int64 quota
) WARN_UNUSED_RESULT
;
85 // Sends a WebSocketMsg_NotifyClosing IPC
86 WebSocketHostState
NotifyClosingHandshake(int routing_id
) WARN_UNUSED_RESULT
;
88 // Sends a WebSocketMsg_NotifyStartOpeningHandshake IPC.
89 WebSocketHostState
NotifyStartOpeningHandshake(
91 const WebSocketHandshakeRequest
& request
) WARN_UNUSED_RESULT
;
93 // Sends a WebSocketMsg_NotifyFinishOpeningHandshake IPC.
94 WebSocketHostState
NotifyFinishOpeningHandshake(
96 const WebSocketHandshakeResponse
& response
) WARN_UNUSED_RESULT
;
98 // Sends a WebSocketMsg_NotifyFailure IPC and deletes and unregisters the
100 WebSocketHostState
NotifyFailure(
102 const std::string
& message
) WARN_UNUSED_RESULT
;
104 // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the
106 WebSocketHostState
DoDropChannel(
110 const std::string
& reason
) WARN_UNUSED_RESULT
;
112 // Returns whether the associated renderer process can read raw cookies.
113 bool CanReadRawCookies() const;
116 typedef base::hash_map
<int, WebSocketHost
*> WebSocketHostTable
;
118 virtual ~WebSocketDispatcherHost();
120 WebSocketHost
* CreateWebSocketHost(int routing_id
);
122 // Looks up a WebSocketHost object by |routing_id|. Returns the object if one
123 // is found, or NULL otherwise.
124 WebSocketHost
* GetHost(int routing_id
) const;
126 // Sends the passed in IPC::Message via the BrowserMessageFilter::Send()
127 // method. If sending the IPC fails, assumes that this connection is no
128 // longer useable, calls DeleteWebSocketHost(), and returns
129 // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types.
130 WebSocketHostState
SendOrDrop(IPC::Message
* message
) WARN_UNUSED_RESULT
;
132 // Deletes the WebSocketHost object associated with the given |routing_id| and
133 // removes it from the |hosts_| table.
134 void DeleteWebSocketHost(int routing_id
);
136 // Table of WebSocketHost objects, owned by this object, indexed by
138 WebSocketHostTable hosts_
;
140 // The the process ID of the associated renderer process.
141 const int process_id_
;
143 // A callback which returns the appropriate net::URLRequestContext for us to
145 GetRequestContextCallback get_context_callback_
;
147 WebSocketHostFactory websocket_host_factory_
;
149 DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost
);
152 } // namespace content
154 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_