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 File { | |
46 enum Seekable { | |
47 SEEKABLE, | |
48 NON_SEEKABLE, | |
49 }; | |
50 | |
51 // Content URL of a file. | |
52 std::string url; | |
53 | |
54 // The content of a file. | |
55 std::string content; | |
56 | |
57 // Whether this file is seekable or not. | |
58 Seekable seekable; | |
59 | |
60 File(); | |
hidehiko
2017/01/27 15:50:00
Could you get rid of map::operator[], and replace
Shuhei Takahashi
2017/01/30 02:27:36
Well, of course I know I can get rid of the defaul
| |
61 File(const std::string& url, const std::string& content, Seekable seekable); | |
62 File(const File& rhs); | |
hidehiko
2017/01/27 15:50:00
IIUC, because this is not DISALLOW_COPY_AND_ASSIGN
Shuhei Takahashi
2017/01/30 02:27:36
As I wrote in earlier comment, we can't drop copy-
| |
63 ~File(); | |
64 }; | |
65 | |
66 // Specification of a fake document available to documents provider based | |
67 // methods. | |
68 struct Document { | |
69 // Authority. | |
70 std::string authority; | |
71 | |
72 // ID of this document. | |
73 std::string document_id; | |
74 | |
75 // ID of the parent document. Can be nullptr string if this is a root. | |
hidehiko
2017/01/27 15:50:00
If you use std::string, |nullptr| is out-dated com
Shuhei Takahashi
2017/01/30 02:27:36
Good catch.
| |
76 std::string parent_document_id; | |
77 | |
78 // File name displayed to users. | |
79 std::string display_name; | |
80 | |
81 // MIME type. | |
82 std::string mime_type; | |
83 | |
84 // File size in bytes. Set to -1 if size is not available. | |
85 int64_t size; | |
86 | |
87 // Last modified time in milliseconds from the UNIX epoch. | |
88 // TODO(crbug.com/672737): Use base::Time once the corresponding field | |
89 // in file_system.mojom stops using uint64. | |
90 uint64_t last_modified; | |
91 | |
92 Document(); | |
93 Document(const std::string& authority, | |
94 const std::string& document_id, | |
95 const std::string& parent_document_id, | |
96 const std::string& display_name, | |
97 const std::string& mime_type, | |
98 int64_t size, | |
99 uint64_t last_modified); | |
100 Document(const Document& rhs); | |
101 ~Document(); | |
102 }; | |
103 | |
20 FakeFileSystemInstance(); | 104 FakeFileSystemInstance(); |
105 ~FakeFileSystemInstance() override; | |
106 | |
107 // Adds a file accessible by content URL based methods. | |
108 void AddFile(const File& file); | |
109 | |
110 // Adds a document accessible by document provider based methods. | |
111 void AddDocument(const Document& document); | |
21 | 112 |
22 // mojom::FileSystemInstance: | 113 // mojom::FileSystemInstance: |
23 ~FakeFileSystemInstance() override; | |
24 | |
25 void GetChildDocuments(const std::string& authority, | 114 void GetChildDocuments(const std::string& authority, |
26 const std::string& document_id, | 115 const std::string& document_id, |
27 const GetChildDocumentsCallback& callback) override; | 116 const GetChildDocumentsCallback& callback) override; |
28 | |
29 void GetDocument(const std::string& authority, | 117 void GetDocument(const std::string& authority, |
30 const std::string& document_id, | 118 const std::string& document_id, |
31 const GetDocumentCallback& callback) override; | 119 const GetDocumentCallback& callback) override; |
32 | |
33 void GetFileSize(const std::string& url, | 120 void GetFileSize(const std::string& url, |
34 const GetFileSizeCallback& callback) override; | 121 const GetFileSizeCallback& callback) override; |
35 | |
36 void OpenFileToRead(const std::string& url, | 122 void OpenFileToRead(const std::string& url, |
37 const OpenFileToReadCallback& callback) override; | 123 const OpenFileToReadCallback& callback) override; |
38 | |
39 void RequestMediaScan(const std::vector<std::string>& paths) override; | 124 void RequestMediaScan(const std::vector<std::string>& paths) override; |
40 | 125 |
41 private: | 126 private: |
127 // A pair of an authority and a document ID which identifies the location | |
128 // of a document in documents providers. | |
129 using DocumentKey = std::pair<std::string, std::string>; | |
130 | |
131 base::ThreadChecker thread_checker_; | |
132 | |
133 base::ScopedTempDir temp_dir_; | |
134 | |
135 // Mapping from a content URL to a file. | |
136 std::map<std::string, File> files_; | |
137 | |
138 // Mapping from a document key to a document. | |
139 std::map<DocumentKey, Document> documents_; | |
140 | |
141 // Mapping from a document key to its child documents. | |
142 std::map<DocumentKey, std::vector<DocumentKey>> child_documents_; | |
143 | |
42 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInstance); | 144 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInstance); |
43 }; | 145 }; |
44 | 146 |
45 } // namespace arc | 147 } // namespace arc |
46 | 148 |
47 #endif // COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ | 149 #endif // COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ |
OLD | NEW |