OLD | NEW |
1 {{+bindTo:partials.standard_nacl_article}} | 1 {{+bindTo:partials.standard_nacl_article}} |
2 | 2 |
3 <section id="url-loading"> | 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> | 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" 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="id1">Introduction</a>
</li> | 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> | 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
> | 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> | 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"> | 11 <ul class="small-gap"> |
12 <li><a class="reference internal" href="#url-loading-overview" id="id5">URL load
ing overview</a></li> | 12 <li><a class="reference internal" href="#url-loading-overview" id="id5">URL load
ing overview</a></li> |
13 </ul> | 13 </ul> |
14 </li> | 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> | 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"> | 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> | 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> | 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> | 19 <li><a class="reference internal" href="#displaying-a-result" id="id9">Displayin
g a result</a></li> |
20 </ul> | 20 </ul> |
21 </li> | 21 </li> |
22 </ul> | 22 </ul> |
23 | 23 |
24 </div><section id="introduction"> | 24 </div><h2 id="introduction">Introduction</h2> |
25 <h2 id="introduction">Introduction</h2> | |
26 <p>This chapter describes how to use the <a class="reference external" href="/na
tive-client/pepper_stable/cpp/classpp_1_1_u_r_l_loader">URLLoader API</a> to loa
d resources | 25 <p>This chapter describes how to use the <a class="reference external" href="/na
tive-client/pepper_stable/cpp/classpp_1_1_u_r_l_loader">URLLoader API</a> to loa
d resources |
27 such as images and sound files from a server into your application.</p> | 26 such as images and sound files from a server into your application.</p> |
28 <p>The example discussed in this chapter is included in the SDK in the directory | 27 <p>The example discussed in this chapter is included in the SDK in the directory |
29 <code>examples/api/url_loader</code>.</p> | 28 <code>examples/api/url_loader</code>.</p> |
30 </section><section id="reference-information"> | |
31 <h2 id="reference-information">Reference information</h2> | 29 <h2 id="reference-information">Reference information</h2> |
32 <p>For reference information related to loading data from URLs, see the | 30 <p>For reference information related to loading data from URLs, see the |
33 following documentation:</p> | 31 following documentation:</p> |
34 <ul class="small-gap"> | 32 <ul class="small-gap"> |
35 <li><a class="reference external" href="/native-client/pepper_stable/cpp/url__lo
ader_8h">url_loader.h</a> - Contains | 33 <li><a class="reference external" href="/native-client/pepper_stable/cpp/url__lo
ader_8h">url_loader.h</a> - Contains |
36 <code>URLLoader</code> class for loading data from URLs</li> | 34 <code>URLLoader</code> class for loading data from URLs</li> |
37 <li><a class="reference external" href="/native-client/pepper_stable/cpp/url__re
quest__info_8h">url_request_info.h</a> - Contains | 35 <li><a class="reference external" href="/native-client/pepper_stable/cpp/url__re
quest__info_8h">url_request_info.h</a> - Contains |
38 <code>URLRequest</code> class for creating and manipulating URL requests</li> | 36 <code>URLRequest</code> class for creating and manipulating URL requests</li> |
39 <li><a class="reference external" href="/native-client/pepper_stable/cpp/url__re
sponse__info_8h">url_response_info.h</a> - Contains | 37 <li><a class="reference external" href="/native-client/pepper_stable/cpp/url__re
sponse__info_8h">url_response_info.h</a> - Contains |
40 <code>URLResponse</code> class for examaning URL responses</li> | 38 <code>URLResponse</code> class for examaning URL responses</li> |
41 </ul> | 39 </ul> |
42 </section><section id="background"> | |
43 <h2 id="background">Background</h2> | 40 <h2 id="background">Background</h2> |
44 <p>When a user launches your Native Client web application, Chrome downloads and | 41 <p>When a user launches your Native Client web application, Chrome downloads and |
45 caches your application’s HTML file, manifest file (.nmf), and Native Clie
nt | 42 caches your application’s HTML file, manifest file (.nmf), and Native Clie
nt |
46 module (.pexe or .nexe). If your application needs additional assets, such as | 43 module (.pexe or .nexe). If your application needs additional assets, such as |
47 images and sound files, it must explicitly load those assets. You can use the | 44 images and sound files, it must explicitly load those assets. You can use the |
48 Pepper APIs described in this chapter to load assets from a URL into your | 45 Pepper APIs described in this chapter to load assets from a URL into your |
49 application.</p> | 46 application.</p> |
50 <p>After you’ve loaded assets into your application, Chrome will cache tho
se | 47 <p>After you’ve loaded assets into your application, Chrome will cache tho
se |
51 assets. To avoid being at the whim of the Chrome cache, however, you may want | 48 assets. To avoid being at the whim of the Chrome cache, however, you may want |
52 to use the <a class="reference external" href="/native-client/pepper_stable/cpp/
classpp_1_1_file_i_o">Pepper FileIO API</a> to write those assets | 49 to use the <a class="reference external" href="/native-client/pepper_stable/cpp/
classpp_1_1_file_i_o">Pepper FileIO API</a> to write those assets |
53 to a persistent, sandboxed location on the user’s file system.</p> | 50 to a persistent, sandboxed location on the user’s file system.</p> |
54 </section><section id="the-url-loader-example"> | |
55 <h2 id="the-url-loader-example">The <code>url_loader</code> example</h2> | 51 <h2 id="the-url-loader-example">The <code>url_loader</code> example</h2> |
56 <p>The SDK includes an example called <code>url_loader</code> demonstrating down
loading | 52 <p>The SDK includes an example called <code>url_loader</code> demonstrating down
loading |
57 files from a server. This example has these primary files:</p> | 53 files from a server. This example has these primary files:</p> |
58 <ul class="small-gap"> | 54 <ul class="small-gap"> |
59 <li><code>index.html</code> - The HTML code that launches the Native Client modu
le.</li> | 55 <li><code>index.html</code> - The HTML code that launches the Native Client modu
le.</li> |
60 <li><code>example.js</code> - The JavaScript file for index.html. It has code th
at sends | 56 <li><code>example.js</code> - The JavaScript file for index.html. It has code th
at sends |
61 a PostMessage request to the Native Client module when the “Get URL”
button | 57 a PostMessage request to the Native Client module when the “Get URL”
button |
62 is clicked.</li> | 58 is clicked.</li> |
63 <li><code>url_loader_success.html</code> - An HTML file on the server whose cont
ents are | 59 <li><code>url_loader_success.html</code> - An HTML file on the server whose cont
ents are |
64 being retrieved using the <code>URLLoader</code> API.</li> | 60 being retrieved using the <code>URLLoader</code> API.</li> |
65 <li><code>url_loader.cc</code> - The code that sets up and provides and entry po
int into | 61 <li><code>url_loader.cc</code> - The code that sets up and provides and entry po
int into |
66 the Native client module.</li> | 62 the Native client module.</li> |
67 <li><code>url_loader_handler.cc</code> - The code that retrieves the contents of
the | 63 <li><code>url_loader_handler.cc</code> - The code that retrieves the contents of
the |
68 url_loader_success.html file and returns the results (this is where the | 64 url_loader_success.html file and returns the results (this is where the |
69 bulk of the work is done).</li> | 65 bulk of the work is done).</li> |
70 </ul> | 66 </ul> |
71 <p>The remainder of this document covers the code in the <code>url_loader.cc</co
de> and | 67 <p>The remainder of this document covers the code in the <code>url_loader.cc</co
de> and |
72 <code>url_loader_handler.cc</code> files.</p> | 68 <code>url_loader_handler.cc</code> files.</p> |
73 <section id="url-loading-overview"> | |
74 <h3 id="url-loading-overview">URL loading overview</h3> | 69 <h3 id="url-loading-overview">URL loading overview</h3> |
75 <p>Like many Pepper APIs, the <code>URLLoader</code> API includes a set of metho
ds that | 70 <p>Like many Pepper APIs, the <code>URLLoader</code> API includes a set of metho
ds that |
76 execute asynchronously and that invoke callback functions in your Native Client | 71 execute asynchronously and that invoke callback functions in your Native Client |
77 module. The high-level flow for the <code>url_loader</code> example is described
below. | 72 module. The high-level flow for the <code>url_loader</code> example is described
below. |
78 Note that methods in the namespace <code>pp::URLLoader</code> are part of the Pe
pper | 73 Note that methods in the namespace <code>pp::URLLoader</code> are part of the Pe
pper |
79 <code>URLLoader</code> API, while the rest of the functions are part of the code
in the | 74 <code>URLLoader</code> API, while the rest of the functions are part of the code
in the |
80 Native Client module (specifically in the file <code>url_loader_handler.cc</code
>). The | 75 Native Client module (specifically in the file <code>url_loader_handler.cc</code
>). The |
81 following image shows the flow of the <code>url_loader_handler</code> code:</p> | 76 following image shows the flow of the <code>url_loader_handler</code> code:</p> |
82 <img alt="/native-client/images/pepper-urlloader-api.png" src="/native-client/im
ages/pepper-urlloader-api.png" /> | 77 <img alt="/native-client/images/pepper-urlloader-api.png" src="/native-client/im
ages/pepper-urlloader-api.png" /> |
83 <p>Following are the high-level steps involved in URL loading.</p> | 78 <p>Following are the high-level steps involved in URL loading.</p> |
84 <ol class="arabic simple"> | 79 <ol class="arabic simple"> |
85 <li>The Native Client module calls <code>pp::URLLoader::Open</code> to begin ope
ning the | 80 <li>The Native Client module calls <code>pp::URLLoader::Open</code> to begin ope
ning the |
86 URL.</li> | 81 URL.</li> |
87 <li>When <code>Open</code> completes, it invokes a callback function in the Nati
ve Client | 82 <li>When <code>Open</code> completes, it invokes a callback function in the Nati
ve Client |
88 module (in this case, <code>OnOpen</code>).</li> | 83 module (in this case, <code>OnOpen</code>).</li> |
89 <li>The Native Client module calls the Pepper function | 84 <li>The Native Client module calls the Pepper function |
90 <code>URLLoader::ReadResponseBody</code> to begin reading the response body with
the | 85 <code>URLLoader::ReadResponseBody</code> to begin reading the response body with
the |
91 data. <code>ReadResponseBody</code> is passed an optional callback function in t
he | 86 data. <code>ReadResponseBody</code> is passed an optional callback function in t
he |
92 Native Client module (in this case, On <code>Read</code>). The callback function
is | 87 Native Client module (in this case, On <code>Read</code>). The callback function
is |
93 an optional callback because <code>ReadResponseBody</code> may read data and ret
urn | 88 an optional callback because <code>ReadResponseBody</code> may read data and ret
urn |
94 synchronously if data is available (this improves performance for large | 89 synchronously if data is available (this improves performance for large |
95 files and fast connections).</li> | 90 files and fast connections).</li> |
96 </ol> | 91 </ol> |
97 <p>The remainder of this document demonstrates how the previous steps are | 92 <p>The remainder of this document demonstrates how the previous steps are |
98 implemented in the <code>url_loader</code> example.</p> | 93 implemented in the <code>url_loader</code> example.</p> |
99 </section></section><section id="url-loader-deep-dive"> | |
100 <h2 id="url-loader-deep-dive"><code>url_loader</code> deep dive</h2> | 94 <h2 id="url-loader-deep-dive"><code>url_loader</code> deep dive</h2> |
101 <section id="setting-up-the-request"> | |
102 <h3 id="setting-up-the-request">Setting up the request</h3> | 95 <h3 id="setting-up-the-request">Setting up the request</h3> |
103 <p><code>HandleMessage</code> in <code>url_loader.cc</code> creates a <code>URLL
oaderHandler</code> instance | 96 <p><code>HandleMessage</code> in <code>url_loader.cc</code> creates a <code>URLL
oaderHandler</code> instance |
104 and passes it the URL of the asset to be retrieved. Then <code>HandleMessage</co
de> | 97 and passes it the URL of the asset to be retrieved. Then <code>HandleMessage</co
de> |
105 calls <code>Start</code> to start retrieving the asset from the server:</p> | 98 calls <code>Start</code> to start retrieving the asset from the server:</p> |
106 <pre class="prettyprint"> | 99 <pre class="prettyprint"> |
107 void URLLoaderInstance::HandleMessage(const pp::Var& var_message) { | 100 void URLLoaderInstance::HandleMessage(const pp::Var& var_message) { |
108 if (!var_message.is_string()) { | 101 if (!var_message.is_string()) { |
109 return; | 102 return; |
110 } | 103 } |
111 std::string message = var_message.AsString(); | 104 std::string message = var_message.AsString(); |
(...skipping 27 matching lines...) Expand all Loading... |
139 url_(url), | 132 url_(url), |
140 url_request_(instance), | 133 url_request_(instance), |
141 url_loader_(instance), | 134 url_loader_(instance), |
142 buffer_(new char[READ_BUFFER_SIZE]), | 135 buffer_(new char[READ_BUFFER_SIZE]), |
143 cc_factory_(this) { | 136 cc_factory_(this) { |
144 url_request_.SetURL(url); | 137 url_request_.SetURL(url); |
145 url_request_.SetMethod("GET"); | 138 url_request_.SetMethod("GET"); |
146 url_request_.SetRecordDownloadProgress(true); | 139 url_request_.SetRecordDownloadProgress(true); |
147 } | 140 } |
148 </pre> | 141 </pre> |
149 </section><section id="downloading-the-data"> | |
150 <h3 id="downloading-the-data">Downloading the data</h3> | 142 <h3 id="downloading-the-data">Downloading the data</h3> |
151 <p><code>Start</code> in <code>url_loader_handler.cc</code> creates a callback (
<code>cc</code>) using a | 143 <p><code>Start</code> in <code>url_loader_handler.cc</code> creates a callback (
<code>cc</code>) using a |
152 <code>CompletionCallbackFactory</code>. The callback is passed to <code>Open</co
de> to be called | 144 <code>CompletionCallbackFactory</code>. The callback is passed to <code>Open</co
de> to be called |
153 upon its completion. <code>Open</code> begins loading the <code>URLRequestInfo</
code>.</p> | 145 upon its completion. <code>Open</code> begins loading the <code>URLRequestInfo</
code>.</p> |
154 <pre class="prettyprint"> | 146 <pre class="prettyprint"> |
155 void URLLoaderHandler::Start() { | 147 void URLLoaderHandler::Start() { |
156 pp::CompletionCallback cc = | 148 pp::CompletionCallback cc = |
157 cc_factory_.NewCallback(&URLLoaderHandler::OnOpen); | 149 cc_factory_.NewCallback(&URLLoaderHandler::OnOpen); |
158 url_loader_.Open(url_request_, cc); | 150 url_loader_.Open(url_request_, cc); |
159 } | 151 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 num_bytes = std::min(READ_BUFFER_SIZE, num_bytes); | 204 num_bytes = std::min(READ_BUFFER_SIZE, num_bytes); |
213 url_response_body_.insert( | 205 url_response_body_.insert( |
214 url_response_body_.end(), buffer, buffer + num_bytes); | 206 url_response_body_.end(), buffer, buffer + num_bytes); |
215 } | 207 } |
216 </pre> | 208 </pre> |
217 <p>Eventually either all the bytes have been read for the entire file (resulting | 209 <p>Eventually either all the bytes have been read for the entire file (resulting |
218 in <code>PP_OK</code> or 0), all the bytes have been read for what has been | 210 in <code>PP_OK</code> or 0), all the bytes have been read for what has been |
219 downloaded, but more is to be downloaded (<code>PP_OK_COMPLETIONPENDING</code> o
r -1), | 211 downloaded, but more is to be downloaded (<code>PP_OK_COMPLETIONPENDING</code> o
r -1), |
220 or there is an error (less than -1). <code>OnRead</code> is called in the event
of an | 212 or there is an error (less than -1). <code>OnRead</code> is called in the event
of an |
221 error or <code>PP_OK</code>.</p> | 213 error or <code>PP_OK</code>.</p> |
222 </section><section id="displaying-a-result"> | |
223 <h3 id="displaying-a-result">Displaying a result</h3> | 214 <h3 id="displaying-a-result">Displaying a result</h3> |
224 <p>OnRead calls <code>ReportResultAndDie</code> when either an error or <code>PP
_OK</code> is | 215 <p>OnRead calls <code>ReportResultAndDie</code> when either an error or <code>PP
_OK</code> is |
225 returned to indicate streaming of file is complete. <code>ReportResultAndDie</co
de> then | 216 returned to indicate streaming of file is complete. <code>ReportResultAndDie</co
de> then |
226 calls <code>ReportResult,</code> which calls <code>PostMessage</code> to send th
e result back to | 217 calls <code>ReportResult,</code> which calls <code>PostMessage</code> to send th
e result back to |
227 the HTML page.</p> | 218 the HTML page.</p> |
228 </section></section></section> | 219 </section> |
229 | 220 |
230 {{/partials.standard_nacl_article}} | 221 {{/partials.standard_nacl_article}} |
OLD | NEW |