Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / tests / nacl_io_test / fake_ppapi / fake_pepper_interface_html5_fs.h
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.
4
5 #ifndef LIBRARIES_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_HTML5_FS_H_
6 #define LIBRARIES_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_HTML5_FS_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include <ppapi/c/pp_directory_entry.h>
13
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"
19
20 // This class is a fake implementation of the interfaces necessary to access
21 // the HTML5 Filesystem from NaCl.
22 //
23 // Example:
24 //   FakePepperInterfaceHtml5Fs ppapi_html5fs;
25 //   ...
26 //   PP_Resource ref_resource = ppapi_html5fs.GetFileRefInterface()->Create(
27 //       ppapi_html5fs.GetInstance(),
28 //       "/some/path");
29 //   ...
30 //
31 // NOTE: This pepper interface creates an instance resource that can only be
32 // used with FakePepperInterfaceHtml5Fs, not other fake pepper implementations.
33
34 class FakeHtml5FsNode {
35  public:
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);
40
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; }
49
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; }
54
55   const std::vector<uint8_t>& contents() const { return contents_; }
56
57  private:
58   PP_FileInfo info_;
59   std::vector<uint8_t> contents_;
60 };
61
62 class FakeHtml5FsFilesystem {
63  public:
64   typedef std::string Path;
65
66   struct DirectoryEntry {
67     Path path;
68     const FakeHtml5FsNode* node;
69   };
70   typedef std::vector<DirectoryEntry> DirectoryEntries;
71
72   FakeHtml5FsFilesystem();
73   explicit FakeHtml5FsFilesystem(PP_FileSystemType type);
74   FakeHtml5FsFilesystem(const FakeHtml5FsFilesystem& filesystem,
75                         PP_FileSystemType type);
76
77   void Clear();
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);
87
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);
93
94  private:
95   typedef std::map<Path, FakeHtml5FsNode> NodeMap;
96   NodeMap node_map_;
97   PP_FileSystemType filesystem_type_;
98 };
99
100 class FakeFileIoInterface : public nacl_io::FileIoInterface {
101  public:
102   explicit FakeFileIoInterface(FakeCoreInterface* core_interface);
103
104   virtual PP_Resource Create(PP_Resource instance);
105   virtual int32_t Open(PP_Resource file_io,
106                        PP_Resource file_ref,
107                        int32_t open_flags,
108                        PP_CompletionCallback callback);
109   virtual int32_t Query(PP_Resource file_io,
110                         PP_FileInfo* info,
111                         PP_CompletionCallback callback);
112   virtual int32_t Read(PP_Resource file_io,
113                        int64_t offset,
114                        char* buffer,
115                        int32_t bytes_to_read,
116                        PP_CompletionCallback callback);
117   virtual int32_t Write(PP_Resource file_io,
118                         int64_t offset,
119                         const char* buffer,
120                         int32_t bytes_to_write,
121                         PP_CompletionCallback callback);
122   virtual int32_t SetLength(PP_Resource file_io,
123                             int64_t length,
124                             PP_CompletionCallback callback);
125   virtual int32_t Flush(PP_Resource file_io, PP_CompletionCallback callback);
126   virtual void Close(PP_Resource file_io);
127
128  private:
129   FakeCoreInterface* core_interface_;  // Weak reference.
130
131   DISALLOW_COPY_AND_ASSIGN(FakeFileIoInterface);
132 };
133
134 class FakeFileRefInterface : public nacl_io::FileRefInterface {
135  public:
136   FakeFileRefInterface(FakeCoreInterface* core_interface,
137                        FakeVarInterface* var_interface);
138
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,
146                         PP_FileInfo* info,
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);
154
155  private:
156   FakeCoreInterface* core_interface_;  // Weak reference.
157   FakeVarInterface* var_interface_;  // Weak reference.
158   FakeVarManager* var_manager_;  // Weak reference
159
160   DISALLOW_COPY_AND_ASSIGN(FakeFileRefInterface);
161 };
162
163 class FakeFileSystemInterface : public nacl_io::FileSystemInterface {
164  public:
165   FakeFileSystemInterface(FakeCoreInterface* core_interface);
166
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);
171
172  private:
173   FakeCoreInterface* core_interface_;  // Weak reference.
174
175   DISALLOW_COPY_AND_ASSIGN(FakeFileSystemInterface);
176 };
177
178 class FakePepperInterfaceHtml5Fs : public nacl_io::PepperInterfaceDummy {
179  public:
180   FakePepperInterfaceHtml5Fs();
181   explicit FakePepperInterfaceHtml5Fs(const FakeHtml5FsFilesystem& filesystem);
182   ~FakePepperInterfaceHtml5Fs();
183
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();
190
191   FakeHtml5FsFilesystem* filesystem_template() { return &filesystem_template_; }
192
193  private:
194   void Init();
195
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_;
205
206   DISALLOW_COPY_AND_ASSIGN(FakePepperInterfaceHtml5Fs);
207 };
208
209 #endif  // LIBRARIES_NACL_IO_TEST_FAKE_PEPPER_INTERFACE_HTML5_FS_H_