Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(532)

Side by Side Diff: native_client_sdk/src/doc/_developer.chrome.com_generated/devguide/coding/url-loading.html

Issue 140993006: [NaCl SDK Docs] Check in the generated NaCl SDK Documentation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: try without pepper_{dev,beta,stable} Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 {{+bindTo:partials.standard_nacl_article}}
2
3 <section id="url-loading">
4 <span id="devguide-coding-url-loading"></span><h1 id="url-loading"><span id="dev guide-coding-url-loading"></span>URL Loading</h1>
5 <div class="contents local topic" id="contents">
6 <ul class="small-gap">
7 <li><a class="reference internal" href="#introduction" id="id1">Introduction</a> </li>
8 <li><a class="reference internal" href="#reference-information" id="id2">Referen ce information</a></li>
9 <li><a class="reference internal" href="#background" id="id3">Background</a></li >
10 <li><p class="first"><a class="reference internal" href="#the-url-loader-example " id="id4">The <code>url_loader</code> example</a></p>
11 <ul class="small-gap">
12 <li><a class="reference internal" href="#url-loading-overview" id="id5">URL load ing overview</a></li>
13 </ul>
14 </li>
15 <li><p class="first"><a class="reference internal" href="#url-loader-deep-dive" id="id6"><code>url_loader</code> deep dive</a></p>
16 <ul class="small-gap">
17 <li><a class="reference internal" href="#setting-up-the-request" id="id7">Settin g up the request</a></li>
18 <li><a class="reference internal" href="#downloading-the-data" id="id8">Download ing the data</a></li>
19 <li><a class="reference internal" href="#displaying-a-result" id="id9">Displayin g a result</a></li>
20 </ul>
21 </li>
22 </ul>
23 </div>
24 <section id="introduction">
25 <h2 id="introduction">Introduction</h2>
26 <p>This chapter describes how to use the <a class="reference external" href="htt ps://developers.google.com/native-client/peppercpp/classpp_1_1_u_r_l_loader">URL Loader API</a>
27 to load resources such as images and sound files from a server into your
28 application.</p>
29 <p>The example discussed in this chapter is included in the SDK in the directory
30 <code>examples/api/url_loader</code>.</p>
31 </section><section id="reference-information">
32 <h2 id="reference-information">Reference information</h2>
33 <p>For reference information related to loading data from URLs, see the
34 following documentation:</p>
35 <ul class="small-gap">
36 <li><a class="reference external" href="https://developers.google.com/native-cli ent/peppercpp/url__loader_8h">url_loader.h</a> -
37 Contains <code>URLLoader</code> class for loading data from URLs</li>
38 <li><a class="reference external" href="https://developers.google.com/native-cli ent/peppercpp/url__request__info_8h">url_request_info.h</a>
39 - Contains <code>URLRequest</code> class for creating and manipulating URL reque sts</li>
40 <li><a class="reference external" href="https://developers.google.com/native-cli ent/peppercpp/url__response__info_8h">url_response_info.h</a>
41 - Contains <code>URLResponse</code> class for examaning URL responses</li>
42 </ul>
43 </section><section id="background">
44 <h2 id="background">Background</h2>
45 <p>When a user launches your Native Client web application, Chrome downloads and
46 caches your application&#8217;s HTML file, manifest file (.nmf), and Native Clie nt
47 module (.pexe or .nexe). If your application needs additional assets, such as
48 images and sound files, it must explicitly load those assets. You can use the
49 Pepper APIs described in this chapter to load assets from a URL into your
50 application.</p>
51 <p>After you&#8217;ve loaded assets into your application, Chrome will cache tho se
52 assets. To avoid being at the whim of the Chrome cache, however, you may want
53 to use the <a class="reference external" href="https://developers.google.com/nat ive-client/peppercpp/classpp_1_1_file_i_o">Pepper FileIO API</a>
54 to write those assets to a persistent, sandboxed location on the user&#8217;s fi le
55 system.</p>
56 </section><section id="the-url-loader-example">
57 <h2 id="the-url-loader-example">The <code>url_loader</code> example</h2>
58 <p>The SDK includes an example called <code>url_loader</code> demonstrating down loading
59 files from a server. This example has these primary files:</p>
60 <ul class="small-gap">
61 <li><code>index.html</code> - The HTML code that launches the Native Client modu le.</li>
62 <li><code>example.js</code> - The JavaScript file for index.html. It has code th at sends
63 a PostMessage request to the Native Client module when the &#8220;Get URL&#8221; button
64 is clicked.</li>
65 <li><code>url_loader_success.html</code> - An HTML file on the server whose cont ents are
66 being retrieved using the <code>URLLoader</code> API.</li>
67 <li><code>url_loader.cc</code> - The code that sets up and provides and entry po int into
68 the Native client module.</li>
69 <li><code>url_loader_handler.cc</code> - The code that retrieves the contents of the
70 url_loader_success.html file and returns the results (this is where the
71 bulk of the work is done).</li>
72 </ul>
73 <p>The remainder of this document covers the code in the <code>url_loader.cc</co de> and
74 <code>url_loader_handler.cc</code> files.</p>
75 <section id="url-loading-overview">
76 <h3 id="url-loading-overview">URL loading overview</h3>
77 <p>Like many Pepper APIs, the <code>URLLoader</code> API includes a set of metho ds that
78 execute asynchronously and that invoke callback functions in your Native Client
79 module. The high-level flow for the <code>url_loader</code> example is described below.
80 Note that methods in the namespace <code>pp::URLLoader</code> are part of the Pe pper
81 <code>URLLoader</code> API, while the rest of the functions are part of the code in the
82 Native Client module (specifically in the file <code>url_loader_handler.cc</code >). The
83 following image shows the flow of the <code>url_loader_handler</code> code:</p>
84 <img alt="/native-client/images/pepper-urlloader-api.png" src="/native-client/im ages/pepper-urlloader-api.png" />
85 <p>Following are the high-level steps involved in URL loading.</p>
86 <ol class="arabic simple">
87 <li>The Native Client module calls <code>pp::URLLoader::Open</code> to begin ope ning the
88 URL.</li>
89 <li>When <code>Open</code> completes, it invokes a callback function in the Nati ve Client
90 module (in this case, <code>OnOpen</code>).</li>
91 <li>The Native Client module calls the Pepper function
92 <code>URLLoader::ReadResponseBody</code> to begin reading the response body with the
93 data. <code>ReadResponseBody</code> is passed an optional callback function in t he
94 Native Client module (in this case, On <code>Read</code>). The callback function is
95 an optional callback because <code>ReadResponseBody</code> may read data and ret urn
96 synchronously if data is available (this improves performance for large
97 files and fast connections).</li>
98 </ol>
99 <p>The remainder of this document demonstrates how the previous steps are
100 implemented in the <code>url_loader</code> example.</p>
101 </section></section><section id="url-loader-deep-dive">
102 <h2 id="url-loader-deep-dive"><code>url_loader</code> deep dive</h2>
103 <section id="setting-up-the-request">
104 <h3 id="setting-up-the-request">Setting up the request</h3>
105 <p><code>HandleMessage</code> in <code>url_loader.cc</code> creates a <code>URLL oaderHandler</code> instance
106 and passes it the URL of the asset to be retrieved. Then <code>HandleMessage</co de>
107 calls <code>Start</code> to start retrieving the asset from the server:</p>
108 <pre class="prettyprint">
109 void URLLoaderInstance::HandleMessage(const pp::Var&amp; var_message) {
110 if (!var_message.is_string()) {
111 return;
112 }
113 std::string message = var_message.AsString();
114 if (message.find(kLoadUrlMethodId) == 0) {
115 // The argument to getUrl is everything after the first ':'.
116 size_t sep_pos = message.find_first_of(kMessageArgumentSeparator);
117 if (sep_pos != std::string::npos) {
118 std::string url = message.substr(sep_pos + 1);
119 printf(&quot;URLLoaderInstance::HandleMessage('%s', '%s')\n&quot;,
120 message.c_str(),
121 url.c_str());
122 fflush(stdout);
123 URLLoaderHandler* handler = URLLoaderHandler::Create(this, url);
124 if (handler != NULL) {
125 // Starts asynchronous download. When download is finished or when an
126 // error occurs, |handler| posts the results back to the browser
127 // vis PostMessage and self-destroys.
128 handler-&gt;Start();
129 }
130 }
131 }
132 }
133 </pre>
134 <p>Notice that the constructor for <code>URLLoaderHandler</code> in
135 <code>url_loader_handler.cc</code> sets up the parameters of the URL request (us ing
136 <code>SetURL,</code> <code>SetMethod</code>, and <code>SetRecordDownloadProgress </code>):</p>
137 <pre class="prettyprint">
138 URLLoaderHandler::URLLoaderHandler(pp::Instance* instance,
139 const std::string&amp; url)
140 : instance_(instance),
141 url_(url),
142 url_request_(instance),
143 url_loader_(instance),
144 buffer_(new char[READ_BUFFER_SIZE]),
145 cc_factory_(this) {
146 url_request_.SetURL(url);
147 url_request_.SetMethod(&quot;GET&quot;);
148 url_request_.SetRecordDownloadProgress(true);
149 }
150 </pre>
151 </section><section id="downloading-the-data">
152 <h3 id="downloading-the-data">Downloading the data</h3>
153 <p><code>Start</code> in <code>url_loader_handler.cc</code> creates a callback ( <code>cc</code>) using a
154 <code>CompletionCallbackFactory</code>. The callback is passed to <code>Open</co de> to be called
155 upon its completion. <code>Open</code> begins loading the <code>URLRequestInfo</ code>.</p>
156 <pre class="prettyprint">
157 void URLLoaderHandler::Start() {
158 pp::CompletionCallback cc =
159 cc_factory_.NewCallback(&amp;URLLoaderHandler::OnOpen);
160 url_loader_.Open(url_request_, cc);
161 }
162 </pre>
163 <p><code>OnOpen</code> ensures that the Open call was successful and, if so, cal ls
164 <code>GetDownloadProgress</code> to determine the amount of data to be downloade d so it
165 can allocate memory for the response body.</p>
166 <p>Note that the amount of data to be downloaded may be unknown, in which case
167 <code>GetDownloadProgress</code> sets <code>total_bytes_to_be_received</code> to -1. It is not a
168 problem if <code>total_bytes_to_be_received</code> is set to -1 or if
169 <code>GetDownloadProgress</code> fails; in these scenarios memory for the read b uffer
170 can&#8217;t be allocated in advance and must be allocated as data is received.</ p>
171 <p>Finally, <code>OnOpen</code> calls <code>ReadBody.</code></p>
172 <pre class="prettyprint">
173 void URLLoaderHandler::OnOpen(int32_t result) {
174 if (result != PP_OK) {
175 ReportResultAndDie(url_, &quot;pp::URLLoader::Open() failed&quot;, false);
176 return;
177 }
178 int64_t bytes_received = 0;
179 int64_t total_bytes_to_be_received = 0;
180 if (url_loader_.GetDownloadProgress(&amp;bytes_received,
181 &amp;total_bytes_to_be_received)) {
182 if (total_bytes_to_be_received &gt; 0) {
183 url_response_body_.reserve(total_bytes_to_be_received);
184 }
185 }
186 url_request_.SetRecordDownloadProgress(false);
187 ReadBody();
188 }
189 </pre>
190 <p><code>ReadBody</code> creates another <code>CompletionCallback</code> (a <cod e>NewOptionalCallback</code>)
191 and passes it to <code>ReadResponseBody,</code> which reads the response body, a nd
192 <code>AppendDataBytes,</code> which appends the resulting data to the previously read
193 data.</p>
194 <pre class="prettyprint">
195 void URLLoaderHandler::ReadBody() {
196 pp::CompletionCallback cc =
197 cc_factory_.NewOptionalCallback(&amp;URLLoaderHandler::OnRead);
198 int32_t result = PP_OK;
199 do {
200 result = url_loader_.ReadResponseBody(buffer_, READ_BUFFER_SIZE, cc);
201 if (result &gt; 0) {
202 AppendDataBytes(buffer_, result);
203 }
204 } while (result &gt; 0);
205
206 if (result != PP_OK_COMPLETIONPENDING) {
207 cc.Run(result);
208 }
209 }
210
211 void URLLoaderHandler::AppendDataBytes(const char* buffer, int32_t num_bytes) {
212 if (num_bytes &lt;= 0)
213 return;
214 num_bytes = std::min(READ_BUFFER_SIZE, num_bytes);
215 url_response_body_.insert(
216 url_response_body_.end(), buffer, buffer + num_bytes);
217 }
218 </pre>
219 <p>Eventually either all the bytes have been read for the entire file (resulting
220 in <code>PP_OK</code> or 0), all the bytes have been read for what has been
221 downloaded, but more is to be downloaded (<code>PP_OK_COMPLETIONPENDING</code> o r -1),
222 or there is an error (less than -1). <code>OnRead</code> is called in the event of an
223 error or <code>PP_OK</code>.</p>
224 </section><section id="displaying-a-result">
225 <h3 id="displaying-a-result">Displaying a result</h3>
226 <p>OnRead calls <code>ReportResultAndDie</code> when either an error or <code>PP _OK</code> is
227 returned to indicate streaming of file is complete. <code>ReportResultAndDie</co de> then
228 calls <code>ReportResult,</code> which calls <code>PostMessage</code> to send th e result back to
229 the HTML page.</p>
230 </section></section></section>
231
232 {{/partials.standard_nacl_article}}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698