Commit 5ef4fe1c authored by Erik Jensen's avatar Erik Jensen Committed by Commit Bot

Create new FileOperations interface

This creates a basic FileOperations interface for use with file
transfers that should be relatively straightforward to proxy over IPC.

It also creates a ThreadedFileOperations implementation of the
FileOperations interface using FileProxy, which will eventually replace
FileProxyWrapper. This implementation should be platform agnostic. A
future CL will add an IpcFileOperations implementation for Windows, which
will proxy operations from the network process to a ThreadedFileOperation
instance in the desktop process.

Bug: 679313
Change-Id: Ic7d6ce35f498e469f86f9ed7a8fbcc8bd0cafda9
Reviewed-on: https://chromium-review.googlesource.com/c/1325213Reviewed-by: default avatarJoe Downing <joedow@chromium.org>
Commit-Queue: Erik Jensen <rkjnsn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612377}
parent 7edce81a
...@@ -508,6 +508,7 @@ source_set("unit_tests") { ...@@ -508,6 +508,7 @@ source_set("unit_tests") {
deps = [ deps = [
":host", ":host",
":test_support", ":test_support",
"//remoting/host/file_transfer:unit_tests",
"//remoting/host/it2me:common", "//remoting/host/it2me:common",
"//remoting/host/native_messaging", "//remoting/host/native_messaging",
"//remoting/host/security_key:unit_tests", "//remoting/host/security_key:unit_tests",
......
# Copyright 2018 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.
import("//remoting/build/config/remoting_build.gni")
source_set("file_transfer") {
public = [
"file_operations.h",
"local_file_operations.h",
]
sources = [
"local_file_operations.cc",
]
deps = [
"//base",
"//remoting/protocol",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"local_file_operations_unittest.cc",
]
deps = [
":file_transfer",
"//remoting/protocol",
"//testing/gtest",
]
}
// Copyright 2018 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.
#ifndef REMOTING_HOST_FILE_TRANSFER_FILE_OPERATIONS_H_
#define REMOTING_HOST_FILE_TRANSFER_FILE_OPERATIONS_H_
#include <cstddef>
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/optional.h"
#include "remoting/proto/file_transfer.pb.h"
namespace remoting {
// Interface for reading and writing file transfers.
class FileOperations {
public:
enum State {
// The file has been opened. WriteChunk(), ReadChunk(), and Close() can be
// called.
kReady = 0,
// A file operation is currently being processed. WriteChunk(), ReadChunk(),
// and Close() cannot be called until the state changes back to kReady.
kBusy = 1,
// Close() has been called and succeeded.
kClosed = 2,
// Cancel() has been called or an error occured.
kFailed = 3,
};
class Writer {
public:
// |error| will be nullopt on success or contain error details on failure.
using Callback = base::OnceCallback<void(
base::Optional<protocol::FileTransfer_Error> error)>;
// Destructing FileWriter before calling Close will implicitly call Cancel.
virtual ~Writer() {}
// Writes a chuck to the file. Chunks cannot be queued; the caller must
// wait until callback is called before calling WriteChunk again or calling
// Close.
virtual void WriteChunk(std::string data, Callback callback) = 0;
// Closes the file, flushing any data still in the OS buffer and moving the
// the file to its final location.
virtual void Close(Callback callback) = 0;
// Cancels writing the file. The partially written file will be deleted. May
// be called at any time (including when an operation is pending).
virtual void Cancel() = 0;
virtual State state() = 0;
};
class Reader {
public:
// |error| will be nullopt on success or contain error details on failure.
// In the event of an error, |data| will contain the data successfully read
// before the error, if any.
using Callback = base::OnceCallback<void(
base::Optional<protocol::FileTransfer_Error> error,
std::string data)>;
virtual ~Reader() {}
// Reads a chunk of the given size from the file.
virtual void ReadChunk(std::size_t size, Callback callback) = 0;
virtual void Close() = 0;
virtual State state() = 0;
};
// On success, |error| will be nullopt and |writer| can be used to write data
// to the file. On failure, |error| will contain the error details and
// |writer| will be null.
using WriteFileCallback = base::OnceCallback<void(
base::Optional<protocol::FileTransfer_Error> error,
std::unique_ptr<Writer> writer)>;
// On success, |error| will be nullopt and |reader| can be used to read data
// from the file. On failure, |error| will contain the error details and
// |reader| will be null.
using ReadFileCallback = base::OnceCallback<void(
base::Optional<protocol::FileTransfer_Error> error,
std::unique_ptr<Reader> reader)>;
virtual ~FileOperations() {}
// Starts writing a new file to the default location. This will create a temp
// file at the location, which will be renamed when writing is complete.
virtual void WriteFile(const base::FilePath& filename,
WriteFileCallback callback) = 0;
// Prompt the user to select a file and start reading it.
virtual void ReadFile(ReadFileCallback) = 0;
};
} // namespace remoting
#endif // REMOTING_HOST_FILE_TRANSFER_FILE_OPERATIONS_H_
This diff is collapsed.
// Copyright 2018 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.
#ifndef REMOTING_HOST_FILE_TRANSFER_LOCAL_FILE_OPERATIONS_H_
#define REMOTING_HOST_FILE_TRANSFER_LOCAL_FILE_OPERATIONS_H_
#include "remoting/host/file_transfer/file_operations.h"
namespace remoting {
// Implementation of FileOperations that uses base::FileProxy to perform file
// operations on a dedicated thread.
class LocalFileOperations : public FileOperations {
public:
void WriteFile(const base::FilePath& filename,
WriteFileCallback callback) override;
void ReadFile(ReadFileCallback) override;
};
} // namespace remoting
#endif // REMOTING_HOST_FILE_TRANSFER_LOCAL_FILE_OPERATIONS_H_
// Copyright 2018 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 "remoting/host/file_transfer/local_file_operations.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/containers/queue.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/test/scoped_path_override.h"
#include "base/test/scoped_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
class LocalFileOperationsTest : public testing::Test {
public:
LocalFileOperationsTest();
// testing::Test implementation.
void SetUp() override;
void TearDown() override;
protected:
const base::FilePath kTestFilename =
base::FilePath::FromUTF8Unsafe("test-file.txt");
const base::FilePath kTestFilenameSecondary =
base::FilePath::FromUTF8Unsafe("test-file (1).txt");
const std::string kTestDataOne = "this is the first test string";
const std::string kTestDataTwo = "this is the second test string";
const std::string kTestDataThree = "this is the third test string";
base::FilePath TestDir();
void WriteFile(const base::FilePath& filename,
base::queue<std::string> chunks,
bool close);
void OnFileCreated(base::queue<std::string> chunks,
bool close,
base::Optional<protocol::FileTransfer_Error> error,
std::unique_ptr<FileOperations::Writer> writer);
void OnWriteComplete(base::queue<std::string> remaining_chunks,
bool close,
base::Optional<protocol::FileTransfer_Error> error);
void OnCloseComplete(base::Optional<protocol::FileTransfer_Error> error);
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::ScopedPathOverride scoped_path_override_;
std::unique_ptr<FileOperations> file_operations_;
std::unique_ptr<FileOperations::Writer> file_writer_;
bool operation_completed_ = false;
};
LocalFileOperationsTest::LocalFileOperationsTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::DEFAULT,
base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED),
// Points DIR_USER_DESKTOP at a scoped temporary directory.
scoped_path_override_(base::DIR_USER_DESKTOP),
file_operations_(std::make_unique<LocalFileOperations>()) {}
void LocalFileOperationsTest::SetUp() {}
void LocalFileOperationsTest::TearDown() {}
base::FilePath LocalFileOperationsTest::TestDir() {
base::FilePath result;
EXPECT_TRUE(base::PathService::Get(base::DIR_USER_DESKTOP, &result));
return result;
}
void LocalFileOperationsTest::WriteFile(const base::FilePath& filename,
base::queue<std::string> chunks,
bool close) {
operation_completed_ = false;
file_operations_->WriteFile(
filename,
base::BindOnce(&LocalFileOperationsTest::OnFileCreated,
base::Unretained(this), std::move(chunks), close));
}
void LocalFileOperationsTest::OnFileCreated(
base::queue<std::string> chunks,
bool close,
base::Optional<protocol::FileTransfer_Error> error,
std::unique_ptr<FileOperations::Writer> writer) {
file_writer_ = std::move(writer);
OnWriteComplete(std::move(chunks), close, error);
}
void LocalFileOperationsTest::OnWriteComplete(
base::queue<std::string> remaining_chunks,
bool close,
base::Optional<protocol::FileTransfer_Error> error) {
ASSERT_FALSE(error);
if (!remaining_chunks.empty()) {
std::string next_chunk = std::move(remaining_chunks.front());
remaining_chunks.pop();
file_writer_->WriteChunk(
std::move(next_chunk),
base::BindOnce(&LocalFileOperationsTest::OnWriteComplete,
base::Unretained(this), std::move(remaining_chunks),
close));
} else if (close) {
file_writer_->Close(base::BindOnce(
&LocalFileOperationsTest::OnCloseComplete, base::Unretained(this)));
} else {
operation_completed_ = true;
}
}
void LocalFileOperationsTest::OnCloseComplete(
base::Optional<protocol::FileTransfer_Error> error) {
ASSERT_FALSE(error);
operation_completed_ = true;
}
// Verifies that a file consisting of three chunks can be written successfully.
TEST_F(LocalFileOperationsTest, WritesThreeChunks) {
WriteFile(
kTestFilename,
base::queue<std::string>({kTestDataOne, kTestDataTwo, kTestDataThree}),
true /* close */);
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(operation_completed_);
std::string actual_file_data;
ASSERT_TRUE(base::ReadFileToString(TestDir().Append(kTestFilename),
&actual_file_data));
ASSERT_EQ(kTestDataOne + kTestDataTwo + kTestDataThree, actual_file_data);
}
// Verifies that LocalFileOperations will write to a file named
// "file (1).txt" if "file.txt" already exists.
TEST_F(LocalFileOperationsTest, RenamesFileIfExists) {
WriteFile(kTestFilename, base::queue<std::string>({kTestDataOne}),
true /* close */);
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(operation_completed_);
WriteFile(kTestFilename, base::queue<std::string>({kTestDataTwo}),
true /* close */);
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(operation_completed_);
std::string actual_file_data_one;
ASSERT_TRUE(base::ReadFileToString(TestDir().Append(kTestFilename),
&actual_file_data_one));
ASSERT_EQ(kTestDataOne, actual_file_data_one);
std::string actual_file_data_two;
ASSERT_TRUE(base::ReadFileToString(TestDir().Append(kTestFilenameSecondary),
&actual_file_data_two));
ASSERT_EQ(kTestDataTwo, actual_file_data_two);
}
// Verifies that calling Cancel() deletes the temporary file.
TEST_F(LocalFileOperationsTest, CancelDeletesTemp) {
WriteFile(
kTestFilename,
base::queue<std::string>({kTestDataOne, kTestDataTwo, kTestDataThree}),
false /* close */);
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(operation_completed_);
file_writer_->Cancel();
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(base::IsDirectoryEmpty(TestDir()));
}
// Verifies that Cancel() can be called while an operation is pending.
TEST_F(LocalFileOperationsTest, CancelsWhileOperationPending) {
WriteFile(kTestFilename, base::queue<std::string>({kTestDataOne}),
false /* close */);
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(operation_completed_);
file_writer_->WriteChunk(kTestDataTwo, base::DoNothing());
file_writer_->Cancel();
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(base::IsDirectoryEmpty(TestDir()));
}
// Verifies that dropping an unclosed FileWriter is the same as canceling it.
TEST_F(LocalFileOperationsTest, CancelsWhenDestructed) {
WriteFile(
kTestFilename,
base::queue<std::string>({kTestDataOne, kTestDataTwo, kTestDataThree}),
false /* close */);
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(operation_completed_);
file_writer_->WriteChunk(kTestDataTwo, base::DoNothing());
file_writer_.reset();
scoped_task_environment_.RunUntilIdle();
ASSERT_TRUE(base::IsDirectoryEmpty(TestDir()));
}
} // namespace remoting
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment