OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/webdriver/session_manager.h" | 5 #include "chrome/test/webdriver/session_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/test/webdriver/utility_functions.h" | 8 #include "chrome/test/webdriver/utility_functions.h" |
9 #include "net/base/net_util.h" | 9 #include "net/base/net_util.h" |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 if (host_entry) | 23 if (host_entry) |
24 hostname = host_entry->h_name; | 24 hostname = host_entry->h_name; |
25 } | 25 } |
26 #endif | 26 #endif |
27 if (hostname.empty()) { | 27 if (hostname.empty()) { |
28 hostname = "localhost"; | 28 hostname = "localhost"; |
29 } | 29 } |
30 return hostname + ":" + port_; | 30 return hostname + ":" + port_; |
31 } | 31 } |
32 | 32 |
33 Session* SessionManager::Create() { | 33 void SessionManager::Add(Session* session) { |
34 std::string id = GenerateRandomID(); | |
35 { | |
36 base::AutoLock lock(map_lock_); | |
37 if (map_.find(id) != map_.end()) { | |
38 LOG(ERROR) << "Failed to generate a unique session ID"; | |
39 return NULL; | |
40 } | |
41 } | |
42 | |
43 Session* session = new Session(id); | |
44 base::AutoLock lock(map_lock_); | 34 base::AutoLock lock(map_lock_); |
45 map_[id] = session; | 35 map_[session->id()] = session; |
46 return session; | |
47 } | 36 } |
48 | 37 |
49 bool SessionManager::Has(const std::string& id) const { | 38 bool SessionManager::Has(const std::string& id) const { |
50 base::AutoLock lock(map_lock_); | 39 base::AutoLock lock(map_lock_); |
51 return map_.find(id) != map_.end(); | 40 return map_.find(id) != map_.end(); |
52 } | 41 } |
53 | 42 |
54 bool SessionManager::Delete(const std::string& id) { | 43 bool SessionManager::Remove(const std::string& id) { |
55 std::map<std::string, Session*>::iterator it; | 44 std::map<std::string, Session*>::iterator it; |
56 | |
57 Session* session; | 45 Session* session; |
58 { | 46 base::AutoLock lock(map_lock_); |
59 base::AutoLock lock(map_lock_); | 47 it = map_.find(id); |
60 it = map_.find(id); | 48 if (it == map_.end()) { |
61 if (it == map_.end()) { | 49 VLOG(1) << "No such session with ID " << id; |
62 VLOG(1) << "No such session with ID " << id; | 50 return false; |
63 return false; | |
64 } | |
65 session = it->second; | |
66 map_.erase(it); | |
67 } | 51 } |
68 | 52 session = it->second; |
69 VLOG(1) << "Deleting session with ID " << id; | 53 map_.erase(it); |
70 delete session; | |
71 return true; | 54 return true; |
72 } | 55 } |
73 | 56 |
74 Session* SessionManager::GetSession(const std::string& id) const { | 57 Session* SessionManager::GetSession(const std::string& id) const { |
75 std::map<std::string, Session*>::const_iterator it; | 58 std::map<std::string, Session*>::const_iterator it; |
76 base::AutoLock lock(map_lock_); | 59 base::AutoLock lock(map_lock_); |
77 it = map_.find(id); | 60 it = map_.find(id); |
78 if (it == map_.end()) { | 61 if (it == map_.end()) { |
79 VLOG(1) << "No such session with ID " << id; | 62 VLOG(1) << "No such session with ID " << id; |
80 return NULL; | 63 return NULL; |
81 } | 64 } |
82 return it->second; | 65 return it->second; |
83 } | 66 } |
84 | 67 |
85 SessionManager::SessionManager() : port_("") {} | 68 SessionManager::SessionManager() : port_("") {} |
86 | 69 |
87 SessionManager::~SessionManager() {} | 70 SessionManager::~SessionManager() {} |
88 | 71 |
89 // static | 72 // static |
90 SessionManager* SessionManager::GetInstance() { | 73 SessionManager* SessionManager::GetInstance() { |
91 return Singleton<SessionManager>::get(); | 74 return Singleton<SessionManager>::get(); |
92 } | 75 } |
93 | 76 |
94 } // namespace webdriver | 77 } // namespace webdriver |
OLD | NEW |