summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/file_upload_command.cc
blob: 4469147b659bfb7e3140971d4934439ef738b3c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/webdriver/commands/file_upload_command.h"

#include "base/file_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/test/webdriver/commands/response.h"
#include "chrome/test/webdriver/webdriver_error.h"
#include "chrome/test/webdriver/webdriver_session.h"
#include "chrome/test/webdriver/webdriver_util.h"

namespace webdriver {

FileUploadCommand::FileUploadCommand(
    const std::vector<std::string>& path_segments,
    DictionaryValue* parameters)
    : WebDriverCommand(path_segments, parameters) {
}

FileUploadCommand::~FileUploadCommand() {
}

bool FileUploadCommand::DoesPost() {
  return true;
}

void FileUploadCommand::ExecutePost(Response* const response) {
  std::string base64_zip_data;
  if (!GetStringParameter("file", &base64_zip_data)) {
    response->SetError(new Error(kUnknownError, "Missing or invalid 'file'"));
    return;
  }
  std::string zip_data;
  if (!Base64Decode(base64_zip_data, &zip_data)) {
    response->SetError(new Error(kUnknownError, "Unable to decode 'file'"));
    return;
  }

  base::FilePath upload_dir;
  if (!file_util::CreateTemporaryDirInDir(
          session_->temp_dir(), FILE_PATH_LITERAL("upload"), &upload_dir)) {
    response->SetError(new Error(kUnknownError, "Failed to create temp dir"));
    return;
  }
  std::string error_msg;
  base::FilePath upload;
  if (!UnzipSoleFile(upload_dir, zip_data, &upload, &error_msg)) {
    response->SetError(new Error(kUnknownError, error_msg));
    return;
  }

#if defined(OS_WIN)
  response->SetValue(new base::StringValue(WideToUTF8(upload.value())));
#else
  response->SetValue(new base::StringValue(upload.value()));
#endif
}

}  // namespace webdriver