Home | History | Annotate | Download | only in doc
      1 .. _overview:
      2 
      3 ##################
      4 Technical 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
     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.
     18 
     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.
     24 
     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
     28 will 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 
     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>`.
     41 
     42 .. _why-use-native-client:
     43 
     44 Why use Native Client?
     45 ======================
     46 
     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
     51 such as:
     52 
     53 - 3D games
     54 - multimedia editors
     55 - CAD modeling
     56 - client-side data analytics
     57 - interactive simulations.
     58 
     59 Native Client gives C and C++ (and other languages targeting it) the same level
     60 of portability and safety as JavaScript.
     61 
     62 .. _benefits-of-native-client:
     63 
     64 Benefits of Native Client
     65 =========================
     66 
     67 Benefits 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   
     90 Common use cases
     91 ================
     92 
     93 Typical 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 
    126 How Native Client works
    127 =======================
    128 
    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:
    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 
    140 The 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 
    149 Toolchains
    150 ----------
    151 
    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.
    155 
    156 The 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 
    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
    171 open web.
    172 
    173 .. _security:
    174 
    175 Security
    176 --------
    177 
    178 Since Native Client permits the execution of native code on client machines,
    179 special 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 
    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.
    193 
    194 .. _portability:
    195 
    196 Portability
    197 -----------
    198 
    199 Portable Native Client (PNaCl, prounounced "pinnacle") employs state-of-the-art
    200 compiler technology to compile C/C++ source code to a portable bitcode
    201 executable (**pexe**). PNaCl bitcode is an OS- and architecture-independent
    202 format that can be freely distributed on the web and :ref:`embedded in web
    203 applications<link_nacl_in_web_apps>`.
    204 
    205 The PNaCl translator is a component embedded in the Chrome browser; its task is
    206 to 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
    208 as described above. The translator uses intelligent caching to avoid
    209 re-compiling the pexe if it was previously compiled on the client's browser.
    210 
    211 Native Client also supports the execution of nexe modules directly in the
    212 browser. However, since nexes contain architecture-specific machine code, they
    213 are not allowed to be distributed on the open web. They can only be used as part
    214 of applications and extensions that are installed from the Chrome Web Store.
    215 
    216 For 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 
    221 Structure of a web application
    222 ==============================
    223 
    224 .. _application_files:
    225 
    226 A 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 
    254 For more details, see :doc:`Application Structure
    255 <devguide/coding/application-structure>`.
    256 
    257 .. _link_pepper:
    258 
    259 Pepper plug-in API
    260 ------------------
    261 
    262 The Pepper plug-in API (PPAPI), called **Pepper** for convenience, is an
    263 open-source, cross-platform C/C++ API for web browser plug-ins. Pepper allows a 
    264 C/C++ module to communicate with the hosting browser and to access system-level
    265 functions in a safe and portable way. One of the security constraints in Native
    266 Client is that modules cannot make OS-level calls. Pepper provides analogous
    267 APIs that modules can use instead.
    268 
    269 You can use the Pepper APIs to gain access to the full array of browser
    270 capabilities, 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 
    278 Pepper includes both a :doc:`C API </c-api>` and a :doc:`C++ API </cpp-api>`.
    279 The C++ API is a set of bindings written on top of the C API. For additional
    280 information about Pepper, see `Pepper Concepts 
    281 <http://code.google.com/p/ppapi/wiki/Concepts>`_.
    282 
    283 Where to start
    284 ==============
    285 
    286 The :doc:`Quick Start <quick-start>` document provides links to downloads and
    287 documentation to help you get started with developing and distributing Native
    288 Client applications.
    289