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, |
| 26 const base::FilePath& temp_dir) { |
| 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.data(), content.size()); |
| 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 } |
18 | 36 |
19 void FakeFileSystemInstance::GetChildDocuments( | 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 = |
| 44 base::WriteFileDescriptor(fd_write.get(), content.data(), content.size()); |
| 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(const File& that) = default; |
| 62 |
| 63 FakeFileSystemInstance::File::File(const std::string& url, |
| 64 const std::string& content, |
| 65 Seekable seekable) |
| 66 : url(url), content(content), seekable(seekable) {} |
| 67 |
| 68 FakeFileSystemInstance::File::~File() = default; |
| 69 |
| 70 FakeFileSystemInstance::Document::Document(const Document& that) = default; |
| 71 |
| 72 FakeFileSystemInstance::Document::Document( |
20 const std::string& authority, | 73 const std::string& authority, |
21 const std::string& document_id, | 74 const std::string& document_id, |
22 const GetChildDocumentsCallback& callback) { | 75 const std::string& parent_document_id, |
| 76 const std::string& display_name, |
| 77 const std::string& mime_type, |
| 78 int64_t size, |
| 79 uint64_t last_modified) |
| 80 : authority(authority), |
| 81 document_id(document_id), |
| 82 parent_document_id(parent_document_id), |
| 83 display_name(display_name), |
| 84 mime_type(mime_type), |
| 85 size(size), |
| 86 last_modified(last_modified) {} |
| 87 |
| 88 FakeFileSystemInstance::Document::~Document() = default; |
| 89 |
| 90 FakeFileSystemInstance::FakeFileSystemInstance() { |
| 91 bool temp_dir_created = temp_dir_.CreateUniqueTempDir(); |
| 92 DCHECK(temp_dir_created); |
| 93 } |
| 94 |
| 95 FakeFileSystemInstance::~FakeFileSystemInstance() { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 } |
| 98 |
| 99 void FakeFileSystemInstance::AddFile(const File& file) { |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 DCHECK_EQ(0u, files_.count(std::string(file.url))); |
| 102 files_.insert(std::make_pair(std::string(file.url), file)); |
| 103 } |
| 104 |
| 105 void FakeFileSystemInstance::AddDocument(const Document& document) { |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 107 DocumentKey key(document.authority, document.document_id); |
| 108 DCHECK_EQ(0u, documents_.count(key)); |
| 109 documents_.insert(std::make_pair(key, document)); |
| 110 child_documents_[key]; // Allocate a vector. |
| 111 if (!document.parent_document_id.empty()) { |
| 112 DocumentKey parent_key(document.authority, document.parent_document_id); |
| 113 DCHECK_EQ(1u, documents_.count(parent_key)); |
| 114 child_documents_[parent_key].push_back(key); |
| 115 } |
| 116 } |
| 117 |
| 118 void FakeFileSystemInstance::GetFileSize(const std::string& url, |
| 119 const GetFileSizeCallback& callback) { |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 auto iter = files_.find(url); |
| 122 if (iter == files_.end()) { |
| 123 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 124 base::Bind(callback, -1)); |
| 125 return; |
| 126 } |
| 127 const File& file = iter->second; |
23 base::ThreadTaskRunnerHandle::Get()->PostTask( | 128 base::ThreadTaskRunnerHandle::Get()->PostTask( |
24 FROM_HERE, base::Bind(callback, base::nullopt)); | 129 FROM_HERE, base::Bind(callback, file.content.size())); |
| 130 } |
| 131 |
| 132 void FakeFileSystemInstance::OpenFileToRead( |
| 133 const std::string& url, |
| 134 const OpenFileToReadCallback& callback) { |
| 135 DCHECK(thread_checker_.CalledOnValidThread()); |
| 136 auto iter = files_.find(url); |
| 137 if (iter == files_.end()) { |
| 138 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 139 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); |
| 140 return; |
| 141 } |
| 142 const File& file = iter->second; |
| 143 base::ScopedFD fd = |
| 144 file.seekable == File::Seekable::YES |
| 145 ? CreateRegularFileDescriptor(file.content, temp_dir_.GetPath()) |
| 146 : CreateStreamFileDescriptor(file.content); |
| 147 mojo::edk::ScopedPlatformHandle platform_handle( |
| 148 mojo::edk::PlatformHandle(fd.release())); |
| 149 MojoHandle wrapped_handle; |
| 150 MojoResult result = mojo::edk::CreatePlatformHandleWrapper( |
| 151 std::move(platform_handle), &wrapped_handle); |
| 152 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 154 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle( |
| 155 mojo::Handle(wrapped_handle))))); |
25 } | 156 } |
26 | 157 |
27 void FakeFileSystemInstance::GetDocument(const std::string& authority, | 158 void FakeFileSystemInstance::GetDocument(const std::string& authority, |
28 const std::string& document_id, | 159 const std::string& document_id, |
29 const GetDocumentCallback& callback) { | 160 const GetDocumentCallback& callback) { |
| 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 162 auto iter = documents_.find(DocumentKey(authority, document_id)); |
| 163 if (iter == documents_.end()) { |
| 164 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 165 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); |
| 166 return; |
| 167 } |
30 base::ThreadTaskRunnerHandle::Get()->PostTask( | 168 base::ThreadTaskRunnerHandle::Get()->PostTask( |
31 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); | 169 FROM_HERE, |
| 170 base::Bind(callback, base::Passed(MakeDocument(iter->second)))); |
32 } | 171 } |
33 | 172 |
34 void FakeFileSystemInstance::GetFileSize(const std::string& url, | 173 void FakeFileSystemInstance::GetChildDocuments( |
35 const GetFileSizeCallback& callback) { | 174 const std::string& authority, |
36 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 175 const std::string& parent_document_id, |
37 base::Bind(callback, -1)); | 176 const GetChildDocumentsCallback& callback) { |
38 } | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
39 | 178 auto child_iter = |
40 void FakeFileSystemInstance::OpenFileToRead( | 179 child_documents_.find(DocumentKey(authority, parent_document_id)); |
41 const std::string& url, | 180 if (child_iter == child_documents_.end()) { |
42 const OpenFileToReadCallback& callback) { | 181 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 182 FROM_HERE, base::Bind(callback, base::nullopt)); |
| 183 return; |
| 184 } |
| 185 std::vector<mojom::DocumentPtr> children; |
| 186 for (const auto& child_key : child_iter->second) { |
| 187 auto doc_iter = documents_.find(child_key); |
| 188 DCHECK(doc_iter != documents_.end()); |
| 189 children.emplace_back(MakeDocument(doc_iter->second)); |
| 190 } |
43 base::ThreadTaskRunnerHandle::Get()->PostTask( | 191 base::ThreadTaskRunnerHandle::Get()->PostTask( |
44 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); | 192 FROM_HERE, |
| 193 base::Bind(callback, |
| 194 base::Passed(base::make_optional(std::move(children))))); |
45 } | 195 } |
46 | 196 |
47 void FakeFileSystemInstance::RequestMediaScan( | 197 void FakeFileSystemInstance::RequestMediaScan( |
48 const std::vector<std::string>& paths) { | 198 const std::vector<std::string>& paths) { |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); |
49 // Do nothing and pretend we scaned them. | 200 // Do nothing and pretend we scaned them. |
50 } | 201 } |
51 | 202 |
52 } // namespace arc | 203 } // namespace arc |
OLD | NEW |