| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "components/cronet/ios/cronet_c_for_grpc.h" |
| 6 |
| 7 #include <stdbool.h> |
| 8 |
| 9 #include <memory> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/bind.h" |
| 14 #include "base/location.h" |
| 15 #include "base/logging.h" |
| 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" |
| 21 #include "components/cronet/ios/cronet_bidirectional_stream.h" |
| 22 #include "components/cronet/ios/cronet_environment.h" |
| 23 #include "net/base/io_buffer.h" |
| 24 #include "net/base/net_errors.h" |
| 25 #include "net/base/request_priority.h" |
| 26 #include "net/http/bidirectional_stream.h" |
| 27 #include "net/http/bidirectional_stream_request_info.h" |
| 28 #include "net/http/http_network_session.h" |
| 29 #include "net/http/http_response_headers.h" |
| 30 #include "net/http/http_status_code.h" |
| 31 #include "net/http/http_transaction_factory.h" |
| 32 #include "net/http/http_util.h" |
| 33 #include "net/spdy/spdy_header_block.h" |
| 34 #include "net/ssl/ssl_info.h" |
| 35 #include "net/url_request/http_user_agent_settings.h" |
| 36 #include "net/url_request/url_request_context.h" |
| 37 #include "url/gurl.h" |
| 38 |
| 39 namespace { |
| 40 |
| 41 class HeadersArray : public cronet_bidirectional_stream_header_array { |
| 42 public: |
| 43 HeadersArray(const net::SpdyHeaderBlock& header_block); |
| 44 ~HeadersArray(); |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(HeadersArray); |
| 48 base::StringPairs headers_strings_; |
| 49 }; |
| 50 |
| 51 HeadersArray::HeadersArray(const net::SpdyHeaderBlock& header_block) |
| 52 : headers_strings_(header_block.size()) { |
| 53 // Count and headers are inherited from parent structure. |
| 54 count = capacity = header_block.size(); |
| 55 headers = new cronet_bidirectional_stream_header[count]; |
| 56 size_t i = 0; |
| 57 // Copy headers into |headers_strings_| because string pieces are not |
| 58 // '\0'-terminated. |
| 59 for (const auto& it : header_block) { |
| 60 headers_strings_[i].first = it.first.as_string(); |
| 61 headers_strings_[i].second = it.second.as_string(); |
| 62 headers[i].key = headers_strings_[i].first.c_str(); |
| 63 headers[i].value = headers_strings_[i].second.c_str(); |
| 64 ++i; |
| 65 } |
| 66 } |
| 67 |
| 68 HeadersArray::~HeadersArray() { |
| 69 delete[] headers; |
| 70 } |
| 71 |
| 72 class CronetBidirectionalStreamAdapter |
| 73 : public cronet::CronetBidirectionalStream::Delegate { |
| 74 public: |
| 75 CronetBidirectionalStreamAdapter( |
| 76 cronet_engine* engine, |
| 77 void* annotation, |
| 78 cronet_bidirectional_stream_callback* callback); |
| 79 |
| 80 virtual ~CronetBidirectionalStreamAdapter(); |
| 81 |
| 82 void OnStreamReady() override; |
| 83 |
| 84 void OnHeadersReceived(const net::SpdyHeaderBlock& headers_block, |
| 85 const char* negotiated_protocol) override; |
| 86 |
| 87 void OnDataRead(char* data, int size) override; |
| 88 |
| 89 void OnDataSent(const char* data) override; |
| 90 |
| 91 void OnTrailersReceived(const net::SpdyHeaderBlock& trailers_block) override; |
| 92 |
| 93 void OnSucceeded() override; |
| 94 |
| 95 void OnFailed(int error) override; |
| 96 |
| 97 void OnCanceled() override; |
| 98 |
| 99 cronet_bidirectional_stream* c_stream() const { return c_stream_.get(); } |
| 100 |
| 101 static cronet::CronetBidirectionalStream* GetCronetStream( |
| 102 cronet_bidirectional_stream* stream); |
| 103 |
| 104 static void DestroyAdapterForStream(cronet_bidirectional_stream* stream); |
| 105 |
| 106 private: |
| 107 void DestroyOnNetworkThread(); |
| 108 |
| 109 // None of these objects are owned by |this|. |
| 110 cronet::CronetEnvironment* cronet_environment_; |
| 111 cronet::CronetBidirectionalStream* cronet_bidirectional_stream_; |
| 112 // C side |
| 113 std::unique_ptr<cronet_bidirectional_stream> c_stream_; |
| 114 cronet_bidirectional_stream_callback* c_callback_; |
| 115 }; |
| 116 |
| 117 CronetBidirectionalStreamAdapter::CronetBidirectionalStreamAdapter( |
| 118 cronet_engine* engine, |
| 119 void* annotation, |
| 120 cronet_bidirectional_stream_callback* callback) |
| 121 : cronet_environment_( |
| 122 reinterpret_cast<cronet::CronetEnvironment*>(engine->obj)), |
| 123 c_stream_(base::MakeUnique<cronet_bidirectional_stream>()), |
| 124 c_callback_(callback) { |
| 125 DCHECK(cronet_environment_); |
| 126 cronet_bidirectional_stream_ = |
| 127 new cronet::CronetBidirectionalStream(cronet_environment_, this); |
| 128 c_stream_->obj = this; |
| 129 c_stream_->annotation = annotation; |
| 130 } |
| 131 |
| 132 CronetBidirectionalStreamAdapter::~CronetBidirectionalStreamAdapter() {} |
| 133 |
| 134 void CronetBidirectionalStreamAdapter::OnStreamReady() { |
| 135 DCHECK(c_callback_->on_stream_ready); |
| 136 c_callback_->on_stream_ready(c_stream()); |
| 137 } |
| 138 |
| 139 void CronetBidirectionalStreamAdapter::OnHeadersReceived( |
| 140 const net::SpdyHeaderBlock& headers_block, |
| 141 const char* negotiated_protocol) { |
| 142 DCHECK(c_callback_->on_response_headers_received); |
| 143 HeadersArray response_headers(headers_block); |
| 144 c_callback_->on_response_headers_received(c_stream(), &response_headers, |
| 145 negotiated_protocol); |
| 146 } |
| 147 |
| 148 void CronetBidirectionalStreamAdapter::OnDataRead(char* data, int size) { |
| 149 DCHECK(c_callback_->on_read_completed); |
| 150 c_callback_->on_read_completed(c_stream(), data, size); |
| 151 } |
| 152 |
| 153 void CronetBidirectionalStreamAdapter::OnDataSent(const char* data) { |
| 154 DCHECK(c_callback_->on_write_completed); |
| 155 c_callback_->on_write_completed(c_stream(), data); |
| 156 } |
| 157 |
| 158 void CronetBidirectionalStreamAdapter::OnTrailersReceived( |
| 159 const net::SpdyHeaderBlock& trailers_block) { |
| 160 DCHECK(c_callback_->on_response_trailers_received); |
| 161 HeadersArray response_trailers(trailers_block); |
| 162 c_callback_->on_response_trailers_received(c_stream(), &response_trailers); |
| 163 } |
| 164 |
| 165 void CronetBidirectionalStreamAdapter::OnSucceeded() { |
| 166 DCHECK(c_callback_->on_succeded); |
| 167 c_callback_->on_succeded(c_stream()); |
| 168 } |
| 169 |
| 170 void CronetBidirectionalStreamAdapter::OnFailed(int error) { |
| 171 DCHECK(c_callback_->on_failed); |
| 172 c_callback_->on_failed(c_stream(), error); |
| 173 } |
| 174 |
| 175 void CronetBidirectionalStreamAdapter::OnCanceled() { |
| 176 DCHECK(c_callback_->on_canceled); |
| 177 c_callback_->on_canceled(c_stream()); |
| 178 } |
| 179 |
| 180 cronet::CronetBidirectionalStream* |
| 181 CronetBidirectionalStreamAdapter::GetCronetStream( |
| 182 cronet_bidirectional_stream* stream) { |
| 183 DCHECK(stream); |
| 184 CronetBidirectionalStreamAdapter* adapter = |
| 185 static_cast<CronetBidirectionalStreamAdapter*>(stream->obj); |
| 186 DCHECK(adapter->c_stream() == stream); |
| 187 DCHECK(adapter->cronet_bidirectional_stream_); |
| 188 return adapter->cronet_bidirectional_stream_; |
| 189 } |
| 190 |
| 191 void CronetBidirectionalStreamAdapter::DestroyAdapterForStream( |
| 192 cronet_bidirectional_stream* stream) { |
| 193 DCHECK(stream); |
| 194 CronetBidirectionalStreamAdapter* adapter = |
| 195 static_cast<CronetBidirectionalStreamAdapter*>(stream->obj); |
| 196 DCHECK(adapter->c_stream() == stream); |
| 197 // Destroy could be called from any thread, including network thread (if |
| 198 // posting task to executor throws an exception), but is posted, so |this| |
| 199 // is valid until calling task is complete. |
| 200 adapter->cronet_bidirectional_stream_->Destroy(); |
| 201 adapter->cronet_environment_->PostToNetworkThread( |
| 202 FROM_HERE, |
| 203 base::Bind(&CronetBidirectionalStreamAdapter::DestroyOnNetworkThread, |
| 204 base::Unretained(adapter))); |
| 205 } |
| 206 |
| 207 void CronetBidirectionalStreamAdapter::DestroyOnNetworkThread() { |
| 208 DCHECK(cronet_environment_->IsOnNetworkThread()); |
| 209 delete this; |
| 210 } |
| 211 |
| 212 } // namespace |
| 213 |
| 214 cronet_bidirectional_stream* cronet_bidirectional_stream_create( |
| 215 cronet_engine* engine, |
| 216 void* annotation, |
| 217 cronet_bidirectional_stream_callback* callback) { |
| 218 // Allocate new C++ adapter that will invoke |callback|. |
| 219 CronetBidirectionalStreamAdapter* stream_adapter = |
| 220 new CronetBidirectionalStreamAdapter(engine, annotation, callback); |
| 221 return stream_adapter->c_stream(); |
| 222 } |
| 223 |
| 224 int cronet_bidirectional_stream_destroy(cronet_bidirectional_stream* stream) { |
| 225 CronetBidirectionalStreamAdapter::DestroyAdapterForStream(stream); |
| 226 return 1; |
| 227 } |
| 228 |
| 229 void cronet_bidirectional_stream_disable_auto_flush( |
| 230 cronet_bidirectional_stream* stream, |
| 231 bool disable_auto_flush) { |
| 232 CronetBidirectionalStreamAdapter::GetCronetStream(stream)->disable_auto_flush( |
| 233 disable_auto_flush); |
| 234 } |
| 235 |
| 236 void cronet_bidirectional_stream_delay_request_headers_until_flush( |
| 237 cronet_bidirectional_stream* stream, |
| 238 bool delay_headers_until_flush) { |
| 239 CronetBidirectionalStreamAdapter::GetCronetStream(stream) |
| 240 ->delay_headers_until_flush(delay_headers_until_flush); |
| 241 } |
| 242 |
| 243 int cronet_bidirectional_stream_start( |
| 244 cronet_bidirectional_stream* stream, |
| 245 const char* url, |
| 246 int priority, |
| 247 const char* method, |
| 248 const cronet_bidirectional_stream_header_array* headers, |
| 249 bool end_of_stream) { |
| 250 cronet::CronetBidirectionalStream* cronet_stream = |
| 251 CronetBidirectionalStreamAdapter::GetCronetStream(stream); |
| 252 net::HttpRequestHeaders request_headers; |
| 253 if (headers) { |
| 254 for (size_t i = 0; i < headers->count; ++i) { |
| 255 std::string name(headers->headers[i].key); |
| 256 std::string value(headers->headers[i].value); |
| 257 if (!net::HttpUtil::IsValidHeaderName(name) || |
| 258 !net::HttpUtil::IsValidHeaderValue(value)) { |
| 259 DLOG(ERROR) << "Invalid Header " << name << "=" << value; |
| 260 return i + 1; |
| 261 } |
| 262 request_headers.SetHeader(name, value); |
| 263 } |
| 264 } |
| 265 return cronet_stream->Start(url, priority, method, request_headers, |
| 266 end_of_stream); |
| 267 } |
| 268 |
| 269 int cronet_bidirectional_stream_read(cronet_bidirectional_stream* stream, |
| 270 char* buffer, |
| 271 int capacity) { |
| 272 return CronetBidirectionalStreamAdapter::GetCronetStream(stream)->ReadData( |
| 273 buffer, capacity); |
| 274 } |
| 275 |
| 276 int cronet_bidirectional_stream_write(cronet_bidirectional_stream* stream, |
| 277 const char* buffer, |
| 278 int count, |
| 279 bool end_of_stream) { |
| 280 return CronetBidirectionalStreamAdapter::GetCronetStream(stream)->WriteData( |
| 281 buffer, count, end_of_stream); |
| 282 } |
| 283 |
| 284 void cronet_bidirectional_stream_flush(cronet_bidirectional_stream* stream) { |
| 285 return CronetBidirectionalStreamAdapter::GetCronetStream(stream)->Flush(); |
| 286 } |
| 287 |
| 288 void cronet_bidirectional_stream_cancel(cronet_bidirectional_stream* stream) { |
| 289 CronetBidirectionalStreamAdapter::GetCronetStream(stream)->Cancel(); |
| 290 } |
| OLD | NEW |