Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / socket_stream_host.cc
blob364b8d09ddecd488570b4f96066d8b61ced5182a
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.
5 #include "content/browser/renderer_host/socket_stream_host.h"
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"
13 namespace content {
14 namespace {
16 const char* kSocketIdKey = "socketId";
18 class SocketStreamId : public net::SocketStream::UserData {
19 public:
20 explicit SocketStreamId(int socket_id) : socket_id_(socket_id) {}
21 virtual ~SocketStreamId() {}
22 int socket_id() const { return socket_id_; }
24 private:
25 int socket_id_;
28 } // namespace
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 DCHECK_NE(socket_id_, kNoSocketId);
40 VLOG(1) << "SocketStreamHost: render_frame_id=" << render_frame_id
41 << " socket_id=" << socket_id_;
44 /* static */
45 int SocketStreamHost::SocketIdFromSocketStream(
46 const net::SocketStream* socket) {
47 net::SocketStream::UserData* d = socket->GetUserData(kSocketIdKey);
48 if (d) {
49 SocketStreamId* socket_stream_id = static_cast<SocketStreamId*>(d);
50 return socket_stream_id->socket_id();
52 return kNoSocketId;
55 SocketStreamHost::~SocketStreamHost() {
56 VLOG(1) << "SocketStreamHost destructed socket_id=" << socket_id_;
57 job_->DetachContext();
58 job_->DetachDelegate();
61 void SocketStreamHost::Connect(const GURL& url,
62 net::URLRequestContext* request_context) {
63 VLOG(1) << "SocketStreamHost::Connect url=" << url;
64 job_ = net::SocketStreamJob::CreateSocketStreamJob(
65 url, delegate_, request_context->transport_security_state(),
66 request_context->ssl_config_service(),
67 request_context,
68 GetContentClient()->browser()->OverrideCookieStoreForRenderProcess(
69 child_id_));
70 job_->SetUserData(kSocketIdKey, new SocketStreamId(socket_id_));
71 job_->Connect();
74 bool SocketStreamHost::SendData(const std::vector<char>& data) {
75 VLOG(1) << "SocketStreamHost::SendData";
76 return job_.get() && job_->SendData(&data[0], data.size());
79 void SocketStreamHost::Close() {
80 VLOG(1) << "SocketStreamHost::Close";
81 if (!job_.get())
82 return;
83 job_->Close();
86 void SocketStreamHost::CancelWithError(int error) {
87 VLOG(1) << "SocketStreamHost::CancelWithError: error=" << error;
88 if (!job_.get())
89 return;
90 job_->CancelWithError(error);
93 void SocketStreamHost::CancelWithSSLError(const net::SSLInfo& ssl_info) {
94 VLOG(1) << "SocketStreamHost::CancelWithSSLError";
95 if (!job_.get())
96 return;
97 job_->CancelWithSSLError(ssl_info);
100 void SocketStreamHost::ContinueDespiteError() {
101 VLOG(1) << "SocketStreamHost::ContinueDespiteError";
102 if (!job_.get())
103 return;
104 job_->ContinueDespiteError();
107 } // namespace content