summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/session_manager.cc
blob: f37611bb3cef74d00b005a7b3582e02ce110ddf3 (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
// Copyright (c) 2011 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"

#include "base/logging.h"
#include "chrome/test/webdriver/utility_functions.h"
#include "net/base/net_util.h"

#if defined(OS_WIN)
#include <Winsock2.h>
#endif

namespace webdriver {

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;
  }
#endif
  if (hostname.empty()) {
    hostname = "localhost";
  }
  return hostname + ":" + port_ + url_base_;
}

void SessionManager::Add(Session* session) {
  base::AutoLock lock(map_lock_);
  map_[session->id()] = session;
}

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

bool SessionManager::Remove(const std::string& id) {
  std::map<std::string, Session*>::iterator it;
  Session* session;
  base::AutoLock lock(map_lock_);
  it = map_.find(id);
  if (it == map_.end()) {
    VLOG(1) << "No such session with ID " << id;
    return false;
  }
  session = it->second;
  map_.erase(it);
  return true;
}

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

void SessionManager::set_port(const std::string& port) {
  port_ = port;
}

void SessionManager::set_url_base(const std::string& url_base) {
  url_base_ = url_base;
}

std::string SessionManager::url_base() const {
  return url_base_;
}

void SessionManager::set_chrome_dir(const FilePath& chrome_dir) {
  chrome_dir_ = chrome_dir;
}

FilePath SessionManager::chrome_dir() const {
  return chrome_dir_;
}

SessionManager::SessionManager() : port_(""), url_base_("") {}

SessionManager::~SessionManager() {}

// static
SessionManager* SessionManager::GetInstance() {
  return Singleton<SessionManager>::get();
}

}  // namespace webdriver