Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/GetCurrentUrlCommandHandler.cpp
2868 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 "GetCurrentUrlCommandHandler.h"
18
#include "errorcodes.h"
19
#include "logging.h"
20
#include "../Browser.h"
21
#include "../IECommandExecutor.h"
22
#include "../StringUtilities.h"
23
24
namespace webdriver {
25
26
GetCurrentUrlCommandHandler::GetCurrentUrlCommandHandler(void) {
27
}
28
29
GetCurrentUrlCommandHandler::~GetCurrentUrlCommandHandler(void) {
30
}
31
32
void GetCurrentUrlCommandHandler::ExecuteInternal(
33
const IECommandExecutor& executor,
34
const ParametersMap& command_parameters,
35
Response* response) {
36
BrowserHandle browser_wrapper;
37
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
38
if (status_code != WD_SUCCESS) {
39
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");
40
return;
41
}
42
43
// Start with the browser URL.
44
std::string current_url = browser_wrapper->GetBrowserUrl();
45
CComPtr<IHTMLDocument2> top_level_document;
46
browser_wrapper->GetDocument(true, &top_level_document);
47
if (!top_level_document) {
48
LOG(WARN) << "Unable to get document from browser. Are you viewing a "
49
<< "non-HTML document? Falling back to potentially "
50
<< "inconsistent method for obtaining URL.";
51
} else {
52
CComBSTR url;
53
HRESULT hr = top_level_document->get_URL(&url);
54
if (FAILED(hr)) {
55
LOGHR(WARN, hr) << "IHTMLDocument2::get_URL failed.";
56
}
57
58
std::wstring converted_url(url, ::SysStringLen(url));
59
60
// HACK: If the URL starts with "res://", an internal
61
// resource was loaded, so don't get the document URL.
62
if (converted_url.find_first_of(L"res://") != 0) {
63
current_url = StringUtilities::ToString(converted_url);
64
}
65
}
66
67
response->SetSuccessResponse(current_url);
68
}
69
70
} // namespace webdriver
71
72