summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/socket_stream_host.cc
blob: 2d42089b4335fc30e67bb19c6bddb5b6130c76b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/socket_stream_host.h"

#include "base/logging.h"
#include "content/common/socket_stream.h"
#include "net/socket_stream/socket_stream_job.h"
#include "net/url_request/url_request_context.h"

namespace content {
namespace {

const char* kSocketIdKey = "socketId";

class SocketStreamId : public net::SocketStream::UserData {
 public:
  explicit SocketStreamId(int socket_id) : socket_id_(socket_id) {}
  virtual ~SocketStreamId() {}
  int socket_id() const { return socket_id_; }

 private:
  int socket_id_;
};

}  // namespace

SocketStreamHost::SocketStreamHost(
    net::SocketStream::Delegate* delegate,
    int render_view_id,
    int socket_id)
    : delegate_(delegate),
      render_view_id_(render_view_id),
      socket_id_(socket_id) {
  DCHECK_NE(socket_id_, kNoSocketId);
  VLOG(1) << "SocketStreamHost: render_view_id=" << render_view_id
          << " socket_id=" << socket_id_;
}

/* static */
int SocketStreamHost::SocketIdFromSocketStream(
    const net::SocketStream* socket) {
  net::SocketStream::UserData* d = socket->GetUserData(kSocketIdKey);
  if (d) {
    SocketStreamId* socket_stream_id = static_cast<SocketStreamId*>(d);
    return socket_stream_id->socket_id();
  }
  return kNoSocketId;
}

SocketStreamHost::~SocketStreamHost() {
  VLOG(1) << "SocketStreamHost destructed socket_id=" << socket_id_;
  socket_->DetachDelegate();
}

void SocketStreamHost::Connect(const GURL& url,
                               net::URLRequestContext* request_context) {
  VLOG(1) << "SocketStreamHost::Connect url=" << url;
  socket_ = net::SocketStreamJob::CreateSocketStreamJob(
      url, delegate_, request_context->transport_security_state(),
      request_context->ssl_config_service());
  socket_->set_context(request_context);
  socket_->SetUserData(kSocketIdKey, new SocketStreamId(socket_id_));
  socket_->Connect();
}

bool SocketStreamHost::SendData(const std::vector<char>& data) {
  VLOG(1) << "SocketStreamHost::SendData";
  return socket_.get() && socket_->SendData(&data[0], data.size());
}

void SocketStreamHost::Close() {
  VLOG(1) << "SocketStreamHost::Close";
  if (!socket_.get())
    return;
  socket_->Close();
}

void SocketStreamHost::CancelWithError(int error) {
  VLOG(1) << "SocketStreamHost::CancelWithError: error=" << error;
  if (!socket_.get())
    return;
  socket_->CancelWithError(error);
}

void SocketStreamHost::CancelWithSSLError(const net::SSLInfo& ssl_info) {
  VLOG(1) << "SocketStreamHost::CancelWithSSLError";
  if (!socket_.get())
    return;
  socket_->CancelWithSSLError(ssl_info);
}

void SocketStreamHost::ContinueDespiteError() {
  VLOG(1) << "SocketStreamHost::ContinueDespiteError";
  if (!socket_.get())
    return;
  socket_->ContinueDespiteError();
}

}  // namespace content