summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/session_manager.cc
blob: 245143a1859706abc9ac90e5c07f29ad681c112d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/webdriver/session_manager.h"

#ifdef OS_POSIX
  #include <netdb.h>
  #include <unistd.h>
  #include <arpa/inet.h>
  #include <net/if.h>
  #include <sys/ioctl.h>
  #include <sys/socket.h>
  #include <sys/types.h>
#elif OS_WIN
  #include <Shellapi.h>
  #include <Winsock2.h>
#endif

#include "base/command_line.h"
#include "base/lock.h"
#include "base/logging.h"
#include "base/process.h"
#include "base/process_util.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/test/test_timeouts.h"

#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"

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;
  }
  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
}

std::string SessionManager::GenerateSessionID() {
  static const char text[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ"\
                             "RSTUVWXYZ0123456789";
  session_generation_.Acquire();
  std::string id = "";
  count_++;  // For every connection made increment the global count.
  for (int i = 0; i < 32; ++i) {
#ifdef OS_POSIX
    id += text[random() % (sizeof text - 1)];
#else
    id += text[rand() % (sizeof text - 1)];
#endif
    id += count_;  // Append the global count to generate a unique id.
  }
  session_generation_.Release();
  return id;
}

bool SessionManager::Create(std::string* id) {
  MessageLoop loop;
  TestTimeouts::Initialize();

  *id = GenerateSessionID();
  if (map_.find(*id) != map_.end()) {
    LOG(ERROR) << "Failed to generate a unique session ID";
    return false;
  }

  // Start chrome, if it doesn't startup quit.
  const int ap_timeout = TestTimeouts::command_execution_timeout_ms();
  LOG(INFO) << "Waiting for a max of " << ap_timeout << " ms to "
            << "start the chrome browser";

  scoped_ptr<Session> session(new Session(*id));

  if (!session->Init()) {
    LOG(ERROR) << "Could not establish a valid connection to the browser";
    return false;
  }

  map_[*id] = session.release();
  return true;
}

bool SessionManager::Has(const std::string& id) const {
  return map_.find(id) != map_.end();
}

bool SessionManager::Delete(const std::string& id) {
  std::map<std::string, Session*>::iterator it;

  LOG(INFO) << "Deleting session with ID " << id;
  it = map_.find(id);
  if (it == map_.end()) {
    LOG(INFO) << "No such session with ID " << id;
    return false;
  }

  it->second->Terminate();
  map_.erase(it);
  return true;
}

Session* SessionManager::GetSession(const std::string& id) const {
  std::map<std::string, Session*>::const_iterator it;
  it = map_.find(id);
  if (it == map_.end()) {
    LOG(INFO) << "No such session with ID " << id;
    return NULL;
  }
  return it->second;
}

}  // namespace webdriver