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/chromeos/web_socket_proxy.h" | 5 #include "chrome/browser/chromeos/web_socket_proxy.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <limits> | 12 #include <limits> |
13 #include <list> | 13 #include <list> |
14 #include <map> | 14 #include <map> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include <arpa/inet.h> | 17 #include <arpa/inet.h> |
18 #include <errno.h> | 18 #include <errno.h> |
19 #include <fcntl.h> | 19 #include <fcntl.h> |
20 #include <netinet/in.h> | 20 #include <netinet/in.h> |
21 #include <signal.h> | 21 #include <signal.h> |
22 #include <sys/socket.h> | 22 #include <sys/socket.h> |
23 #include <sys/types.h> | 23 #include <sys/types.h> |
24 #include <sys/wait.h> | 24 #include <sys/wait.h> |
25 | 25 |
26 #include "base/base64.h" | 26 #include "base/base64.h" |
27 #include "base/basictypes.h" | 27 #include "base/basictypes.h" |
28 #include "base/bind.h" | 28 #include "base/bind.h" |
| 29 #include "base/lazy_instance.h" |
29 #include "base/logging.h" | 30 #include "base/logging.h" |
30 #include "base/memory/ref_counted.h" | 31 #include "base/memory/ref_counted.h" |
31 #include "base/memory/scoped_ptr.h" | 32 #include "base/memory/scoped_ptr.h" |
32 #include "base/sha1.h" | 33 #include "base/sha1.h" |
33 #include "base/stl_util.h" | 34 #include "base/stl_util.h" |
34 #include "base/string_number_conversions.h" | 35 #include "base/string_number_conversions.h" |
35 #include "base/string_util.h" | 36 #include "base/string_util.h" |
36 #include "chrome/browser/internal_auth.h" | 37 #include "chrome/browser/internal_auth.h" |
37 #include "chrome/common/chrome_notification_types.h" | 38 #include "chrome/common/chrome_notification_types.h" |
38 #include "chrome/common/url_constants.h" | 39 #include "chrome/common/url_constants.h" |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 bool do_tls_; | 455 bool do_tls_; |
455 | 456 |
456 // We try to DNS resolve hostname in both IPv4 and IPv6 domains. | 457 // We try to DNS resolve hostname in both IPv4 and IPv6 domains. |
457 // Track resolution failures here. | 458 // Track resolution failures here. |
458 bool destresolution_ipv4_failed_; | 459 bool destresolution_ipv4_failed_; |
459 bool destresolution_ipv6_failed_; | 460 bool destresolution_ipv6_failed_; |
460 | 461 |
461 // Used to schedule a timeout for initial phase of connection. | 462 // Used to schedule a timeout for initial phase of connection. |
462 scoped_ptr<struct event> destconnect_timeout_event_; | 463 scoped_ptr<struct event> destconnect_timeout_event_; |
463 | 464 |
464 static EventKeyMap evkey_map_; | 465 static base::LazyInstance<EventKeyMap, |
| 466 base::LeakyLazyInstanceTraits<EventKeyMap> > |
| 467 evkey_map_; |
465 static EventKey last_evkey_; | 468 static EventKey last_evkey_; |
466 | 469 |
467 DISALLOW_COPY_AND_ASSIGN(Conn); | 470 DISALLOW_COPY_AND_ASSIGN(Conn); |
468 }; | 471 }; |
469 | 472 |
470 class SSLChan : public MessageLoopForIO::Watcher { | 473 class SSLChan : public MessageLoopForIO::Watcher { |
471 public: | 474 public: |
472 static void Start(const net::AddressList& address_list, | 475 static void Start(const net::AddressList& address_list, |
473 const net::HostPortPair& host_port_pair, | 476 const net::HostPortPair& host_port_pair, |
474 int read_pipe, | 477 int read_pipe, |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 Conn::Conn(Serv* master) | 1138 Conn::Conn(Serv* master) |
1136 : master_(master), | 1139 : master_(master), |
1137 phase_(PHASE_WAIT_HANDSHAKE), | 1140 phase_(PHASE_WAIT_HANDSHAKE), |
1138 frame_bytes_remaining_(0), | 1141 frame_bytes_remaining_(0), |
1139 frame_mask_index_(0), | 1142 frame_mask_index_(0), |
1140 primchan_(this), | 1143 primchan_(this), |
1141 destchan_(this), | 1144 destchan_(this), |
1142 do_tls_(false), | 1145 do_tls_(false), |
1143 destresolution_ipv4_failed_(false), | 1146 destresolution_ipv4_failed_(false), |
1144 destresolution_ipv6_failed_(false) { | 1147 destresolution_ipv6_failed_(false) { |
1145 while (evkey_map_.find(last_evkey_) != evkey_map_.end()) { | 1148 while (evkey_map_.Get().find(last_evkey_) != evkey_map_.Get().end()) { |
1146 last_evkey_ = reinterpret_cast<EventKey>(reinterpret_cast<size_t>( | 1149 last_evkey_ = reinterpret_cast<EventKey>(reinterpret_cast<size_t>( |
1147 last_evkey_) + 1); | 1150 last_evkey_) + 1); |
1148 } | 1151 } |
1149 evkey_ = last_evkey_; | 1152 evkey_ = last_evkey_; |
1150 evkey_map_[evkey_] = this; | 1153 evkey_map_.Get()[evkey_] = this; |
1151 // Schedule timeout for initial phase of connection. | 1154 // Schedule timeout for initial phase of connection. |
1152 destconnect_timeout_event_.reset(new struct event); | 1155 destconnect_timeout_event_.reset(new struct event); |
1153 evtimer_set(destconnect_timeout_event_.get(), | 1156 evtimer_set(destconnect_timeout_event_.get(), |
1154 &OnDestConnectTimeout, evkey_); | 1157 &OnDestConnectTimeout, evkey_); |
1155 event_base_set(master_->evbase(), | 1158 event_base_set(master_->evbase(), |
1156 destconnect_timeout_event_.get()); | 1159 destconnect_timeout_event_.get()); |
1157 | 1160 |
1158 struct timeval tv; | 1161 struct timeval tv; |
1159 tv.tv_sec = 20; | 1162 tv.tv_sec = 20; |
1160 tv.tv_usec = 0; | 1163 tv.tv_usec = 0; |
1161 evtimer_add(destconnect_timeout_event_.get(), &tv); | 1164 evtimer_add(destconnect_timeout_event_.get(), &tv); |
1162 } | 1165 } |
1163 | 1166 |
1164 Conn::~Conn() { | 1167 Conn::~Conn() { |
1165 phase_ = PHASE_DEFUNCT; | 1168 phase_ = PHASE_DEFUNCT; |
1166 event_del(destconnect_timeout_event_.get()); | 1169 event_del(destconnect_timeout_event_.get()); |
1167 if (evkey_map_[evkey_] == this) | 1170 if (evkey_map_.Get()[evkey_] == this) |
1168 evkey_map_.erase(evkey_); | 1171 evkey_map_.Get().erase(evkey_); |
1169 else | 1172 else |
1170 NOTREACHED(); | 1173 NOTREACHED(); |
1171 } | 1174 } |
1172 | 1175 |
1173 Conn* Conn::Get(EventKey evkey) { | 1176 Conn* Conn::Get(EventKey evkey) { |
1174 EventKeyMap::iterator it = evkey_map_.find(evkey); | 1177 EventKeyMap::iterator it = evkey_map_.Get().find(evkey); |
1175 if (it == evkey_map_.end()) | 1178 if (it == evkey_map_.Get().end()) |
1176 return NULL; | 1179 return NULL; |
1177 Conn* cs = it->second; | 1180 Conn* cs = it->second; |
1178 if (cs == NULL || | 1181 if (cs == NULL || |
1179 cs->evkey_ != evkey || | 1182 cs->evkey_ != evkey || |
1180 cs->master_ == NULL || | 1183 cs->master_ == NULL || |
1181 cs->phase_ < 0 || | 1184 cs->phase_ < 0 || |
1182 cs->phase_ > PHASE_SHUT || | 1185 cs->phase_ > PHASE_SHUT || |
1183 !cs->master_->IsConnSane(cs)) { | 1186 !cs->master_->IsConnSane(cs)) { |
1184 return NULL; | 1187 return NULL; |
1185 } | 1188 } |
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1902 return; | 1905 return; |
1903 } | 1906 } |
1904 cs->destchan_.set_write_pending(false); | 1907 cs->destchan_.set_write_pending(false); |
1905 if (cs->phase_ >= PHASE_SHUT) | 1908 if (cs->phase_ >= PHASE_SHUT) |
1906 cs->master_->ZapConn(cs); | 1909 cs->master_->ZapConn(cs); |
1907 else | 1910 else |
1908 cs->Shut(WS_CLOSE_DESTINATION_ERROR, | 1911 cs->Shut(WS_CLOSE_DESTINATION_ERROR, |
1909 "Failure reported on destination channel"); | 1912 "Failure reported on destination channel"); |
1910 } | 1913 } |
1911 | 1914 |
| 1915 // static |
1912 Conn::EventKey Conn::last_evkey_ = 0; | 1916 Conn::EventKey Conn::last_evkey_ = 0; |
1913 Conn::EventKeyMap Conn::evkey_map_; | 1917 |
| 1918 // static |
| 1919 base::LazyInstance<Conn::EventKeyMap, |
| 1920 base::LeakyLazyInstanceTraits<Conn::EventKeyMap> > |
| 1921 Conn::evkey_map_ = LAZY_INSTANCE_INITIALIZER; |
1914 | 1922 |
1915 } // namespace | 1923 } // namespace |
1916 | 1924 |
1917 WebSocketProxy::WebSocketProxy(const std::vector<std::string>& allowed_origins) | 1925 WebSocketProxy::WebSocketProxy(const std::vector<std::string>& allowed_origins) |
1918 : impl_(new Serv(allowed_origins)) { | 1926 : impl_(new Serv(allowed_origins)) { |
1919 } | 1927 } |
1920 | 1928 |
1921 WebSocketProxy::~WebSocketProxy() { | 1929 WebSocketProxy::~WebSocketProxy() { |
1922 delete static_cast<Serv*>(impl_); | 1930 delete static_cast<Serv*>(impl_); |
1923 impl_ = NULL; | 1931 impl_ = NULL; |
1924 } | 1932 } |
1925 | 1933 |
1926 void WebSocketProxy::Run() { | 1934 void WebSocketProxy::Run() { |
1927 static_cast<Serv*>(impl_)->Run(); | 1935 static_cast<Serv*>(impl_)->Run(); |
1928 } | 1936 } |
1929 | 1937 |
1930 void WebSocketProxy::Shutdown() { | 1938 void WebSocketProxy::Shutdown() { |
1931 static_cast<Serv*>(impl_)->Shutdown(); | 1939 static_cast<Serv*>(impl_)->Shutdown(); |
1932 } | 1940 } |
1933 | 1941 |
1934 void WebSocketProxy::OnNetworkChange() { | 1942 void WebSocketProxy::OnNetworkChange() { |
1935 static_cast<Serv*>(impl_)->OnNetworkChange(); | 1943 static_cast<Serv*>(impl_)->OnNetworkChange(); |
1936 } | 1944 } |
1937 | 1945 |
1938 } // namespace chromeos | 1946 } // namespace chromeos |
OLD | NEW |