• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _overview:
2
3##################
4Technical Overview
5##################
6
7.. contents::
8  :local:
9  :backlinks: none
10  :depth: 2
11
12**Native Client** (NaCl) is an open-source technology for running native
13compiled code in the browser, with the goal of maintaining the portability
14and safety that users expect from web applications. Native Client expands web
15programming beyond JavaScript, enabling you to enhance your web applications
16using your preferred language. This document describes some of the key benefits
17and common use cases of Native Client.
18
19Google has implemented the open-source `Native Client project
20<http://www.chromium.org/nativeclient>`_ in the Chrome browser on Windows, Mac,
21Linux, and Chrome OS. The :doc:`Native Client Software Development Kit (SDK)
22<sdk/download>`, itself an open-source project, lets you create web applications
23that use NaCl and run in Chrome across multiple platforms.
24
25A Native Client web application consists of JavaScript, HTML, CSS, and a NaCl
26module written in a language supported by the SDK. The NaCl SDK currently
27supports C and C++; as compilers for additional languages are developed, the SDK
28will be updated.
29
30.. figure:: /images/web-app-with-nacl.png
31   :alt: A web application with and without Native Client
32
33   A web application with and without Native Client
34
35Native Client comes in two flavors: traditional (NaCl) and portable (PNaCl).
36Traditional, which must be distributed through the Chrome Web Store lets you
37target a specific hardware platform. Portable can run on the open web. A
38bitcode file that can be loaded from any web server is downloaded to a client
39machine and converted to hardware-specific code before any execution. For
40details, see :doc:`NaCl and PNaCl </nacl-and-pnacl>`.
41
42.. _why-use-native-client:
43
44Why use Native Client?
45======================
46
47Native Client open-source technology is designed to run compiled code
48securely inside a browser at near-native speeds. Native Client gives web
49applications some advantages of desktop software. Specifically, it provides the
50means to fully harness the client's computational resources for applications
51such as:
52
53- 3D games
54- multimedia editors
55- CAD modeling
56- client-side data analytics
57- interactive simulations.
58
59Native Client gives C and C++ (and other languages targeting it) the same level
60of portability and safety as JavaScript.
61
62.. _benefits-of-native-client:
63
64Benefits of Native Client
65=========================
66
67Benefits of Native Client include:
68
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
72  to install a plug-in.
73* **Portability:** Writing your applications once and running them on multiple
74  operating systems (Windows, Linux, Mac, and Chrome OS) and CPU architectures
75  (x86 and ARM).
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.
87
88.. _common-use-cases:
89
90Common use cases
91================
92
93Typical use cases for Native Client include the following:
94
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
104  browser.
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
109  over the network.
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
118  many platforms.
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.
123
124.. _link_how_nacl_works:
125
126How Native Client works
127=======================
128
129Native Client is an umbrella name for a set of related software components for
130developing C/C++ applications and running them securely on the web. At a high
131level, Native Client consists of:
132
133* **Toolchains:** collections of development tools (compilers, linkers, etc.)
134  that transform C/C++ code to Portable Native Client modules or Native Client
135  modules.
136* **Runtime components:** components embedded in the browser or other host
137  platforms that allow execution of Native Client modules securely and
138  efficiently.
139
140The following diagram shows how these components interact:
141
142.. figure:: /images/nacl-pnacl-component-diagram.png
143   :alt: The Native Client toolchains and their outputs
144
145   The Native Client toolchains and their outputs
146
147.. _toolchains:
148
149Toolchains
150----------
151
152A Native Client toolchain consists of a compiler, a linker, an assembler and
153other tools that are used to convert C/C++ source code into a module that is
154loadable by a browser.
155
156The Native Client SDK provides two toolchains:
157
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
162  architecture.
163
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.
168
169The PNaCl toolchain is recommended for most applications. The NaCl-GCC
170toolchain should only be used for applications that won't be distributed on the
171open web.
172
173.. _security:
174
175Security
176--------
177
178Since Native Client permits the execution of native code on client machines,
179special security measures have to be implemented:
180
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.
186
187These security measures are in addition to the existing sandbox in the
188Chrome browser. The Native Client module always executes in a process with
189restricted permissions. The only interaction between this process and the
190outside world is through defined browser interfaces. Because of the
191combination of the NaCl sandbox and the Chrome sandbox, we say that
192Native Client employs a **double sandbox** design.
193
194.. _portability:
195
196Portability
197-----------
198
199Portable Native Client (PNaCl, prounounced "pinnacle") employs state-of-the-art
200compiler technology to compile C/C++ source code to a portable bitcode
201executable (**pexe**). PNaCl bitcode is an OS- and architecture-independent
202format that can be freely distributed on the web and :ref:`embedded in web
203applications<link_nacl_in_web_apps>`.
204
205The PNaCl translator is a component embedded in the Chrome browser; its task is
206to run pexe modules. Internally, the translator compiles a pexe to a nexe
207(described above), and then executes the nexe within the Native Client sandbox
208as described above. The translator uses intelligent caching to avoid
209re-compiling the pexe if it was previously compiled on the client's browser.
210
211Native Client also supports the execution of nexe modules directly in the
212browser. However, since nexes contain architecture-specific machine code, they
213are not allowed to be distributed on the open web. They can only be used as part
214of applications and extensions that are installed from the Chrome Web Store.
215
216For more details on the difference between NaCl and PNaCl, see
217:doc:`NaCl and PNaCl <nacl-and-pnacl>`.
218
219.. _link_nacl_in_web_apps:
220
221Structure of a web application
222==============================
223
224.. _application_files:
225
226A Native Client application consists of a set of files:
227
228* **HTML and CSS:** The HTML file tells the browser where to find the manifest
229  (nmf file) through the embed tag.
230
231  .. naclcode::
232
233    <embed name="mygame" src="mygame.nmf" type="application/x-pnacl" />
234
235* **Manifest:** The manifest identifies the module to load and specifies
236  options. For example, "mygame.nmf" might look like this:
237
238  .. naclcode::
239
240    {...
241      ...
242      "url": "mygame.pexe",
243    }
244
245* **pexe (portable NaCl file):** A compiled Native Client module. It uses the
246  :ref:`Pepper API <link_pepper>`, which provides a bridge to JavaScript and
247  other browser resources.
248
249.. figure:: /images/nacl-in-a-web-app.png
250   :alt: Structure of a web application
251
252   Structure of a web application
253
254For more details, see :doc:`Application Structure
255<devguide/coding/application-structure>`.
256
257.. _link_pepper:
258
259Pepper plug-in API
260------------------
261
262The Pepper plug-in API (PPAPI), called **Pepper** for convenience, is an
263open-source, cross-platform C/C++ API for web browser plug-ins. Pepper allows a
264C/C++ module to communicate with the hosting browser and to access system-level
265functions in a safe and portable way. One of the security constraints in Native
266Client is that modules cannot make OS-level calls. Pepper provides analogous
267APIs that modules can use instead.
268
269You can use the Pepper APIs to gain access to the full array of browser
270capabilities, including:
271
272* :doc:`Talking to the JavaScript code in your application
273  <devguide/coding/message-system>` from the C++ code in your NaCl module.
274* :doc:`Doing file I/O <devguide/coding/file-io>`.
275* :doc:`Playing audio <devguide/coding/audio>`.
276* :doc:`Rendering 3D graphics <devguide/coding/3D-graphics>`.
277
278Pepper includes both a :doc:`C API </c-api>` and a :doc:`C++ API </cpp-api>`.
279The C++ API is a set of bindings written on top of the C API. For additional
280information about Pepper, see `Pepper Concepts
281<http://code.google.com/p/ppapi/wiki/Concepts>`_.
282
283Where to start
284==============
285
286The :doc:`Quick Start <quick-start>` document provides links to downloads and
287documentation to help you get started with developing and distributing Native
288Client applications.
289