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