Path: blob/trunk/cpp/iedriver/ElementRepository.cpp
2867 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516#include "ElementRepository.h"1718#include "logging.h"19#include "errorcodes.h"2021#include "Element.h"2223namespace webdriver {2425ElementRepository::ElementRepository(void) {26}2728ElementRepository::~ElementRepository(void) {29}3031int ElementRepository::GetManagedElement(const std::string& element_id,32ElementHandle* element_wrapper) const {33LOG(TRACE) << "Entering ElementRepository::GetManagedElement";3435ElementMap::const_iterator found_iterator = this->managed_elements_.find(element_id);36if (found_iterator == this->managed_elements_.end()) {37LOG(DEBUG) << "Unable to find managed element with id " << element_id;38return ENOSUCHELEMENT;39}4041*element_wrapper = found_iterator->second;42return WD_SUCCESS;43}4445bool ElementRepository::AddManagedElement(ElementHandle element_wrapper) {46this->managed_elements_[element_wrapper->element_id()] = element_wrapper;47return true;48}4950bool ElementRepository::AddManagedElement(BrowserHandle current_browser,51IHTMLElement* element,52ElementHandle* element_wrapper) {53LOG(TRACE) << "Entering ElementRepository::AddManagedElement";5455bool element_already_managed = this->IsElementManaged(element, element_wrapper);56if (!element_already_managed) {57LOG(DEBUG) << "Element is not yet managed";58HWND containing_window_handle = NULL;59if (current_browser != NULL) {60containing_window_handle = current_browser->GetContentWindowHandle();61}62ElementHandle new_wrapper(new Element(element,63containing_window_handle));64this->managed_elements_[new_wrapper->element_id()] = new_wrapper;65*element_wrapper = new_wrapper;66} else {67LOG(DEBUG) << "Element is already managed";68}69return !element_already_managed;70}7172bool ElementRepository::IsElementManaged(IHTMLElement* element,73ElementHandle* element_wrapper) {74// TODO: This method needs much work. If we are already managing a75// given element, we don't want to assign it a new ID, but to find76// out if we're managing it already, we need to compare to all of77// the elements already in our map, which means iterating through78// the map. For long-running tests, this means the addition of a79// new managed element may take longer and longer as we have no80// good algorithm for removing dead elements from the map.81ElementMap::iterator it = this->managed_elements_.begin();82for (; it != this->managed_elements_.end(); ++it) {83if (it->second->element() == element) {84*element_wrapper = it->second;85return true;86}87}88return false;89}9091void ElementRepository::RemoveManagedElement(const std::string& element_id) {92LOG(TRACE) << "Entering ElementRepository::RemoveManagedElement";9394ElementMap::iterator found_iterator = this->managed_elements_.find(element_id);95if (found_iterator != this->managed_elements_.end()) {96this->managed_elements_.erase(element_id);97} else {98LOG(DEBUG) << "Unable to find element to remove with id " << element_id;99}100}101102void ElementRepository::ListManagedElements() {103LOG(TRACE) << "Entering ElementRepository::ListManagedElements";104105ElementMap::iterator it = this->managed_elements_.begin();106for (; it != this->managed_elements_.end(); ++it) {107LOG(DEBUG) << "Managed element: " << it->first;108}109}110111void ElementRepository::ClearCache() {112// Logic explanation: We can't just remove the elements from the113// managed elements map, within the loop as that would invalidate114// the iterator. So we add the keys to a vector, and use the vector115// to remove the elements from the map.116std::vector<std::string> bad_elements;117ElementMap::const_iterator managed_iterator = this->managed_elements_.begin();118ElementMap::const_iterator last_managed_element = this->managed_elements_.end();119for(; managed_iterator != last_managed_element; ++managed_iterator) {120if (!managed_iterator->second->IsAttachedToDom()) {121bad_elements.push_back(managed_iterator->first);122}123}124125LOG(DEBUG) << "Refreshing managed element cache. Found "126<< bad_elements.size()127<< " to remove from cache.";128129std::vector<std::string>::const_iterator id_iterator = bad_elements.begin();130std::vector<std::string>::const_iterator last_id = bad_elements.end();131for (; id_iterator != last_id; ++id_iterator) {132this->RemoveManagedElement(*id_iterator);133}134}135136void ElementRepository::Clear() {137this->managed_elements_.clear();138}139140} // namespace webdriver141142143