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