OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h" | 5 #include "chrome/browser/extensions/extension_web_socket_proxy_private_api.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "chrome/browser/internal_auth.h" | 10 #include "chrome/browser/internal_auth.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 NOTREACHED(); | 34 NOTREACHED(); |
35 #endif | 35 #endif |
36 timer_.Stop(); // Cancel timeout timer. | 36 timer_.Stop(); // Cancel timeout timer. |
37 Finalize(); | 37 Finalize(); |
38 } | 38 } |
39 | 39 |
40 void WebSocketProxyPrivate::Finalize() { | 40 void WebSocketProxyPrivate::Finalize() { |
41 if (is_finalized_) | 41 if (is_finalized_) |
42 return; | 42 return; |
43 is_finalized_ = true; | 43 is_finalized_ = true; |
44 SendResponse(true); | 44 SendResponse(listening_port_ > 0); |
45 Release(); | 45 Release(); |
46 } | 46 } |
47 | 47 |
48 WebSocketProxyPrivateGetPassportForTCPFunction:: | 48 WebSocketProxyPrivateGetPassportForTCPFunction:: |
49 WebSocketProxyPrivateGetPassportForTCPFunction() { | 49 WebSocketProxyPrivateGetPassportForTCPFunction() { |
50 // This obsolete API uses fixed port to listen websocket connections. | 50 // This obsolete API uses fixed port to listen websocket connections. |
51 listening_port_ = 10101; | 51 listening_port_ = 10101; |
52 } | 52 } |
53 | 53 |
54 void WebSocketProxyPrivateGetURLForTCPFunction::Observe( | 54 void WebSocketProxyPrivateGetURLForTCPFunction::Observe( |
55 int type, const content::NotificationSource& source, | 55 int type, const content::NotificationSource& source, |
56 const content::NotificationDetails& details) { | 56 const content::NotificationDetails& details) { |
57 listening_port_ = *content::Details<int>(details).ptr(); | 57 listening_port_ = *content::Details<int>(details).ptr(); |
58 WebSocketProxyPrivate::Observe(type, source, details); | 58 WebSocketProxyPrivate::Observe(type, source, details); |
59 } | 59 } |
60 | 60 |
61 void WebSocketProxyPrivateGetURLForTCPFunction::Finalize() { | 61 void WebSocketProxyPrivateGetURLForTCPFunction::Finalize() { |
| 62 #if defined(OS_CHROMEOS) |
62 StringValue* url = Value::CreateStringValue(std::string( | 63 StringValue* url = Value::CreateStringValue(std::string( |
63 "ws://127.0.0.1:" + base::IntToString(listening_port_) + | 64 "ws://127.0.0.1:" + base::IntToString(listening_port_) + |
64 "/tcpproxy?" + query_)); | 65 "/tcpproxy?" + query_)); |
65 result_.reset(url); | 66 result_.reset(url); |
| 67 if (listening_port_ < 1) |
| 68 listening_port_ = chromeos::WebSocketProxyController::GetPort(); |
| 69 #endif |
66 WebSocketProxyPrivate::Finalize(); | 70 WebSocketProxyPrivate::Finalize(); |
67 } | 71 } |
68 | 72 |
69 bool WebSocketProxyPrivateGetPassportForTCPFunction::RunImpl() { | 73 bool WebSocketProxyPrivateGetPassportForTCPFunction::RunImpl() { |
70 AddRef(); | 74 AddRef(); |
71 bool delay_response = false; | 75 bool delay_response = false; |
72 result_.reset(Value::CreateStringValue("")); | 76 result_.reset(Value::CreateStringValue("")); |
73 | 77 |
74 #if defined(OS_CHROMEOS) | 78 #if defined(OS_CHROMEOS) |
75 std::string hostname; | 79 std::string hostname; |
76 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &hostname)); | 80 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &hostname)); |
77 int port = -1; | 81 int port = -1; |
78 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &port)); | 82 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &port)); |
79 | 83 |
80 if (chromeos::WebSocketProxyController::CheckCredentials( | 84 if (chromeos::WebSocketProxyController::CheckCredentials( |
81 extension_id(), hostname, port, | 85 extension_id(), hostname, port, |
82 chromeos::WebSocketProxyController::PLAIN_TCP)) { | 86 chromeos::WebSocketProxyController::PLAIN_TCP)) { |
83 if (!chromeos::WebSocketProxyController::IsInitiated()) { | 87 listening_port_ = chromeos::WebSocketProxyController::GetPort(); |
| 88 if (listening_port_ < 1) { |
84 delay_response = true; | 89 delay_response = true; |
85 registrar_.Add( | 90 registrar_.Add( |
86 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, | 91 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, |
87 content::NotificationService::AllSources()); | 92 content::NotificationService::AllSources()); |
88 chromeos::WebSocketProxyController::Initiate(); | |
89 } | 93 } |
90 | 94 |
91 std::map<std::string, std::string> map; | 95 std::map<std::string, std::string> map; |
92 map["hostname"] = hostname; | 96 map["hostname"] = hostname; |
93 map["port"] = base::IntToString(port); | 97 map["port"] = base::IntToString(port); |
94 map["extension_id"] = extension_id(); | 98 map["extension_id"] = extension_id(); |
95 StringValue* passport = Value::CreateStringValue( | 99 StringValue* passport = Value::CreateStringValue( |
96 browser::InternalAuthGeneration::GeneratePassport( | 100 browser::InternalAuthGeneration::GeneratePassport( |
97 "web_socket_proxy", map)); | 101 "web_socket_proxy", map)); |
98 result_.reset(passport); | 102 result_.reset(passport); |
(...skipping 26 matching lines...) Expand all Loading... |
125 const char kTlsOption[] = "tls"; | 129 const char kTlsOption[] = "tls"; |
126 if (qualification->HasKey(kTlsOption)) { | 130 if (qualification->HasKey(kTlsOption)) { |
127 EXTENSION_FUNCTION_VALIDATE(qualification->GetBoolean( | 131 EXTENSION_FUNCTION_VALIDATE(qualification->GetBoolean( |
128 kTlsOption, &do_tls)); | 132 kTlsOption, &do_tls)); |
129 } | 133 } |
130 } | 134 } |
131 if (chromeos::WebSocketProxyController::CheckCredentials( | 135 if (chromeos::WebSocketProxyController::CheckCredentials( |
132 extension_id(), hostname, port, | 136 extension_id(), hostname, port, |
133 do_tls ? chromeos::WebSocketProxyController::TLS_OVER_TCP : | 137 do_tls ? chromeos::WebSocketProxyController::TLS_OVER_TCP : |
134 chromeos::WebSocketProxyController::PLAIN_TCP)) { | 138 chromeos::WebSocketProxyController::PLAIN_TCP)) { |
135 if (!chromeos::WebSocketProxyController::IsInitiated()) { | 139 listening_port_ = chromeos::WebSocketProxyController::GetPort(); |
| 140 if (listening_port_ < 1) { |
136 delay_response = true; | 141 delay_response = true; |
137 registrar_.Add( | 142 registrar_.Add( |
138 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, | 143 this, chrome::NOTIFICATION_WEB_SOCKET_PROXY_STARTED, |
139 content::NotificationService::AllSources()); | 144 content::NotificationService::AllSources()); |
140 chromeos::WebSocketProxyController::Initiate(); | |
141 } | 145 } |
142 | 146 |
143 std::map<std::string, std::string> map; | 147 std::map<std::string, std::string> map; |
144 map["hostname"] = hostname; | 148 map["hostname"] = hostname; |
145 map["port"] = base::IntToString(port); | 149 map["port"] = base::IntToString(port); |
146 map["extension_id"] = extension_id(); | 150 map["extension_id"] = extension_id(); |
147 map["tls"] = do_tls ? "true" : "false"; | 151 map["tls"] = do_tls ? "true" : "false"; |
148 std::string passport = browser::InternalAuthGeneration::GeneratePassport( | 152 std::string passport = browser::InternalAuthGeneration::GeneratePassport( |
149 "web_socket_proxy", map); | 153 "web_socket_proxy", map); |
150 query_ = std::string("hostname=") + | 154 query_ = std::string("hostname=") + |
151 net::EscapeQueryParamValue(hostname, false) + "&port=" + map["port"] + | 155 net::EscapeQueryParamValue(hostname, false) + "&port=" + map["port"] + |
152 "&tls=" + map["tls"] + "&passport=" + | 156 "&tls=" + map["tls"] + "&passport=" + |
153 net::EscapeQueryParamValue(passport, false); | 157 net::EscapeQueryParamValue(passport, false); |
154 } | 158 } |
155 #endif // defined(OS_CHROMEOS) | 159 #endif // defined(OS_CHROMEOS) |
156 | 160 |
157 if (delay_response) { | 161 if (delay_response) { |
158 const int kTimeout = 3; | 162 const int kTimeout = 12; |
159 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeout), | 163 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeout), |
160 this, &WebSocketProxyPrivate::Finalize); | 164 this, &WebSocketProxyPrivate::Finalize); |
161 } else { | 165 } else { |
162 Finalize(); | 166 Finalize(); |
163 } | 167 } |
164 return true; | 168 return true; |
165 } | 169 } |
166 | 170 |
OLD | NEW |