Path: blob/trunk/cpp/iedriver/CommandHandlers/GetActiveElementCommandHandler.cpp
2868 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 "GetActiveElementCommandHandler.h"17#include "errorcodes.h"18#include "../Browser.h"19#include "../Element.h"20#include "../IECommandExecutor.h"2122namespace webdriver {2324GetActiveElementCommandHandler::GetActiveElementCommandHandler(void) {25}2627GetActiveElementCommandHandler::~GetActiveElementCommandHandler(void) {28}2930void GetActiveElementCommandHandler::ExecuteInternal(31const IECommandExecutor& executor,32const ParametersMap& command_parameters,33Response* response) {34BrowserHandle browser_wrapper;35int status_code = executor.GetCurrentBrowser(&browser_wrapper);36if (status_code != WD_SUCCESS) {37response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");38return;39}4041CComPtr<IHTMLDocument2> doc;42browser_wrapper->GetDocument(&doc);43if (!doc) {44response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Document is not found");45return;46}4748CComPtr<IHTMLElement> element(NULL);49HRESULT hr = doc->get_activeElement(&element);5051if (FAILED(hr)) {52// For some contentEditable frames, the <body> element will be the53// active element. However, to properly have focus, we must explicitly54// set focus to the element.55CComPtr<IHTMLBodyElement> body_element;56HRESULT body_hr = element->QueryInterface<IHTMLBodyElement>(&body_element);57if (body_element) {58CComPtr<IHTMLElement2> body_element2;59body_element->QueryInterface<IHTMLElement2>(&body_element2);60body_element2->focus();61}62}6364// If we don't have an element at this point, but the document65// has a body element, we should return a null result, as that's66// what document.activeElement() returns. However, if there is no67// body element, throw no such element.68if (!element) {69CComPtr<IHTMLElement> body;70hr = doc->get_body(&body);71if (body) {72response->SetSuccessResponse(Json::Value::null);73} else {74response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "No active element found, and no body element present.");75}76return;77}7879if (element) {80IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);81ElementHandle element_wrapper;82mutable_executor.AddManagedElement(element, &element_wrapper);83response->SetSuccessResponse(element_wrapper->ConvertToJson());84} else {85response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "An unexpected error occurred getting the active element");86}87}8889} // namespace webdriver909192