16
16
17
17
#include " SetWindowRectCommandHandler.h"
18
18
#include " errorcodes.h"
19
+ #include " json.h"
19
20
#include " ../Browser.h"
20
21
#include " ../IECommandExecutor.h"
21
22
@@ -37,32 +38,79 @@ void SetWindowRectCommandHandler::ExecuteInternal(
37
38
int y = -1 ;
38
39
39
40
std::string argument_error_message = " " ;
40
- if (!GetNumericParameter (command_parameters, " width" , &width, &argument_error_message)) {
41
- response->SetErrorResponse (ERROR_INVALID_ARGUMENT, argument_error_message);
42
- return ;
41
+ Json::Value width_parameter;
42
+ bool is_width_defined = this ->IsParameterDefined (command_parameters,
43
+ " width" ,
44
+ &width_parameter);
45
+
46
+ if (is_width_defined) {
47
+ if (!this ->GetNumericParameter (" width" ,
48
+ true ,
49
+ width_parameter,
50
+ &width,
51
+ &argument_error_message)) {
52
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT,
53
+ argument_error_message);
54
+ return ;
55
+ }
43
56
}
44
57
45
- if (!GetNumericParameter (command_parameters, " height" , &height, &argument_error_message)) {
46
- response->SetErrorResponse (ERROR_INVALID_ARGUMENT, argument_error_message);
47
- return ;
58
+ Json::Value height_parameter;
59
+ bool is_height_defined = this ->IsParameterDefined (command_parameters,
60
+ " height" ,
61
+ &height_parameter);
62
+ if (is_height_defined) {
63
+ if (!this ->GetNumericParameter (" height" ,
64
+ true ,
65
+ height_parameter,
66
+ &height,
67
+ &argument_error_message)) {
68
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT,
69
+ argument_error_message);
70
+ return ;
71
+ }
48
72
}
49
73
50
- if (!GetNumericParameter (command_parameters, " x" , &x, &argument_error_message)) {
51
- response->SetErrorResponse (ERROR_INVALID_ARGUMENT, argument_error_message);
52
- return ;
74
+ Json::Value x_parameter;
75
+ bool is_x_defined = this ->IsParameterDefined (command_parameters,
76
+ " x" ,
77
+ &x_parameter);
78
+ if (is_x_defined) {
79
+ if (!this ->GetNumericParameter (" x" ,
80
+ false ,
81
+ x_parameter,
82
+ &x,
83
+ &argument_error_message)) {
84
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT,
85
+ argument_error_message);
86
+ return ;
87
+ }
53
88
}
54
89
55
- if (!GetNumericParameter (command_parameters, " y" , &y, &argument_error_message)) {
56
- response->SetErrorResponse (ERROR_INVALID_ARGUMENT, argument_error_message);
57
- return ;
90
+ Json::Value y_parameter;
91
+ bool is_y_defined = this ->IsParameterDefined (command_parameters,
92
+ " y" ,
93
+ &y_parameter);
94
+
95
+ if (is_y_defined) {
96
+ if (!GetNumericParameter (" y" ,
97
+ false ,
98
+ y_parameter,
99
+ &y,
100
+ &argument_error_message)) {
101
+ response->SetErrorResponse (ERROR_INVALID_ARGUMENT,
102
+ argument_error_message);
103
+ return ;
104
+ }
58
105
}
59
106
60
107
int status_code = WD_SUCCESS;
61
108
62
109
BrowserHandle browser_wrapper;
63
110
status_code = executor.GetCurrentBrowser (&browser_wrapper);
64
111
if (status_code != WD_SUCCESS) {
65
- response->SetErrorResponse (ERROR_NO_SUCH_WINDOW, " Error retrieving current window" );
112
+ response->SetErrorResponse (ERROR_NO_SUCH_WINDOW,
113
+ " Error retrieving current window" );
66
114
return ;
67
115
}
68
116
@@ -73,16 +121,22 @@ void SetWindowRectCommandHandler::ExecuteInternal(
73
121
HWND window_handle = browser_wrapper->GetTopLevelWindowHandle ();
74
122
RECT current_window_rect;
75
123
::GetWindowRect (window_handle, ¤t_window_rect);
76
- if (x < 0 || y < 0 ) {
124
+ if (!is_x_defined || !is_y_defined ) {
77
125
x = current_window_rect.left ;
78
126
y = current_window_rect.top ;
79
127
}
80
- if (height < 0 || width < 0 ) {
128
+ if (!is_height_defined || !is_width_defined ) {
81
129
height = current_window_rect.bottom - current_window_rect.top ;
82
130
width = current_window_rect.right - current_window_rect.left ;
83
131
}
84
132
85
- BOOL set_window_pos_result = ::SetWindowPos (window_handle, NULL , x, y, width, height, 0 );
133
+ BOOL set_window_pos_result = ::SetWindowPos (window_handle,
134
+ NULL ,
135
+ x,
136
+ y,
137
+ width,
138
+ height,
139
+ 0 );
86
140
if (!set_window_pos_result) {
87
141
response->SetErrorResponse (ERROR_UNKNOWN_ERROR,
88
142
" Unexpected error setting window size (SetWindowPos API failed)" );
@@ -101,28 +155,49 @@ void SetWindowRectCommandHandler::ExecuteInternal(
101
155
}
102
156
103
157
bool SetWindowRectCommandHandler::GetNumericParameter (
104
- const ParametersMap& command_parameters,
105
158
const std::string& argument_name,
159
+ const bool is_positive_required,
160
+ const Json::Value& parameter_value,
106
161
int * argument_value,
107
162
std::string* error_message) {
108
- ParametersMap::const_iterator parameter_iterator = command_parameters.find (argument_name);
109
- if (parameter_iterator != command_parameters.end ()) {
110
- if (parameter_iterator->second .isNull ()) {
111
- return true ;
112
- }
113
- if (!parameter_iterator->second .isNumeric ()) {
114
- *error_message = argument_name + " must be a numeric parameter." ;
115
- return false ;
116
- }
117
- int value = parameter_iterator->second .asInt ();
118
- if (value < 0 ) {
119
- *error_message = argument_name + " must be a numeric parameter greater than zero." ;
120
- return false ;
121
- }
163
+ int max_value = MAXINT;
164
+ std::string max_value_description = " 2^31 - 1" ;
165
+ int min_value = MININT;
166
+ std::string min_value_description = " -2^31" ;
167
+ if (is_positive_required) {
168
+ min_value = 0 ;
169
+ min_value_description = " zero" ;
170
+ }
171
+ if (!parameter_value.isNumeric ()) {
172
+ *error_message = argument_name + " must be a numeric parameter." ;
173
+ return false ;
174
+ }
175
+ int value = parameter_value.asInt ();
176
+ if (value < min_value) {
177
+ *error_message = argument_name + " must be a numeric parameter greater than " + min_value_description;
178
+ return false ;
179
+ }
180
+ if (value > max_value) {
181
+ *error_message = argument_name + " must be a numeric parameter less than " + max_value_description;
182
+ return false ;
183
+ }
122
184
123
- *argument_value = value;
124
- return true ;
185
+ *argument_value = value;
186
+ return true ;
187
+ }
188
+
189
+ bool SetWindowRectCommandHandler::IsParameterDefined (
190
+ const ParametersMap& command_parameters,
191
+ const std::string& parameter_name,
192
+ Json::Value* parameter_value) {
193
+ ParametersMap::const_iterator parameter_iterator = command_parameters.find (parameter_name);
194
+ if (parameter_iterator == command_parameters.end ()) {
195
+ return false ;
196
+ }
197
+ if (parameter_iterator->second .isNull ()) {
198
+ return false ;
125
199
}
200
+ *parameter_value = parameter_iterator->second ;
126
201
return true ;
127
202
}
128
203
0 commit comments