Path: blob/trunk/cpp/iedriver/CommandHandlers/GetCurrentUrlCommandHandler.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 "GetCurrentUrlCommandHandler.h"17#include "errorcodes.h"18#include "logging.h"19#include "../Browser.h"20#include "../IECommandExecutor.h"21#include "../StringUtilities.h"2223namespace webdriver {2425GetCurrentUrlCommandHandler::GetCurrentUrlCommandHandler(void) {26}2728GetCurrentUrlCommandHandler::~GetCurrentUrlCommandHandler(void) {29}3031void GetCurrentUrlCommandHandler::ExecuteInternal(32const IECommandExecutor& executor,33const ParametersMap& command_parameters,34Response* response) {35BrowserHandle browser_wrapper;36int status_code = executor.GetCurrentBrowser(&browser_wrapper);37if (status_code != WD_SUCCESS) {38response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");39return;40}4142// Start with the browser URL.43std::string current_url = browser_wrapper->GetBrowserUrl();44CComPtr<IHTMLDocument2> top_level_document;45browser_wrapper->GetDocument(true, &top_level_document);46if (!top_level_document) {47LOG(WARN) << "Unable to get document from browser. Are you viewing a "48<< "non-HTML document? Falling back to potentially "49<< "inconsistent method for obtaining URL.";50} else {51CComBSTR url;52HRESULT hr = top_level_document->get_URL(&url);53if (FAILED(hr)) {54LOGHR(WARN, hr) << "IHTMLDocument2::get_URL failed.";55}5657std::wstring converted_url(url, ::SysStringLen(url));5859// HACK: If the URL starts with "res://", an internal60// resource was loaded, so don't get the document URL.61if (converted_url.find_first_of(L"res://") != 0) {62current_url = StringUtilities::ToString(converted_url);63}64}6566response->SetSuccessResponse(current_url);67}6869} // namespace webdriver707172