[email protected] | 808d103 | 2011-02-09 20:16:55 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 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 "chrome/test/webdriver/session_manager.h" | ||||
6 | |||||
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 48a3569 | 2011-02-11 00:45:55 | [diff] [blame] | 8 | #include "chrome/test/webdriver/utility_functions.h" |
[email protected] | 9b82c01 | 2011-02-13 04:18:09 | [diff] [blame] | 9 | #include "net/base/net_util.h" |
[email protected] | 0bc24b5 | 2011-01-11 01:19:47 | [diff] [blame] | 10 | |
[email protected] | 9b82c01 | 2011-02-13 04:18:09 | [diff] [blame] | 11 | #if defined(OS_WIN) |
[email protected] | 0bc24b5 | 2011-01-11 01:19:47 | [diff] [blame] | 12 | #include <Winsock2.h> |
13 | #endif | ||||
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 14 | |
15 | namespace webdriver { | ||||
16 | |||||
[email protected] | 9b82c01 | 2011-02-13 04:18:09 | [diff] [blame] | 17 | std::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] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 26 | #endif |
[email protected] | 9b82c01 | 2011-02-13 04:18:09 | [diff] [blame] | 27 | if (hostname.empty()) { |
28 | hostname = "localhost"; | ||||
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 29 | } |
[email protected] | 9b82c01 | 2011-02-13 04:18:09 | [diff] [blame] | 30 | return hostname + ":" + port_; |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 31 | } |
32 | |||||
[email protected] | 496e579 | 2011-02-17 17:02:53 | [diff] [blame^] | 33 | void SessionManager::Add(Session* session) { |
[email protected] | 2a28e0b2 | 2011-02-03 21:49:03 | [diff] [blame] | 34 | base::AutoLock lock(map_lock_); |
[email protected] | 496e579 | 2011-02-17 17:02:53 | [diff] [blame^] | 35 | map_[session->id()] = session; |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 36 | } |
37 | |||||
38 | bool SessionManager::Has(const std::string& id) const { | ||||
[email protected] | 2a28e0b2 | 2011-02-03 21:49:03 | [diff] [blame] | 39 | base::AutoLock lock(map_lock_); |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 40 | return map_.find(id) != map_.end(); |
41 | } | ||||
42 | |||||
[email protected] | 496e579 | 2011-02-17 17:02:53 | [diff] [blame^] | 43 | bool SessionManager::Remove(const std::string& id) { |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 44 | std::map<std::string, Session*>::iterator it; |
[email protected] | 2a28e0b2 | 2011-02-03 21:49:03 | [diff] [blame] | 45 | Session* session; |
[email protected] | 496e579 | 2011-02-17 17:02:53 | [diff] [blame^] | 46 | 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] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 51 | } |
[email protected] | 496e579 | 2011-02-17 17:02:53 | [diff] [blame^] | 52 | session = it->second; |
53 | map_.erase(it); | ||||
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 54 | return true; |
55 | } | ||||
56 | |||||
57 | Session* SessionManager::GetSession(const std::string& id) const { | ||||
58 | std::map<std::string, Session*>::const_iterator it; | ||||
[email protected] | 2a28e0b2 | 2011-02-03 21:49:03 | [diff] [blame] | 59 | base::AutoLock lock(map_lock_); |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 60 | it = map_.find(id); |
61 | if (it == map_.end()) { | ||||
[email protected] | ca8cf5f | 2010-10-20 20:22:07 | [diff] [blame] | 62 | VLOG(1) << "No such session with ID " << id; |
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 63 | return NULL; |
64 | } | ||||
65 | return it->second; | ||||
66 | } | ||||
[email protected] | 2cbc7939 | 2010-10-07 00:34:40 | [diff] [blame] | 67 | |
[email protected] | 9b82c01 | 2011-02-13 04:18:09 | [diff] [blame] | 68 | SessionManager::SessionManager() : port_("") {} |
[email protected] | 808d103 | 2011-02-09 20:16:55 | [diff] [blame] | 69 | |
70 | SessionManager::~SessionManager() {} | ||||
71 | |||||
[email protected] | 687b960 | 2010-12-08 10:43:08 | [diff] [blame] | 72 | // static |
73 | SessionManager* SessionManager::GetInstance() { | ||||
74 | return Singleton<SessionManager>::get(); | ||||
75 | } | ||||
76 | |||||
[email protected] | 35a3695 | 2010-08-19 00:57:51 | [diff] [blame] | 77 | } // namespace webdriver |