OLD | NEW |
1 {{+bindTo:partials.standard_nacl_article}} | 1 {{+bindTo:partials.standard_nacl_article}} |
2 | 2 |
3 <section id="c-tutorial-getting-started-part-2"> | 3 <section id="c-tutorial-getting-started-part-2"> |
4 <span id="tutorial2"></span><h1 id="c-tutorial-getting-started-part-2"><span id=
"tutorial2"></span>C++ Tutorial: Getting Started (Part 2)</h1> | 4 <span id="tutorial2"></span><h1 id="c-tutorial-getting-started-part-2"><span id=
"tutorial2"></span>C++ Tutorial: Getting Started (Part 2)</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="#overview" id="id1">Overview</a></li> | 7 <li><a class="reference internal" href="#overview" id="id1">Overview</a></li> |
8 <li><p class="first"><a class="reference internal" href="#using-the-native-clien
t-sdk-build-system" id="id2">Using the Native Client SDK build system</a></p> | 8 <li><p class="first"><a class="reference internal" href="#using-the-native-clien
t-sdk-build-system" id="id2">Using the Native Client SDK build system</a></p> |
9 <ul class="small-gap"> | 9 <ul class="small-gap"> |
10 <li><a class="reference internal" href="#simplifying-the-makefile" id="id3">Simp
lifying the Makefile</a></li> | 10 <li><a class="reference internal" href="#simplifying-the-makefile" id="id3">Simp
lifying the Makefile</a></li> |
(...skipping 10 matching lines...) Expand all Loading... |
21 </ul> | 21 </ul> |
22 </li> | 22 </li> |
23 <li><p class="first"><a class="reference internal" href="#sharing-common-code-wi
th-common-js" id="id11">Sharing common code with common.js</a></p> | 23 <li><p class="first"><a class="reference internal" href="#sharing-common-code-wi
th-common-js" id="id11">Sharing common code with common.js</a></p> |
24 <ul class="small-gap"> | 24 <ul class="small-gap"> |
25 <li><a class="reference internal" href="#loading-the-page-and-creating-the-modul
e" id="id12">Loading the page and creating the module</a></li> | 25 <li><a class="reference internal" href="#loading-the-page-and-creating-the-modul
e" id="id12">Loading the page and creating the module</a></li> |
26 </ul> | 26 </ul> |
27 </li> | 27 </li> |
28 <li><a class="reference internal" href="#example-specific-behavior-with-example-
js" id="id13">Example-specific behavior with example.js</a></li> | 28 <li><a class="reference internal" href="#example-specific-behavior-with-example-
js" id="id13">Example-specific behavior with example.js</a></li> |
29 </ul> | 29 </ul> |
30 | 30 |
31 </div><section id="overview"> | 31 </div><h2 id="overview">Overview</h2> |
32 <h2 id="overview">Overview</h2> | |
33 <p>This tutorial shows how to convert the finished PNaCl web application from | 32 <p>This tutorial shows how to convert the finished PNaCl web application from |
34 <a class="reference internal" href="/native-client/devguide/tutorial/tutorial-pa
rt1.html"><em>Part 1</em></a> to use the Native Client SDK build system and | 33 <a class="reference internal" href="/native-client/devguide/tutorial/tutorial-pa
rt1.html"><em>Part 1</em></a> to use the Native Client SDK build system and |
35 common JavaScript files. It also demonstrates some techniques to make your web | 34 common JavaScript files. It also demonstrates some techniques to make your web |
36 application <a class="reference external" href="/apps/contentSecurityPolicy">Con
tent Security Policy (CSP)-compliant</a>, which is necessary for <a class="refer
ence external" href="/apps/about_apps">Chrome Apps</a>.</p> | 35 application <a class="reference external" href="/apps/contentSecurityPolicy">Con
tent Security Policy (CSP)-compliant</a>, which is necessary for <a class="refer
ence external" href="/apps/about_apps">Chrome Apps</a>.</p> |
37 <p>Using the Native Client SDK build system makes it easy to build with all of t
he | 36 <p>Using the Native Client SDK build system makes it easy to build with all of t
he |
38 SDK toolchains, and switch between the Debug and Release configurations. It | 37 SDK toolchains, and switch between the Debug and Release configurations. It |
39 also simplifies the makefiles for your project, as we’ll see in the next | 38 also simplifies the makefiles for your project, as we’ll see in the next |
40 section. Finally, it adds some useful commands for <a class="reference internal"
href="/native-client/sdk/examples.html#id1"><em>running</em></a> and <a class="
reference internal" href="/native-client/sdk/examples.html#debugging-the-sdk-exa
mples"><em>debugging</em></a> | 39 section. Finally, it adds some useful commands for <a class="reference internal"
href="/native-client/sdk/examples.html#id1"><em>running</em></a> and <a class="
reference internal" href="/native-client/sdk/examples.html#debugging-the-sdk-exa
mples"><em>debugging</em></a> |
41 your application.</p> | 40 your application.</p> |
42 <p>The finished code for this example can be found in the | 41 <p>The finished code for this example can be found in the |
43 <code>pepper_$(VERSION)/getting_started/part2</code> directory in the Native Cli
ent SDK | 42 <code>pepper_$(VERSION)/getting_started/part2</code> directory in the Native Cli
ent SDK |
44 download.</p> | 43 download.</p> |
45 </section><section id="using-the-native-client-sdk-build-system"> | |
46 <h2 id="using-the-native-client-sdk-build-system">Using the Native Client SDK bu
ild system</h2> | 44 <h2 id="using-the-native-client-sdk-build-system">Using the Native Client SDK bu
ild system</h2> |
47 <p>This section describes how to use the SDK build system. To do so, we’ll
make | 45 <p>This section describes how to use the SDK build system. To do so, we’ll
make |
48 changes in the makefile. Because the makefile in part1 and part2 are so | 46 changes in the makefile. Because the makefile in part1 and part2 are so |
49 different, it is easier to start from scratch. Here is the contents of the new | 47 different, it is easier to start from scratch. Here is the contents of the new |
50 makefile. The following sections will describe it in more detail.</p> | 48 makefile. The following sections will describe it in more detail.</p> |
51 <section id="simplifying-the-makefile"> | |
52 <h3 id="simplifying-the-makefile">Simplifying the Makefile</h3> | 49 <h3 id="simplifying-the-makefile">Simplifying the Makefile</h3> |
53 <p>The makefile from part1 only supports one toolchain (PNaCl) and one | 50 <p>The makefile from part1 only supports one toolchain (PNaCl) and one |
54 configuration (Release). It also only supports one source file. It’s relat
ively | 51 configuration (Release). It also only supports one source file. It’s relat
ively |
55 simple, but if we want to add support for multiple toolchains, configurations, | 52 simple, but if we want to add support for multiple toolchains, configurations, |
56 source files, or build steps, it would grow increasingly complex. The SDK build | 53 source files, or build steps, it would grow increasingly complex. The SDK build |
57 system uses a set of variables and macros to make this possible, without | 54 system uses a set of variables and macros to make this possible, without |
58 significantly increasing the complexity of the makefile.</p> | 55 significantly increasing the complexity of the makefile.</p> |
59 <p>Here is the new makefile, supporting three toolchains (PNaCl, Newlib NaCl, | 56 <p>Here is the new makefile, supporting three toolchains (PNaCl, Newlib NaCl, |
60 Glibc NaCl) and two configurations (Debug, Release).</p> | 57 Glibc NaCl) and two configurations (Debug, Release).</p> |
61 <pre class="prettyprint"> | 58 <pre class="prettyprint"> |
(...skipping 16 matching lines...) Expand all Loading... |
78 # On NaCl, only produce a stripped binary for Release configs (not Debug). | 75 # On NaCl, only produce a stripped binary for Release configs (not Debug). |
79 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) | 76 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) |
80 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) | 77 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) |
81 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) | 78 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) |
82 else | 79 else |
83 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) | 80 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) |
84 endif | 81 endif |
85 | 82 |
86 $(eval $(call NMF_RULE,$(TARGET),)) | 83 $(eval $(call NMF_RULE,$(TARGET),)) |
87 </pre> | 84 </pre> |
88 </section><section id="choosing-valid-toolchains-and-including-common-mk"> | |
89 <h3 id="choosing-valid-toolchains-and-including-common-mk">Choosing valid toolch
ains, and including common.mk</h3> | 85 <h3 id="choosing-valid-toolchains-and-including-common-mk">Choosing valid toolch
ains, and including common.mk</h3> |
90 <p>The makefile begins by specifying the toolchains that are valid for this | 86 <p>The makefile begins by specifying the toolchains that are valid for this |
91 project. The Native Client SDK build system supports multi-toolchain projects | 87 project. The Native Client SDK build system supports multi-toolchain projects |
92 for its examples and libraries, but generally you will choose one toolchain | 88 for its examples and libraries, but generally you will choose one toolchain |
93 when you begin your project and never change it. Please see the | 89 when you begin your project and never change it. Please see the |
94 <a class="reference internal" href="/native-client/overview.html#toolchains"><em
>Toolchains section of the Native Client overview</em></a> for more | 90 <a class="reference internal" href="/native-client/overview.html#toolchains"><em
>Toolchains section of the Native Client overview</em></a> for more |
95 information.</p> | 91 information.</p> |
96 <p>For this example, we support the <code>pnacl</code>, <code>newlib</code> and
<code>glibc</code> toolchains.</p> | 92 <p>For this example, we support the <code>pnacl</code>, <code>newlib</code> and
<code>glibc</code> toolchains.</p> |
97 <pre class="prettyprint"> | 93 <pre class="prettyprint"> |
98 VALID_TOOLCHAINS := pnacl newlib glibc | 94 VALID_TOOLCHAINS := pnacl newlib glibc |
(...skipping 10 matching lines...) Expand all Loading... |
109 here. You can also override this default by setting the <code>NACL_SDK_ROOT</cod
e> | 105 here. You can also override this default by setting the <code>NACL_SDK_ROOT</cod
e> |
110 environment variable. See <a class="reference internal" href="/native-client/dev
guide/tutorial/tutorial-part1.html#tutorial-step-5"><em>Step 5 of Part 1 of this
tutorial</em></a> for more details.</div></blockquote> | 106 environment variable. See <a class="reference internal" href="/native-client/dev
guide/tutorial/tutorial-part1.html#tutorial-step-5"><em>Step 5 of Part 1 of this
tutorial</em></a> for more details.</div></blockquote> |
111 | 107 |
112 </aside> | 108 </aside> |
113 <p>Next, we include the file <code>tools/common.mk</code>. This file provides th
e | 109 <p>Next, we include the file <code>tools/common.mk</code>. This file provides th
e |
114 functionality for the Native Client SDK build system, including new build rules | 110 functionality for the Native Client SDK build system, including new build rules |
115 to compile and link a project, which we’ll use below.</p> | 111 to compile and link a project, which we’ll use below.</p> |
116 <pre class="prettyprint"> | 112 <pre class="prettyprint"> |
117 include $(NACL_SDK_ROOT)/tools/common.mk | 113 include $(NACL_SDK_ROOT)/tools/common.mk |
118 </pre> | 114 </pre> |
119 </section><section id="configuring-your-project"> | |
120 <h3 id="configuring-your-project">Configuring your project</h3> | 115 <h3 id="configuring-your-project">Configuring your project</h3> |
121 <p>After including <code>tools/common.mk</code>, we configure the project by spe
cifying its | 116 <p>After including <code>tools/common.mk</code>, we configure the project by spe
cifying its |
122 name, the sources and libraries it uses:</p> | 117 name, the sources and libraries it uses:</p> |
123 <pre class="prettyprint"> | 118 <pre class="prettyprint"> |
124 TARGET = part2 | 119 TARGET = part2 |
125 LIBS = ppapi_cpp ppapi | 120 LIBS = ppapi_cpp ppapi |
126 | 121 |
127 CFLAGS = -Wall | 122 CFLAGS = -Wall |
128 SOURCES = hello_tutorial.cc | 123 SOURCES = hello_tutorial.cc |
129 </pre> | 124 </pre> |
(...skipping 24 matching lines...) Expand all Loading... |
154 <dd>A list of C or C++ sources to compile, separated by spaces. If you have a | 149 <dd>A list of C or C++ sources to compile, separated by spaces. If you have a |
155 long list of sources, it may be easier to read if you put each file on its | 150 long list of sources, it may be easier to read if you put each file on its |
156 own line, and use <code>\</code> as a line-continuation character. Here’s
an example:</dd> | 151 own line, and use <code>\</code> as a line-continuation character. Here’s
an example:</dd> |
157 </dl> | 152 </dl> |
158 <pre class="prettyprint"> | 153 <pre class="prettyprint"> |
159 SOURCES = foo.cc \ | 154 SOURCES = foo.cc \ |
160 bar.cc \ | 155 bar.cc \ |
161 baz.cc \ | 156 baz.cc \ |
162 quux.cc | 157 quux.cc |
163 </pre> | 158 </pre> |
164 </section><section id="build-macros"> | |
165 <h3 id="build-macros">Build macros</h3> | 159 <h3 id="build-macros">Build macros</h3> |
166 <p>For many projects, the following build macros do not need to be changed; they | 160 <p>For many projects, the following build macros do not need to be changed; they |
167 will use the variables we’ve defined above.</p> | 161 will use the variables we’ve defined above.</p> |
168 <pre class="prettyprint"> | 162 <pre class="prettyprint"> |
169 $(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) | 163 $(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS)))) |
170 | 164 |
171 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) | 165 ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG)))) |
172 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) | 166 $(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS))) |
173 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) | 167 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) |
174 else | 168 else |
(...skipping 23 matching lines...) Expand all Loading... |
198 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) | 192 $(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped)) |
199 else | 193 else |
200 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) | 194 $(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS))) |
201 endif | 195 endif |
202 </pre> | 196 </pre> |
203 <p>Finally, the NMF rule generates a NaCl manifest file (<code>.nmf</code>) that
references | 197 <p>Finally, the NMF rule generates a NaCl manifest file (<code>.nmf</code>) that
references |
204 each executable generated in the previous step:</p> | 198 each executable generated in the previous step:</p> |
205 <pre class="prettyprint"> | 199 <pre class="prettyprint"> |
206 $(eval $(call NMF_RULE,$(TARGET),)) | 200 $(eval $(call NMF_RULE,$(TARGET),)) |
207 </pre> | 201 </pre> |
208 </section></section><section id="making-index-html-work-for-chrome-apps"> | |
209 <h2 id="making-index-html-work-for-chrome-apps">Making index.html work for Chrom
e Apps</h2> | 202 <h2 id="making-index-html-work-for-chrome-apps">Making index.html work for Chrom
e Apps</h2> |
210 <p>This section describes the changes necessary to make the HTML and JavaScript
in | 203 <p>This section describes the changes necessary to make the HTML and JavaScript
in |
211 part1 CSP-compliant. This is required if you want to build a <a class="reference
external" href="/apps/about_apps">Chrome App</a>, but is not necessary if you w
ant to use PNaCl on the open | 204 part1 CSP-compliant. This is required if you want to build a <a class="reference
external" href="/apps/about_apps">Chrome App</a>, but is not necessary if you w
ant to use PNaCl on the open |
212 web.</p> | 205 web.</p> |
213 <section id="csp-rules"> | |
214 <h3 id="csp-rules">CSP rules</h3> | 206 <h3 id="csp-rules">CSP rules</h3> |
215 <p><a class="reference external" href="/apps/contentSecurityPolicy#what">Chrome
Apps CSP</a> restricts you from doing | 207 <p><a class="reference external" href="/apps/contentSecurityPolicy#what">Chrome
Apps CSP</a> restricts you from doing |
216 the following:</p> | 208 the following:</p> |
217 <ul class="small-gap"> | 209 <ul class="small-gap"> |
218 <li>You can’t use inline scripting in your Chrome App pages. The restriction | 210 <li>You can’t use inline scripting in your Chrome App pages. The restriction |
219 bans both <code><script></code> blocks and event handlers (<code><butto
n onclick="..."></code>).</li> | 211 bans both <code><script></code> blocks and event handlers (<code><butto
n onclick="..."></code>).</li> |
220 <li>You can’t reference any external resources in any of your app files (except | 212 <li>You can’t reference any external resources in any of your app files (except |
221 for video and audio resources). You can’t embed external resources in an | 213 for video and audio resources). You can’t embed external resources in an |
222 iframe.</li> | 214 iframe.</li> |
223 <li>You can’t use string-to-JavaScript methods like <code>eval()</code> and <cod
e>new | 215 <li>You can’t use string-to-JavaScript methods like <code>eval()</code> and <cod
e>new |
224 Function()</code>.</li> | 216 Function()</code>.</li> |
225 </ul> | 217 </ul> |
226 </section><section id="making-index-html-csp-compliant"> | |
227 <h3 id="making-index-html-csp-compliant">Making index.html CSP-compliant</h3> | 218 <h3 id="making-index-html-csp-compliant">Making index.html CSP-compliant</h3> |
228 <p>To make our application CSP-compliant, we have to remove inline scripting. As | 219 <p>To make our application CSP-compliant, we have to remove inline scripting. As |
229 described above, we can’t use inline <code><script></code> blocks or
event handlers. This | 220 described above, we can’t use inline <code><script></code> blocks or
event handlers. This |
230 is easy to do—we’ll just reference some new files from our script ta
g, and | 221 is easy to do—we’ll just reference some new files from our script ta
g, and |
231 remove all of our inlined scripts:</p> | 222 remove all of our inlined scripts:</p> |
232 <pre class="prettyprint"> | 223 <pre class="prettyprint"> |
233 <head> | 224 <head> |
234 ... | 225 ... |
235 <script type="text/javascript" src="common.js"></
script> | 226 <script type="text/javascript" src="common.js"></
script> |
236 <script type="text/javascript" src="example.js"><
/script> | 227 <script type="text/javascript" src="example.js"><
/script> |
237 </head> | 228 </head> |
238 </pre> | 229 </pre> |
239 <p><code>common.js</code> has shared code used by all SDK examples, and is descr
ibed | 230 <p><code>common.js</code> has shared code used by all SDK examples, and is descr
ibed |
240 later in this document. <code>example.js</code> is a script that has code specif
ic to | 231 later in this document. <code>example.js</code> is a script that has code specif
ic to |
241 this example.</p> | 232 this example.</p> |
242 <p>We also need to remove the inline event handler on the body tag:</p> | 233 <p>We also need to remove the inline event handler on the body tag:</p> |
243 <pre class="prettyprint"> | 234 <pre class="prettyprint"> |
244 <body onload="pageDidLoad()"> | 235 <body onload="pageDidLoad()"> |
245 ... | 236 ... |
246 </pre> | 237 </pre> |
247 <p>This logic is now handled by <code>common.js</code>.</p> | 238 <p>This logic is now handled by <code>common.js</code>.</p> |
248 </section><section id="making-index-html-support-different-toolchains-and-config
urations"> | |
249 <h3 id="making-index-html-support-different-toolchains-and-configurations">Makin
g index.html support different toolchains and configurations</h3> | 239 <h3 id="making-index-html-support-different-toolchains-and-configurations">Makin
g index.html support different toolchains and configurations</h3> |
250 <p>Finally, there are a few changes to <code>index.html</code> that are not nece
ssary for | 240 <p>Finally, there are a few changes to <code>index.html</code> that are not nece
ssary for |
251 CSP-compliance, but help make the SDK examples more generic.</p> | 241 CSP-compliance, but help make the SDK examples more generic.</p> |
252 <p>First, we add some <a class="reference external" href="https://developer.mozi
lla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes">data attributes</a> | 242 <p>First, we add some <a class="reference external" href="https://developer.mozi
lla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes">data attributes</a> |
253 to the body element to specify the name, supported toolchains, supported | 243 to the body element to specify the name, supported toolchains, supported |
254 configurations, and path to the <code>.nmf</code> file:</p> | 244 configurations, and path to the <code>.nmf</code> file:</p> |
255 <pre class="prettyprint"> | 245 <pre class="prettyprint"> |
256 <body data-name="part2" | 246 <body data-name="part2" |
257 data-tools="newlib glibc pnacl" | 247 data-tools="newlib glibc pnacl" |
258 data-configs="Debug Release" | 248 data-configs="Debug Release" |
259 data-path="{tc}/{config}"> | 249 data-path="{tc}/{config}"> |
260 ... | 250 ... |
261 </pre> | 251 </pre> |
262 <p><code>common.js</code> will read these data attributes to allow you to load t
he same | 252 <p><code>common.js</code> will read these data attributes to allow you to load t
he same |
263 example with different toolchains by changing the URL’s <a class="referenc
e external" href="http://en.wikipedia.org/wiki/Query_string">query string</a>. F
or example, you can load the | 253 example with different toolchains by changing the URL’s <a class="referenc
e external" href="http://en.wikipedia.org/wiki/Query_string">query string</a>. F
or example, you can load the |
264 glibc Debug version of this example by navigating to | 254 glibc Debug version of this example by navigating to |
265 <code>index.html?tc=glibc&config=Debug</code>.</p> | 255 <code>index.html?tc=glibc&config=Debug</code>.</p> |
266 <p>Next, we remove the <code>embed</code> element that is described in HTML. Thi
s will be | 256 <p>Next, we remove the <code>embed</code> element that is described in HTML. Thi
s will be |
267 automatically added for us by <code>common.js</code>, based on the current | 257 automatically added for us by <code>common.js</code>, based on the current |
268 toolchain/configuration combination:</p> | 258 toolchain/configuration combination:</p> |
269 <pre class="prettyprint"> | 259 <pre class="prettyprint"> |
270 <!-- | 260 <!-- |
271 Just as in part1, the <embed> element will be wrapped inside the <div&g
t; | 261 Just as in part1, the <embed> element will be wrapped inside the <div&g
t; |
272 element with the id "listener". In part1, the embed was specified in H
TML, | 262 element with the id "listener". In part1, the embed was specified in H
TML, |
273 here the common.js module creates a new <embed> element and adds it to the | 263 here the common.js module creates a new <embed> element and adds it to the |
274 <div> for us. | 264 <div> for us. |
275 --> | 265 --> |
276 <div id="listener"></div> | 266 <div id="listener"></div> |
277 </pre> | 267 </pre> |
278 </section></section><section id="sharing-common-code-with-common-js"> | |
279 <h2 id="sharing-common-code-with-common-js">Sharing common code with common.js</
h2> | 268 <h2 id="sharing-common-code-with-common-js">Sharing common code with common.js</
h2> |
280 <p><code>common.js</code> contains JavaScript code that each example uses to cre
ate a | 269 <p><code>common.js</code> contains JavaScript code that each example uses to cre
ate a |
281 NaCl module, handle messages from that module and other common tasks like | 270 NaCl module, handle messages from that module and other common tasks like |
282 displaying the module load status and logging messages. Explaining all of | 271 displaying the module load status and logging messages. Explaining all of |
283 <code>common.js</code> is outside the scope of this document, but please look at
the | 272 <code>common.js</code> is outside the scope of this document, but please look at
the |
284 documentation in that file for more information.</p> | 273 documentation in that file for more information.</p> |
285 <section id="loading-the-page-and-creating-the-module"> | |
286 <h3 id="loading-the-page-and-creating-the-module">Loading the page and creating
the module</h3> | 274 <h3 id="loading-the-page-and-creating-the-module">Loading the page and creating
the module</h3> |
287 <p>Since we’ve added <code><script></code> tags for <code>common.js<
/code> and <code>example.js</code> to the | 275 <p>Since we’ve added <code><script></code> tags for <code>common.js<
/code> and <code>example.js</code> to the |
288 <code>head</code> element, they will be loaded and executed before the rest of t
he | 276 <code>head</code> element, they will be loaded and executed before the rest of t
he |
289 document has been parsed. As a result, we have to wait for the page to finish | 277 document has been parsed. As a result, we have to wait for the page to finish |
290 loading before we try to create the embed element and add it to the page.</p> | 278 loading before we try to create the embed element and add it to the page.</p> |
291 <p>We can do that by calling <code>addEventListener</code> and listening for the | 279 <p>We can do that by calling <code>addEventListener</code> and listening for the |
292 <code>DOMContentLoaded</code> event:</p> | 280 <code>DOMContentLoaded</code> event:</p> |
293 <pre class="prettyprint"> | 281 <pre class="prettyprint"> |
294 // Listen for the DOM content to be loaded. This event is fired when parsing of | 282 // Listen for the DOM content to be loaded. This event is fired when parsing of |
295 // the page's document has finished. | 283 // the page's document has finished. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 <pre class="prettyprint"> | 390 <pre class="prettyprint"> |
403 function moduleDidLoad() { | 391 function moduleDidLoad() { |
404 common.naclModule = document.getElementById('nacl_module'); | 392 common.naclModule = document.getElementById('nacl_module'); |
405 updateStatus('RUNNING'); | 393 updateStatus('RUNNING'); |
406 | 394 |
407 if (typeof window.moduleDidLoad !== 'undefined') { | 395 if (typeof window.moduleDidLoad !== 'undefined') { |
408 window.moduleDidLoad(); | 396 window.moduleDidLoad(); |
409 } | 397 } |
410 } | 398 } |
411 </pre> | 399 </pre> |
412 </section></section><section id="example-specific-behavior-with-example-js"> | |
413 <h2 id="example-specific-behavior-with-example-js">Example-specific behavior wit
h example.js</h2> | 400 <h2 id="example-specific-behavior-with-example-js">Example-specific behavior wit
h example.js</h2> |
414 <p>As described in the previous section, <code>common.js</code> will call certai
n functions | 401 <p>As described in the previous section, <code>common.js</code> will call certai
n functions |
415 during the module loading process. This example only needs to respond to two: | 402 during the module loading process. This example only needs to respond to two: |
416 <code>moduleDidLoad</code> and <code>handleMessage</code>.</p> | 403 <code>moduleDidLoad</code> and <code>handleMessage</code>.</p> |
417 <pre class="prettyprint"> | 404 <pre class="prettyprint"> |
418 // This function is called by common.js when the NaCl module is | 405 // This function is called by common.js when the NaCl module is |
419 // loaded. | 406 // loaded. |
420 function moduleDidLoad() { | 407 function moduleDidLoad() { |
421 // Once we load, hide the plugin. In this example, we don't display anything | 408 // Once we load, hide the plugin. In this example, we don't display anything |
422 // in the plugin, so it is fine to hide it. | 409 // in the plugin, so it is fine to hide it. |
423 common.hideModule(); | 410 common.hideModule(); |
424 | 411 |
425 // After the NaCl module has loaded, common.naclModule is a reference to the | 412 // After the NaCl module has loaded, common.naclModule is a reference to the |
426 // NaCl module's <embed> element. | 413 // NaCl module's <embed> element. |
427 // | 414 // |
428 // postMessage sends a message to it. | 415 // postMessage sends a message to it. |
429 common.naclModule.postMessage('hello'); | 416 common.naclModule.postMessage('hello'); |
430 } | 417 } |
431 | 418 |
432 // This function is called by common.js when a message is received from the | 419 // This function is called by common.js when a message is received from the |
433 // NaCl module. | 420 // NaCl module. |
434 function handleMessage(message) { | 421 function handleMessage(message) { |
435 var logEl = document.getElementById('log'); | 422 var logEl = document.getElementById('log'); |
436 logEl.textContent += message.data; | 423 logEl.textContent += message.data; |
437 } | 424 } |
438 </pre> | 425 </pre> |
439 </section></section> | 426 </section> |
440 | 427 |
441 {{/partials.standard_nacl_article}} | 428 {{/partials.standard_nacl_article}} |
OLD | NEW |