1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/test/webdriver/commands/file_upload_command.h" 6 7 #include "base/file_util.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "base/values.h" 10 #include "build/build_config.h" 11 #include "chrome/test/webdriver/commands/response.h" 12 #include "chrome/test/webdriver/webdriver_error.h" 13 #include "chrome/test/webdriver/webdriver_session.h" 14 #include "chrome/test/webdriver/webdriver_util.h" 15 16 namespace webdriver { 17 18 FileUploadCommand::FileUploadCommand( 19 const std::vector<std::string>& path_segments, 20 DictionaryValue* parameters) 21 : WebDriverCommand(path_segments, parameters) { 22 } 23 24 FileUploadCommand::~FileUploadCommand() { 25 } 26 27 bool FileUploadCommand::DoesPost() { 28 return true; 29 } 30 31 void FileUploadCommand::ExecutePost(Response* const response) { 32 std::string base64_zip_data; 33 if (!GetStringParameter("file", &base64_zip_data)) { 34 response->SetError(new Error(kUnknownError, "Missing or invalid 'file'")); 35 return; 36 } 37 std::string zip_data; 38 if (!Base64Decode(base64_zip_data, &zip_data)) { 39 response->SetError(new Error(kUnknownError, "Unable to decode 'file'")); 40 return; 41 } 42 43 base::FilePath upload_dir; 44 if (!file_util::CreateTemporaryDirInDir( 45 session_->temp_dir(), FILE_PATH_LITERAL("upload"), &upload_dir)) { 46 response->SetError(new Error(kUnknownError, "Failed to create temp dir")); 47 return; 48 } 49 std::string error_msg; 50 base::FilePath upload; 51 if (!UnzipSoleFile(upload_dir, zip_data, &upload, &error_msg)) { 52 response->SetError(new Error(kUnknownError, error_msg)); 53 return; 54 } 55 56 #if defined(OS_WIN) 57 response->SetValue(new base::StringValue(WideToUTF8(upload.value()))); 58 #else 59 response->SetValue(new base::StringValue(upload.value())); 60 #endif 61 } 62 63 } // namespace webdriver 64