OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "net/socket_stream/socket_stream.h" | 5 #include "net/socket_stream/socket_stream.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 void SetOnAuthRequired( | 83 void SetOnAuthRequired( |
84 const base::Callback<void(SocketStreamEvent*)>& callback) { | 84 const base::Callback<void(SocketStreamEvent*)>& callback) { |
85 on_auth_required_ = callback; | 85 on_auth_required_ = callback; |
86 } | 86 } |
87 void SetOnError(const base::Callback<void(SocketStreamEvent*)>& callback) { | 87 void SetOnError(const base::Callback<void(SocketStreamEvent*)>& callback) { |
88 on_error_ = callback; | 88 on_error_ = callback; |
89 } | 89 } |
90 | 90 |
91 virtual int OnStartOpenConnection( | 91 virtual int OnStartOpenConnection( |
92 SocketStream* socket, | 92 SocketStream* socket, |
93 const CompletionCallback& callback) OVERRIDE { | 93 const CompletionCallback& callback) override { |
94 connection_callback_ = callback; | 94 connection_callback_ = callback; |
95 events_.push_back( | 95 events_.push_back( |
96 SocketStreamEvent(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, | 96 SocketStreamEvent(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, |
97 socket, 0, std::string(), NULL, OK)); | 97 socket, 0, std::string(), NULL, OK)); |
98 if (!on_start_open_connection_.is_null()) | 98 if (!on_start_open_connection_.is_null()) |
99 return on_start_open_connection_.Run(&events_.back()); | 99 return on_start_open_connection_.Run(&events_.back()); |
100 return OK; | 100 return OK; |
101 } | 101 } |
102 virtual void OnConnected(SocketStream* socket, | 102 virtual void OnConnected(SocketStream* socket, |
103 int num_pending_send_allowed) OVERRIDE { | 103 int num_pending_send_allowed) override { |
104 events_.push_back( | 104 events_.push_back( |
105 SocketStreamEvent(SocketStreamEvent::EVENT_CONNECTED, | 105 SocketStreamEvent(SocketStreamEvent::EVENT_CONNECTED, |
106 socket, num_pending_send_allowed, std::string(), | 106 socket, num_pending_send_allowed, std::string(), |
107 NULL, OK)); | 107 NULL, OK)); |
108 if (!on_connected_.is_null()) | 108 if (!on_connected_.is_null()) |
109 on_connected_.Run(&events_.back()); | 109 on_connected_.Run(&events_.back()); |
110 } | 110 } |
111 virtual void OnSentData(SocketStream* socket, | 111 virtual void OnSentData(SocketStream* socket, |
112 int amount_sent) OVERRIDE { | 112 int amount_sent) override { |
113 events_.push_back( | 113 events_.push_back( |
114 SocketStreamEvent(SocketStreamEvent::EVENT_SENT_DATA, socket, | 114 SocketStreamEvent(SocketStreamEvent::EVENT_SENT_DATA, socket, |
115 amount_sent, std::string(), NULL, OK)); | 115 amount_sent, std::string(), NULL, OK)); |
116 if (!on_sent_data_.is_null()) | 116 if (!on_sent_data_.is_null()) |
117 on_sent_data_.Run(&events_.back()); | 117 on_sent_data_.Run(&events_.back()); |
118 } | 118 } |
119 virtual void OnReceivedData(SocketStream* socket, | 119 virtual void OnReceivedData(SocketStream* socket, |
120 const char* data, int len) OVERRIDE { | 120 const char* data, int len) override { |
121 events_.push_back( | 121 events_.push_back( |
122 SocketStreamEvent(SocketStreamEvent::EVENT_RECEIVED_DATA, socket, len, | 122 SocketStreamEvent(SocketStreamEvent::EVENT_RECEIVED_DATA, socket, len, |
123 std::string(data, len), NULL, OK)); | 123 std::string(data, len), NULL, OK)); |
124 if (!on_received_data_.is_null()) | 124 if (!on_received_data_.is_null()) |
125 on_received_data_.Run(&events_.back()); | 125 on_received_data_.Run(&events_.back()); |
126 } | 126 } |
127 virtual void OnClose(SocketStream* socket) OVERRIDE { | 127 virtual void OnClose(SocketStream* socket) override { |
128 events_.push_back( | 128 events_.push_back( |
129 SocketStreamEvent(SocketStreamEvent::EVENT_CLOSE, socket, 0, | 129 SocketStreamEvent(SocketStreamEvent::EVENT_CLOSE, socket, 0, |
130 std::string(), NULL, OK)); | 130 std::string(), NULL, OK)); |
131 if (!on_close_.is_null()) | 131 if (!on_close_.is_null()) |
132 on_close_.Run(&events_.back()); | 132 on_close_.Run(&events_.back()); |
133 if (!callback_.is_null()) | 133 if (!callback_.is_null()) |
134 callback_.Run(OK); | 134 callback_.Run(OK); |
135 } | 135 } |
136 virtual void OnAuthRequired(SocketStream* socket, | 136 virtual void OnAuthRequired(SocketStream* socket, |
137 AuthChallengeInfo* auth_info) OVERRIDE { | 137 AuthChallengeInfo* auth_info) override { |
138 events_.push_back( | 138 events_.push_back( |
139 SocketStreamEvent(SocketStreamEvent::EVENT_AUTH_REQUIRED, socket, 0, | 139 SocketStreamEvent(SocketStreamEvent::EVENT_AUTH_REQUIRED, socket, 0, |
140 std::string(), auth_info, OK)); | 140 std::string(), auth_info, OK)); |
141 if (!on_auth_required_.is_null()) | 141 if (!on_auth_required_.is_null()) |
142 on_auth_required_.Run(&events_.back()); | 142 on_auth_required_.Run(&events_.back()); |
143 } | 143 } |
144 virtual void OnError(const SocketStream* socket, int error) OVERRIDE { | 144 virtual void OnError(const SocketStream* socket, int error) override { |
145 events_.push_back( | 145 events_.push_back( |
146 SocketStreamEvent(SocketStreamEvent::EVENT_ERROR, NULL, 0, | 146 SocketStreamEvent(SocketStreamEvent::EVENT_ERROR, NULL, 0, |
147 std::string(), NULL, error)); | 147 std::string(), NULL, error)); |
148 if (!on_error_.is_null()) | 148 if (!on_error_.is_null()) |
149 on_error_.Run(&events_.back()); | 149 on_error_.Run(&events_.back()); |
150 if (!callback_.is_null()) | 150 if (!callback_.is_null()) |
151 callback_.Run(error); | 151 callback_.Run(error); |
152 } | 152 } |
153 | 153 |
154 void DoClose(SocketStreamEvent* event) { | 154 void DoClose(SocketStreamEvent* event) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 // This is used for the test OnErrorDetachDelegate. | 191 // This is used for the test OnErrorDetachDelegate. |
192 class SelfDeletingDelegate : public SocketStream::Delegate { | 192 class SelfDeletingDelegate : public SocketStream::Delegate { |
193 public: | 193 public: |
194 // |callback| must cause the test message loop to exit when called. | 194 // |callback| must cause the test message loop to exit when called. |
195 explicit SelfDeletingDelegate(const CompletionCallback& callback) | 195 explicit SelfDeletingDelegate(const CompletionCallback& callback) |
196 : socket_stream_(), callback_(callback) {} | 196 : socket_stream_(), callback_(callback) {} |
197 | 197 |
198 virtual ~SelfDeletingDelegate() {} | 198 virtual ~SelfDeletingDelegate() {} |
199 | 199 |
200 // Call DetachDelegate(), delete |this|, then run the callback. | 200 // Call DetachDelegate(), delete |this|, then run the callback. |
201 virtual void OnError(const SocketStream* socket, int error) OVERRIDE { | 201 virtual void OnError(const SocketStream* socket, int error) override { |
202 // callback_ will be deleted when we delete |this|, so copy it to call it | 202 // callback_ will be deleted when we delete |this|, so copy it to call it |
203 // afterwards. | 203 // afterwards. |
204 CompletionCallback callback = callback_; | 204 CompletionCallback callback = callback_; |
205 socket_stream_->DetachDelegate(); | 205 socket_stream_->DetachDelegate(); |
206 delete this; | 206 delete this; |
207 callback.Run(OK); | 207 callback.Run(OK); |
208 } | 208 } |
209 | 209 |
210 // This can't be passed in the constructor because this object needs to be | 210 // This can't be passed in the constructor because this object needs to be |
211 // created before SocketStream. | 211 // created before SocketStream. |
212 void set_socket_stream(const scoped_refptr<SocketStream>& socket_stream) { | 212 void set_socket_stream(const scoped_refptr<SocketStream>& socket_stream) { |
213 socket_stream_ = socket_stream; | 213 socket_stream_ = socket_stream; |
214 EXPECT_EQ(socket_stream_->delegate(), this); | 214 EXPECT_EQ(socket_stream_->delegate(), this); |
215 } | 215 } |
216 | 216 |
217 virtual void OnConnected(SocketStream* socket, int max_pending_send_allowed) | 217 virtual void OnConnected(SocketStream* socket, int max_pending_send_allowed) |
218 OVERRIDE { | 218 override { |
219 ADD_FAILURE() << "OnConnected() should not be called"; | 219 ADD_FAILURE() << "OnConnected() should not be called"; |
220 } | 220 } |
221 virtual void OnSentData(SocketStream* socket, int amount_sent) OVERRIDE { | 221 virtual void OnSentData(SocketStream* socket, int amount_sent) override { |
222 ADD_FAILURE() << "OnSentData() should not be called"; | 222 ADD_FAILURE() << "OnSentData() should not be called"; |
223 } | 223 } |
224 virtual void OnReceivedData(SocketStream* socket, const char* data, int len) | 224 virtual void OnReceivedData(SocketStream* socket, const char* data, int len) |
225 OVERRIDE { | 225 override { |
226 ADD_FAILURE() << "OnReceivedData() should not be called"; | 226 ADD_FAILURE() << "OnReceivedData() should not be called"; |
227 } | 227 } |
228 virtual void OnClose(SocketStream* socket) OVERRIDE { | 228 virtual void OnClose(SocketStream* socket) override { |
229 ADD_FAILURE() << "OnClose() should not be called"; | 229 ADD_FAILURE() << "OnClose() should not be called"; |
230 } | 230 } |
231 | 231 |
232 private: | 232 private: |
233 scoped_refptr<SocketStream> socket_stream_; | 233 scoped_refptr<SocketStream> socket_stream_; |
234 const CompletionCallback callback_; | 234 const CompletionCallback callback_; |
235 | 235 |
236 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDelegate); | 236 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDelegate); |
237 }; | 237 }; |
238 | 238 |
239 class TestURLRequestContextWithProxy : public TestURLRequestContext { | 239 class TestURLRequestContextWithProxy : public TestURLRequestContext { |
240 public: | 240 public: |
241 explicit TestURLRequestContextWithProxy(const std::string& proxy) | 241 explicit TestURLRequestContextWithProxy(const std::string& proxy) |
242 : TestURLRequestContext(true) { | 242 : TestURLRequestContext(true) { |
243 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); | 243 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
244 Init(); | 244 Init(); |
245 } | 245 } |
246 virtual ~TestURLRequestContextWithProxy() {} | 246 virtual ~TestURLRequestContextWithProxy() {} |
247 }; | 247 }; |
248 | 248 |
249 class TestSocketStreamNetworkDelegate : public TestNetworkDelegate { | 249 class TestSocketStreamNetworkDelegate : public TestNetworkDelegate { |
250 public: | 250 public: |
251 TestSocketStreamNetworkDelegate() | 251 TestSocketStreamNetworkDelegate() |
252 : before_connect_result_(OK) {} | 252 : before_connect_result_(OK) {} |
253 virtual ~TestSocketStreamNetworkDelegate() {} | 253 virtual ~TestSocketStreamNetworkDelegate() {} |
254 | 254 |
255 virtual int OnBeforeSocketStreamConnect( | 255 virtual int OnBeforeSocketStreamConnect( |
256 SocketStream* stream, | 256 SocketStream* stream, |
257 const CompletionCallback& callback) OVERRIDE { | 257 const CompletionCallback& callback) override { |
258 return before_connect_result_; | 258 return before_connect_result_; |
259 } | 259 } |
260 | 260 |
261 void SetBeforeConnectResult(int result) { | 261 void SetBeforeConnectResult(int result) { |
262 before_connect_result_ = result; | 262 before_connect_result_ = result; |
263 } | 263 } |
264 | 264 |
265 private: | 265 private: |
266 int before_connect_result_; | 266 int before_connect_result_; |
267 }; | 267 }; |
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1035 | 1035 |
1036 EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, | 1036 EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, |
1037 events[0].event_type); | 1037 events[0].event_type); |
1038 EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[1].event_type); | 1038 EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[1].event_type); |
1039 EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[2].event_type); | 1039 EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[2].event_type); |
1040 EXPECT_EQ(SocketStreamEvent::EVENT_RECEIVED_DATA, events[3].event_type); | 1040 EXPECT_EQ(SocketStreamEvent::EVENT_RECEIVED_DATA, events[3].event_type); |
1041 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[4].event_type); | 1041 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[4].event_type); |
1042 } | 1042 } |
1043 | 1043 |
1044 } // namespace net | 1044 } // namespace net |
OLD | NEW |