| 278 | } |
| 279 | |
| 280 | void SendKeysCommandHandler::UploadFile(BrowserHandle browser_wrapper, |
| 281 | ElementHandle element_wrapper, |
| 282 | const IECommandExecutor& executor, |
| 283 | const std::wstring& keys, |
| 284 | Response* response) { |
| 285 | bool allows_multiple = this->HasMultipleAttribute(element_wrapper); |
| 286 | |
| 287 | std::vector<std::wstring> file_list; |
| 288 | StringUtilities::Split(keys, L"\n", &file_list); |
| 289 | |
| 290 | if (file_list.size() == 0) { |
| 291 | response->SetErrorResponse(EINVALIDARGUMENT, |
| 292 | "Upload file cannot be an empty string."); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | if (!allows_multiple && file_list.size() > 1) { |
| 297 | response->SetErrorResponse(EINVALIDARGUMENT, |
| 298 | "Attempting to upload multiple files to file upload element without multiple attribute."); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | std::wstring file_directory = L""; |
| 303 | std::wstring file_dialog_keys = L""; |
| 304 | std::vector<std::wstring>::const_iterator iterator = file_list.begin(); |
| 305 | for (; iterator < file_list.end(); ++iterator) { |
| 306 | std::wstring file_name = *iterator; |
| 307 | // Key sequence should be a path and file name. Check |
| 308 | // to see that the file exists before invoking the file |
| 309 | // selection dialog. Note that we also error if the file |
| 310 | // path passed in is valid, but is a directory instead of |
| 311 | // a file. |
| 312 | bool path_exists = ::PathFileExists(file_name.c_str()) == TRUE; |
| 313 | if (!path_exists) { |
| 314 | response->SetErrorResponse(EINVALIDARGUMENT, |
| 315 | "Attempting to upload file '" + StringUtilities::ToString(file_name) + "' which does not exist."); |
| 316 | return; |
| 317 | } |
| 318 | bool path_is_directory = ::PathIsDirectory(file_name.c_str()) == TRUE; |
| 319 | if (path_is_directory) { |
| 320 | response->SetErrorResponse(EINVALIDARGUMENT, |
| 321 | "Attempting to upload file '" + StringUtilities::ToString(file_name) + "' which is a directory."); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (allows_multiple) { |
| 326 | std::vector<wchar_t> file_name_buffer; |
| 327 | StringUtilities::ToBuffer(file_name, &file_name_buffer); |
| 328 | ::PathRemoveFileSpec(&file_name_buffer[0]); |
| 329 | std::wstring current_file_directory = &file_name_buffer[0]; |
| 330 | if (file_directory.size() == 0) { |
| 331 | file_directory = current_file_directory; |
| 332 | } |
| 333 | |
| 334 | if (file_directory != current_file_directory) { |
| 335 | response->SetErrorResponse(EINVALIDARGUMENT, |
| 336 | "Attempting to upload multiple files, but all files must be in the same directory."); |
| 337 | return; |
no test coverage detected