- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / websocket_dispatcher_host.h
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.
4
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
7
8 #include <string>
9 #include <vector>
10
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"
18
19 namespace net {
20 class URLRequestContext;
21 }  // namespace net
22
23 namespace content {
24
25 class WebSocketHost;
26
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 {
30  public:
31   typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback;
32
33   // Given a routing_id, WebSocketHostFactory returns a new instance of
34   // WebSocketHost or its subclass.
35   typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory;
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
46   explicit WebSocketDispatcherHost(
47       const GetRequestContextCallback& get_context_callback);
48
49   // For testing. Specify a factory method that creates mock version of
50   // WebSocketHost.
51   WebSocketDispatcherHost(
52       const GetRequestContextCallback& get_context_callback,
53       const WebSocketHostFactory& websocket_host_factory);
54
55   // BrowserMessageFilter:
56   virtual bool OnMessageReceived(const IPC::Message& message,
57                                  bool* message_was_ok) OVERRIDE;
58
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.
63
64   // Sends a WebSocketMsg_AddChannelResponse IPC, and then deletes and
65   // unregisters the WebSocketHost if |fail| is true.
66   WebSocketHostState SendAddChannelResponse(
67       int routing_id,
68       bool fail,
69       const std::string& selected_protocol,
70       const std::string& extensions) WARN_UNUSED_RESULT;
71
72   // Sends a WebSocketMsg_SendFrame IPC.
73   WebSocketHostState SendFrame(int routing_id,
74                                bool fin,
75                                WebSocketMessageType type,
76                                const std::vector<char>& data);
77
78   // Sends a WebSocketMsg_FlowControl IPC.
79   WebSocketHostState SendFlowControl(int routing_id,
80                                      int64 quota) WARN_UNUSED_RESULT;
81
82   // Sends a WebSocketMsg_SendClosing IPC
83   WebSocketHostState SendClosing(int routing_id) WARN_UNUSED_RESULT;
84
85   // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the
86   // channel.
87   WebSocketHostState DoDropChannel(
88       int routing_id,
89       uint16 code,
90       const std::string& reason) WARN_UNUSED_RESULT;
91
92  private:
93   typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable;
94
95   virtual ~WebSocketDispatcherHost();
96
97   WebSocketHost* CreateWebSocketHost(int routing_id);
98
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;
102
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;
108
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);
112
113   // Table of WebSocketHost objects, owned by this object, indexed by
114   // routing_id.
115   WebSocketHostTable hosts_;
116
117   // A callback which returns the appropriate net::URLRequestContext for us to
118   // use.
119   GetRequestContextCallback get_context_callback_;
120
121   WebSocketHostFactory websocket_host_factory_;
122
123   DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost);
124 };
125
126 }  // namespace content
127
128 #endif  // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_