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_WEBSOCKETS_WEBSOCKET_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_WEBSOCKETS_WEBSOCKET_IMPL_H_ |
6 #define CONTENT_BROWSER_WEBSOCKETS_WEBSOCKET_IMPL_H_ | 6 #define CONTENT_BROWSER_WEBSOCKETS_WEBSOCKET_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 18 #include "content/common/websocket.mojom.h" |
18 #include "mojo/public/cpp/bindings/binding.h" | 19 #include "mojo/public/cpp/bindings/binding.h" |
19 #include "third_party/WebKit/public/platform/modules/websockets/websocket.mojom.
h" | |
20 | 20 |
21 class GURL; | 21 class GURL; |
22 | 22 |
23 namespace url { | 23 namespace url { |
24 class Origin; | 24 class Origin; |
25 } // namespace url | 25 } // namespace url |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 class WebSocketChannel; | 28 class WebSocketChannel; |
29 } // namespace net | 29 } // namespace net |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 class StoragePartition; | 32 class StoragePartition; |
33 | 33 |
34 // Host of net::WebSocketChannel. | 34 // Host of net::WebSocketChannel. |
35 class CONTENT_EXPORT WebSocketImpl | 35 class CONTENT_EXPORT WebSocketImpl |
36 : NON_EXPORTED_BASE(public blink::mojom::WebSocket) { | 36 : NON_EXPORTED_BASE(public mojom::WebSocket) { |
37 public: | 37 public: |
38 class Delegate { | 38 class Delegate { |
39 public: | 39 public: |
40 virtual ~Delegate() {} | 40 virtual ~Delegate() {} |
41 virtual int GetClientProcessId() = 0; | 41 virtual int GetClientProcessId() = 0; |
42 virtual StoragePartition* GetStoragePartition() = 0; | 42 virtual StoragePartition* GetStoragePartition() = 0; |
43 virtual void OnReceivedResponseFromServer(WebSocketImpl* impl) = 0; | 43 virtual void OnReceivedResponseFromServer(WebSocketImpl* impl) = 0; |
44 virtual void OnLostConnectionToClient(WebSocketImpl* impl) = 0; | 44 virtual void OnLostConnectionToClient(WebSocketImpl* impl) = 0; |
45 }; | 45 }; |
46 | 46 |
47 WebSocketImpl(Delegate* delegate, | 47 WebSocketImpl(Delegate* delegate, |
48 blink::mojom::WebSocketRequest request, | 48 mojom::WebSocketRequest request, |
49 int frame_id, | 49 int frame_id, |
50 base::TimeDelta delay); | 50 base::TimeDelta delay); |
51 ~WebSocketImpl() override; | 51 ~WebSocketImpl() override; |
52 | 52 |
53 // The renderer process is going away. | 53 // The renderer process is going away. |
54 // This function is virtual for testing. | 54 // This function is virtual for testing. |
55 virtual void GoAway(); | 55 virtual void GoAway(); |
56 | 56 |
57 // blink::mojom::WebSocket methods: | 57 // mojom::WebSocket methods: |
58 void AddChannelRequest(const GURL& url, | 58 void AddChannelRequest(const GURL& url, |
59 const std::vector<std::string>& requested_protocols, | 59 const std::vector<std::string>& requested_protocols, |
60 const url::Origin& origin, | 60 const url::Origin& origin, |
61 const GURL& first_party_for_cookies, | 61 const GURL& first_party_for_cookies, |
62 const std::string& user_agent_override, | 62 const std::string& user_agent_override, |
63 blink::mojom::WebSocketClientPtr client) override; | 63 mojom::WebSocketClientPtr client) override; |
64 void SendFrame(bool fin, | 64 void SendFrame(bool fin, |
65 blink::mojom::WebSocketMessageType type, | 65 mojom::WebSocketMessageType type, |
66 const std::vector<uint8_t>& data) override; | 66 const std::vector<uint8_t>& data) override; |
67 void SendFlowControl(int64_t quota) override; | 67 void SendFlowControl(int64_t quota) override; |
68 void StartClosingHandshake(uint16_t code, const std::string& reason) override; | 68 void StartClosingHandshake(uint16_t code, const std::string& reason) override; |
69 | 69 |
70 bool handshake_succeeded() const { return handshake_succeeded_; } | 70 bool handshake_succeeded() const { return handshake_succeeded_; } |
71 void OnHandshakeSucceeded() { handshake_succeeded_ = true; } | 71 void OnHandshakeSucceeded() { handshake_succeeded_ = true; } |
72 | 72 |
73 protected: | 73 protected: |
74 class WebSocketEventHandler; | 74 class WebSocketEventHandler; |
75 | 75 |
76 void OnConnectionError(); | 76 void OnConnectionError(); |
77 void AddChannel(const GURL& socket_url, | 77 void AddChannel(const GURL& socket_url, |
78 const std::vector<std::string>& requested_protocols, | 78 const std::vector<std::string>& requested_protocols, |
79 const url::Origin& origin, | 79 const url::Origin& origin, |
80 const GURL& first_party_for_cookies, | 80 const GURL& first_party_for_cookies, |
81 const std::string& user_agent_override); | 81 const std::string& user_agent_override); |
82 | 82 |
83 Delegate* delegate_; | 83 Delegate* delegate_; |
84 mojo::Binding<blink::mojom::WebSocket> binding_; | 84 mojo::Binding<mojom::WebSocket> binding_; |
85 | 85 |
86 blink::mojom::WebSocketClientPtr client_; | 86 mojom::WebSocketClientPtr client_; |
87 | 87 |
88 // The channel we use to send events to the network. | 88 // The channel we use to send events to the network. |
89 std::unique_ptr<net::WebSocketChannel> channel_; | 89 std::unique_ptr<net::WebSocketChannel> channel_; |
90 | 90 |
91 // Delay used for per-renderer WebSocket throttling. | 91 // Delay used for per-renderer WebSocket throttling. |
92 base::TimeDelta delay_; | 92 base::TimeDelta delay_; |
93 | 93 |
94 // SendFlowControl() is delayed when OnFlowControl() is called before | 94 // SendFlowControl() is delayed when OnFlowControl() is called before |
95 // AddChannel() is called. | 95 // AddChannel() is called. |
96 // Zero indicates there is no pending SendFlowControl(). | 96 // Zero indicates there is no pending SendFlowControl(). |
97 int64_t pending_flow_control_quota_; | 97 int64_t pending_flow_control_quota_; |
98 | 98 |
99 int frame_id_; | 99 int frame_id_; |
100 | 100 |
101 // handshake_succeeded_ is set and used by WebSocketManager to manage | 101 // handshake_succeeded_ is set and used by WebSocketManager to manage |
102 // counters for per-renderer WebSocket throttling. | 102 // counters for per-renderer WebSocket throttling. |
103 bool handshake_succeeded_; | 103 bool handshake_succeeded_; |
104 | 104 |
105 base::WeakPtrFactory<WebSocketImpl> weak_ptr_factory_; | 105 base::WeakPtrFactory<WebSocketImpl> weak_ptr_factory_; |
106 | 106 |
107 DISALLOW_COPY_AND_ASSIGN(WebSocketImpl); | 107 DISALLOW_COPY_AND_ASSIGN(WebSocketImpl); |
108 }; | 108 }; |
109 | 109 |
110 } // namespace content | 110 } // namespace content |
111 | 111 |
112 #endif // CONTENT_BROWSER_WEBSOCKETS_WEBSOCKET_IMPL_H_ | 112 #endif // CONTENT_BROWSER_WEBSOCKETS_WEBSOCKET_IMPL_H_ |
OLD | NEW |