Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/ElementRepository.cpp
2867 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "ElementRepository.h"
18
19
#include "logging.h"
20
#include "errorcodes.h"
21
22
#include "Element.h"
23
24
namespace webdriver {
25
26
ElementRepository::ElementRepository(void) {
27
}
28
29
ElementRepository::~ElementRepository(void) {
30
}
31
32
int ElementRepository::GetManagedElement(const std::string& element_id,
33
ElementHandle* element_wrapper) const {
34
LOG(TRACE) << "Entering ElementRepository::GetManagedElement";
35
36
ElementMap::const_iterator found_iterator = this->managed_elements_.find(element_id);
37
if (found_iterator == this->managed_elements_.end()) {
38
LOG(DEBUG) << "Unable to find managed element with id " << element_id;
39
return ENOSUCHELEMENT;
40
}
41
42
*element_wrapper = found_iterator->second;
43
return WD_SUCCESS;
44
}
45
46
bool ElementRepository::AddManagedElement(ElementHandle element_wrapper) {
47
this->managed_elements_[element_wrapper->element_id()] = element_wrapper;
48
return true;
49
}
50
51
bool ElementRepository::AddManagedElement(BrowserHandle current_browser,
52
IHTMLElement* element,
53
ElementHandle* element_wrapper) {
54
LOG(TRACE) << "Entering ElementRepository::AddManagedElement";
55
56
bool element_already_managed = this->IsElementManaged(element, element_wrapper);
57
if (!element_already_managed) {
58
LOG(DEBUG) << "Element is not yet managed";
59
HWND containing_window_handle = NULL;
60
if (current_browser != NULL) {
61
containing_window_handle = current_browser->GetContentWindowHandle();
62
}
63
ElementHandle new_wrapper(new Element(element,
64
containing_window_handle));
65
this->managed_elements_[new_wrapper->element_id()] = new_wrapper;
66
*element_wrapper = new_wrapper;
67
} else {
68
LOG(DEBUG) << "Element is already managed";
69
}
70
return !element_already_managed;
71
}
72
73
bool ElementRepository::IsElementManaged(IHTMLElement* element,
74
ElementHandle* element_wrapper) {
75
// TODO: This method needs much work. If we are already managing a
76
// given element, we don't want to assign it a new ID, but to find
77
// out if we're managing it already, we need to compare to all of
78
// the elements already in our map, which means iterating through
79
// the map. For long-running tests, this means the addition of a
80
// new managed element may take longer and longer as we have no
81
// good algorithm for removing dead elements from the map.
82
ElementMap::iterator it = this->managed_elements_.begin();
83
for (; it != this->managed_elements_.end(); ++it) {
84
if (it->second->element() == element) {
85
*element_wrapper = it->second;
86
return true;
87
}
88
}
89
return false;
90
}
91
92
void ElementRepository::RemoveManagedElement(const std::string& element_id) {
93
LOG(TRACE) << "Entering ElementRepository::RemoveManagedElement";
94
95
ElementMap::iterator found_iterator = this->managed_elements_.find(element_id);
96
if (found_iterator != this->managed_elements_.end()) {
97
this->managed_elements_.erase(element_id);
98
} else {
99
LOG(DEBUG) << "Unable to find element to remove with id " << element_id;
100
}
101
}
102
103
void ElementRepository::ListManagedElements() {
104
LOG(TRACE) << "Entering ElementRepository::ListManagedElements";
105
106
ElementMap::iterator it = this->managed_elements_.begin();
107
for (; it != this->managed_elements_.end(); ++it) {
108
LOG(DEBUG) << "Managed element: " << it->first;
109
}
110
}
111
112
void ElementRepository::ClearCache() {
113
// Logic explanation: We can't just remove the elements from the
114
// managed elements map, within the loop as that would invalidate
115
// the iterator. So we add the keys to a vector, and use the vector
116
// to remove the elements from the map.
117
std::vector<std::string> bad_elements;
118
ElementMap::const_iterator managed_iterator = this->managed_elements_.begin();
119
ElementMap::const_iterator last_managed_element = this->managed_elements_.end();
120
for(; managed_iterator != last_managed_element; ++managed_iterator) {
121
if (!managed_iterator->second->IsAttachedToDom()) {
122
bad_elements.push_back(managed_iterator->first);
123
}
124
}
125
126
LOG(DEBUG) << "Refreshing managed element cache. Found "
127
<< bad_elements.size()
128
<< " to remove from cache.";
129
130
std::vector<std::string>::const_iterator id_iterator = bad_elements.begin();
131
std::vector<std::string>::const_iterator last_id = bad_elements.end();
132
for (; id_iterator != last_id; ++id_iterator) {
133
this->RemoveManagedElement(*id_iterator);
134
}
135
}
136
137
void ElementRepository::Clear() {
138
this->managed_elements_.clear();
139
}
140
141
} // namespace webdriver
142
143