| Index: components/arc/test/fake_file_system_instance.cc
|
| diff --git a/components/arc/test/fake_file_system_instance.cc b/components/arc/test/fake_file_system_instance.cc
|
| index a0a0aca33a31f367c5ebfe1b89565a64c568eced..d4c94371ac57ccccc46698a546fd0baf4cc4ff5d 100644
|
| --- a/components/arc/test/fake_file_system_instance.cc
|
| +++ b/components/arc/test/fake_file_system_instance.cc
|
| @@ -4,48 +4,199 @@
|
|
|
| #include "components/arc/test/fake_file_system_instance.h"
|
|
|
| +#include <string.h>
|
| +#include <unistd.h>
|
| +
|
| #include "base/bind.h"
|
| +#include "base/files/file.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/files/scoped_file.h"
|
| #include "base/location.h"
|
| +#include "base/logging.h"
|
| #include "base/optional.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| -#include "mojo/public/cpp/system/handle.h"
|
| +#include "mojo/edk/embedder/embedder.h"
|
|
|
| namespace arc {
|
|
|
| -FakeFileSystemInstance::FakeFileSystemInstance() = default;
|
| +namespace {
|
|
|
| -FakeFileSystemInstance::~FakeFileSystemInstance() = default;
|
| +base::ScopedFD CreateRegularFileDescriptor(const std::string& content,
|
| + const base::FilePath& temp_dir) {
|
| + base::FilePath path;
|
| + bool create_success = base::CreateTemporaryFileInDir(temp_dir, &path);
|
| + DCHECK(create_success);
|
| + int written_size = base::WriteFile(path, content.data(), content.size());
|
| + DCHECK_EQ(static_cast<int>(content.size()), written_size);
|
| + base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
|
| + DCHECK(file.IsValid());
|
| + return base::ScopedFD(file.TakePlatformFile());
|
| +}
|
|
|
| -void FakeFileSystemInstance::GetChildDocuments(
|
| +base::ScopedFD CreateStreamFileDescriptor(const std::string& content) {
|
| + int fds[2];
|
| + int ret = pipe(fds);
|
| + DCHECK_EQ(0, ret);
|
| + base::ScopedFD fd_read(fds[0]);
|
| + base::ScopedFD fd_write(fds[1]);
|
| + bool write_success =
|
| + base::WriteFileDescriptor(fd_write.get(), content.data(), content.size());
|
| + DCHECK(write_success);
|
| + return fd_read;
|
| +}
|
| +
|
| +mojom::DocumentPtr MakeDocument(const FakeFileSystemInstance::Document& doc) {
|
| + mojom::DocumentPtr document = mojom::Document::New();
|
| + document->document_id = doc.document_id;
|
| + document->display_name = doc.display_name;
|
| + document->mime_type = doc.mime_type;
|
| + document->size = doc.size;
|
| + document->last_modified = doc.last_modified;
|
| + return document;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +FakeFileSystemInstance::File::File(const File& rhs) = default;
|
| +
|
| +FakeFileSystemInstance::File::File(const std::string& url,
|
| + const std::string& content,
|
| + Seekable seekable)
|
| + : url(url), content(content), seekable(seekable) {}
|
| +
|
| +FakeFileSystemInstance::File::~File() = default;
|
| +
|
| +FakeFileSystemInstance::Document::Document(const Document& rhs) = default;
|
| +
|
| +FakeFileSystemInstance::Document::Document(
|
| const std::string& authority,
|
| const std::string& document_id,
|
| - const GetChildDocumentsCallback& callback) {
|
| - base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| - FROM_HERE, base::Bind(callback, base::nullopt));
|
| + const std::string& parent_document_id,
|
| + const std::string& display_name,
|
| + const std::string& mime_type,
|
| + int64_t size,
|
| + uint64_t last_modified)
|
| + : authority(authority),
|
| + document_id(document_id),
|
| + parent_document_id(parent_document_id),
|
| + display_name(display_name),
|
| + mime_type(mime_type),
|
| + size(size),
|
| + last_modified(last_modified) {}
|
| +
|
| +FakeFileSystemInstance::Document::~Document() = default;
|
| +
|
| +FakeFileSystemInstance::FakeFileSystemInstance() {
|
| + bool temp_dir_created = temp_dir_.CreateUniqueTempDir();
|
| + DCHECK(temp_dir_created);
|
| }
|
|
|
| -void FakeFileSystemInstance::GetDocument(const std::string& authority,
|
| - const std::string& document_id,
|
| - const GetDocumentCallback& callback) {
|
| - base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| - FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr())));
|
| +FakeFileSystemInstance::~FakeFileSystemInstance() {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| +}
|
| +
|
| +void FakeFileSystemInstance::AddFile(const File& file) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK_EQ(0u, files_.count(std::string(file.url)));
|
| + files_.insert(std::make_pair(std::string(file.url), file));
|
| +}
|
| +
|
| +void FakeFileSystemInstance::AddDocument(const Document& document) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + DocumentKey key(document.authority, document.document_id);
|
| + DCHECK_EQ(0u, documents_.count(key));
|
| + documents_.insert(std::make_pair(key, document));
|
| + child_documents_[key]; // Allocate a vector.
|
| + if (!document.parent_document_id.empty()) {
|
| + DocumentKey parent_key(document.authority, document.parent_document_id);
|
| + DCHECK_EQ(1u, documents_.count(parent_key));
|
| + child_documents_[parent_key].push_back(key);
|
| + }
|
| }
|
|
|
| void FakeFileSystemInstance::GetFileSize(const std::string& url,
|
| const GetFileSizeCallback& callback) {
|
| - base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
|
| - base::Bind(callback, -1));
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + auto iter = files_.find(url);
|
| + if (iter == files_.end()) {
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
|
| + base::Bind(callback, -1));
|
| + return;
|
| + }
|
| + const File& file = iter->second;
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(callback, file.content.size()));
|
| }
|
|
|
| void FakeFileSystemInstance::OpenFileToRead(
|
| const std::string& url,
|
| const OpenFileToReadCallback& callback) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + auto iter = files_.find(url);
|
| + if (iter == files_.end()) {
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle())));
|
| + return;
|
| + }
|
| + const File& file = iter->second;
|
| + base::ScopedFD fd =
|
| + file.seekable == File::Seekable::YES
|
| + ? CreateRegularFileDescriptor(file.content, temp_dir_.GetPath())
|
| + : CreateStreamFileDescriptor(file.content);
|
| + mojo::edk::ScopedPlatformHandle platform_handle(
|
| + mojo::edk::PlatformHandle(fd.release()));
|
| + MojoHandle wrapped_handle;
|
| + MojoResult result = mojo::edk::CreatePlatformHandleWrapper(
|
| + std::move(platform_handle), &wrapped_handle);
|
| + DCHECK_EQ(MOJO_RESULT_OK, result);
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle(
|
| + mojo::Handle(wrapped_handle)))));
|
| +}
|
| +
|
| +void FakeFileSystemInstance::GetDocument(const std::string& authority,
|
| + const std::string& document_id,
|
| + const GetDocumentCallback& callback) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + auto iter = documents_.find(DocumentKey(authority, document_id));
|
| + if (iter == documents_.end()) {
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr())));
|
| + return;
|
| + }
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(callback, base::Passed(MakeDocument(iter->second))));
|
| +}
|
| +
|
| +void FakeFileSystemInstance::GetChildDocuments(
|
| + const std::string& authority,
|
| + const std::string& parent_document_id,
|
| + const GetChildDocumentsCallback& callback) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| + auto child_iter =
|
| + child_documents_.find(DocumentKey(authority, parent_document_id));
|
| + if (child_iter == child_documents_.end()) {
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(callback, base::nullopt));
|
| + return;
|
| + }
|
| + std::vector<mojom::DocumentPtr> children;
|
| + for (const auto& child_key : child_iter->second) {
|
| + auto doc_iter = documents_.find(child_key);
|
| + DCHECK(doc_iter != documents_.end());
|
| + children.emplace_back(MakeDocument(doc_iter->second));
|
| + }
|
| base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| - FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle())));
|
| + FROM_HERE,
|
| + base::Bind(callback,
|
| + base::Passed(base::make_optional(std::move(children)))));
|
| }
|
|
|
| void FakeFileSystemInstance::RequestMediaScan(
|
| const std::vector<std::string>& paths) {
|
| + DCHECK(thread_checker_.CalledOnValidThread());
|
| // Do nothing and pretend we scaned them.
|
| }
|
|
|
|
|