Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/web_socket_server_socket.h" | 5 #include "net/socket/web_socket_server_socket.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 | 9 |
| 10 #include "base/callback_old.h" | 10 #include "base/callback_old.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 | 72 |
| 73 // TODO(dilmah): consider switching to socket_test_util.h | 73 // TODO(dilmah): consider switching to socket_test_util.h |
| 74 // Simulates reading from |sample| stream; data supplied in Write() calls are | 74 // Simulates reading from |sample| stream; data supplied in Write() calls are |
| 75 // stored in |answer| buffer. | 75 // stored in |answer| buffer. |
| 76 class TestingTransportSocket : public net::Socket { | 76 class TestingTransportSocket : public net::Socket { |
| 77 public: | 77 public: |
| 78 TestingTransportSocket( | 78 TestingTransportSocket( |
| 79 net::DrainableIOBuffer* sample, net::DrainableIOBuffer* answer) | 79 net::DrainableIOBuffer* sample, net::DrainableIOBuffer* answer) |
| 80 : sample_(sample), | 80 : sample_(sample), |
| 81 answer_(answer), | 81 answer_(answer), |
| 82 final_read_callback_(NULL), | 82 old_final_read_callback_(NULL), |
| 83 method_factory_(this) { | 83 method_factory_(this) { |
| 84 } | 84 } |
| 85 | 85 |
| 86 ~TestingTransportSocket() { | 86 ~TestingTransportSocket() { |
| 87 if (final_read_callback_) { | 87 if (old_final_read_callback_) { |
| 88 MessageLoop::current()->PostTask(FROM_HERE, | 88 MessageLoop::current()->PostTask(FROM_HERE, |
| 89 method_factory_.NewRunnableMethod( | 89 method_factory_.NewRunnableMethod( |
| 90 &TestingTransportSocket::DoOldReadCallback, | |
| 91 old_final_read_callback_, 0)); | |
| 92 } else if (!final_read_callback_.is_null()) { | |
| 93 MessageLoop::current()->PostTask( | |
| 94 FROM_HERE, | |
| 95 method_factory_.NewRunnableMethod( | |
| 90 &TestingTransportSocket::DoReadCallback, | 96 &TestingTransportSocket::DoReadCallback, |
| 91 final_read_callback_, 0)); | 97 final_read_callback_, 0)); |
| 92 } | 98 } |
| 93 } | 99 } |
| 94 | 100 |
| 95 // Socket implementation. | 101 // Socket implementation. |
| 96 virtual int Read(net::IOBuffer* buf, int buf_len, | 102 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 97 net::OldCompletionCallback* callback) { | 103 net::OldCompletionCallback* callback) { |
| 98 CHECK_GT(buf_len, 0); | 104 CHECK_GT(buf_len, 0); |
| 99 int remaining = sample_->BytesRemaining(); | 105 int remaining = sample_->BytesRemaining(); |
| 100 if (remaining < 1) { | 106 if (remaining < 1) { |
| 101 if (final_read_callback_) | 107 if (old_final_read_callback_ || !final_read_callback_.is_null()) |
| 108 return 0; | |
| 109 old_final_read_callback_ = callback; | |
| 110 return net::ERR_IO_PENDING; | |
| 111 } | |
| 112 int lot = GetRand(1, std::min(remaining, buf_len)); | |
| 113 std::copy(sample_->data(), sample_->data() + lot, buf->data()); | |
| 114 sample_->DidConsume(lot); | |
| 115 if (GetRand(0, 1)) { | |
| 116 return lot; | |
| 117 } | |
| 118 MessageLoop::current()->PostTask(FROM_HERE, | |
| 119 method_factory_.NewRunnableMethod( | |
| 120 &TestingTransportSocket::DoOldReadCallback, callback, lot)); | |
| 121 return net::ERR_IO_PENDING; | |
| 122 } | |
| 123 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 124 const net::CompletionCallback& callback) { | |
| 125 CHECK_GT(buf_len, 0); | |
| 126 int remaining = sample_->BytesRemaining(); | |
| 127 if (remaining < 1) { | |
| 128 if (old_final_read_callback_ || !final_read_callback_.is_null()) | |
| 102 return 0; | 129 return 0; |
| 103 final_read_callback_ = callback; | 130 final_read_callback_ = callback; |
| 104 return net::ERR_IO_PENDING; | 131 return net::ERR_IO_PENDING; |
| 105 } | 132 } |
| 106 int lot = GetRand(1, std::min(remaining, buf_len)); | 133 int lot = GetRand(1, std::min(remaining, buf_len)); |
| 107 std::copy(sample_->data(), sample_->data() + lot, buf->data()); | 134 std::copy(sample_->data(), sample_->data() + lot, buf->data()); |
| 108 sample_->DidConsume(lot); | 135 sample_->DidConsume(lot); |
| 109 if (GetRand(0, 1)) { | 136 if (GetRand(0, 1)) { |
| 110 return lot; | 137 return lot; |
| 111 } | 138 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 137 virtual bool SetReceiveBufferSize(int32 size) { | 164 virtual bool SetReceiveBufferSize(int32 size) { |
| 138 return true; | 165 return true; |
| 139 } | 166 } |
| 140 | 167 |
| 141 virtual bool SetSendBufferSize(int32 size) { | 168 virtual bool SetSendBufferSize(int32 size) { |
| 142 return true; | 169 return true; |
| 143 } | 170 } |
| 144 | 171 |
| 145 net::DrainableIOBuffer* answer() { return answer_.get(); } | 172 net::DrainableIOBuffer* answer() { return answer_.get(); } |
| 146 | 173 |
| 147 void DoReadCallback(net::OldCompletionCallback* callback, int result) { | 174 void DoOldReadCallback(net::OldCompletionCallback* callback, int result) { |
| 175 if (result == 0 && !is_closed_) { | |
| 176 MessageLoop::current()->PostTask(FROM_HERE, | |
| 177 method_factory_.NewRunnableMethod( | |
| 178 &TestingTransportSocket::DoOldReadCallback, callback, 0)); | |
| 179 } else { | |
| 180 if (callback) | |
| 181 callback->Run(result); | |
| 182 } | |
| 183 } | |
| 184 void DoReadCallback(const net::CompletionCallback& callback, int result) { | |
| 148 if (result == 0 && !is_closed_) { | 185 if (result == 0 && !is_closed_) { |
| 149 MessageLoop::current()->PostTask(FROM_HERE, | 186 MessageLoop::current()->PostTask(FROM_HERE, |
| 150 method_factory_.NewRunnableMethod( | 187 method_factory_.NewRunnableMethod( |
| 151 &TestingTransportSocket::DoReadCallback, callback, 0)); | 188 &TestingTransportSocket::DoReadCallback, callback, 0)); |
| 152 } else { | 189 } else { |
| 153 if (callback) | 190 if (!callback.is_null()) |
| 154 callback->Run(result); | 191 callback.Run(result); |
| 155 } | 192 } |
| 156 } | 193 } |
| 157 | 194 |
| 158 void DoWriteCallback(net::OldCompletionCallback* callback, int result) { | 195 void DoWriteCallback(net::OldCompletionCallback* callback, int result) { |
| 159 if (callback) | 196 if (callback) |
| 160 callback->Run(result); | 197 callback->Run(result); |
| 161 } | 198 } |
| 162 | 199 |
| 163 bool is_closed_; | 200 bool is_closed_; |
| 164 | 201 |
| 165 // Data to return for Read requests. | 202 // Data to return for Read requests. |
| 166 scoped_refptr<net::DrainableIOBuffer> sample_; | 203 scoped_refptr<net::DrainableIOBuffer> sample_; |
| 167 | 204 |
| 168 // Data pushed to us by server socket (using Write calls). | 205 // Data pushed to us by server socket (using Write calls). |
| 169 scoped_refptr<net::DrainableIOBuffer> answer_; | 206 scoped_refptr<net::DrainableIOBuffer> answer_; |
| 170 | 207 |
| 171 // Final read callback to report zero (zero stands for EOF). | 208 // Final read callback to report zero (zero stands for EOF). |
| 172 net::OldCompletionCallback* final_read_callback_; | 209 net::OldCompletionCallback* old_final_read_callback_; |
| 210 net::CompletionCallback final_read_callback_; | |
| 173 | 211 |
| 174 ScopedRunnableMethodFactory<TestingTransportSocket> method_factory_; | 212 ScopedRunnableMethodFactory<TestingTransportSocket> method_factory_; |
|
csilv
2011/12/07 00:03:48
could we update this to a WeakFactory and get rid
James Hawkins
2011/12/07 00:19:37
Prob best done in a followup CL.
| |
| 175 }; | 213 }; |
| 176 | 214 |
| 177 class Validator : public net::WebSocketServerSocket::Delegate { | 215 class Validator : public net::WebSocketServerSocket::Delegate { |
| 178 public: | 216 public: |
| 179 Validator(const std::string& resource, | 217 Validator(const std::string& resource, |
| 180 const std::string& origin, | 218 const std::string& origin, |
| 181 const std::string& host) | 219 const std::string& host) |
| 182 : resource_(resource), origin_(origin), host_(host) { | 220 : resource_(resource), origin_(origin), host_(host) { |
| 183 } | 221 } |
| 184 | 222 |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 MessageLoop::current()->RunAllPending(); | 625 MessageLoop::current()->RunAllPending(); |
| 588 | 626 |
| 589 for (size_t i = kill_list.size(); i--;) | 627 for (size_t i = kill_list.size(); i--;) |
| 590 delete kill_list[i]; | 628 delete kill_list[i]; |
| 591 for (size_t i = tracker_list.size(); i--;) | 629 for (size_t i = tracker_list.size(); i--;) |
| 592 delete tracker_list[i]; | 630 delete tracker_list[i]; |
| 593 MessageLoop::current()->RunAllPending(); | 631 MessageLoop::current()->RunAllPending(); |
| 594 } | 632 } |
| 595 | 633 |
| 596 } // namespace net | 634 } // namespace net |
| OLD | NEW |