Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / websocket_host.h
blob89b368685d6b64df526f0244f18de97bda3c8f54
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_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "content/common/content_export.h"
15 #include "content/common/websocket.h"
17 class GURL;
19 namespace url {
20 class Origin;
21 } // namespace url
23 namespace net {
24 class WebSocketChannel;
25 class URLRequestContext;
26 } // namespace net
28 namespace IPC {
29 class Message;
30 } // namespace IPC
32 namespace content {
34 class WebSocketDispatcherHost;
36 // Host of net::WebSocketChannel. The lifetime of an instance of this class is
37 // completely controlled by the WebSocketDispatcherHost object.
38 class CONTENT_EXPORT WebSocketHost {
39 public:
40 WebSocketHost(int routing_id,
41 WebSocketDispatcherHost* dispatcher,
42 net::URLRequestContext* url_request_context,
43 base::TimeDelta delay);
44 virtual ~WebSocketHost();
46 // The renderer process is going away.
47 // This function is virtual for testing.
48 virtual void GoAway();
50 // General message dispatch. WebSocketDispatcherHost::OnMessageReceived
51 // delegates to this method after looking up the |routing_id|.
52 virtual bool OnMessageReceived(const IPC::Message& message);
54 int routing_id() const { return routing_id_; }
56 bool handshake_succeeded() const { return handshake_succeeded_; }
57 void OnHandshakeSucceeded() { handshake_succeeded_ = true; }
59 private:
60 // Handlers for each message type, dispatched by OnMessageReceived(), as
61 // defined in content/common/websocket_messages.h
63 void OnAddChannelRequest(const GURL& socket_url,
64 const std::vector<std::string>& requested_protocols,
65 const url::Origin& origin,
66 int render_frame_id);
68 void AddChannel(const GURL& socket_url,
69 const std::vector<std::string>& requested_protocols,
70 const url::Origin& origin,
71 int render_frame_id);
73 void OnSendFrame(bool fin,
74 WebSocketMessageType type,
75 const std::vector<char>& data);
77 void OnFlowControl(int64 quota);
79 void OnDropChannel(bool was_clean, uint16 code, const std::string& reason);
81 // The channel we use to send events to the network.
82 scoped_ptr<net::WebSocketChannel> channel_;
84 // The WebSocketHostDispatcher that created this object.
85 WebSocketDispatcherHost* const dispatcher_;
87 // The URL request context for the channel.
88 net::URLRequestContext* const url_request_context_;
90 // The ID used to route messages.
91 const int routing_id_;
93 // Delay used for per-renderer WebSocket throttling.
94 base::TimeDelta delay_;
96 // SendFlowControl() is delayed when OnFlowControl() is called before
97 // AddChannel() is called.
98 // Zero indicates there is no pending SendFlowControl().
99 int64_t pending_flow_control_quota_;
101 // handshake_succeeded_ is set and used by WebSocketDispatcherHost
102 // to manage counters for per-renderer WebSocket throttling.
103 bool handshake_succeeded_;
105 base::WeakPtrFactory<WebSocketHost> weak_ptr_factory_;
107 DISALLOW_COPY_AND_ASSIGN(WebSocketHost);
110 } // namespace content
112 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_HOST_H_