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 "web_socket_proxy_controller.h" | 5 #include "web_socket_proxy_controller.h" |
6 | 6 |
7 #include <netinet/in.h> | 7 #include <netinet/in.h> |
8 #include <sys/wait.h> | 8 #include <sys/wait.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/chromeos/web_socket_proxy.h" | 15 #include "chrome/browser/chromeos/web_socket_proxy.h" |
16 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char* kAllowedOrigins[] = { | |
21 "haiffjcadagjlijoggckpgfnoeiflnem" | |
22 }; | |
23 const std::vector<std::string> kAllowedOriginsVector( | |
Dmitry Polukhin
2011/05/17 10:58:39
static objects with c-tors are prohibited in style
Denis Lagno
2011/05/17 22:15:08
Done.
| |
24 kAllowedOrigins, kAllowedOrigins + arraysize(kAllowedOrigins)); | |
25 | |
20 class ProxyTask : public Task { | 26 class ProxyTask : public Task { |
21 virtual void Run() OVERRIDE; | 27 virtual void Run() OVERRIDE; |
22 }; | 28 }; |
23 | 29 |
24 struct ProxyLifetime { | 30 struct ProxyLifetime { |
25 ProxyLifetime() : delay_ms(1000), shutdown_requested(false) { | 31 ProxyLifetime() : delay_ms(1000), shutdown_requested(false) { |
26 BrowserThread::PostTask( | 32 BrowserThread::PostTask( |
27 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); | 33 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); |
28 } | 34 } |
29 | 35 |
30 // Delay between next attempt to run proxy. | 36 // Delay between next attempt to run proxy. |
31 int delay_ms; | 37 int delay_ms; |
32 | 38 |
33 chromeos::WebSocketProxy* volatile server; | 39 chromeos::WebSocketProxy* volatile server; |
34 | 40 |
35 volatile bool shutdown_requested; | 41 volatile bool shutdown_requested; |
36 | 42 |
37 base::Lock shutdown_lock; | 43 base::Lock shutdown_lock; |
38 }; | 44 }; |
39 | 45 |
40 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); | 46 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); |
41 | 47 |
42 void ProxyTask::Run() { | 48 void ProxyTask::Run() { |
43 LOG(INFO) << "Attempt to run web socket proxy task"; | 49 LOG(INFO) << "Attempt to run web socket proxy task"; |
44 const int kPort = 10101; | 50 const int kPort = 10101; |
45 | 51 |
46 // Configure allowed origins. Empty vector allows any origin. | |
47 std::vector<std::string> allowed_origins; | |
48 allowed_origins.push_back( | |
49 "chrome-extension://haiffjcadagjlijoggckpgfnoeiflnem"); | |
50 | |
51 struct sockaddr_in sa; | 52 struct sockaddr_in sa; |
52 memset(&sa, 0, sizeof(sa)); | 53 memset(&sa, 0, sizeof(sa)); |
53 sa.sin_family = AF_INET; | 54 sa.sin_family = AF_INET; |
54 sa.sin_port = htons(kPort); | 55 sa.sin_port = htons(kPort); |
55 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | 56 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
56 | 57 |
57 chromeos::WebSocketProxy* server = new chromeos::WebSocketProxy( | 58 chromeos::WebSocketProxy* server = new chromeos::WebSocketProxy( |
58 allowed_origins, reinterpret_cast<sockaddr*>(&sa), sizeof(sa)); | 59 kAllowedOriginsVector, reinterpret_cast<sockaddr*>(&sa), sizeof(sa)); |
59 { | 60 { |
60 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); | 61 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); |
61 if (g_proxy_lifetime.Get().shutdown_requested) | 62 if (g_proxy_lifetime.Get().shutdown_requested) |
62 return; | 63 return; |
63 delete g_proxy_lifetime.Get().server; | 64 delete g_proxy_lifetime.Get().server; |
64 g_proxy_lifetime.Get().server = server; | 65 g_proxy_lifetime.Get().server = server; |
65 } | 66 } |
66 server->Run(); | 67 server->Run(); |
67 { | 68 { |
68 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); | 69 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); |
69 delete server; | 70 delete server; |
70 g_proxy_lifetime.Get().server = NULL; | 71 g_proxy_lifetime.Get().server = NULL; |
71 if (!g_proxy_lifetime.Get().shutdown_requested) { | 72 if (!g_proxy_lifetime.Get().shutdown_requested) { |
72 // Proxy terminated unexpectedly or failed to start (it can happen due to | 73 // Proxy terminated unexpectedly or failed to start (it can happen due to |
73 // a network problem). Keep trying. | 74 // a network problem). Keep trying. |
74 if (g_proxy_lifetime.Get().delay_ms < 100 * 1000) | 75 if (g_proxy_lifetime.Get().delay_ms < 100 * 1000) |
75 (g_proxy_lifetime.Get().delay_ms *= 3) /= 2; | 76 (g_proxy_lifetime.Get().delay_ms *= 3) /= 2; |
76 BrowserThread::PostDelayedTask( | 77 BrowserThread::PostDelayedTask( |
77 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask(), | 78 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask(), |
78 g_proxy_lifetime.Get().delay_ms); | 79 g_proxy_lifetime.Get().delay_ms); |
79 } | 80 } |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 } // namespace | 84 } // namespace |
84 | 85 |
85 namespace chromeos { | 86 namespace chromeos { |
86 | 87 |
88 const char* kAllowedOrigins[] = { | |
Dmitry Polukhin
2011/05/17 10:58:39
Redefinition?
Denis Lagno
2011/05/17 22:15:08
Done.
| |
89 "chrome-extension://haiffjcadagjlijoggckpgfnoeiflnem" | |
90 }; | |
87 // static | 91 // static |
88 void WebSocketProxyController::Initiate() { | 92 void WebSocketProxyController::Initiate() { |
89 LOG(INFO) << "WebSocketProxyController initiation"; | 93 LOG(INFO) << "WebSocketProxyController initiation"; |
90 g_proxy_lifetime.Get(); | 94 g_proxy_lifetime.Get(); |
91 } | 95 } |
92 | 96 |
93 // static | 97 // static |
94 bool WebSocketProxyController::IsInitiated() { | 98 bool WebSocketProxyController::IsInitiated() { |
95 return !(g_proxy_lifetime == NULL); | 99 return !(g_proxy_lifetime == NULL); |
96 } | 100 } |
97 | 101 |
98 // static | 102 // static |
99 void WebSocketProxyController::Shutdown() { | 103 void WebSocketProxyController::Shutdown() { |
100 if (IsInitiated()) { | 104 if (IsInitiated()) { |
101 LOG(INFO) << "WebSocketProxyController shutdown"; | 105 LOG(INFO) << "WebSocketProxyController shutdown"; |
102 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); | 106 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); |
103 g_proxy_lifetime.Get().shutdown_requested = true; | 107 g_proxy_lifetime.Get().shutdown_requested = true; |
104 if (g_proxy_lifetime.Get().server) | 108 if (g_proxy_lifetime.Get().server) |
105 g_proxy_lifetime.Get().server->Shutdown(); | 109 g_proxy_lifetime.Get().server->Shutdown(); |
106 } | 110 } |
107 } | 111 } |
108 | 112 |
113 // static | |
114 bool WebSocketProxyController::CheckCredentials( | |
115 const std::string& extension_id, | |
116 const std::string& hostname, | |
117 unsigned short port, | |
118 ConnectionFlags flags) { | |
119 if (flags & TLS_OVER_TCP) { | |
120 NOTIMPLEMENTED(); | |
121 return false; | |
122 } | |
123 DCHECK(kAllowedOriginsVector.size() < 7) << "consider binary search"; | |
Dmitry Polukhin
2011/05/17 10:58:39
Compile time assert with arraysize(kAllowedOrigins
Denis Lagno
2011/05/17 22:15:08
Done.
| |
124 if (std::find( | |
125 kAllowedOriginsVector.begin(), | |
126 kAllowedOriginsVector.end(), | |
127 extension_id) == kAllowedOriginsVector.end()) { | |
128 return false; | |
129 } | |
130 return true; | |
131 } | |
132 | |
109 } // namespace chromeos | 133 } // namespace chromeos |
110 | 134 |
OLD | NEW |