12 **Native Client** (NaCl) is an open-source technology for running native
13 compiled code in the browser, with the goal of maintaining the portability
14 and safety that users expect from web applications. Native Client expands web
15 programming beyond JavaScript, enabling you to enhance your web applications
16 using your preferred language. This document describes some of the key benefits
17 and common use cases of Native Client.
19 Google has implemented the open-source `Native Client project
20 <http://www.chromium.org/nativeclient>`_ in the Chrome browser on Windows, Mac,
21 Linux, and Chrome OS. The :doc:`Native Client Software Development Kit (SDK)
22 <sdk/download>`, itself an open-source project, lets you create web applications
23 that use NaCl and run in Chrome across multiple platforms.
25 A Native Client web application consists of JavaScript, HTML, CSS, and a NaCl
26 module written in a language supported by the SDK. The NaCl SDK currently
27 supports C and C++; as compilers for additional languages are developed, the SDK
30 .. figure:: /images/web-app-with-nacl.png
31 :alt: A web application with and without Native Client
33 A web application with and without Native Client
35 Native Client comes in two flavors: traditional (NaCl) and portable (PNaCl).
36 Traditional, which must be distributed through the Chrome Web Store lets you
37 target a specific hardware platform. Portable can run on the open web. A
38 bitcode file that can be loaded from any web server is downloaded to a client
39 machine and converted to hardware-specific code before any execution. For
40 details, see :doc:`NaCl and PNaCl </nacl-and-pnacl>`.
42 .. _why-use-native-client:
44 Why use Native Client?
45 ======================
47 Native Client open-source technology is designed to run compiled code
48 securely inside a browser at near-native speeds. Native Client gives web
49 applications some advantages of desktop software. Specifically, it provides the
50 means to fully harness the client's computational resources for applications
56 - client-side data analytics
57 - interactive simulations.
59 Native Client gives C and C++ (and other languages targeting it) the same level
60 of portability and safety as JavaScript.
62 .. _benefits-of-native-client:
64 Benefits of Native Client
65 =========================
67 Benefits of Native Client include:
69 * **Graphics, audio, and much more:** Running native code modules that render 2D
70 and 3D graphics, play audio, respond to mouse and keyboard events, run on
71 multiple threads, and access memory directly---all without requiring the user
73 * **Portability:** Writing your applications once and running them on multiple
74 operating systems (Windows, Linux, Mac, and Chrome OS) and CPU architectures
76 * **Easy migration path to the web:** Leveraging years of work in existing
77 desktop applications. Native Client makes the transition from the desktop to
78 a web application significantly easier because it supports C and C++.
79 * **Security:** Protecting the user's system from malicious or buggy
80 applications through Native Client's double sandbox model. This model offers
81 the safety of traditional web applications without sacrificing performance
82 and without requiring users to install a plug-in.
83 * **Performance:** Running at speeds within 5% to 15% of a native desktop
84 application. Native Client also allows applications to harness all available
85 CPU cores via a threading API. This enables demanding applications such as
86 console-quality games to run inside the browser.
93 Typical use cases for Native Client include the following:
95 * **Existing software components:** Native Client lets you repurpose existing
96 C and C++ software in web applications. You don't need to rewrite and debug
97 code that already works. It also lets your application take advantage of
98 things the browser does well such as handling user interaction and processing
99 events. You can also take advantage of the latest developments in HTML5.
100 * **Legacy desktop applications:** Native Client provides a smooth migration
101 path from desktop applications to the web. You can port and recompile existing
102 code for the computation engine of your application directly to Native Client,
103 and need rebuild only the user interface and event handling portions for the
105 * **Heavy computation in enterprise applications:** Native Client can handle the
106 number crunching required by large-scale enterprise applications. To ensure
107 protection of user data, Native Client lets you run complex cryptographic
108 algorithms directly in the browser so that unencrypted data never goes out
110 * **Multimedia applications:** Codecs for processing sounds, images, and movies
111 can be added to the browser in a Native Client module.
112 * **Games:** Native Client lets web applications run at close to native
113 speed, reuse existing multithreaded/multicore C/C++ code bases, and
114 access low-latency audio, networking APIs, and OpenGL ES with programmable
115 shaders. Native Client is a natural fit for running a physics engine or
116 artificial intelligence module that powers a sophisticated web game.
117 Native Client also enables applications to run unchanged across
119 * **Any application that requires acceleration:** Native Client fits seamlessly
120 into web applications. It's up to you to decide to what extent to use it.
121 Use of Native Client covers the full spectrum from complete applications to
122 small optimized routines that accelerate vital parts of web applications.
124 .. _link_how_nacl_works:
126 How Native Client works
127 =======================
129 Native Client is an umbrella name for a set of related software components for
130 developing C/C++ applications and running them securely on the web. At a high
131 level, Native Client consists of:
133 * **Toolchains:** collections of development tools (compilers, linkers, etc.)
134 that transform C/C++ code to Portable Native Client modules or Native Client
136 * **Runtime components:** components embedded in the browser or other host
137 platforms that allow execution of Native Client modules securely and
140 The following diagram shows how these components interact:
142 .. figure:: /images/nacl-pnacl-component-diagram.png
143 :alt: The Native Client toolchains and their outputs
145 The Native Client toolchains and their outputs
152 A Native Client toolchain consists of a compiler, a linker, an assembler and
153 other tools that are used to convert C/C++ source code into a module that is
154 loadable by a browser.
156 The Native Client SDK provides two toolchains:
158 * The left side of the diagram shows **Portable Native Client** (PNaCl,
159 pronounced "pinnacle"). An LLVM based toolchain produces a single, portable
160 (**pexe**) module. At runtime an ahead-of-time (AOT) translator, built into
161 the browser, translates the pexe into native code for the relevant client
164 * The right side of the diagram shows **(non-portable) Native Client**. A GCC
165 based toolchain produces multiple architecture-dependent (**nexe**) modules,
166 which are packaged into an application. At runtime the browser determines
167 which nexe to load based on the architecture of the client machine.
169 The PNaCl toolchain is recommended for most applications. The NaCl-GCC
170 toolchain should only be used for applications that won't be distributed on the
178 Since Native Client permits the execution of native code on client machines,
179 special security measures have to be implemented:
181 * The NaCl sandbox ensures that code accesses system resources only through
182 safe, whitelisted APIs, and operates within its limits without attempting to
183 interfere with other code running either within the browser or outside it.
184 * The NaCl validator statically analyzes code before running it to make sure it
185 only uses code and data patterns that are permitted and safe.
187 These security measures are in addition to the existing sandbox in the
188 Chrome browser. The Native Client module always executes in a process with
189 restricted permissions. The only interaction between this process and the
190 outside world is through defined browser interfaces. Because of the
191 combination of the NaCl sandbox and the Chrome sandbox, we say that
192 Native Client employs a **double sandbox** design.
196 .. _link_for_pnacl_translator:
201 Portable Native Client (PNaCl, prounounced "pinnacle") employs state-of-the-art
202 compiler technology to compile C/C++ source code to a portable bitcode
203 executable (**pexe**). PNaCl bitcode is an OS- and architecture-independent
204 format that can be freely distributed on the web and :ref:`embedded in web
205 applications<link_nacl_in_web_apps>`.
207 The PNaCl translator is a component embedded in the Chrome browser; its task is
208 to run pexe modules. Internally, the translator compiles a pexe to a nexe
209 (described above), and then executes the nexe within the Native Client sandbox
210 as described above. The translator uses intelligent caching to avoid
211 re-compiling the pexe if it was previously compiled on the client's browser.
213 Native Client also supports the execution of nexe modules directly in the
214 browser. However, since nexes contain architecture-specific machine code, they
215 are not allowed to be distributed on the open web. They can only be used as part
216 of applications and extensions that are installed from the Chrome Web Store.
218 For more details on the difference between NaCl and PNaCl, see
219 :doc:`NaCl and PNaCl <nacl-and-pnacl>`.
221 .. _link_nacl_in_web_apps:
223 Structure of a web application
224 ==============================
226 .. _application_files:
228 A Native Client application consists of a set of files:
230 * **HTML and CSS:** The HTML file tells the browser where to find the manifest
231 (nmf file) through the embed tag.
235 <embed name="mygame" src="mygame.nmf" type="application/x-pnacl" />
237 * **Manifest:** The manifest identifies the module to load and specifies
238 options. For example, "mygame.nmf" might look like this:
244 "url": "mygame.pexe",
247 * **pexe (portable NaCl file):** A compiled Native Client module. It uses the
248 :ref:`Pepper API <link_pepper>`, which provides a bridge to JavaScript and
249 other browser resources.
251 .. figure:: /images/nacl-in-a-web-app.png
252 :alt: Structure of a web application
254 Structure of a web application
256 For more details, see :doc:`Application Structure
257 <devguide/coding/application-structure>`.
264 The Pepper plug-in API (PPAPI), called **Pepper** for convenience, is an
265 open-source, cross-platform C/C++ API for web browser plug-ins. Pepper allows a
266 C/C++ module to communicate with the hosting browser and to access system-level
267 functions in a safe and portable way. One of the security constraints in Native
268 Client is that modules cannot make OS-level calls. Pepper provides analogous
269 APIs that modules can use instead.
271 You can use the Pepper APIs to gain access to the full array of browser
272 capabilities, including:
274 * :doc:`Talking to the JavaScript code in your application
275 <devguide/coding/message-system>` from the C++ code in your NaCl module.
276 * :doc:`Doing file I/O <devguide/coding/file-io>`.
277 * :doc:`Playing audio <devguide/coding/audio>`.
278 * :doc:`Rendering 3D graphics <devguide/coding/3D-graphics>`.
280 Pepper includes both a :doc:`C API </c-api>` and a :doc:`C++ API </cpp-api>`.
281 The C++ API is a set of bindings written on top of the C API. For additional
282 information about Pepper, see `Pepper Concepts
283 <http://code.google.com/p/ppapi/wiki/Concepts>`_.
288 The :doc:`Quick Start <quick-start>` document provides links to downloads and
289 documentation to help you get started with developing and distributing Native