OLD | NEW |
1 {{+bindTo:partials.standard_nacl_article}} | 1 {{+bindTo:partials.standard_nacl_article}} |
2 | 2 |
3 <section id="file-i-o"> | 3 <section id="file-i-o"> |
4 <span id="devguide-coding-fileio"></span><h1 id="file-i-o"><span id="devguide-co
ding-fileio"></span>File I/O</h1> | 4 <span id="devguide-coding-fileio"></span><h1 id="file-i-o"><span id="devguide-co
ding-fileio"></span>File I/O</h1> |
5 <div class="contents local" id="contents" style="display: none"> | 5 <div class="contents local" id="contents" style="display: none"> |
6 <ul class="small-gap"> | 6 <ul class="small-gap"> |
7 <li><a class="reference internal" href="#introduction" id="id2">Introduction</a>
</li> | 7 <li><a class="reference internal" href="#introduction" id="id2">Introduction</a>
</li> |
8 <li><a class="reference internal" href="#reference-information" id="id3">Referen
ce information</a></li> | 8 <li><a class="reference internal" href="#reference-information" id="id3">Referen
ce information</a></li> |
9 <li><p class="first"><a class="reference internal" href="#local-file-i-o" id="id
4">Local file I/O</a></p> | 9 <li><p class="first"><a class="reference internal" href="#local-file-i-o" id="id
4">Local file I/O</a></p> |
10 <ul class="small-gap"> | 10 <ul class="small-gap"> |
(...skipping 17 matching lines...) Expand all Loading... |
28 <li><a class="reference internal" href="#handling-messages-from-javascript" id="
id16">Handling messages from JavaScript</a></li> | 28 <li><a class="reference internal" href="#handling-messages-from-javascript" id="
id16">Handling messages from JavaScript</a></li> |
29 <li><a class="reference internal" href="#saving-a-file" id="id17">Saving a file<
/a></li> | 29 <li><a class="reference internal" href="#saving-a-file" id="id17">Saving a file<
/a></li> |
30 <li><a class="reference internal" href="#loading-a-file" id="id18">Loading a fil
e</a></li> | 30 <li><a class="reference internal" href="#loading-a-file" id="id18">Loading a fil
e</a></li> |
31 <li><a class="reference internal" href="#id1" id="id19">Deleting a file</a></li> | 31 <li><a class="reference internal" href="#id1" id="id19">Deleting a file</a></li> |
32 <li><a class="reference internal" href="#listing-files-in-a-directory" id="id20"
>Listing files in a directory</a></li> | 32 <li><a class="reference internal" href="#listing-files-in-a-directory" id="id20"
>Listing files in a directory</a></li> |
33 <li><a class="reference internal" href="#making-a-new-directory" id="id21">Makin
g a new directory</a></li> | 33 <li><a class="reference internal" href="#making-a-new-directory" id="id21">Makin
g a new directory</a></li> |
34 </ul> | 34 </ul> |
35 </li> | 35 </li> |
36 </ul> | 36 </ul> |
37 | 37 |
38 </div><section id="introduction"> | 38 </div><h2 id="introduction">Introduction</h2> |
39 <h2 id="introduction">Introduction</h2> | |
40 <p>This chapter describes how to use the <a class="reference external" href="/na
tive-client/pepper_stable/cpp/classpp_1_1_file_i_o">FileIO API</a> to read and w
rite | 39 <p>This chapter describes how to use the <a class="reference external" href="/na
tive-client/pepper_stable/cpp/classpp_1_1_file_i_o">FileIO API</a> to read and w
rite |
41 files using a local secure data store.</p> | 40 files using a local secure data store.</p> |
42 <p>You might use the File IO API with the URL Loading APIs to create an overall | 41 <p>You might use the File IO API with the URL Loading APIs to create an overall |
43 data download and caching solution for your NaCl applications. For example:</p> | 42 data download and caching solution for your NaCl applications. For example:</p> |
44 <ol class="arabic simple"> | 43 <ol class="arabic simple"> |
45 <li>Use the File IO APIs to check the local disk to see if a file exists that | 44 <li>Use the File IO APIs to check the local disk to see if a file exists that |
46 your program needs.</li> | 45 your program needs.</li> |
47 <li>If the file exists locally, load it into memory using the File IO API. If | 46 <li>If the file exists locally, load it into memory using the File IO API. If |
48 the file doesn’t exist locally, use the URL Loading API to retrieve the | 47 the file doesn’t exist locally, use the URL Loading API to retrieve the |
49 file from the server.</li> | 48 file from the server.</li> |
50 <li>Use the File IO API to write the file to disk.</li> | 49 <li>Use the File IO API to write the file to disk.</li> |
51 <li>Load the file into memory using the File IO API when needed by your | 50 <li>Load the file into memory using the File IO API when needed by your |
52 application.</li> | 51 application.</li> |
53 </ol> | 52 </ol> |
54 <p>The example discussed in this chapter is included in the SDK in the directory | 53 <p>The example discussed in this chapter is included in the SDK in the directory |
55 <code>examples/api/file_io</code>.</p> | 54 <code>examples/api/file_io</code>.</p> |
56 </section><section id="reference-information"> | |
57 <h2 id="reference-information">Reference information</h2> | 55 <h2 id="reference-information">Reference information</h2> |
58 <p>For reference information related to FileIO, see the following documentation:
</p> | 56 <p>For reference information related to FileIO, see the following documentation:
</p> |
59 <ul class="small-gap"> | 57 <ul class="small-gap"> |
60 <li><a class="reference external" href="/native-client/pepper_stable/cpp/file__i
o_8h">file_io.h</a> - API to create a | 58 <li><a class="reference external" href="/native-client/pepper_stable/cpp/file__i
o_8h">file_io.h</a> - API to create a |
61 FileIO object</li> | 59 FileIO object</li> |
62 <li><a class="reference external" href="/native-client/pepper_stable/cpp/file__r
ef_8h">file_ref.h</a> - API to create | 60 <li><a class="reference external" href="/native-client/pepper_stable/cpp/file__r
ef_8h">file_ref.h</a> - API to create |
63 a file reference or “weak pointer” to a file in a file system</li> | 61 a file reference or “weak pointer” to a file in a file system</li> |
64 <li><a class="reference external" href="/native-client/pepper_stable/cpp/file__s
ystem_8h">file_system.h</a> - API to | 62 <li><a class="reference external" href="/native-client/pepper_stable/cpp/file__s
ystem_8h">file_system.h</a> - API to |
65 create a file system associated with a file</li> | 63 create a file system associated with a file</li> |
66 </ul> | 64 </ul> |
67 </section><section id="local-file-i-o"> | |
68 <h2 id="local-file-i-o">Local file I/O</h2> | 65 <h2 id="local-file-i-o">Local file I/O</h2> |
69 <p>Chrome provides an obfuscated, restricted area on disk to which a web app can | 66 <p>Chrome provides an obfuscated, restricted area on disk to which a web app can |
70 safely <a class="reference external" href="https://developers.google.com/chrome/
whitepapers/storage#persistent">read and write files</a>. The | 67 safely <a class="reference external" href="https://developers.google.com/chrome/
whitepapers/storage#persistent">read and write files</a>. The |
71 Pepper FileIO, FileRef, and FileSystem APIs (collectively called the File IO | 68 Pepper FileIO, FileRef, and FileSystem APIs (collectively called the File IO |
72 APIs) allow you to access this sandboxed local disk so you can read and write | 69 APIs) allow you to access this sandboxed local disk so you can read and write |
73 files and manage caching yourself. The data is persistent between launches of | 70 files and manage caching yourself. The data is persistent between launches of |
74 Chrome, and is not removed unless your application deletes it or the user | 71 Chrome, and is not removed unless your application deletes it or the user |
75 manually deletes it. There is no limit to the amount of local data you can | 72 manually deletes it. There is no limit to the amount of local data you can |
76 use, other than the actual space available on the local drive.</p> | 73 use, other than the actual space available on the local drive.</p> |
77 <section id="enabling-local-file-i-o"> | 74 <h3 id="enabling-local-file-i-o"><span id="enabling-file-access"></span><span id
="quota-management"></span>Enabling local file I/O</h3> |
78 <span id="enabling-file-access"></span><span id="quota-management"></span><h3 id
="enabling-local-file-i-o"><span id="enabling-file-access"></span><span id="quot
a-management"></span>Enabling local file I/O</h3> | |
79 <p>The easiest way to enable the writing of persistent local data is to include | 75 <p>The easiest way to enable the writing of persistent local data is to include |
80 the <a class="reference external" href="/extensions/declare_permissions#unlimite
dStorage">unlimitedStorage permission</a> in your Chrome Web Store | 76 the <a class="reference external" href="/extensions/declare_permissions#unlimite
dStorage">unlimitedStorage permission</a> in your Chrome Web Store |
81 manifest file. With this permission you can use the Pepper FileIO API without | 77 manifest file. With this permission you can use the Pepper FileIO API without |
82 the need to request disk space at run time. When the user installs the app | 78 the need to request disk space at run time. When the user installs the app |
83 Chrome displays a message announcing that the app writes to the local disk.</p> | 79 Chrome displays a message announcing that the app writes to the local disk.</p> |
84 <p>If you do not use the <code>unlimitedStorage</code> permission you must inclu
de | 80 <p>If you do not use the <code>unlimitedStorage</code> permission you must inclu
de |
85 JavaScript code that calls the <a class="reference external" href="http://update
s.html5rocks.com/2011/11/Quota-Management-API-Fast-Facts">HTML5 Quota Management
API</a> to | 81 JavaScript code that calls the <a class="reference external" href="http://update
s.html5rocks.com/2011/11/Quota-Management-API-Fast-Facts">HTML5 Quota Management
API</a> to |
86 explicitly request local disk space before using the FileIO API. In this case | 82 explicitly request local disk space before using the FileIO API. In this case |
87 Chrome will prompt the user to accept a requestQuota call every time one is | 83 Chrome will prompt the user to accept a requestQuota call every time one is |
88 made.</p> | 84 made.</p> |
89 </section><section id="testing-local-file-i-o"> | |
90 <h3 id="testing-local-file-i-o">Testing local file I/O</h3> | 85 <h3 id="testing-local-file-i-o">Testing local file I/O</h3> |
91 <p>You should be aware that using the <code>unlimitedStorage</code> manifest per
mission | 86 <p>You should be aware that using the <code>unlimitedStorage</code> manifest per
mission |
92 constrains the way you can test your app. Three of the four techniques | 87 constrains the way you can test your app. Three of the four techniques |
93 described in <a class="reference internal" href="/native-client/devguide/devcycl
e/running.html"><em>Running Native Client Applications</em></a> | 88 described in <a class="reference internal" href="/native-client/devguide/devcycl
e/running.html"><em>Running Native Client Applications</em></a> |
94 read the Chrome Web Store manifest file and enable the <code>unlimitedStorage</c
ode> | 89 read the Chrome Web Store manifest file and enable the <code>unlimitedStorage</c
ode> |
95 permission when it appears, but the first technique (local server) does not. | 90 permission when it appears, but the first technique (local server) does not. |
96 If you want to test the file IO portion of your app with a simple local server, | 91 If you want to test the file IO portion of your app with a simple local server, |
97 you need to include JavaScript code that calls the HTML5 Quota Management API. | 92 you need to include JavaScript code that calls the HTML5 Quota Management API. |
98 When you deliver your application you can replace this code with the | 93 When you deliver your application you can replace this code with the |
99 <code>unlimitedStorage</code> manifest permission.</p> | 94 <code>unlimitedStorage</code> manifest permission.</p> |
100 </section></section><section id="the-file-io-example"> | |
101 <h2 id="the-file-io-example">The <code>file_io</code> example</h2> | 95 <h2 id="the-file-io-example">The <code>file_io</code> example</h2> |
102 <p>The Native Client SDK includes an example, <code>file_io</code>, that demonst
rates how | 96 <p>The Native Client SDK includes an example, <code>file_io</code>, that demonst
rates how |
103 to read and write a local disk file. Since you will probably run the example | 97 to read and write a local disk file. Since you will probably run the example |
104 from a local server without a Chrome Web Store manifest file, the example’
s | 98 from a local server without a Chrome Web Store manifest file, the example’
s |
105 index file uses JavaScript to perform the Quota Management setup as described | 99 index file uses JavaScript to perform the Quota Management setup as described |
106 above. The example has these primary files:</p> | 100 above. The example has these primary files:</p> |
107 <ul class="small-gap"> | 101 <ul class="small-gap"> |
108 <li><code>index.html</code> - The HTML code that launches the Native Client modu
le and | 102 <li><code>index.html</code> - The HTML code that launches the Native Client modu
le and |
109 displays the user interface.</li> | 103 displays the user interface.</li> |
110 <li><code>example.js</code> - JavaScript code that requests quota (as described
above). It | 104 <li><code>example.js</code> - JavaScript code that requests quota (as described
above). It |
111 also listens for user interaction with the user interface, and forwards the | 105 also listens for user interaction with the user interface, and forwards the |
112 requests to the Native Client module.</li> | 106 requests to the Native Client module.</li> |
113 <li><code>file_io.cc</code> - The code that sets up and provides an entry point
to the | 107 <li><code>file_io.cc</code> - The code that sets up and provides an entry point
to the |
114 Native Client module.</li> | 108 Native Client module.</li> |
115 </ul> | 109 </ul> |
116 <p>The remainder of this section covers the code in the <code>file_io.cc</code>
file for | 110 <p>The remainder of this section covers the code in the <code>file_io.cc</code>
file for |
117 reading and writing files.</p> | 111 reading and writing files.</p> |
118 <section id="file-i-o-overview"> | |
119 <h3 id="file-i-o-overview">File I/O overview</h3> | 112 <h3 id="file-i-o-overview">File I/O overview</h3> |
120 <p>Like many Pepper APIs, the File IO API includes a set of methods that execute | 113 <p>Like many Pepper APIs, the File IO API includes a set of methods that execute |
121 asynchronously and that invoke callback functions in your Native Client module. | 114 asynchronously and that invoke callback functions in your Native Client module. |
122 Unlike most other examples, the <code>file_io</code> example also demonstrates h
ow to | 115 Unlike most other examples, the <code>file_io</code> example also demonstrates h
ow to |
123 make Pepper calls synchronously on a worker thread.</p> | 116 make Pepper calls synchronously on a worker thread.</p> |
124 <p>It is illegal to make blocking calls to Pepper on the module’s main thr
ead. | 117 <p>It is illegal to make blocking calls to Pepper on the module’s main thr
ead. |
125 This restriction is lifted when running on a worker thread—this is called | 118 This restriction is lifted when running on a worker thread—this is called |
126 “calling Pepper off the main thread”. This often simplifies the logi
c of your | 119 “calling Pepper off the main thread”. This often simplifies the logi
c of your |
127 code; multiple asynchronous Pepper functions can be called from one function on | 120 code; multiple asynchronous Pepper functions can be called from one function on |
128 your worker thread, so you can use the stack and standard control flow | 121 your worker thread, so you can use the stack and standard control flow |
129 structures normally.</p> | 122 structures normally.</p> |
130 <p>The high-level flow for the <code>file_io</code> example is described below.
Note that | 123 <p>The high-level flow for the <code>file_io</code> example is described below.
Note that |
131 methods in the namespace <code>pp</code> are part of the Pepper C++ API.</p> | 124 methods in the namespace <code>pp</code> are part of the Pepper C++ API.</p> |
132 </section><section id="creating-and-writing-a-file"> | |
133 <h3 id="creating-and-writing-a-file">Creating and writing a file</h3> | 125 <h3 id="creating-and-writing-a-file">Creating and writing a file</h3> |
134 <p>Following are the high-level steps involved in creating and writing to a | 126 <p>Following are the high-level steps involved in creating and writing to a |
135 file:</p> | 127 file:</p> |
136 <ol class="arabic simple"> | 128 <ol class="arabic simple"> |
137 <li><code>pp::FileIO::Open</code> is called with the <code>PP_FILEOPEN_FLAG_CREA
TE</code> flag to | 129 <li><code>pp::FileIO::Open</code> is called with the <code>PP_FILEOPEN_FLAG_CREA
TE</code> flag to |
138 create a file. Because the callback function is <code>pp::BlockUntilComplete</c
ode>, | 130 create a file. Because the callback function is <code>pp::BlockUntilComplete</c
ode>, |
139 this thread is blocked until <code>Open</code> succeeds or fails.</li> | 131 this thread is blocked until <code>Open</code> succeeds or fails.</li> |
140 <li><code>pp::FileIO::Write</code> is called to write the contents. Again, the t
hread is | 132 <li><code>pp::FileIO::Write</code> is called to write the contents. Again, the t
hread is |
141 blocked until the call to <code>Write</code> completes. If there is more data to | 133 blocked until the call to <code>Write</code> completes. If there is more data to |
142 write, <code>Write</code> is called again.</li> | 134 write, <code>Write</code> is called again.</li> |
143 <li>When there is no more data to write, call <code>pp::FileIO::Flush</code>.</l
i> | 135 <li>When there is no more data to write, call <code>pp::FileIO::Flush</code>.</l
i> |
144 </ol> | 136 </ol> |
145 </section><section id="opening-and-reading-a-file"> | |
146 <h3 id="opening-and-reading-a-file">Opening and reading a file</h3> | 137 <h3 id="opening-and-reading-a-file">Opening and reading a file</h3> |
147 <p>Following are the high-level steps involved in opening and reading a file:</p
> | 138 <p>Following are the high-level steps involved in opening and reading a file:</p
> |
148 <ol class="arabic simple"> | 139 <ol class="arabic simple"> |
149 <li><code>pp::FileIO::Open</code> is called to open the file. Because the callba
ck | 140 <li><code>pp::FileIO::Open</code> is called to open the file. Because the callba
ck |
150 function is <code>pp::BlockUntilComplete</code>, this thread is blocked until Op
en | 141 function is <code>pp::BlockUntilComplete</code>, this thread is blocked until Op
en |
151 succeeds or fails.</li> | 142 succeeds or fails.</li> |
152 <li><code>pp::FileIO::Query</code> is called to query information about the file
, such as | 143 <li><code>pp::FileIO::Query</code> is called to query information about the file
, such as |
153 its file size. The thread is blocked until <code>Query</code> completes.</li> | 144 its file size. The thread is blocked until <code>Query</code> completes.</li> |
154 <li><code>pp::FileIO::Read</code> is called to read the contents. The thread is
blocked | 145 <li><code>pp::FileIO::Read</code> is called to read the contents. The thread is
blocked |
155 until <code>Read</code> completes. If there is more data to read, <code>Read</co
de> is called | 146 until <code>Read</code> completes. If there is more data to read, <code>Read</co
de> is called |
156 again.</li> | 147 again.</li> |
157 </ol> | 148 </ol> |
158 </section><section id="deleting-a-file"> | |
159 <h3 id="deleting-a-file">Deleting a file</h3> | 149 <h3 id="deleting-a-file">Deleting a file</h3> |
160 <p>Deleting a file is straightforward: call <code>pp::FileRef::Delete</code>. Th
e thread is | 150 <p>Deleting a file is straightforward: call <code>pp::FileRef::Delete</code>. Th
e thread is |
161 blocked until <code>Delete</code> completes.</p> | 151 blocked until <code>Delete</code> completes.</p> |
162 </section><section id="making-a-directory"> | |
163 <h3 id="making-a-directory">Making a directory</h3> | 152 <h3 id="making-a-directory">Making a directory</h3> |
164 <p>Making a directory is also straightforward: call <code>pp::File::MakeDirector
y</code>. | 153 <p>Making a directory is also straightforward: call <code>pp::File::MakeDirector
y</code>. |
165 The thread is blocked until <code>MakeDirectory</code> completes.</p> | 154 The thread is blocked until <code>MakeDirectory</code> completes.</p> |
166 </section><section id="listing-the-contents-of-a-directory"> | |
167 <h3 id="listing-the-contents-of-a-directory">Listing the contents of a directory
</h3> | 155 <h3 id="listing-the-contents-of-a-directory">Listing the contents of a directory
</h3> |
168 <p>Following are the high-level steps involved in listing a directory:</p> | 156 <p>Following are the high-level steps involved in listing a directory:</p> |
169 <ol class="arabic simple"> | 157 <ol class="arabic simple"> |
170 <li><code>pp::FileRef::ReadDirectoryEntries</code> is called, and given a direct
ory entry | 158 <li><code>pp::FileRef::ReadDirectoryEntries</code> is called, and given a direct
ory entry |
171 to list. A callback is given as well; many of the other functions use | 159 to list. A callback is given as well; many of the other functions use |
172 <code>pp::BlockUntilComplete</code>, but <code>ReadDirectoryEntries</code> retur
ns results in | 160 <code>pp::BlockUntilComplete</code>, but <code>ReadDirectoryEntries</code> retur
ns results in |
173 its callback, so it must be specified.</li> | 161 its callback, so it must be specified.</li> |
174 <li>When the call to <code>ReadDirectoryEntries</code> completes, it calls | 162 <li>When the call to <code>ReadDirectoryEntries</code> completes, it calls |
175 <code>ListCallback</code> which packages up the results into a string message, a
nd | 163 <code>ListCallback</code> which packages up the results into a string message, a
nd |
176 sends it to JavaScript.</li> | 164 sends it to JavaScript.</li> |
177 </ol> | 165 </ol> |
178 </section></section><section id="file-io-deep-dive"> | |
179 <h2 id="file-io-deep-dive"><code>file_io</code> deep dive</h2> | 166 <h2 id="file-io-deep-dive"><code>file_io</code> deep dive</h2> |
180 <p>The <code>file_io</code> example displays a user interface with a couple of f
ields and | 167 <p>The <code>file_io</code> example displays a user interface with a couple of f
ields and |
181 several buttons. Following is a screenshot of the <code>file_io</code> example:<
/p> | 168 several buttons. Following is a screenshot of the <code>file_io</code> example:<
/p> |
182 <img alt="/native-client/images/fileioexample.png" src="/native-client/images/fi
leioexample.png" /> | 169 <img alt="/native-client/images/fileioexample.png" src="/native-client/images/fi
leioexample.png" /> |
183 <p>Each radio button is a file operation you can perform, with some reasonable | 170 <p>Each radio button is a file operation you can perform, with some reasonable |
184 default values for filenames. Try typing a message in the large input box and | 171 default values for filenames. Try typing a message in the large input box and |
185 clicking <code>Save</code>, then switching to the <code>Load File</code> operati
on, and | 172 clicking <code>Save</code>, then switching to the <code>Load File</code> operati
on, and |
186 clicking <code>Load</code>.</p> | 173 clicking <code>Load</code>.</p> |
187 <p>Let’s take a look at what is going on under the hood.</p> | 174 <p>Let’s take a look at what is going on under the hood.</p> |
188 <section id="opening-a-file-system-and-preparing-for-file-i-o"> | |
189 <h3 id="opening-a-file-system-and-preparing-for-file-i-o">Opening a file system
and preparing for file I/O</h3> | 175 <h3 id="opening-a-file-system-and-preparing-for-file-i-o">Opening a file system
and preparing for file I/O</h3> |
190 <p><code>pp::Instance::Init</code> is called when an instance of a module is cre
ated. In | 176 <p><code>pp::Instance::Init</code> is called when an instance of a module is cre
ated. In |
191 this example, <code>Init</code> starts a new thread (via the <code>pp::SimpleThr
ead</code> | 177 this example, <code>Init</code> starts a new thread (via the <code>pp::SimpleThr
ead</code> |
192 class), and tells it to open the filesystem:</p> | 178 class), and tells it to open the filesystem:</p> |
193 <pre class="prettyprint"> | 179 <pre class="prettyprint"> |
194 virtual bool Init(uint32_t /*argc*/, | 180 virtual bool Init(uint32_t /*argc*/, |
195 const char * /*argn*/ [], | 181 const char * /*argn*/ [], |
196 const char * /*argv*/ []) { | 182 const char * /*argv*/ []) { |
197 file_thread_.Start(); | 183 file_thread_.Start(); |
198 // Open the file system on the file_thread_. Since this is the first | 184 // Open the file system on the file_thread_. Since this is the first |
(...skipping 20 matching lines...) Expand all Loading... |
219 int32_t rv = file_system_.Open(1024 * 1024, pp::BlockUntilComplete()); | 205 int32_t rv = file_system_.Open(1024 * 1024, pp::BlockUntilComplete()); |
220 if (rv == PP_OK) { | 206 if (rv == PP_OK) { |
221 file_system_ready_ = true; | 207 file_system_ready_ = true; |
222 // Notify the user interface that we're ready | 208 // Notify the user interface that we're ready |
223 PostMessage("READY|"); | 209 PostMessage("READY|"); |
224 } else { | 210 } else { |
225 ShowErrorMessage("Failed to open file system", rv); | 211 ShowErrorMessage("Failed to open file system", rv); |
226 } | 212 } |
227 } | 213 } |
228 </pre> | 214 </pre> |
229 </section><section id="handling-messages-from-javascript"> | |
230 <h3 id="handling-messages-from-javascript">Handling messages from JavaScript</h3
> | 215 <h3 id="handling-messages-from-javascript">Handling messages from JavaScript</h3
> |
231 <p>When you click the <code>Save</code> button, JavaScript posts a message to th
e NaCl | 216 <p>When you click the <code>Save</code> button, JavaScript posts a message to th
e NaCl |
232 module with the file operation to perform sent as a string (See <a class="refere
nce internal" href="/native-client/devguide/coding/message-system.html"><em>Mess
aging | 217 module with the file operation to perform sent as a string (See <a class="refere
nce internal" href="/native-client/devguide/coding/message-system.html"><em>Mess
aging |
233 System</em></a> for more details on message passing). The string is | 218 System</em></a> for more details on message passing). The string is |
234 parsed by <code>HandleMessage</code>, and new work is added to the file thread:<
/p> | 219 parsed by <code>HandleMessage</code>, and new work is added to the file thread:<
/p> |
235 <pre class="prettyprint"> | 220 <pre class="prettyprint"> |
236 virtual void HandleMessage(const pp::Var& var_message) { | 221 virtual void HandleMessage(const pp::Var& var_message) { |
237 if (!var_message.is_string()) | 222 if (!var_message.is_string()) |
238 return; | 223 return; |
239 | 224 |
(...skipping 13 matching lines...) Expand all Loading... |
253 | 238 |
254 // Dispatch the instruction | 239 // Dispatch the instruction |
255 if (instruction == kLoadPrefix) { | 240 if (instruction == kLoadPrefix) { |
256 file_thread_.message_loop().PostWork( | 241 file_thread_.message_loop().PostWork( |
257 callback_factory_.NewCallback(&FileIoInstance::Load, file_name)); | 242 callback_factory_.NewCallback(&FileIoInstance::Load, file_name)); |
258 } else if (instruction == kSavePrefix) { | 243 } else if (instruction == kSavePrefix) { |
259 ... | 244 ... |
260 } | 245 } |
261 } | 246 } |
262 </pre> | 247 </pre> |
263 </section><section id="saving-a-file"> | |
264 <h3 id="saving-a-file">Saving a file</h3> | 248 <h3 id="saving-a-file">Saving a file</h3> |
265 <p><code>FileIoInstance::Save</code> is called when the <code>Save</code> button
is pressed. First, | 249 <p><code>FileIoInstance::Save</code> is called when the <code>Save</code> button
is pressed. First, |
266 it checks to see that the FileSystem has been successfully opened:</p> | 250 it checks to see that the FileSystem has been successfully opened:</p> |
267 <pre class="prettyprint"> | 251 <pre class="prettyprint"> |
268 if (!file_system_ready_) { | 252 if (!file_system_ready_) { |
269 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); | 253 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); |
270 return; | 254 return; |
271 } | 255 } |
272 </pre> | 256 </pre> |
273 <p>It then creates a <code>pp::FileRef</code> resource with the name of the file
. A | 257 <p>It then creates a <code>pp::FileRef</code> resource with the name of the file
. A |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } while (bytes_written < static_cast<int64_t>(file_contents.length())); | 301 } while (bytes_written < static_cast<int64_t>(file_contents.length())); |
318 </pre> | 302 </pre> |
319 <p>Finally, the file is flushed to push all changes to disk:</p> | 303 <p>Finally, the file is flushed to push all changes to disk:</p> |
320 <pre class="prettyprint"> | 304 <pre class="prettyprint"> |
321 int32_t flush_result = file.Flush(pp::BlockUntilComplete()); | 305 int32_t flush_result = file.Flush(pp::BlockUntilComplete()); |
322 if (flush_result != PP_OK) { | 306 if (flush_result != PP_OK) { |
323 ShowErrorMessage("File fail to flush", flush_result); | 307 ShowErrorMessage("File fail to flush", flush_result); |
324 return; | 308 return; |
325 } | 309 } |
326 </pre> | 310 </pre> |
327 </section><section id="loading-a-file"> | |
328 <h3 id="loading-a-file">Loading a file</h3> | 311 <h3 id="loading-a-file">Loading a file</h3> |
329 <p><code>FileIoInstance::Load</code> is called when the <code>Load</code> button
is pressed. Like | 312 <p><code>FileIoInstance::Load</code> is called when the <code>Load</code> button
is pressed. Like |
330 the <code>Save</code> function, <code>Load</code> first checks to see if the Fil
eSystem has been | 313 the <code>Save</code> function, <code>Load</code> first checks to see if the Fil
eSystem has been |
331 successfully opened, and creates a new <code>FileRef</code>:</p> | 314 successfully opened, and creates a new <code>FileRef</code>:</p> |
332 <pre class="prettyprint"> | 315 <pre class="prettyprint"> |
333 if (!file_system_ready_) { | 316 if (!file_system_ready_) { |
334 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); | 317 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); |
335 return; | 318 return; |
336 } | 319 } |
337 pp::FileRef ref(file_system_, file_name.c_str()); | 320 pp::FileRef ref(file_system_, file_name.c_str()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } | 370 } |
388 </pre> | 371 </pre> |
389 <p>Finally, the contents of the file are sent back to JavaScript, to be displaye
d | 372 <p>Finally, the contents of the file are sent back to JavaScript, to be displaye
d |
390 on the page. This example uses “<code>DISP|</code>” as a prefix comm
and for display | 373 on the page. This example uses “<code>DISP|</code>” as a prefix comm
and for display |
391 information:</p> | 374 information:</p> |
392 <pre class="prettyprint"> | 375 <pre class="prettyprint"> |
393 std::string string_data(data.begin(), data.end()); | 376 std::string string_data(data.begin(), data.end()); |
394 PostMessage("DISP|" + string_data); | 377 PostMessage("DISP|" + string_data); |
395 ShowStatusMessage("Load success"); | 378 ShowStatusMessage("Load success"); |
396 </pre> | 379 </pre> |
397 </section><section id="id1"> | |
398 <h3 id="id1">Deleting a file</h3> | 380 <h3 id="id1">Deleting a file</h3> |
399 <p><code>FileIoInstance::Delete</code> is called when the <code>Delete</code> bu
tton is pressed. | 381 <p><code>FileIoInstance::Delete</code> is called when the <code>Delete</code> bu
tton is pressed. |
400 First, it checks whether the FileSystem has been opened, and creates a new | 382 First, it checks whether the FileSystem has been opened, and creates a new |
401 <code>FileRef</code>:</p> | 383 <code>FileRef</code>:</p> |
402 <pre class="prettyprint"> | 384 <pre class="prettyprint"> |
403 if (!file_system_ready_) { | 385 if (!file_system_ready_) { |
404 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); | 386 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); |
405 return; | 387 return; |
406 } | 388 } |
407 pp::FileRef ref(file_system_, file_name.c_str()); | 389 pp::FileRef ref(file_system_, file_name.c_str()); |
408 </pre> | 390 </pre> |
409 <p>Unlike <code>Save</code> and <code>Load</code>, <code>Delete</code> is called
on the <code>FileRef</code> resource, | 391 <p>Unlike <code>Save</code> and <code>Load</code>, <code>Delete</code> is called
on the <code>FileRef</code> resource, |
410 not a <code>FileIO</code> resource. Note that the result is checked for | 392 not a <code>FileIO</code> resource. Note that the result is checked for |
411 <code>PP_ERROR_FILENOTFOUND</code> to give a better error message when trying to
delete | 393 <code>PP_ERROR_FILENOTFOUND</code> to give a better error message when trying to
delete |
412 a non-existent file:</p> | 394 a non-existent file:</p> |
413 <pre class="prettyprint"> | 395 <pre class="prettyprint"> |
414 int32_t result = ref.Delete(pp::BlockUntilComplete()); | 396 int32_t result = ref.Delete(pp::BlockUntilComplete()); |
415 if (result == PP_ERROR_FILENOTFOUND) { | 397 if (result == PP_ERROR_FILENOTFOUND) { |
416 ShowStatusMessage("File/Directory not found"); | 398 ShowStatusMessage("File/Directory not found"); |
417 return; | 399 return; |
418 } else if (result != PP_OK) { | 400 } else if (result != PP_OK) { |
419 ShowErrorMessage("Deletion failed", result); | 401 ShowErrorMessage("Deletion failed", result); |
420 return; | 402 return; |
421 } | 403 } |
422 </pre> | 404 </pre> |
423 </section><section id="listing-files-in-a-directory"> | |
424 <h3 id="listing-files-in-a-directory">Listing files in a directory</h3> | 405 <h3 id="listing-files-in-a-directory">Listing files in a directory</h3> |
425 <p><code>FileIoInstance::List</code> is called when the <code>List Directory</co
de> button is | 406 <p><code>FileIoInstance::List</code> is called when the <code>List Directory</co
de> button is |
426 pressed. Like all other operations, it checks whether the FileSystem has been | 407 pressed. Like all other operations, it checks whether the FileSystem has been |
427 opened and creates a new <code>FileRef</code>:</p> | 408 opened and creates a new <code>FileRef</code>:</p> |
428 <pre class="prettyprint"> | 409 <pre class="prettyprint"> |
429 if (!file_system_ready_) { | 410 if (!file_system_ready_) { |
430 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); | 411 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); |
431 return; | 412 return; |
432 } | 413 } |
433 | 414 |
(...skipping 29 matching lines...) Expand all Loading... |
463 for (size_t i = 0; i < entries.size(); ++i) { | 444 for (size_t i = 0; i < entries.size(); ++i) { |
464 pp::Var name = entries[i].file_ref().GetName(); | 445 pp::Var name = entries[i].file_ref().GetName(); |
465 if (name.is_string()) { | 446 if (name.is_string()) { |
466 ss << "|" << name.AsString(); | 447 ss << "|" << name.AsString(); |
467 } | 448 } |
468 } | 449 } |
469 PostMessage(ss.str()); | 450 PostMessage(ss.str()); |
470 ShowStatusMessage("List success"); | 451 ShowStatusMessage("List success"); |
471 } | 452 } |
472 </pre> | 453 </pre> |
473 </section><section id="making-a-new-directory"> | |
474 <h3 id="making-a-new-directory">Making a new directory</h3> | 454 <h3 id="making-a-new-directory">Making a new directory</h3> |
475 <p><code>FileIoInstance::MakeDir</code> is called when the <code>Make Directory<
/code> button is | 455 <p><code>FileIoInstance::MakeDir</code> is called when the <code>Make Directory<
/code> button is |
476 pressed. Like all other operations, it checks whether the FileSystem has been | 456 pressed. Like all other operations, it checks whether the FileSystem has been |
477 opened and creates a new <code>FileRef</code>:</p> | 457 opened and creates a new <code>FileRef</code>:</p> |
478 <pre class="prettyprint"> | 458 <pre class="prettyprint"> |
479 if (!file_system_ready_) { | 459 if (!file_system_ready_) { |
480 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); | 460 ShowErrorMessage("File system is not open", PP_ERROR_FAILED); |
481 return; | 461 return; |
482 } | 462 } |
483 pp::FileRef ref(file_system_, dir_name.c_str()); | 463 pp::FileRef ref(file_system_, dir_name.c_str()); |
484 </pre> | 464 </pre> |
485 <p>Then the <code>pp::FileRef::MakeDirectory</code> function is called.</p> | 465 <p>Then the <code>pp::FileRef::MakeDirectory</code> function is called.</p> |
486 <pre class="prettyprint"> | 466 <pre class="prettyprint"> |
487 int32_t result = ref.MakeDirectory( | 467 int32_t result = ref.MakeDirectory( |
488 PP_MAKEDIRECTORYFLAG_NONE, pp::BlockUntilComplete()); | 468 PP_MAKEDIRECTORYFLAG_NONE, pp::BlockUntilComplete()); |
489 if (result != PP_OK) { | 469 if (result != PP_OK) { |
490 ShowErrorMessage("Make directory failed", result); | 470 ShowErrorMessage("Make directory failed", result); |
491 return; | 471 return; |
492 } | 472 } |
493 ShowStatusMessage("Make directory success"); | 473 ShowStatusMessage("Make directory success"); |
494 </pre> | 474 </pre> |
495 </section></section></section> | 475 </section> |
496 | 476 |
497 {{/partials.standard_nacl_article}} | 477 {{/partials.standard_nacl_article}} |
OLD | NEW |