summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/socket_stream_host.cc
blob: a6f8ddd3e43b0e75303578d23019799432488aba (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
// Copyright (c) 2010 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 "chrome/browser/renderer_host/socket_stream_host.h"

#include "base/logging.h"
#include "chrome/browser/profile.h"
#include "chrome/common/net/socket_stream.h"
#include "chrome/common/net/url_request_context_getter.h"
#include "chrome/common/render_messages.h"
#include "net/socket_stream/socket_stream_job.h"

static const char* kSocketHostKey = "socketHost";

class SocketStreamInfo : public net::SocketStream::UserData {
 public:
  explicit SocketStreamInfo(SocketStreamHost* host) : host_(host) {}
  virtual ~SocketStreamInfo() {}
  SocketStreamHost* host() const { return host_; }

 private:
  SocketStreamHost* host_;
};

SocketStreamHost::SocketStreamHost(
    net::SocketStream::Delegate* delegate,
    ResourceDispatcherHost::Receiver* receiver,
    int socket_id)
    : delegate_(delegate),
      receiver_(receiver),
      socket_id_(socket_id) {
  DCHECK_NE(socket_id_, chrome_common_net::kNoSocketId);
  VLOG(1) << "SocketStreamHost: socket_id=" << socket_id_;
}

/* static */
SocketStreamHost*
SocketStreamHost::GetSocketStreamHost(net::SocketStream* socket) {
  net::SocketStream::UserData* d = socket->GetUserData(kSocketHostKey);
  if (d) {
    SocketStreamInfo* info = static_cast<SocketStreamInfo*>(d);
    return info->host();
  }
  return NULL;
}

SocketStreamHost::~SocketStreamHost() {
  VLOG(1) << "SocketStreamHost destructed socket_id=" << socket_id_;
  if (!receiver_->Send(new ViewMsg_SocketStream_Closed(socket_id_)))
    LOG(ERROR) << "ViewMsg_SocketStream_Closed failed.";
  socket_->DetachDelegate();
}

void SocketStreamHost::Connect(const GURL& url) {
  VLOG(1) << "SocketStreamHost::Connect url=" << url;
  socket_ = net::SocketStreamJob::CreateSocketStreamJob(url, delegate_);
  URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext();
  if (context_getter)
      socket_->set_context(context_getter->GetURLRequestContext());
  socket_->SetUserData(kSocketHostKey, new SocketStreamInfo(this));
  socket_->Connect();
}

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

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

bool SocketStreamHost::Connected(int max_pending_send_allowed) {
  return receiver_->Send(new ViewMsg_SocketStream_Connected(
      socket_id_, max_pending_send_allowed));
}

bool SocketStreamHost::SentData(int amount_sent) {
  return receiver_->Send(new ViewMsg_SocketStream_SentData(
      socket_id_, amount_sent));
}

bool SocketStreamHost::ReceivedData(const char* data, int len) {
  return receiver_->Send(new ViewMsg_SocketStream_ReceivedData(
      socket_id_, std::vector<char>(data, data + len)));
}