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 #ifndef COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ | 5 #ifndef COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ |
6 #define COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ | 6 #define COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ |
7 | 7 |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
8 #include <string> | 11 #include <string> |
12 #include <utility> | |
9 #include <vector> | 13 #include <vector> |
10 | 14 |
15 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/threading/thread_checker.h" | |
12 #include "components/arc/common/file_system.mojom.h" | 18 #include "components/arc/common/file_system.mojom.h" |
13 | 19 |
14 namespace arc { | 20 namespace arc { |
15 | 21 |
16 // A fake implementation which returns errors for all requests or just ignores | 22 // Fake implementation to operate on documents in memory. |
17 // them. | 23 // |
24 // ArcFileSystemOperationRunner provides two types of methods: content URL | |
25 // based and documents provider based. According to backend type, you need | |
26 // to setup the fake with different functions. | |
27 // | |
28 // Content URL based functions are: | |
29 // - GetFileSize() | |
30 // - OpenFileToRead() | |
31 // Fake files for those functions can be set up by AddFile(). | |
32 // | |
33 // Documents provider based functions are: | |
34 // - GetDocument() | |
35 // - GetChildDocuments() | |
36 // Fake documents for those functions can be set up by AddDocument(). | |
37 // | |
38 // Notes: | |
39 // - GetChildDocuments() returns child documents in the same order as they were | |
40 // added with AddDocument(). | |
41 // - All member functions must be called on the same thread. | |
18 class FakeFileSystemInstance : public mojom::FileSystemInstance { | 42 class FakeFileSystemInstance : public mojom::FileSystemInstance { |
19 public: | 43 public: |
44 // Specification of a fake file available to content URL based methods. | |
45 struct FileSpec { | |
46 enum Seekable { | |
hidehiko
2017/01/27 15:50:00
Could you use "enum class"?
Shuhei Takahashi
2017/01/30 02:27:35
Done.
| |
47 SEEKABLE, | |
48 NON_SEEKABLE, | |
49 }; | |
50 | |
51 // Content URL of a file. | |
52 const char* url; | |
53 | |
54 // The content of a file. This must be a null-terminated string. | |
55 const char* content; | |
56 | |
57 // Whether this file is seekable or not. | |
58 Seekable seekable; | |
59 }; | |
60 | |
61 // Specification of a fake document available to documents provider based | |
62 // methods. | |
63 struct DocumentSpec { | |
64 // Authority. | |
65 const char* authority; | |
66 | |
67 // ID of this document. | |
68 const char* document_id; | |
69 | |
70 // ID of the parent document. Can be nullptr string if this is a root. | |
71 const char* parent_document_id; | |
72 | |
73 // File name displayed to users. | |
74 const char* display_name; | |
75 | |
76 // MIME type. | |
77 const char* mime_type; | |
78 | |
79 // File size in bytes. Set to -1 if size is not available. | |
80 int64_t size; | |
81 | |
82 // Last modified time in milliseconds from the UNIX epoch. | |
83 // TODO(crbug.com/672737): Use base::Time once the corresponding field | |
84 // in file_system.mojom stops using uint64. | |
85 uint64_t last_modified; | |
86 }; | |
87 | |
20 FakeFileSystemInstance(); | 88 FakeFileSystemInstance(); |
89 ~FakeFileSystemInstance() override; | |
90 | |
91 // Adds a file accessible by content URL based methods. | |
92 void AddFile(const FileSpec& spec); | |
93 | |
94 // Adds a document accessible by document provider based methods. | |
95 void AddDocument(const DocumentSpec& spec); | |
21 | 96 |
22 // mojom::FileSystemInstance: | 97 // mojom::FileSystemInstance: |
23 ~FakeFileSystemInstance() override; | |
24 | |
25 void GetChildDocuments(const std::string& authority, | 98 void GetChildDocuments(const std::string& authority, |
26 const std::string& document_id, | 99 const std::string& document_id, |
27 const GetChildDocumentsCallback& callback) override; | 100 const GetChildDocumentsCallback& callback) override; |
28 | |
29 void GetDocument(const std::string& authority, | 101 void GetDocument(const std::string& authority, |
30 const std::string& document_id, | 102 const std::string& document_id, |
31 const GetDocumentCallback& callback) override; | 103 const GetDocumentCallback& callback) override; |
32 | |
33 void GetFileSize(const std::string& url, | 104 void GetFileSize(const std::string& url, |
34 const GetFileSizeCallback& callback) override; | 105 const GetFileSizeCallback& callback) override; |
35 | |
36 void OpenFileToRead(const std::string& url, | 106 void OpenFileToRead(const std::string& url, |
37 const OpenFileToReadCallback& callback) override; | 107 const OpenFileToReadCallback& callback) override; |
38 | |
39 void RequestMediaScan(const std::vector<std::string>& paths) override; | 108 void RequestMediaScan(const std::vector<std::string>& paths) override; |
40 | 109 |
41 private: | 110 private: |
111 // A pair of an authority and a document ID which identifies the location | |
112 // of a document in documents providers. | |
113 using DocumentKey = std::pair<std::string, std::string>; | |
114 | |
115 base::ThreadChecker thread_checker_; | |
116 | |
117 base::ScopedTempDir temp_dir_; | |
118 | |
119 // Mapping from a content URL to a file. | |
120 std::map<std::string, FileSpec> files_; | |
121 | |
122 // Mapping from a document key to a document. | |
123 std::map<DocumentKey, DocumentSpec> documents_; | |
124 | |
125 // Mapping from a document key to its child documents. | |
126 std::map<DocumentKey, std::vector<DocumentKey>> child_documents_; | |
127 | |
42 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInstance); | 128 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInstance); |
43 }; | 129 }; |
44 | 130 |
45 } // namespace arc | 131 } // namespace arc |
46 | 132 |
47 #endif // COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ | 133 #endif // COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ |
OLD | NEW |