diff options
-rw-r--r-- | chrome/test/webdriver/commands/create_session.cc | 2 | ||||
-rw-r--r-- | chrome/test/webdriver/server.cc | 4 | ||||
-rw-r--r-- | chrome/test/webdriver/session_manager.cc | 104 | ||||
-rw-r--r-- | chrome/test/webdriver/session_manager.h | 11 |
4 files changed, 21 insertions, 100 deletions
diff --git a/chrome/test/webdriver/commands/create_session.cc b/chrome/test/webdriver/commands/create_session.cc index dfb1838..d8f5112 100644 --- a/chrome/test/webdriver/commands/create_session.cc +++ b/chrome/test/webdriver/commands/create_session.cc @@ -36,7 +36,7 @@ void CreateSession::ExecutePost(Response* const response) { VLOG(1) << "Created session " << session_id; std::ostringstream stream; - stream << "http://" << session_manager->GetIPAddress() << "/session/" + stream << "http://" << session_manager->GetAddress() << "/session/" << session_id; response->set_status(kSeeOther); response->set_value(Value::CreateStringValue(stream.str())); diff --git a/chrome/test/webdriver/server.cc b/chrome/test/webdriver/server.cc index c24242b..8046c04 100644 --- a/chrome/test/webdriver/server.cc +++ b/chrome/test/webdriver/server.cc @@ -155,8 +155,8 @@ int main(int argc, char *argv[]) { root = cmd_line->GetSwitchValueASCII("root"); VLOG(1) << "Using port: " << port; - webdriver::SessionManager* session = webdriver::SessionManager::GetInstance(); - session->SetIPAddress(port); + webdriver::SessionManager* manager = webdriver::SessionManager::GetInstance(); + manager->set_port(port); // Initialize SHTTPD context. // Listen on port 9515 or port specified on command line. diff --git a/chrome/test/webdriver/session_manager.cc b/chrome/test/webdriver/session_manager.cc index ffa85b3..ed9a113 100644 --- a/chrome/test/webdriver/session_manager.cc +++ b/chrome/test/webdriver/session_manager.cc @@ -5,103 +5,29 @@ #include "chrome/test/webdriver/session_manager.h" #include "base/logging.h" -#include "base/scoped_ptr.h" -#include "base/threading/thread.h" #include "chrome/test/webdriver/utility_functions.h" +#include "net/base/net_util.h" -#if defined(OS_POSIX) -#include <arpa/inet.h> -#include <net/if.h> -#include <netdb.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/types.h> -#include <unistd.h> -#elif defined(OS_WIN) -#include <Shellapi.h> +#if defined(OS_WIN) #include <Winsock2.h> #endif namespace webdriver { -std::string SessionManager::GetIPAddress() { - return std::string(addr_) + std::string(":") + port_; -} - -std::string SessionManager::IPLookup(const std::string& nic) { -#ifdef OS_POSIX - int socket_conn; - struct ifreq ifr; - struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; - - memset(&ifr, 0, sizeof(ifr)); - snprintf(ifr.ifr_name, IFNAMSIZ, "%s", nic.c_str()); - sin->sin_family = AF_INET; - - if (0 > (socket_conn = socket(AF_INET, SOCK_STREAM, 0))) { - return std::string(""); - } - - if (0 == ioctl(socket_conn, SIOCGIFADDR, &ifr)) { - return std::string(inet_ntoa(sin->sin_addr)); - } -#endif // Cann't use else since a warning will be generated. - return std::string(""); -} - -bool SessionManager::SetIPAddress(const std::string& p) { - port_ = p; - std::string prefix("192."); -#ifdef OS_POSIX - char buff[32]; - - for (int i = 0; i < 10; ++i) { -#ifdef OS_MACOSX - snprintf(buff, sizeof(buff), "%s%d", "en", i); -#elif OS_LINUX - snprintf(buff, sizeof(buff), "%s%d", "eth", i); -#endif - addr_ = IPLookup(std::string(buff)); - if (addr_.length() > 0) { - if ((addr_.compare("127.0.0.1") != 0) && - (addr_.compare("127.0.1.1") != 0) && - (addr_.compare(0, prefix.size(), prefix) != 0)) { - return true; - } - } - } - return false; -#elif OS_WIN - hostent *h; - char host[1024]; - WORD wVersionRequested; - WSADATA wsaData; - - memset(host, 0, sizeof host); - wVersionRequested = MAKEWORD(2, 0); - if (WSAStartup(wVersionRequested, &wsaData) != 0) { - LOG(ERROR) << "Could not initialize the Windows Sockets library"; - return false; - } - if (gethostname(host, sizeof host) != 0) { - LOG(ERROR) << "Could not find the hostname of this machine"; - WSACleanup(); - return false; +std::string SessionManager::GetAddress() { + std::string hostname = net::GetHostName(); +#if defined(OS_WIN) + if (hostname.length()) { + // Get the fully qualified name. + struct hostent* host_entry = gethostbyname(hostname.c_str()); + if (host_entry) + hostname = host_entry->h_name; } - h = gethostbyname(host); - for (int i = 0; ((h->h_addr_list[i]) && (i < h->h_length)); ++i) { - addr_ = std::string(inet_ntoa(*(struct in_addr *)h->h_addr_list[i])); - if ((addr_.compare("127.0.0.1") != 0) && - (addr_.compare("127.0.1.1") != 0) && - (addr_.compare(0, prefix.size(), prefix) != 0)) { - WSACleanup(); - return true; - } - } - - WSACleanup(); - return false; #endif + if (hostname.empty()) { + hostname = "localhost"; + } + return hostname + ":" + port_; } Session* SessionManager::Create() { @@ -156,7 +82,7 @@ Session* SessionManager::GetSession(const std::string& id) const { return it->second; } -SessionManager::SessionManager() : addr_(""), port_("") {} +SessionManager::SessionManager() : port_("") {} SessionManager::~SessionManager() {} diff --git a/chrome/test/webdriver/session_manager.h b/chrome/test/webdriver/session_manager.h index 2f977f2..1096d83 100644 --- a/chrome/test/webdriver/session_manager.h +++ b/chrome/test/webdriver/session_manager.h @@ -24,8 +24,7 @@ class SessionManager { // Returns the singleton instance. static SessionManager* GetInstance(); - std::string GetIPAddress(); - bool SetIPAddress(const std::string& port); + std::string GetAddress(); Session* Create(); bool Delete(const std::string& id); @@ -33,19 +32,15 @@ class SessionManager { Session* GetSession(const std::string& id) const; + void set_port(const std::string& port) { port_ = port; } + private: SessionManager(); ~SessionManager(); friend struct DefaultSingletonTraits<SessionManager>; - std::string IPLookup(const std::string& nic); std::map<std::string, Session*> map_; mutable base::Lock map_lock_; - // Record the address and port for the HTTP 303 See other redirect. - // We save the IP and Port of the machine chromedriver is running on since - // a HTTP 303, see other, resdirect is sent after a successful creation of - // a session, ie: http://172.22.41.105:8080/session/DFSSE453CV588 - std::string addr_; std::string port_; DISALLOW_COPY_AND_ASSIGN(SessionManager); |