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 3 // found in the LICENSE file. 4 5 #include "content/browser/renderer_host/socket_stream_host.h" 6 7 #include "base/logging.h" 8 #include "content/common/socket_stream.h" 9 #include "net/socket_stream/socket_stream_job.h" 10 #include "net/url_request/url_request_context.h" 11 12 namespace content { 13 namespace { 14 15 const char* kSocketIdKey = "socketId"; 16 17 class SocketStreamId : public net::SocketStream::UserData { 18 public: 19 explicit SocketStreamId(int socket_id) : socket_id_(socket_id) {} 20 virtual ~SocketStreamId() {} 21 int socket_id() const { return socket_id_; } 22 23 private: 24 int socket_id_; 25 }; 26 27 } // namespace 28 29 SocketStreamHost::SocketStreamHost( 30 net::SocketStream::Delegate* delegate, 31 int render_view_id, 32 int socket_id) 33 : delegate_(delegate), 34 render_view_id_(render_view_id), 35 socket_id_(socket_id) { 36 DCHECK_NE(socket_id_, kNoSocketId); 37 VLOG(1) << "SocketStreamHost: render_view_id=" << render_view_id 38 << " socket_id=" << socket_id_; 39 } 40 41 /* static */ 42 int SocketStreamHost::SocketIdFromSocketStream( 43 const net::SocketStream* socket) { 44 net::SocketStream::UserData* d = socket->GetUserData(kSocketIdKey); 45 if (d) { 46 SocketStreamId* socket_stream_id = static_cast<SocketStreamId*>(d); 47 return socket_stream_id->socket_id(); 48 } 49 return kNoSocketId; 50 } 51 52 SocketStreamHost::~SocketStreamHost() { 53 VLOG(1) << "SocketStreamHost destructed socket_id=" << socket_id_; 54 job_->set_context(NULL); 55 job_->DetachDelegate(); 56 } 57 58 void SocketStreamHost::Connect(const GURL& url, 59 net::URLRequestContext* request_context) { 60 VLOG(1) << "SocketStreamHost::Connect url=" << url; 61 job_ = net::SocketStreamJob::CreateSocketStreamJob( 62 url, delegate_, request_context->transport_security_state(), 63 request_context->ssl_config_service()); 64 job_->set_context(request_context); 65 job_->SetUserData(kSocketIdKey, new SocketStreamId(socket_id_)); 66 job_->Connect(); 67 } 68 69 bool SocketStreamHost::SendData(const std::vector<char>& data) { 70 VLOG(1) << "SocketStreamHost::SendData"; 71 return job_.get() && job_->SendData(&data[0], data.size()); 72 } 73 74 void SocketStreamHost::Close() { 75 VLOG(1) << "SocketStreamHost::Close"; 76 if (!job_.get()) 77 return; 78 job_->Close(); 79 } 80 81 void SocketStreamHost::CancelWithError(int error) { 82 VLOG(1) << "SocketStreamHost::CancelWithError: error=" << error; 83 if (!job_.get()) 84 return; 85 job_->CancelWithError(error); 86 } 87 88 void SocketStreamHost::CancelWithSSLError(const net::SSLInfo& ssl_info) { 89 VLOG(1) << "SocketStreamHost::CancelWithSSLError"; 90 if (!job_.get()) 91 return; 92 job_->CancelWithSSLError(ssl_info); 93 } 94 95 void SocketStreamHost::ContinueDespiteError() { 96 VLOG(1) << "SocketStreamHost::ContinueDespiteError"; 97 if (!job_.get()) 98 return; 99 job_->ContinueDespiteError(); 100 } 101 102 } // namespace content 103