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 { |
24 | |
25 base::ScopedFD CreateRegularFileDescriptor(const char* content, | |
26 const base::FilePath& temp_dir) { | |
27 base::FilePath path; | |
28 CHECK(base::CreateTemporaryFileInDir(temp_dir, &path)); | |
hidehiko
2017/01/26 07:24:20
Could you return an error, instead of crash? You c
Shuhei Takahashi
2017/01/27 09:55:10
As we discussed offline, please let me use DCHECK(
hidehiko
2017/01/27 15:50:00
Sounds reasonable to me.
| |
29 CHECK_EQ(static_cast<int>(strlen(content)), | |
30 base::WriteFile(path, content, strlen(content))); | |
31 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
32 CHECK(file.IsValid()); | |
33 return base::ScopedFD(file.TakePlatformFile()); | |
34 } | |
35 | |
36 base::ScopedFD CreateStreamFileDescriptor(const char* content) { | |
37 int fds[2]; | |
38 CHECK_EQ(0, pipe(fds)); | |
39 base::ScopedFD fd_read(fds[0]); | |
40 base::ScopedFD fd_write(fds[1]); | |
41 CHECK(base::WriteFileDescriptor(fd_write.get(), content, strlen(content))); | |
42 return fd_read; | |
43 } | |
44 | |
45 mojom::DocumentPtr MakeDocument( | |
46 const FakeFileSystemInstance::DocumentSpec& spec) { | |
47 mojom::DocumentPtr document = mojom::Document::New(); | |
48 document->document_id = spec.document_id; | |
49 document->display_name = spec.display_name; | |
50 document->mime_type = spec.mime_type; | |
51 document->size = spec.size; | |
52 document->last_modified = spec.last_modified; | |
53 return document; | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 FakeFileSystemInstance::FakeFileSystemInstance() { | |
59 CHECK(temp_dir_.CreateUniqueTempDir()); | |
60 } | |
16 | 61 |
17 FakeFileSystemInstance::~FakeFileSystemInstance() = default; | 62 FakeFileSystemInstance::~FakeFileSystemInstance() = default; |
18 | 63 |
19 void FakeFileSystemInstance::GetChildDocuments( | 64 void FakeFileSystemInstance::AddFile(const FileSpec& spec) { |
20 const std::string& authority, | 65 CHECK_EQ(0u, files_.count(std::string(spec.url))); |
21 const std::string& document_id, | 66 files_[std::string(spec.url)] = spec; |
22 const GetChildDocumentsCallback& callback) { | 67 } |
68 | |
69 void FakeFileSystemInstance::AddDocument(const DocumentSpec& spec) { | |
70 DocumentKey key(spec.authority, spec.document_id); | |
71 CHECK_EQ(0u, documents_.count(key)); | |
72 documents_[key] = spec; | |
73 child_documents_[key]; // Allocate a vector. | |
74 if (spec.parent_document_id) { | |
75 DocumentKey parent_key(spec.authority, spec.parent_document_id); | |
76 CHECK_EQ(1u, documents_.count(parent_key)); | |
77 child_documents_[parent_key].push_back(key); | |
78 } | |
79 } | |
80 | |
81 void FakeFileSystemInstance::GetFileSize(const std::string& url, | |
82 const GetFileSizeCallback& callback) { | |
83 auto iter = files_.find(url); | |
84 if (iter == files_.end()) { | |
85 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
86 base::Bind(callback, -1)); | |
87 return; | |
88 } | |
89 const FileSpec& spec = iter->second; | |
23 base::ThreadTaskRunnerHandle::Get()->PostTask( | 90 base::ThreadTaskRunnerHandle::Get()->PostTask( |
24 FROM_HERE, base::Bind(callback, base::nullopt)); | 91 FROM_HERE, base::Bind(callback, strlen(spec.content))); |
92 } | |
93 | |
94 void FakeFileSystemInstance::OpenFileToRead( | |
95 const std::string& url, | |
96 const OpenFileToReadCallback& callback) { | |
97 auto iter = files_.find(url); | |
98 if (iter == files_.end()) { | |
99 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
100 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); | |
101 return; | |
102 } | |
103 const FileSpec& spec = iter->second; | |
104 base::ScopedFD fd = spec.stream ? CreateStreamFileDescriptor(spec.content) | |
105 : CreateRegularFileDescriptor( | |
106 spec.content, temp_dir_.GetPath()); | |
107 mojo::edk::ScopedPlatformHandle platform_handle( | |
108 mojo::edk::PlatformHandle(fd.release())); | |
109 MojoHandle wrapped_handle; | |
110 CHECK_EQ(MOJO_RESULT_OK, mojo::edk::CreatePlatformHandleWrapper( | |
111 std::move(platform_handle), &wrapped_handle)); | |
112 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
113 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle( | |
114 mojo::Handle(wrapped_handle))))); | |
25 } | 115 } |
26 | 116 |
27 void FakeFileSystemInstance::GetDocument(const std::string& authority, | 117 void FakeFileSystemInstance::GetDocument(const std::string& authority, |
28 const std::string& document_id, | 118 const std::string& document_id, |
29 const GetDocumentCallback& callback) { | 119 const GetDocumentCallback& callback) { |
120 auto iter = documents_.find(DocumentKey(authority, document_id)); | |
121 if (iter == documents_.end()) { | |
122 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
123 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); | |
124 return; | |
125 } | |
30 base::ThreadTaskRunnerHandle::Get()->PostTask( | 126 base::ThreadTaskRunnerHandle::Get()->PostTask( |
31 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); | 127 FROM_HERE, |
128 base::Bind(callback, base::Passed(MakeDocument(iter->second)))); | |
32 } | 129 } |
33 | 130 |
34 void FakeFileSystemInstance::GetFileSize(const std::string& url, | 131 void FakeFileSystemInstance::GetChildDocuments( |
35 const GetFileSizeCallback& callback) { | 132 const std::string& authority, |
36 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 133 const std::string& parent_document_id, |
37 base::Bind(callback, -1)); | 134 const GetChildDocumentsCallback& callback) { |
38 } | 135 auto iter = child_documents_.find(DocumentKey(authority, parent_document_id)); |
39 | 136 if (iter == child_documents_.end()) { |
40 void FakeFileSystemInstance::OpenFileToRead( | 137 base::ThreadTaskRunnerHandle::Get()->PostTask( |
41 const std::string& url, | 138 FROM_HERE, base::Bind(callback, base::nullopt)); |
42 const OpenFileToReadCallback& callback) { | 139 return; |
140 } | |
141 std::vector<mojom::DocumentPtr> children; | |
142 for (const auto& child_key : iter->second) { | |
143 children.emplace_back(MakeDocument(documents_[child_key])); | |
144 } | |
43 base::ThreadTaskRunnerHandle::Get()->PostTask( | 145 base::ThreadTaskRunnerHandle::Get()->PostTask( |
44 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); | 146 FROM_HERE, |
147 base::Bind(callback, | |
148 base::Passed(base::make_optional(std::move(children))))); | |
45 } | 149 } |
46 | 150 |
47 void FakeFileSystemInstance::RequestMediaScan( | 151 void FakeFileSystemInstance::RequestMediaScan( |
48 const std::vector<std::string>& paths) { | 152 const std::vector<std::string>& paths) { |
49 // Do nothing and pretend we scaned them. | 153 // Do nothing and pretend we scaned them. |
50 } | 154 } |
51 | 155 |
52 } // namespace arc | 156 } // namespace arc |
OLD | NEW |