1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef LIBRARIES_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_HTML5_FS_H_
6 #define LIBRARIES_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_HTML5_FS_H_
12 #include <ppapi/c/pp_directory_entry.h>
14 #include "fake_ppapi/fake_core_interface.h"
15 #include "fake_ppapi/fake_var_interface.h"
16 #include "fake_ppapi/fake_var_manager.h"
17 #include "nacl_io/pepper_interface_dummy.h"
18 #include "sdk_util/macros.h"
20 // This class is a fake implementation of the interfaces necessary to access
21 // the HTML5 Filesystem from NaCl.
24 // FakePepperInterfaceHtml5Fs ppapi_html5fs;
26 // PP_Resource ref_resource = ppapi_html5fs.GetFileRefInterface()->Create(
27 // ppapi_html5fs.GetInstance(),
31 // NOTE: This pepper interface creates an instance resource that can only be
32 // used with FakePepperInterfaceHtml5Fs, not other fake pepper implementations.
34 class FakeHtml5FsNode {
36 FakeHtml5FsNode(const PP_FileInfo& info);
37 FakeHtml5FsNode(const PP_FileInfo& info,
38 const std::vector<uint8_t>& contents);
39 FakeHtml5FsNode(const PP_FileInfo& info, const std::string& contents);
41 int32_t Read(int64_t offset, char* buffer, int32_t bytes_to_read);
42 int32_t Write(int64_t offset, const char* buffer, int32_t bytes_to_write);
43 int32_t Append(const char* buffer, int32_t bytes_to_write);
44 int32_t SetLength(int64_t length);
45 void GetInfo(PP_FileInfo* out_info);
46 bool IsRegular() const;
47 bool IsDirectory() const;
48 PP_FileType file_type() const { return info_.type; }
50 // These times are not modified by the fake implementation.
51 void set_creation_time(PP_Time time) { info_.creation_time = time; }
52 void set_last_access_time(PP_Time time) { info_.last_access_time = time; }
53 void set_last_modified_time(PP_Time time) { info_.last_modified_time = time; }
55 const std::vector<uint8_t>& contents() const { return contents_; }
59 std::vector<uint8_t> contents_;
62 class FakeHtml5FsFilesystem {
64 typedef std::string Path;
66 struct DirectoryEntry {
68 const FakeHtml5FsNode* node;
70 typedef std::vector<DirectoryEntry> DirectoryEntries;
72 FakeHtml5FsFilesystem();
73 explicit FakeHtml5FsFilesystem(PP_FileSystemType type);
74 FakeHtml5FsFilesystem(const FakeHtml5FsFilesystem& filesystem,
75 PP_FileSystemType type);
78 bool AddEmptyFile(const Path& path, FakeHtml5FsNode** out_node);
79 bool AddFile(const Path& path,
80 const std::string& contents,
81 FakeHtml5FsNode** out_node);
82 bool AddFile(const Path& path,
83 const std::vector<uint8_t>& contents,
84 FakeHtml5FsNode** out_node);
85 bool AddDirectory(const Path& path, FakeHtml5FsNode** out_node);
86 bool RemoveNode(const Path& path);
88 FakeHtml5FsNode* GetNode(const Path& path);
89 bool GetDirectoryEntries(const Path& path,
90 DirectoryEntries* out_dir_entries) const;
91 PP_FileSystemType filesystem_type() const { return filesystem_type_; }
92 static Path GetParentPath(const Path& path);
95 typedef std::map<Path, FakeHtml5FsNode> NodeMap;
97 PP_FileSystemType filesystem_type_;
100 class FakeFileIoInterface : public nacl_io::FileIoInterface {
102 explicit FakeFileIoInterface(FakeCoreInterface* core_interface);
104 virtual PP_Resource Create(PP_Resource instance);
105 virtual int32_t Open(PP_Resource file_io,
106 PP_Resource file_ref,
108 PP_CompletionCallback callback);
109 virtual int32_t Query(PP_Resource file_io,
111 PP_CompletionCallback callback);
112 virtual int32_t Read(PP_Resource file_io,
115 int32_t bytes_to_read,
116 PP_CompletionCallback callback);
117 virtual int32_t Write(PP_Resource file_io,
120 int32_t bytes_to_write,
121 PP_CompletionCallback callback);
122 virtual int32_t SetLength(PP_Resource file_io,
124 PP_CompletionCallback callback);
125 virtual int32_t Flush(PP_Resource file_io, PP_CompletionCallback callback);
126 virtual void Close(PP_Resource file_io);
129 FakeCoreInterface* core_interface_; // Weak reference.
131 DISALLOW_COPY_AND_ASSIGN(FakeFileIoInterface);
134 class FakeFileRefInterface : public nacl_io::FileRefInterface {
136 FakeFileRefInterface(FakeCoreInterface* core_interface,
137 FakeVarInterface* var_interface);
139 virtual PP_Resource Create(PP_Resource file_system, const char* path);
140 virtual PP_Var GetName(PP_Resource file_ref);
141 virtual int32_t MakeDirectory(PP_Resource directory_ref,
142 PP_Bool make_parents,
143 PP_CompletionCallback callback);
144 virtual int32_t Delete(PP_Resource file_ref, PP_CompletionCallback callback);
145 virtual int32_t Query(PP_Resource file_ref,
147 PP_CompletionCallback callback);
148 virtual int32_t ReadDirectoryEntries(PP_Resource file_ref,
149 const PP_ArrayOutput& output,
150 PP_CompletionCallback callback);
151 virtual int32_t Rename(PP_Resource file_ref,
152 PP_Resource new_file_ref,
153 PP_CompletionCallback callback);
156 FakeCoreInterface* core_interface_; // Weak reference.
157 FakeVarInterface* var_interface_; // Weak reference.
158 FakeVarManager* var_manager_; // Weak reference
160 DISALLOW_COPY_AND_ASSIGN(FakeFileRefInterface);
163 class FakeFileSystemInterface : public nacl_io::FileSystemInterface {
165 FakeFileSystemInterface(FakeCoreInterface* core_interface);
167 virtual PP_Resource Create(PP_Instance instance, PP_FileSystemType type);
168 virtual int32_t Open(PP_Resource file_system,
169 int64_t expected_size,
170 PP_CompletionCallback callback);
173 FakeCoreInterface* core_interface_; // Weak reference.
175 DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInterface);
178 class FakePepperInterfaceHtml5Fs : public nacl_io::PepperInterfaceDummy {
180 FakePepperInterfaceHtml5Fs();
181 explicit FakePepperInterfaceHtml5Fs(const FakeHtml5FsFilesystem& filesystem);
182 ~FakePepperInterfaceHtml5Fs();
184 virtual PP_Instance GetInstance() { return instance_; }
185 virtual nacl_io::CoreInterface* GetCoreInterface();
186 virtual nacl_io::FileSystemInterface* GetFileSystemInterface();
187 virtual nacl_io::FileRefInterface* GetFileRefInterface();
188 virtual nacl_io::FileIoInterface* GetFileIoInterface();
189 virtual nacl_io::VarInterface* GetVarInterface();
191 FakeHtml5FsFilesystem* filesystem_template() { return &filesystem_template_; }
196 FakeResourceManager resource_manager_;
197 FakeCoreInterface core_interface_;
198 FakeVarInterface var_interface_;
199 FakeVarManager var_manager_;
200 FakeHtml5FsFilesystem filesystem_template_;
201 FakeFileSystemInterface file_system_interface_;
202 FakeFileRefInterface file_ref_interface_;
203 FakeFileIoInterface file_io_interface_;
204 PP_Instance instance_;
206 DISALLOW_COPY_AND_ASSIGN(FakePepperInterfaceHtml5Fs);
209 #endif // LIBRARIES_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_HTML5_FS_H_