OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /// @file file_io.cc | 5 /// @file file_io.cc |
6 /// This example demonstrates the use of persistent file I/O | 6 /// This example demonstrates the use of persistent file I/O |
7 | 7 |
8 #define __STDC_LIMIT_MACROS | 8 #define __STDC_LIMIT_MACROS |
9 #include <sstream> | 9 #include <sstream> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "ppapi/c/pp_stdint.h" | 12 #include "ppapi/c/pp_stdint.h" |
13 #include "ppapi/c/ppb_file_io.h" | 13 #include "ppapi/c/ppb_file_io.h" |
| 14 #include "ppapi/cpp/directory_entry.h" |
14 #include "ppapi/cpp/file_io.h" | 15 #include "ppapi/cpp/file_io.h" |
15 #include "ppapi/cpp/file_ref.h" | 16 #include "ppapi/cpp/file_ref.h" |
16 #include "ppapi/cpp/file_system.h" | 17 #include "ppapi/cpp/file_system.h" |
17 #include "ppapi/cpp/instance.h" | 18 #include "ppapi/cpp/instance.h" |
18 #include "ppapi/cpp/message_loop.h" | 19 #include "ppapi/cpp/message_loop.h" |
19 #include "ppapi/cpp/module.h" | 20 #include "ppapi/cpp/module.h" |
20 #include "ppapi/cpp/var.h" | 21 #include "ppapi/cpp/var.h" |
21 #include "ppapi/utility/completion_callback_factory.h" | 22 #include "ppapi/utility/completion_callback_factory.h" |
22 #include "ppapi/utility/threading/simple_thread.h" | 23 #include "ppapi/utility/threading/simple_thread.h" |
23 | 24 |
24 #ifndef INT32_MAX | 25 #ifndef INT32_MAX |
25 #define INT32_MAX (0x7FFFFFFF) | 26 #define INT32_MAX (0x7FFFFFFF) |
26 #endif | 27 #endif |
27 | 28 |
28 #ifdef WIN32 | 29 #ifdef WIN32 |
29 #undef min | 30 #undef min |
30 #undef max | 31 #undef max |
31 #undef PostMessage | 32 #undef PostMessage |
32 | 33 |
33 // Allow 'this' in initializer list | 34 // Allow 'this' in initializer list |
34 #pragma warning(disable : 4355) | 35 #pragma warning(disable : 4355) |
35 #endif | 36 #endif |
36 | 37 |
37 namespace { | 38 namespace { |
38 /// Used for our simple protocol to communicate with Javascript | 39 /// Used for our simple protocol to communicate with Javascript |
39 const char* const kLoadPrefix = "ld"; | 40 const char* const kLoadPrefix = "ld"; |
40 const char* const kSavePrefix = "sv"; | 41 const char* const kSavePrefix = "sv"; |
41 const char* const kDeletePrefix = "de"; | 42 const char* const kDeletePrefix = "de"; |
| 43 const char* const kListPrefix = "ls"; |
42 } | 44 } |
43 | 45 |
44 /// The Instance class. One of these exists for each instance of your NaCl | 46 /// The Instance class. One of these exists for each instance of your NaCl |
45 /// module on the web page. The browser will ask the Module object to create | 47 /// module on the web page. The browser will ask the Module object to create |
46 /// a new Instance for each occurrence of the <embed> tag that has these | 48 /// a new Instance for each occurrence of the <embed> tag that has these |
47 /// attributes: | 49 /// attributes: |
48 /// type="application/x-nacl" | 50 /// type="application/x-nacl" |
49 /// src="file_io.nmf" | 51 /// src="file_io.nmf" |
50 class FileIoInstance : public pp::Instance { | 52 class FileIoInstance : public pp::Instance { |
51 public: | 53 public: |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 file_thread_.message_loop().PostWork(callback_factory_.NewCallback( | 128 file_thread_.message_loop().PostWork(callback_factory_.NewCallback( |
127 &FileIoInstance::Save, file_name, file_text)); | 129 &FileIoInstance::Save, file_name, file_text)); |
128 return; | 130 return; |
129 } | 131 } |
130 | 132 |
131 if (instruction.compare(kDeletePrefix) == 0) { | 133 if (instruction.compare(kDeletePrefix) == 0) { |
132 file_thread_.message_loop().PostWork( | 134 file_thread_.message_loop().PostWork( |
133 callback_factory_.NewCallback(&FileIoInstance::Delete, file_name)); | 135 callback_factory_.NewCallback(&FileIoInstance::Delete, file_name)); |
134 return; | 136 return; |
135 } | 137 } |
| 138 |
| 139 if (instruction.compare(kListPrefix) == 0) { |
| 140 const std::string& dir_name = file_name; |
| 141 file_thread_.message_loop().PostWork( |
| 142 callback_factory_.NewCallback(&FileIoInstance::List, dir_name)); |
| 143 return; |
| 144 } |
136 } | 145 } |
137 | 146 |
138 void OpenFileSystem(int32_t /* result */) { | 147 void OpenFileSystem(int32_t /* result */) { |
139 int32_t rv = file_system_.Open(1024 * 1024, pp::CompletionCallback()); | 148 int32_t rv = file_system_.Open(1024 * 1024, pp::CompletionCallback()); |
140 if (rv == PP_OK) { | 149 if (rv == PP_OK) { |
141 file_system_ready_ = true; | 150 file_system_ready_ = true; |
142 // Notify the user interface that we're ready | 151 // Notify the user interface that we're ready |
143 PostMessage(pp::Var("READY|")); | 152 PostMessage(pp::Var("READY|")); |
144 } else { | 153 } else { |
145 ShowErrorMessage("Failed to open file system", rv); | 154 ShowErrorMessage("Failed to open file system", rv); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 if (result == PP_ERROR_FILENOTFOUND) { | 269 if (result == PP_ERROR_FILENOTFOUND) { |
261 ShowStatusMessage("File not found"); | 270 ShowStatusMessage("File not found"); |
262 return; | 271 return; |
263 } else if (result != PP_OK) { | 272 } else if (result != PP_OK) { |
264 ShowErrorMessage("Deletion failed", result); | 273 ShowErrorMessage("Deletion failed", result); |
265 return; | 274 return; |
266 } | 275 } |
267 ShowStatusMessage("File deleted"); | 276 ShowStatusMessage("File deleted"); |
268 } | 277 } |
269 | 278 |
| 279 void List(int32_t /* result */, const std::string& dir_name) { |
| 280 if (!file_system_ready_) { |
| 281 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); |
| 282 return; |
| 283 } |
| 284 pp::FileRef ref(file_system_, dir_name.c_str()); |
| 285 |
| 286 // Pass ref along to keep it alive. |
| 287 ref.ReadDirectoryEntries(callback_factory_.NewCallbackWithOutput( |
| 288 &FileIoInstance::ListCallback, ref)); |
| 289 } |
| 290 |
| 291 void ListCallback(int32_t result, |
| 292 const std::vector<pp::DirectoryEntry>& entries, |
| 293 pp::FileRef /* unused_ref */) { |
| 294 if (result != PP_OK) { |
| 295 ShowErrorMessage("List failed", result); |
| 296 return; |
| 297 } |
| 298 |
| 299 std::string buffer = "File list:"; |
| 300 for (size_t i = 0; i < entries.size(); ++i) { |
| 301 pp::Var name = entries[i].file_ref().GetName(); |
| 302 if (name.is_string()) |
| 303 buffer += " " + name.AsString(); |
| 304 } |
| 305 ShowStatusMessage(buffer); |
| 306 } |
| 307 |
270 /// Encapsulates our simple javascript communication protocol | 308 /// Encapsulates our simple javascript communication protocol |
271 void ShowErrorMessage(const std::string& message, int32_t result) { | 309 void ShowErrorMessage(const std::string& message, int32_t result) { |
272 std::stringstream ss; | 310 std::stringstream ss; |
273 ss << "ERR|" << message << " -- Error #: " << result; | 311 ss << "ERR|" << message << " -- Error #: " << result; |
274 PostMessage(pp::Var(ss.str())); | 312 PostMessage(pp::Var(ss.str())); |
275 } | 313 } |
276 | 314 |
277 /// Encapsulates our simple javascript communication protocol | 315 /// Encapsulates our simple javascript communication protocol |
278 void ShowStatusMessage(const std::string& message) { | 316 void ShowStatusMessage(const std::string& message) { |
279 std::stringstream ss; | 317 std::stringstream ss; |
(...skipping 19 matching lines...) Expand all Loading... |
299 }; | 337 }; |
300 | 338 |
301 namespace pp { | 339 namespace pp { |
302 /// Factory function called by the browser when the module is first loaded. | 340 /// Factory function called by the browser when the module is first loaded. |
303 /// The browser keeps a singleton of this module. It calls the | 341 /// The browser keeps a singleton of this module. It calls the |
304 /// CreateInstance() method on the object you return to make instances. There | 342 /// CreateInstance() method on the object you return to make instances. There |
305 /// is one instance per <embed> tag on the page. This is the main binding | 343 /// is one instance per <embed> tag on the page. This is the main binding |
306 /// point for your NaCl module with the browser. | 344 /// point for your NaCl module with the browser. |
307 Module* CreateModule() { return new FileIoModule(); } | 345 Module* CreateModule() { return new FileIoModule(); } |
308 } // namespace pp | 346 } // namespace pp |
OLD | NEW |