OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "web_socket_proxy_controller.h" |
| 6 |
| 7 #include <netinet/in.h> |
| 8 #include <sys/wait.h> |
| 9 #include <unistd.h> |
| 10 |
| 11 #include "base/lazy_instance.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chromeos/web_socket_proxy.h" |
| 16 #include "content/browser/browser_thread.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 class ProxyTask : public Task { |
| 21 virtual void Run() OVERRIDE; |
| 22 }; |
| 23 |
| 24 struct ProxyLifetime { |
| 25 ProxyLifetime() : delay_ms(1000), shutdown_requested(false) { |
| 26 BrowserThread::PostTask( |
| 27 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask()); |
| 28 } |
| 29 |
| 30 // Delay between next attempt to run proxy. |
| 31 int delay_ms; |
| 32 |
| 33 chromeos::WebSocketProxy* volatile server; |
| 34 |
| 35 volatile bool shutdown_requested; |
| 36 |
| 37 base::Lock shutdown_lock; |
| 38 }; |
| 39 |
| 40 base::LazyInstance<ProxyLifetime> g_proxy_lifetime(base::LINKER_INITIALIZED); |
| 41 |
| 42 void ProxyTask::Run() { |
| 43 LOG(INFO) << "Attempt to run web socket proxy task"; |
| 44 const int kPort = 10101; |
| 45 |
| 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 memset(&sa, 0, sizeof(sa)); |
| 53 sa.sin_family = AF_INET; |
| 54 sa.sin_port = htons(kPort); |
| 55 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 56 |
| 57 chromeos::WebSocketProxy* server = new chromeos::WebSocketProxy( |
| 58 allowed_origins, reinterpret_cast<sockaddr*>(&sa), sizeof(sa)); |
| 59 { |
| 60 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); |
| 61 if (g_proxy_lifetime.Get().shutdown_requested) |
| 62 return; |
| 63 delete g_proxy_lifetime.Get().server; |
| 64 g_proxy_lifetime.Get().server = server; |
| 65 } |
| 66 server->Run(); |
| 67 { |
| 68 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); |
| 69 delete server; |
| 70 g_proxy_lifetime.Get().server = NULL; |
| 71 if (!g_proxy_lifetime.Get().shutdown_requested) { |
| 72 // Proxy terminated unexpectedly or failed to start (it can happen due to |
| 73 // a network problem). Keep trying. |
| 74 if (g_proxy_lifetime.Get().delay_ms < 100 * 1000) |
| 75 (g_proxy_lifetime.Get().delay_ms *= 3) /= 2; |
| 76 BrowserThread::PostDelayedTask( |
| 77 BrowserThread::WEB_SOCKET_PROXY, FROM_HERE, new ProxyTask(), |
| 78 g_proxy_lifetime.Get().delay_ms); |
| 79 } |
| 80 } |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 85 namespace chromeos { |
| 86 |
| 87 // static |
| 88 void WebSocketProxyController::Initiate() { |
| 89 LOG(INFO) << "WebSocketProxyController initiation"; |
| 90 g_proxy_lifetime.Get(); |
| 91 } |
| 92 |
| 93 // static |
| 94 bool WebSocketProxyController::IsInitiated() { |
| 95 return !(g_proxy_lifetime == NULL); |
| 96 } |
| 97 |
| 98 // static |
| 99 void WebSocketProxyController::Shutdown() { |
| 100 if (IsInitiated()) { |
| 101 LOG(INFO) << "WebSocketProxyController shutdown"; |
| 102 base::AutoLock alk(g_proxy_lifetime.Get().shutdown_lock); |
| 103 g_proxy_lifetime.Get().shutdown_requested = true; |
| 104 if (g_proxy_lifetime.Get().server) |
| 105 g_proxy_lifetime.Get().server->Shutdown(); |
| 106 } |
| 107 } |
| 108 |
| 109 } // namespace chromeos |
| 110 |
OLD | NEW |