Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/arc/test/fake_file_system_instance.h" | 5 #include "components/arc/test/fake_file_system_instance.h" |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 #include <unistd.h> | |
| 9 | |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/files/scoped_file.h" | |
| 8 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/logging.h" | |
| 9 #include "base/optional.h" | 17 #include "base/optional.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "mojo/public/cpp/system/handle.h" | 19 #include "mojo/edk/embedder/embedder.h" |
| 12 | 20 |
| 13 namespace arc { | 21 namespace arc { |
| 14 | 22 |
| 15 FakeFileSystemInstance::FakeFileSystemInstance() = default; | 23 namespace { |
| 16 | 24 |
| 17 FakeFileSystemInstance::~FakeFileSystemInstance() = default; | 25 base::ScopedFD CreateRegularFileDescriptor(const std::string& content, |
| 18 | 26 const base::FilePath& temp_dir) { |
| 19 void FakeFileSystemInstance::GetChildDocuments( | 27 base::FilePath path; |
| 28 bool create_success = base::CreateTemporaryFileInDir(temp_dir, &path); | |
| 29 DCHECK(create_success); | |
| 30 int written_size = base::WriteFile(path, content.c_str(), content.size()); | |
|
hidehiko
2017/01/27 15:50:00
s/content.c_str()/constent.data()/
Here, std::str
Shuhei Takahashi
2017/01/30 02:27:35
I don't have any preference on this.
| |
| 31 DCHECK_EQ(static_cast<int>(content.size()), written_size); | |
| 32 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 33 DCHECK(file.IsValid()); | |
| 34 return base::ScopedFD(file.TakePlatformFile()); | |
| 35 } | |
| 36 | |
| 37 base::ScopedFD CreateStreamFileDescriptor(const std::string& content) { | |
| 38 int fds[2]; | |
| 39 int ret = pipe(fds); | |
| 40 DCHECK_EQ(0, ret); | |
| 41 base::ScopedFD fd_read(fds[0]); | |
| 42 base::ScopedFD fd_write(fds[1]); | |
| 43 bool write_success = base::WriteFileDescriptor( | |
| 44 fd_write.get(), content.c_str(), content.size()); | |
|
hidehiko
2017/01/27 15:50:00
Ditto.
Shuhei Takahashi
2017/01/30 02:27:35
Done.
| |
| 45 DCHECK(write_success); | |
| 46 return fd_read; | |
| 47 } | |
| 48 | |
| 49 mojom::DocumentPtr MakeDocument(const FakeFileSystemInstance::Document& doc) { | |
| 50 mojom::DocumentPtr document = mojom::Document::New(); | |
| 51 document->document_id = doc.document_id; | |
| 52 document->display_name = doc.display_name; | |
| 53 document->mime_type = doc.mime_type; | |
| 54 document->size = doc.size; | |
| 55 document->last_modified = doc.last_modified; | |
| 56 return document; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 FakeFileSystemInstance::File::File() = default; | |
| 62 | |
| 63 FakeFileSystemInstance::File::File(const File& rhs) = default; | |
| 64 | |
| 65 FakeFileSystemInstance::File::File(const std::string& url, | |
| 66 const std::string& content, | |
| 67 Seekable seekable) | |
| 68 : url(url), content(content), seekable(seekable) {} | |
| 69 | |
| 70 FakeFileSystemInstance::File::~File() = default; | |
| 71 | |
| 72 FakeFileSystemInstance::Document::Document() = default; | |
| 73 | |
| 74 FakeFileSystemInstance::Document::Document(const Document& rhs) = default; | |
| 75 | |
| 76 FakeFileSystemInstance::Document::Document( | |
| 20 const std::string& authority, | 77 const std::string& authority, |
| 21 const std::string& document_id, | 78 const std::string& document_id, |
| 22 const GetChildDocumentsCallback& callback) { | 79 const std::string& parent_document_id, |
| 23 base::ThreadTaskRunnerHandle::Get()->PostTask( | 80 const std::string& display_name, |
| 24 FROM_HERE, base::Bind(callback, base::nullopt)); | 81 const std::string& mime_type, |
| 82 int64_t size, | |
| 83 uint64_t last_modified) | |
| 84 : authority(authority), | |
| 85 document_id(document_id), | |
| 86 parent_document_id(parent_document_id), | |
| 87 display_name(display_name), | |
| 88 mime_type(mime_type), | |
| 89 size(size), | |
| 90 last_modified(last_modified) {} | |
| 91 | |
| 92 FakeFileSystemInstance::Document::~Document() = default; | |
| 93 | |
| 94 FakeFileSystemInstance::FakeFileSystemInstance() { | |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 96 bool temp_dir_created = temp_dir_.CreateUniqueTempDir(); | |
| 97 DCHECK(temp_dir_created); | |
| 98 } | |
| 99 | |
| 100 FakeFileSystemInstance::~FakeFileSystemInstance() { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 } | |
| 103 | |
| 104 void FakeFileSystemInstance::AddFile(const File& file) { | |
| 105 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 106 DCHECK_EQ(0u, files_.count(std::string(file.url))); | |
| 107 files_[std::string(file.url)] = file; | |
| 108 } | |
| 109 | |
| 110 void FakeFileSystemInstance::AddDocument(const Document& document) { | |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 112 DocumentKey key(document.authority, document.document_id); | |
| 113 DCHECK_EQ(0u, documents_.count(key)); | |
| 114 documents_[key] = document; | |
| 115 child_documents_[key]; // Allocate a vector. | |
| 116 if (!document.parent_document_id.empty()) { | |
| 117 DocumentKey parent_key(document.authority, document.parent_document_id); | |
| 118 DCHECK_EQ(1u, documents_.count(parent_key)); | |
| 119 child_documents_[parent_key].push_back(key); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 void FakeFileSystemInstance::GetFileSize(const std::string& url, | |
| 124 const GetFileSizeCallback& callback) { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 126 auto iter = files_.find(url); | |
| 127 if (iter == files_.end()) { | |
| 128 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 129 base::Bind(callback, -1)); | |
| 130 return; | |
| 131 } | |
| 132 const File& file = iter->second; | |
| 133 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 134 FROM_HERE, base::Bind(callback, file.content.size())); | |
| 135 } | |
| 136 | |
| 137 void FakeFileSystemInstance::OpenFileToRead( | |
| 138 const std::string& url, | |
| 139 const OpenFileToReadCallback& callback) { | |
| 140 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 141 auto iter = files_.find(url); | |
| 142 if (iter == files_.end()) { | |
| 143 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 144 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); | |
| 145 return; | |
| 146 } | |
| 147 const File& file = iter->second; | |
| 148 base::ScopedFD fd = | |
| 149 file.seekable == File::SEEKABLE | |
| 150 ? CreateRegularFileDescriptor(file.content, temp_dir_.GetPath()) | |
| 151 : CreateStreamFileDescriptor(file.content); | |
| 152 mojo::edk::ScopedPlatformHandle platform_handle( | |
| 153 mojo::edk::PlatformHandle(fd.release())); | |
| 154 MojoHandle wrapped_handle; | |
| 155 MojoResult result = mojo::edk::CreatePlatformHandleWrapper( | |
| 156 std::move(platform_handle), &wrapped_handle); | |
| 157 DCHECK_EQ(MOJO_RESULT_OK, result); | |
| 158 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 159 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle( | |
| 160 mojo::Handle(wrapped_handle))))); | |
| 25 } | 161 } |
| 26 | 162 |
| 27 void FakeFileSystemInstance::GetDocument(const std::string& authority, | 163 void FakeFileSystemInstance::GetDocument(const std::string& authority, |
| 28 const std::string& document_id, | 164 const std::string& document_id, |
| 29 const GetDocumentCallback& callback) { | 165 const GetDocumentCallback& callback) { |
| 30 base::ThreadTaskRunnerHandle::Get()->PostTask( | 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); | 167 auto iter = documents_.find(DocumentKey(authority, document_id)); |
| 32 } | 168 if (iter == documents_.end()) { |
| 33 | 169 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 34 void FakeFileSystemInstance::GetFileSize(const std::string& url, | 170 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); |
| 35 const GetFileSizeCallback& callback) { | 171 return; |
| 36 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 172 } |
| 37 base::Bind(callback, -1)); | 173 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 38 } | 174 FROM_HERE, |
| 39 | 175 base::Bind(callback, base::Passed(MakeDocument(iter->second)))); |
| 40 void FakeFileSystemInstance::OpenFileToRead( | 176 } |
| 41 const std::string& url, | 177 |
| 42 const OpenFileToReadCallback& callback) { | 178 void FakeFileSystemInstance::GetChildDocuments( |
| 43 base::ThreadTaskRunnerHandle::Get()->PostTask( | 179 const std::string& authority, |
| 44 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); | 180 const std::string& parent_document_id, |
| 181 const GetChildDocumentsCallback& callback) { | |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 183 auto iter = child_documents_.find(DocumentKey(authority, parent_document_id)); | |
| 184 if (iter == child_documents_.end()) { | |
| 185 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 186 FROM_HERE, base::Bind(callback, base::nullopt)); | |
| 187 return; | |
| 188 } | |
| 189 std::vector<mojom::DocumentPtr> children; | |
| 190 for (const auto& child_key : iter->second) { | |
| 191 children.emplace_back(MakeDocument(documents_[child_key])); | |
| 192 } | |
| 193 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 194 FROM_HERE, | |
| 195 base::Bind(callback, | |
| 196 base::Passed(base::make_optional(std::move(children))))); | |
| 45 } | 197 } |
| 46 | 198 |
| 47 void FakeFileSystemInstance::RequestMediaScan( | 199 void FakeFileSystemInstance::RequestMediaScan( |
| 48 const std::vector<std::string>& paths) { | 200 const std::vector<std::string>& paths) { |
| 201 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 49 // Do nothing and pretend we scaned them. | 202 // Do nothing and pretend we scaned them. |
| 50 } | 203 } |
| 51 | 204 |
| 52 } // namespace arc | 205 } // namespace arc |
| OLD | NEW |