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 #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" |
| 12 #include "components/arc/common/file_system.mojom.h" | 17 #include "components/arc/common/file_system.mojom.h" |
| 13 | 18 |
| 14 namespace arc { | 19 namespace arc { |
| 15 | 20 |
| 16 // A fake implementation which returns errors for all requests or just ignores | 21 // Fake implementation to operate on documents in memory. |
| 17 // them. | 22 // |
| 23 // ArcFileSystemOperationRunner provides two types of methods: content URL | |
| 24 // based and documents provider based. According to backend type, you need | |
| 25 // to setup the fake with different functions. | |
| 26 // | |
| 27 // Content URL based functions are: | |
| 28 // - GetFileSize() | |
| 29 // - OpenFileToRead() | |
| 30 // Fake files for those functions can be set up by AddFile(). | |
| 31 // | |
| 32 // Documents provider based functions are: | |
| 33 // - GetDocument() | |
| 34 // - GetChildDocuments() | |
| 35 // Fake documents for those functions can be set up by AddDocument(). | |
| 18 class FakeFileSystemInstance : public mojom::FileSystemInstance { | 36 class FakeFileSystemInstance : public mojom::FileSystemInstance { |
| 19 public: | 37 public: |
| 38 // Specification of a fake file available to content URL based methods. | |
| 39 struct FileSpec { | |
| 40 // Content URL of a file. | |
| 41 const char* url; | |
| 42 | |
| 43 // The content of a file. | |
| 44 const char* content; | |
|
hidehiko
2017/01/26 07:24:20
Could you comment this only accept c-style string?
Shuhei Takahashi
2017/01/27 09:55:10
Good point, done.
| |
| 45 | |
| 46 // If this is false, OpenFileToRead() for this file returns a seekable | |
| 47 // file descriptor. Otherwise it returns a non-seekable pipe. | |
| 48 bool stream; | |
|
hidehiko
2017/01/26 07:24:20
Optional: "seekable" may be consistent with your c
Shuhei Takahashi
2017/01/27 09:55:10
Done.
| |
| 49 | |
| 50 constexpr FileSpec() : FileSpec(nullptr, nullptr, false) {} | |
|
hidehiko
2017/01/26 07:24:20
This is POD, so I do not think you need ctors?
hashimoto
2017/01/26 08:05:54
Or, how about replacing const char* with std::stri
Shuhei Takahashi
2017/01/27 09:55:10
std::string makes this class non-constexpr, and th
hashimoto
2017/01/27 10:46:50
I'm concerned about possible use-after-free of str
| |
| 51 constexpr FileSpec(const char* url, const char* content, bool stream) | |
| 52 : url(url), content(content), stream(stream) {} | |
| 53 }; | |
| 54 | |
| 55 // Specification of a fake document available to documents provider based | |
| 56 // methods. | |
| 57 struct DocumentSpec { | |
| 58 // Authority. | |
| 59 const char* authority; | |
| 60 | |
| 61 // ID of this document. | |
| 62 const char* document_id; | |
| 63 | |
| 64 // ID of the parent document. Can be nullptr string if this is a root. | |
| 65 const char* parent_document_id; | |
| 66 | |
| 67 // File name displayed to users. | |
| 68 const char* display_name; | |
| 69 | |
| 70 // MIME type. | |
| 71 const char* mime_type; | |
| 72 | |
| 73 // File size in bytes. Set to -1 if size is not available. | |
| 74 int64_t size; | |
| 75 | |
| 76 // Last modified time in milliseconds from the UNIX epoch. | |
| 77 uint64_t last_modified; | |
|
hashimoto
2017/01/26 08:05:54
base::Time() is Chromium's standard type for time
Shuhei Takahashi
2017/01/27 09:55:10
I know, but in this case it makes more sense to us
| |
| 78 | |
| 79 constexpr DocumentSpec() | |
|
hidehiko
2017/01/26 07:24:20
Same here.
Shuhei Takahashi
2017/01/27 09:55:10
Done.
| |
| 80 : DocumentSpec(nullptr, nullptr, nullptr, nullptr, nullptr, -1, 0) {} | |
| 81 constexpr DocumentSpec(const char* authority, | |
| 82 const char* document_id, | |
| 83 const char* parent_document_id, | |
| 84 const char* display_name, | |
| 85 const char* mime_type, | |
| 86 int64_t size, | |
| 87 uint64_t last_modified) | |
| 88 : authority(authority), | |
| 89 document_id(document_id), | |
| 90 parent_document_id(parent_document_id), | |
| 91 display_name(display_name), | |
| 92 mime_type(mime_type), | |
| 93 size(size), | |
| 94 last_modified(last_modified) {} | |
| 95 }; | |
| 96 | |
| 20 FakeFileSystemInstance(); | 97 FakeFileSystemInstance(); |
| 98 ~FakeFileSystemInstance() override; | |
| 99 | |
| 100 // Adds a file accessible by content URL based methods. | |
| 101 void AddFile(const FileSpec& spec); | |
| 102 | |
| 103 // Adds a document accessible by document provider based methods. | |
| 104 void AddDocument(const DocumentSpec& spec); | |
| 21 | 105 |
| 22 // mojom::FileSystemInstance: | 106 // mojom::FileSystemInstance: |
| 23 ~FakeFileSystemInstance() override; | |
| 24 | |
| 25 void GetChildDocuments(const std::string& authority, | 107 void GetChildDocuments(const std::string& authority, |
| 26 const std::string& document_id, | 108 const std::string& document_id, |
| 27 const GetChildDocumentsCallback& callback) override; | 109 const GetChildDocumentsCallback& callback) override; |
| 28 | |
| 29 void GetDocument(const std::string& authority, | 110 void GetDocument(const std::string& authority, |
| 30 const std::string& document_id, | 111 const std::string& document_id, |
| 31 const GetDocumentCallback& callback) override; | 112 const GetDocumentCallback& callback) override; |
| 32 | |
| 33 void GetFileSize(const std::string& url, | 113 void GetFileSize(const std::string& url, |
| 34 const GetFileSizeCallback& callback) override; | 114 const GetFileSizeCallback& callback) override; |
| 35 | |
| 36 void OpenFileToRead(const std::string& url, | 115 void OpenFileToRead(const std::string& url, |
| 37 const OpenFileToReadCallback& callback) override; | 116 const OpenFileToReadCallback& callback) override; |
| 38 | |
| 39 void RequestMediaScan(const std::vector<std::string>& paths) override; | 117 void RequestMediaScan(const std::vector<std::string>& paths) override; |
| 40 | 118 |
| 41 private: | 119 private: |
| 120 // A pair of an authority and a document ID which identifies the location | |
| 121 // of a document in documents providers. | |
| 122 using DocumentKey = std::pair<std::string, std::string>; | |
| 123 | |
| 124 base::ScopedTempDir temp_dir_; | |
| 125 | |
| 126 std::map<std::string, FileSpec> files_; | |
|
hashimoto
2017/01/26 08:05:54
Please make it clear that this map is from URL to
Shuhei Takahashi
2017/01/27 09:55:10
Added comments.
| |
| 127 | |
| 128 std::map<DocumentKey, DocumentSpec> documents_; | |
| 129 std::map<DocumentKey, std::vector<DocumentKey>> child_documents_; | |
| 130 | |
| 42 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInstance); | 131 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInstance); |
| 43 }; | 132 }; |
| 44 | 133 |
| 45 } // namespace arc | 134 } // namespace arc |
| 46 | 135 |
| 47 #endif // COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ | 136 #endif // COMPONENTS_ARC_TEST_FAKE_FILE_SYSTEM_INSTANCE_H_ |
| OLD | NEW |