Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/CreateNewWindowCommandHandler.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 "CreateNewWindowCommandHandler.h"
18
#include "errorcodes.h"
19
#include "../Browser.h"
20
#include "../IECommandExecutor.h"
21
#include "../WebDriverConstants.h"
22
23
namespace webdriver {
24
25
CreateNewWindowCommandHandler::CreateNewWindowCommandHandler(void) {
26
}
27
28
CreateNewWindowCommandHandler::~CreateNewWindowCommandHandler(void) {
29
}
30
31
void CreateNewWindowCommandHandler::ExecuteInternal(
32
const IECommandExecutor& executor,
33
const ParametersMap& command_parameters,
34
Response* response) {
35
ParametersMap::const_iterator type_parameter_iterator = command_parameters.find("type");
36
if (type_parameter_iterator == command_parameters.end()) {
37
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: type");
38
return;
39
}
40
if (!type_parameter_iterator->second.isString() &&
41
!type_parameter_iterator->second.isNull()) {
42
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "type parameter must be a string or null");
43
return;
44
}
45
46
std::string window_type = WINDOW_WINDOW_TYPE;
47
if (type_parameter_iterator->second.isString()) {
48
std::string parameter_value = type_parameter_iterator->second.asString();
49
if (parameter_value == TAB_WINDOW_TYPE) {
50
window_type = TAB_WINDOW_TYPE;
51
}
52
}
53
54
BrowserHandle browser_wrapper;
55
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
56
if (status_code != WD_SUCCESS) {
57
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW,
58
"Error retrieving current window");
59
return;
60
}
61
62
IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
63
std::string new_window_handle = mutable_executor.OpenNewBrowsingContext(window_type);
64
if (new_window_handle.size() == 0) {
65
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "New window not created");
66
return;
67
}
68
if (window_type == WINDOW_WINDOW_TYPE) {
69
BrowserHandle tmp_browser;
70
executor.GetManagedBrowser(new_window_handle, &tmp_browser);
71
std::string error_message = "";
72
tmp_browser->NavigateToUrl("about:blank", &error_message);
73
}
74
Json::Value result;
75
result["handle"] = new_window_handle;
76
result["type"] = window_type;
77
response->SetSuccessResponse(result);
78
}
79
80
} // namespace webdriver
81
82