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 "content/browser/devtools/tethering_handler.h" | 5 #include "content/browser/devtools/tethering_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 net::StreamSocket* client_socket) | 45 net::StreamSocket* client_socket) |
46 : client_socket_(client_socket), | 46 : client_socket_(client_socket), |
47 delegate_(delegate), | 47 delegate_(delegate), |
48 wire_buffer_size_(0), | 48 wire_buffer_size_(0), |
49 pending_destruction_(false) { | 49 pending_destruction_(false) { |
50 } | 50 } |
51 | 51 |
52 std::string Init() { | 52 std::string Init() { |
53 std::string channel_name; | 53 std::string channel_name; |
54 server_socket_ = delegate_->CreateSocketForTethering(this, &channel_name); | 54 server_socket_ = delegate_->CreateSocketForTethering(this, &channel_name); |
55 if (!server_socket_ || channel_name.empty()) | 55 if (!server_socket_.get() || channel_name.empty()) |
56 SelfDestruct(); | 56 SelfDestruct(); |
57 return channel_name; | 57 return channel_name; |
58 } | 58 } |
59 | 59 |
60 virtual ~SocketPump() { } | 60 virtual ~SocketPump() { } |
61 | 61 |
62 private: | 62 private: |
63 virtual void DidAccept(net::StreamListenSocket* server, | 63 virtual void DidAccept(net::StreamListenSocket* server, |
64 net::StreamListenSocket* socket) OVERRIDE { | 64 net::StreamListenSocket* socket) OVERRIDE { |
65 if (accepted_socket_) | 65 if (accepted_socket_.get()) |
66 return; | 66 return; |
67 | 67 |
68 buffer_ = new net::IOBuffer(kBufferSize); | 68 buffer_ = new net::IOBuffer(kBufferSize); |
69 wire_buffer_ = new net::GrowableIOBuffer(); | 69 wire_buffer_ = new net::GrowableIOBuffer(); |
70 wire_buffer_->SetCapacity(kBufferSize); | 70 wire_buffer_->SetCapacity(kBufferSize); |
71 | 71 |
72 accepted_socket_ = socket; | 72 accepted_socket_ = socket; |
73 int result = client_socket_->Read(buffer_, kBufferSize, | 73 int result = client_socket_->Read( |
74 base::Bind(&SocketPump::OnClientRead, | 74 buffer_.get(), |
75 base::Unretained(this))); | 75 kBufferSize, |
| 76 base::Bind(&SocketPump::OnClientRead, base::Unretained(this))); |
76 if (result != net::ERR_IO_PENDING) | 77 if (result != net::ERR_IO_PENDING) |
77 OnClientRead(result); | 78 OnClientRead(result); |
78 } | 79 } |
79 | 80 |
80 virtual void DidRead(net::StreamListenSocket* socket, | 81 virtual void DidRead(net::StreamListenSocket* socket, |
81 const char* data, | 82 const char* data, |
82 int len) OVERRIDE { | 83 int len) OVERRIDE { |
83 int old_size = wire_buffer_size_; | 84 int old_size = wire_buffer_size_; |
84 wire_buffer_size_ += len; | 85 wire_buffer_size_ += len; |
85 while (wire_buffer_->capacity() < wire_buffer_size_) | 86 while (wire_buffer_->capacity() < wire_buffer_size_) |
86 wire_buffer_->SetCapacity(wire_buffer_->capacity() * 2); | 87 wire_buffer_->SetCapacity(wire_buffer_->capacity() * 2); |
87 memcpy(wire_buffer_->StartOfBuffer() + old_size, data, len); | 88 memcpy(wire_buffer_->StartOfBuffer() + old_size, data, len); |
88 if (old_size != wire_buffer_->offset()) | 89 if (old_size != wire_buffer_->offset()) |
89 return; | 90 return; |
90 OnClientWrite(0); | 91 OnClientWrite(0); |
91 } | 92 } |
92 | 93 |
93 virtual void DidClose(net::StreamListenSocket* socket) OVERRIDE { | 94 virtual void DidClose(net::StreamListenSocket* socket) OVERRIDE { |
94 SelfDestruct(); | 95 SelfDestruct(); |
95 } | 96 } |
96 | 97 |
97 void OnClientRead(int result) { | 98 void OnClientRead(int result) { |
98 if (result <= 0) { | 99 if (result <= 0) { |
99 SelfDestruct(); | 100 SelfDestruct(); |
100 return; | 101 return; |
101 } | 102 } |
102 | 103 |
103 accepted_socket_->Send(buffer_->data(), result); | 104 accepted_socket_->Send(buffer_->data(), result); |
104 result = client_socket_->Read(buffer_, kBufferSize, | 105 result = client_socket_->Read( |
105 base::Bind(&SocketPump::OnClientRead, | 106 buffer_.get(), |
106 base::Unretained(this))); | 107 kBufferSize, |
| 108 base::Bind(&SocketPump::OnClientRead, base::Unretained(this))); |
107 if (result != net::ERR_IO_PENDING) | 109 if (result != net::ERR_IO_PENDING) |
108 OnClientRead(result); | 110 OnClientRead(result); |
109 } | 111 } |
110 | 112 |
111 void OnClientWrite(int result) { | 113 void OnClientWrite(int result) { |
112 if (result < 0) | 114 if (result < 0) |
113 SelfDestruct(); | 115 SelfDestruct(); |
114 | 116 |
115 wire_buffer_->set_offset(wire_buffer_->offset() + result); | 117 wire_buffer_->set_offset(wire_buffer_->offset() + result); |
116 | 118 |
117 int remaining = wire_buffer_size_ - wire_buffer_->offset(); | 119 int remaining = wire_buffer_size_ - wire_buffer_->offset(); |
118 if (remaining == 0) { | 120 if (remaining == 0) { |
119 if (pending_destruction_) | 121 if (pending_destruction_) |
120 SelfDestruct(); | 122 SelfDestruct(); |
121 return; | 123 return; |
122 } | 124 } |
123 | 125 |
124 | 126 |
125 if (remaining > kBufferSize) | 127 if (remaining > kBufferSize) |
126 remaining = kBufferSize; | 128 remaining = kBufferSize; |
127 | 129 |
128 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(remaining); | 130 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(remaining); |
129 memcpy(buffer->data(), wire_buffer_->data(), remaining); | 131 memcpy(buffer->data(), wire_buffer_->data(), remaining); |
130 result = client_socket_->Write( | 132 result = client_socket_->Write( |
131 buffer, remaining, base::Bind(&SocketPump::OnClientWrite, | 133 buffer.get(), |
132 base::Unretained(this))); | 134 remaining, |
| 135 base::Bind(&SocketPump::OnClientWrite, base::Unretained(this))); |
133 | 136 |
134 // Shrink buffer | 137 // Shrink buffer |
135 int offset = wire_buffer_->offset(); | 138 int offset = wire_buffer_->offset(); |
136 if (offset > kBufferSize) { | 139 if (offset > kBufferSize) { |
137 memcpy(wire_buffer_->StartOfBuffer(), wire_buffer_->data(), | 140 memcpy(wire_buffer_->StartOfBuffer(), wire_buffer_->data(), |
138 wire_buffer_size_ - offset); | 141 wire_buffer_size_ - offset); |
139 wire_buffer_size_ -= offset; | 142 wire_buffer_size_ -= offset; |
140 wire_buffer_->set_offset(0); | 143 wire_buffer_->set_offset(0); |
141 } | 144 } |
142 | 145 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 BoundSockets::iterator it = bound_sockets_.find(port); | 300 BoundSockets::iterator it = bound_sockets_.find(port); |
298 if (it == bound_sockets_.end()) | 301 if (it == bound_sockets_.end()) |
299 return command->InternalErrorResponse("Port is not bound"); | 302 return command->InternalErrorResponse("Port is not bound"); |
300 | 303 |
301 delete it->second; | 304 delete it->second; |
302 bound_sockets_.erase(it); | 305 bound_sockets_.erase(it); |
303 return command->SuccessResponse(NULL); | 306 return command->SuccessResponse(NULL); |
304 } | 307 } |
305 | 308 |
306 } // namespace content | 309 } // namespace content |
OLD | NEW |