| 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 "chrome/browser/extensions/extension_web_socket_proxy_private_api.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/browser/internal_auth.h" | |
| 13 #include "chrome/browser/io_thread.h" | |
| 14 #include "chrome/common/chrome_notification_types.h" | |
| 15 #include "chrome/common/extensions/extension.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/notification_service.h" | |
| 18 #include "content/public/browser/notification_details.h" | |
| 19 #include "net/base/escape.h" | |
| 20 #include "net/base/net_errors.h" | |
| 21 #include "net/base/net_log.h" | |
| 22 #include "net/base/net_util.h" | |
| 23 #include "net/base/single_request_host_resolver.h" | |
| 24 | |
| 25 #if defined(OS_CHROMEOS) | |
| 26 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" | |
| 27 #endif | |
| 28 | |
| 29 WebSocketProxyPrivate::WebSocketProxyPrivate() | |
| 30 : port_(-1), | |
| 31 listening_port_(-1), | |
| 32 do_tls_(false), | |
| 33 is_finalized_(false) { | |
| 34 } | |
| 35 | |
| 36 WebSocketProxyPrivate::~WebSocketProxyPrivate() { | |
| 37 } | |
| 38 | |
| 39 void WebSocketProxyPrivate::Finalize() { | |
| 40 CustomFinalize(); | |
| 41 | |
| 42 if (is_finalized_) | |
| 43 return; | |
| 44 is_finalized_ = true; | |
| 45 SendResponse(listening_port_ > 0); | |
| 46 Release(); | |
| 47 } | |
| 48 | |
| 49 void WebSocketProxyPrivate::Observe( | |
| 50 int type, const content::NotificationSource& source, | |
| 51 const content::NotificationDetails& details) { | |
| 52 #if defined(OS_CHROMEOS) | |
| 53 DCHECK_EQ(chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, type); | |
| 54 #else | |
| 55 NOTREACHED(); | |
| 56 #endif | |
| 57 | |
| 58 timer_.Stop(); // Cancel timeout timer. | |
| 59 ResolveHost(); | |
| 60 } | |
| 61 | |
| 62 void WebSocketProxyPrivate::ResolveHost() { | |
| 63 #if defined(OS_CHROMEOS) | |
| 64 IOThread* io_thread = g_browser_process->io_thread(); | |
| 65 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
| 66 base::Bind(&WebSocketProxyPrivate::ResolveHostIOPart, this, io_thread)); | |
| 67 #endif | |
| 68 } | |
| 69 | |
| 70 void WebSocketProxyPrivate::ResolveHostIOPart(IOThread* io_thread) { | |
| 71 #if defined(OS_CHROMEOS) | |
| 72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 73 DCHECK(resolver_ == NULL); | |
| 74 if (io_thread && io_thread->globals()) { | |
| 75 net::HostResolver* host_resolver = | |
| 76 io_thread->globals()->host_resolver.get(); | |
| 77 if (host_resolver) { | |
| 78 resolver_.reset(new net::SingleRequestHostResolver(host_resolver)); | |
| 79 net::HostResolver::RequestInfo info(net::HostPortPair( | |
| 80 hostname_, port_)); | |
| 81 int result = resolver_->Resolve(info, &addr_, | |
| 82 base::Bind(&WebSocketProxyPrivate::OnHostResolution, this), | |
| 83 net::BoundNetLog()); | |
| 84 if (result != net::ERR_IO_PENDING) | |
| 85 OnHostResolution(result); | |
| 86 return; | |
| 87 } | |
| 88 } | |
| 89 NOTREACHED(); | |
| 90 OnHostResolution(net::ERR_UNEXPECTED); | |
| 91 #endif | |
| 92 } | |
| 93 | |
| 94 bool WebSocketProxyPrivate::RunImpl() { | |
| 95 AddRef(); | |
| 96 result_.reset(Value::CreateStringValue("")); | |
| 97 | |
| 98 #if defined(OS_CHROMEOS) | |
| 99 bool delay_response = false; | |
| 100 | |
| 101 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &hostname_)); | |
| 102 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &port_)); | |
| 103 | |
| 104 listening_port_ = chromeos::WebSocketProxyController::GetPort(); | |
| 105 if (listening_port_ < 1) { | |
| 106 delay_response = true; | |
| 107 registrar_.Add( | |
| 108 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, | |
| 109 content::NotificationService::AllSources()); | |
| 110 } | |
| 111 map_["hostname"] = hostname_; | |
| 112 map_["port"] = base::IntToString(port_); | |
| 113 map_["extension_id"] = extension_id(); | |
| 114 | |
| 115 if (delay_response) { | |
| 116 const int kTimeout = 12; | |
| 117 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeout), | |
| 118 this, &WebSocketProxyPrivate::ResolveHost); | |
| 119 } else { | |
| 120 ResolveHost(); | |
| 121 } | |
| 122 #else | |
| 123 Finalize(); | |
| 124 #endif | |
| 125 | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 void WebSocketProxyPrivate::OnHostResolution(int result) { | |
| 130 #if defined(OS_CHROMEOS) | |
| 131 if (result == 0 && !addr_.empty()) { | |
| 132 std::string ip = addr_.front().ToStringWithoutPort(); | |
| 133 if (!ip.empty() && ip.find(':') != std::string::npos && ip[0] != '[') | |
| 134 ip = '[' + ip + ']'; | |
| 135 map_["addr"] = ip; | |
| 136 } | |
| 137 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 138 base::Bind(&WebSocketProxyPrivate::Finalize, this)); | |
| 139 #endif | |
| 140 } | |
| 141 | |
| 142 WebSocketProxyPrivateGetURLForTCPFunction:: | |
| 143 WebSocketProxyPrivateGetURLForTCPFunction() { | |
| 144 } | |
| 145 | |
| 146 WebSocketProxyPrivateGetURLForTCPFunction:: | |
| 147 ~WebSocketProxyPrivateGetURLForTCPFunction() { | |
| 148 } | |
| 149 | |
| 150 void WebSocketProxyPrivateGetURLForTCPFunction::CustomFinalize() { | |
| 151 #if defined(OS_CHROMEOS) | |
| 152 std::string passport = browser::InternalAuthGeneration::GeneratePassport( | |
| 153 "web_socket_proxy", map_); | |
| 154 std::string query = std::string("hostname=") + | |
| 155 net::EscapeQueryParamValue(hostname_, false) + "&port=" + map_["port"] + | |
| 156 "&tls=" + map_["tls"] + "&passport=" + | |
| 157 net::EscapeQueryParamValue(passport, false); | |
| 158 if (ContainsKey(map_, "addr")) | |
| 159 query += std::string("&addr=") + map_["addr"]; | |
| 160 | |
| 161 if (listening_port_ < 1) | |
| 162 listening_port_ = chromeos::WebSocketProxyController::GetPort(); | |
| 163 StringValue* url = Value::CreateStringValue(std::string( | |
| 164 "ws://127.0.0.1:" + base::IntToString(listening_port_) + | |
| 165 "/tcpproxy?" + query)); | |
| 166 result_.reset(url); | |
| 167 #endif | |
| 168 } | |
| 169 | |
| 170 bool WebSocketProxyPrivateGetURLForTCPFunction::RunImpl() { | |
| 171 #if defined(OS_CHROMEOS) | |
| 172 DictionaryValue* qualification = NULL; | |
| 173 if (args_->GetDictionary(2, &qualification)) { | |
| 174 const char kTlsOption[] = "tls"; | |
| 175 if (qualification->HasKey(kTlsOption)) { | |
| 176 EXTENSION_FUNCTION_VALIDATE(qualification->GetBoolean( | |
| 177 kTlsOption, &do_tls_)); | |
| 178 } | |
| 179 } | |
| 180 map_["tls"] = do_tls_ ? "true" : "false"; | |
| 181 #endif | |
| 182 | |
| 183 return WebSocketProxyPrivate::RunImpl(); | |
| 184 } | |
| 185 | |
| 186 WebSocketProxyPrivateGetPassportForTCPFunction:: | |
| 187 WebSocketProxyPrivateGetPassportForTCPFunction() { | |
| 188 // This obsolete API uses fixed port to listen websocket connections. | |
| 189 listening_port_ = 10101; | |
| 190 } | |
| 191 | |
| 192 WebSocketProxyPrivateGetPassportForTCPFunction:: | |
| 193 ~WebSocketProxyPrivateGetPassportForTCPFunction() { | |
| 194 } | |
| 195 | |
| 196 void WebSocketProxyPrivateGetPassportForTCPFunction::CustomFinalize() { | |
| 197 #if defined(OS_CHROMEOS) | |
| 198 std::string passport = | |
| 199 browser::InternalAuthGeneration::GeneratePassport( | |
| 200 "web_socket_proxy", map_) + std::string(":"); | |
| 201 if (ContainsKey(map_, "addr")) | |
| 202 passport += map_["addr"]; | |
| 203 result_.reset(Value::CreateStringValue(passport)); | |
| 204 #endif | |
| 205 } | |
| OLD | NEW |