blob: 8c52fa21e54a8803c057230a5ca07eac51623975 [file] [log] [blame]
[email protected]808d1032011-02-09 20:16:551// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]35a36952010-08-19 00:57:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/test/webdriver/session_manager.h"
6
[email protected]35a36952010-08-19 00:57:517#include "base/logging.h"
[email protected]48a35692011-02-11 00:45:558#include "chrome/test/webdriver/utility_functions.h"
[email protected]9b82c012011-02-13 04:18:099#include "net/base/net_util.h"
[email protected]0bc24b52011-01-11 01:19:4710
[email protected]9b82c012011-02-13 04:18:0911#if defined(OS_WIN)
[email protected]0bc24b52011-01-11 01:19:4712#include <Winsock2.h>
13#endif
[email protected]35a36952010-08-19 00:57:5114
15namespace webdriver {
16
[email protected]9b82c012011-02-13 04:18:0917std::string SessionManager::GetAddress() {
18 std::string hostname = net::GetHostName();
19#if defined(OS_WIN)
20 if (hostname.length()) {
21 // Get the fully qualified name.
22 struct hostent* host_entry = gethostbyname(hostname.c_str());
23 if (host_entry)
24 hostname = host_entry->h_name;
25 }
[email protected]35a36952010-08-19 00:57:5126#endif
[email protected]9b82c012011-02-13 04:18:0927 if (hostname.empty()) {
28 hostname = "localhost";
[email protected]35a36952010-08-19 00:57:5129 }
[email protected]9b82c012011-02-13 04:18:0930 return hostname + ":" + port_;
[email protected]35a36952010-08-19 00:57:5131}
32
[email protected]496e5792011-02-17 17:02:5333void SessionManager::Add(Session* session) {
[email protected]2a28e0b22011-02-03 21:49:0334 base::AutoLock lock(map_lock_);
[email protected]496e5792011-02-17 17:02:5335 map_[session->id()] = session;
[email protected]35a36952010-08-19 00:57:5136}
37
38bool SessionManager::Has(const std::string& id) const {
[email protected]2a28e0b22011-02-03 21:49:0339 base::AutoLock lock(map_lock_);
[email protected]35a36952010-08-19 00:57:5140 return map_.find(id) != map_.end();
41}
42
[email protected]496e5792011-02-17 17:02:5343bool SessionManager::Remove(const std::string& id) {
[email protected]35a36952010-08-19 00:57:5144 std::map<std::string, Session*>::iterator it;
[email protected]2a28e0b22011-02-03 21:49:0345 Session* session;
[email protected]496e5792011-02-17 17:02:5346 base::AutoLock lock(map_lock_);
47 it = map_.find(id);
48 if (it == map_.end()) {
49 VLOG(1) << "No such session with ID " << id;
50 return false;
[email protected]35a36952010-08-19 00:57:5151 }
[email protected]496e5792011-02-17 17:02:5352 session = it->second;
53 map_.erase(it);
[email protected]35a36952010-08-19 00:57:5154 return true;
55}
56
57Session* SessionManager::GetSession(const std::string& id) const {
58 std::map<std::string, Session*>::const_iterator it;
[email protected]2a28e0b22011-02-03 21:49:0359 base::AutoLock lock(map_lock_);
[email protected]35a36952010-08-19 00:57:5160 it = map_.find(id);
61 if (it == map_.end()) {
[email protected]ca8cf5f2010-10-20 20:22:0762 VLOG(1) << "No such session with ID " << id;
[email protected]35a36952010-08-19 00:57:5163 return NULL;
64 }
65 return it->second;
66}
[email protected]2cbc79392010-10-07 00:34:4067
[email protected]9b82c012011-02-13 04:18:0968SessionManager::SessionManager() : port_("") {}
[email protected]808d1032011-02-09 20:16:5569
70SessionManager::~SessionManager() {}
71
[email protected]687b9602010-12-08 10:43:0872// static
73SessionManager* SessionManager::GetInstance() {
74 return Singleton<SessionManager>::get();
75}
76
[email protected]35a36952010-08-19 00:57:5177} // namespace webdriver