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