summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/web_socket_proxy_private/web_socket_proxy_private_api.cc
blob: 91483c502c010680e24c4fcfdfd77983c4742d24 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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 "chrome/browser/extensions/api/web_socket_proxy_private/web_socket_proxy_private_api.h"

#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/internal_auth.h"
#include "chrome/browser/io_thread.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "net/base/escape.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/base/net_util.h"
#include "net/dns/single_request_host_resolver.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/web_socket_proxy_controller.h"
#endif

namespace extensions {

WebSocketProxyPrivate::WebSocketProxyPrivate()
    : port_(-1),
      listening_port_(-1),
      do_tls_(false),
      is_finalized_(false) {
}

WebSocketProxyPrivate::~WebSocketProxyPrivate() {
}

void WebSocketProxyPrivate::Finalize() {
  CustomFinalize();

  if (is_finalized_)
    return;
  is_finalized_ = true;
  SendResponse(listening_port_ > 0);
  Release();
}

void WebSocketProxyPrivate::Observe(
    int type, const content::NotificationSource& source,
    const content::NotificationDetails& details) {
#if defined(OS_CHROMEOS)
  DCHECK_EQ(chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, type);
#else
  NOTREACHED();
#endif

  timer_.Stop();  // Cancel timeout timer.
  ResolveHost();
}

void WebSocketProxyPrivate::ResolveHost() {
#if defined(OS_CHROMEOS)
  IOThread* io_thread = g_browser_process->io_thread();
  content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
      base::Bind(&WebSocketProxyPrivate::ResolveHostIOPart, this, io_thread));
#endif
}

void WebSocketProxyPrivate::ResolveHostIOPart(IOThread* io_thread) {
#if defined(OS_CHROMEOS)
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
  DCHECK(resolver_ == NULL);
  if (io_thread && io_thread->globals()) {
    net::HostResolver* host_resolver =
        io_thread->globals()->host_resolver.get();
    if (host_resolver) {
      resolver_.reset(new net::SingleRequestHostResolver(host_resolver));
      net::HostResolver::RequestInfo info(net::HostPortPair(
          hostname_, port_));
      int result = resolver_->Resolve(info, &addr_,
          base::Bind(&WebSocketProxyPrivate::OnHostResolution, this),
          net::BoundNetLog());
      if (result != net::ERR_IO_PENDING)
        OnHostResolution(result);
      return;
    }
  }
  NOTREACHED();
  OnHostResolution(net::ERR_UNEXPECTED);
#endif
}

bool WebSocketProxyPrivate::RunImpl() {
  AddRef();
  SetResult(Value::CreateStringValue(std::string()));

#if defined(OS_CHROMEOS)
  bool delay_response = false;

  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &hostname_));
  EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &port_));

  listening_port_ = chromeos::WebSocketProxyController::GetPort();
  if (listening_port_ < 1) {
    delay_response = true;
    registrar_.Add(
        this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED,
        content::NotificationService::AllSources());
  }
  map_["hostname"] = hostname_;
  map_["port"] = base::IntToString(port_);
  map_["extension_id"] = extension_id();

  if (delay_response) {
    const int kTimeout = 12;
    timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeout),
        this, &WebSocketProxyPrivate::ResolveHost);
  } else {
    ResolveHost();
  }
#else
  Finalize();
#endif

  return true;
}

void WebSocketProxyPrivate::OnHostResolution(int result) {
#if defined(OS_CHROMEOS)
  if (result == 0 && !addr_.empty()) {
    std::string ip = addr_.front().ToStringWithoutPort();
    if (!ip.empty() && ip.find(':') != std::string::npos && ip[0] != '[')
      ip = '[' + ip + ']';
    map_["addr"] = ip;
  }
  content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
      base::Bind(&WebSocketProxyPrivate::Finalize, this));
#endif
}

WebSocketProxyPrivateGetURLForTCPFunction::
    WebSocketProxyPrivateGetURLForTCPFunction() {
}

WebSocketProxyPrivateGetURLForTCPFunction::
    ~WebSocketProxyPrivateGetURLForTCPFunction() {
}

void WebSocketProxyPrivateGetURLForTCPFunction::CustomFinalize() {
#if defined(OS_CHROMEOS)
  std::string passport = chrome::InternalAuthGeneration::GeneratePassport(
      "web_socket_proxy", map_);
  std::string query = std::string("hostname=") +
      net::EscapeQueryParamValue(hostname_, false) + "&port=" + map_["port"] +
      "&tls=" + map_["tls"] + "&passport=" +
      net::EscapeQueryParamValue(passport, false);
  if (ContainsKey(map_, "addr"))
    query += std::string("&addr=") + map_["addr"];

  if (listening_port_ < 1)
    listening_port_ = chromeos::WebSocketProxyController::GetPort();
  StringValue* url = Value::CreateStringValue(std::string(
      "ws://127.0.0.1:" + base::IntToString(listening_port_) +
      "/tcpproxy?" + query));
  SetResult(url);
#endif
}

bool WebSocketProxyPrivateGetURLForTCPFunction::RunImpl() {
#if defined(OS_CHROMEOS)
  DictionaryValue* qualification = NULL;
  if (args_->GetDictionary(2, &qualification)) {
    const char kTlsOption[] = "tls";
    if (qualification->HasKey(kTlsOption)) {
      EXTENSION_FUNCTION_VALIDATE(qualification->GetBoolean(
          kTlsOption, &do_tls_));
    }
  }
  map_["tls"] = do_tls_ ? "true" : "false";
#endif

  return WebSocketProxyPrivate::RunImpl();
}

WebSocketProxyPrivateGetPassportForTCPFunction::
    WebSocketProxyPrivateGetPassportForTCPFunction() {
  // This obsolete API uses fixed port to listen websocket connections.
  listening_port_ = 10101;
}

WebSocketProxyPrivateGetPassportForTCPFunction::
    ~WebSocketProxyPrivateGetPassportForTCPFunction() {
}

void WebSocketProxyPrivateGetPassportForTCPFunction::CustomFinalize() {
#if defined(OS_CHROMEOS)
  std::string passport = chrome::InternalAuthGeneration::GeneratePassport(
      "web_socket_proxy", map_) + std::string(":");
  if (ContainsKey(map_, "addr"))
    passport += map_["addr"];
  SetResult(Value::CreateStringValue(passport));
#endif
}

}  // namespace extensions