1 {{+bindTo:partials.standard_nacl_article}}
3 <section id=
"native-client-modules">
4 <span id=
"devcycle-native-client-modules"></span><h1 id=
"native-client-modules"><span id=
"devcycle-native-client-modules"></span>Native Client Modules
</h1>
5 <p>This document describes the classes and functions that you need to implement in
6 a Native Client module in order for Chrome to load, initialize, and run it. The
7 requirements are the same regardless of whether or not the module uses PNaCl,
8 but depend on whether the module is written in C or C++.
</p>
9 <div class=
"contents local" id=
"contents" style=
"display: none">
10 <ul class=
"small-gap">
11 <li><a class=
"reference internal" href=
"#introduction" id=
"id2">Introduction
</a></li>
12 <li><a class=
"reference internal" href=
"#writing-modules-in-c" id=
"id3">Writing modules in C
</a></li>
13 <li><a class=
"reference internal" href=
"#id1" id=
"id4">Writing modules in C++
</a></li>
16 </div><h2 id=
"introduction">Introduction
</h2>
17 <p>Native Client modules do not have a
<code>main()
</code> function. When a module loads,
18 the Native Client runtime calls the code in the module to create an instance and
19 initialize the interfaces for the APIs the module uses. This initialization
20 sequence depends on whether the module is written in C or C++ and requires that
21 you implement specific functions in each case.
</p>
22 <h2 id=
"writing-modules-in-c">Writing modules in C
</h2>
23 <p>The C API uses a prefix convention to show whether an interface is implemented
24 in the browser or in a module. Interfaces starting with
<code>PPB_
</code> (which can be
25 read as
“Pepper
<em>browser
</em>”) are implemented in the browser and they are called
26 from your module. Interfaces starting with
<code>PPP_
</code> (
“Pepper
<em>plugin
</em>”) are
27 implemented in the module; they are called from the browser and will execute on
28 the main thread of the module instance.
</p>
29 <p>When you implement a Native Client module in C you must include these components:
</p>
30 <ul class=
"small-gap">
31 <li>The functions
<code>PPP_InitializeModule
</code> and
<code>PPP_GetInterface
</code></li>
32 <li>Code that implements the interface
<code>PPP_Instance
</code> and any other C interfaces
33 that your module uses
</li>
35 <p>For each PPP interface, you must implement all of its functions, create the
36 struct through which the browser calls the interface, and insure that the
37 function
<code>PPP_GetInterface
</code> returns the appropriate struct for the interface.
</p>
38 <p>For each PPB interface, you must declare a pointer to the interface and
39 initialize the pointer with a call to
<code>get_browser
</code> inside
40 <code>PPP_InitializeModule
</code>.
</p>
41 <p>These steps are illustrated in the code excerpt below, which shows the
42 implementation and initialization of the required
<code>PPP_Instance
</code>
43 interface. The code excerpt also shows the initialization of three additional
44 interfaces which are not required:
<code>PPB_Instance
</code> (through which the Native
45 Client module calls back to the browser) and
<code>PPB_InputEvent
</code> and
46 <code>PPP_InputEvent
</code>.
</p>
47 <pre class=
"prettyprint">
48 #include
<stdlib.h
>
49 #include
<string.h
>
50 #include
"ppapi/c/pp_errors.h
"
51 #include
"ppapi/c/ppp.h
"
52 // Include the interface headers.
53 // PPB APIs describe calls from the module to the browser.
54 // PPP APIs describe calls from the browser to the functions defined in your module.
55 #include
"ppapi/c/ppb_instance.h
"
56 #include
"ppapi/c/ppp_instance.h
"
57 #include
"ppapi/c/ppb_input_event.h
"
58 #include
"ppapi/c/ppp_input_event.h
"
60 // Create pointers for each PPB interface that your module uses.
61 static PPB_Instance* ppb_instance_interface = NULL;
62 static PPB_InputEvent* ppb_input_event_interface = NULL;
64 // Define all the functions for each PPP interface that your module uses.
65 // Here is a stub for the first function in PPP_Instance.
66 static PP_Bool Instance_DidCreate(PP_Instance instance,
72 // ... more API functions ...
74 // Define PPP_GetInterface.
75 // This function should return a non-NULL value for every interface you are using.
76 // The string for the name of the interface is defined in the interface's header file.
77 // The browser calls this function to get pointers to the interfaces that your module implements.
78 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
79 // Create structs for each PPP interface.
80 // Assign the interface functions to the data fields.
81 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) ==
0) {
82 static PPP_Instance instance_interface = {
83 &Instance_DidCreate,
84 // The definitions of these functions are not shown
85 &Instance_DidDestroy,
86 &Instance_DidChangeView,
87 &Instance_DidChangeFocus,
88 &Instance_HandleDocumentLoad
90 return
&instance_interface;
93 if (strcmp(interface_name, PPP_INPUT_EVENT_INTERFACE) ==
0) {
94 static PPP_InputEvent input_interface = {
95 // The definition of this function is not shown.
96 &Instance_HandleInput,
98 return
&input_interface;
100 // Return NULL for interfaces that you do not implement.
104 // Define PPP_InitializeModule, the entry point of your module.
105 // Retrieve the API for the browser-side (PPB) interfaces you will use.
106 PP_EXPORT int32_t PPP_InitializeModule(PP_Module a_module_id, PPB_GetInterface get_browser) {
107 ppb_instance_interface = (PPB_Instance*)(get_browser(PPB_INSTANCE_INTERFACE));
108 ppb_input_event_interface = (PPB_InputEvent*)(get_browser(PPB_INPUT_EVENT_INTERFACE));
112 <h2 id=
"id1">Writing modules in C++
</h2>
113 <p>When you implement a Native Client module in C++ you must include these components:
</p>
114 <ul class=
"small-gap">
115 <li>The factory function called
<code>CreateModule()
</code></li>
116 <li>Code that defines your own Module class (derived from the
<code>pp::Module
</code>
118 <li>Code that defines your own Instance class (derived from the
<code>pp:Instance
</code>
121 <p>In the
“Hello tutorial
” example (in the
<code>getting_started/part1
</code> directory of
122 the NaCl SDK), these three components are specified in the file
123 <code>hello_tutorial.cc
</code>. Here is the factory function:
</p>
124 <pre class=
"prettyprint">
126 Module* CreateModule() {
127 return new HelloTutorialModule();
131 <p>The
<code>CreateModule()
</code> factory function is the main binding point between a
132 module and the browser, and serves as the entry point into the module. The
133 browser calls
<code>CreateModule()
</code> when a module is first loaded; this function
134 returns a Module object derived from the
<code>pp::Module
</code> class. The browser keeps
135 a singleton of the Module object.
</p>
136 <p>Below is the Module class from the
“Hello tutorial
” example:
</p>
137 <pre class=
"prettyprint">
138 class HelloTutorialModule : public pp::Module {
140 HelloTutorialModule() : pp::Module() {}
141 virtual ~HelloTutorialModule() {}
143 virtual pp::Instance* CreateInstance(PP_Instance instance) {
144 return new HelloTutorialInstance(instance);
148 <p>The Module class must include a
<code>CreateInstance()
</code> method. The browser calls
149 the
<code>CreateInstance()
</code> method every time it encounters an
<code><embed
></code> element
150 on a web page that references the same module. The
<code>CreateInstance()
</code> function
151 creates and returns an Instance object derived from the
<code>pp::Instance
</code> class.
</p>
152 <p>Below is the Instance class from the
“Hello tutorial
” example:
</p>
153 <pre class=
"prettyprint">
154 class HelloTutorialInstance : public pp::Instance {
156 explicit HelloTutorialInstance(PP_Instance instance) : pp::Instance(instance) {}
157 virtual ~HelloTutorialInstance() {}
159 virtual void HandleMessage(const pp::Var
& var_message) {}
162 <p>As in the example above, the Instance class for your module will likely include
163 an implementation of the
<code>HandleMessage()
</code> function. The browser calls an
164 instance
’s
<code>HandleMessage()
</code> function every time the JavaScript code in an
165 application calls
<code>postMessage()
</code> to send a message to the instance. See the
166 <a class=
"reference internal" href=
"/native-client/devguide/coding/message-system.html"><em>Native Client messaging system
</em></a> for more information about
167 how to send messages between JavaScript code and Native Client modules.
</p>
168 <p>While the
<code>CreateModule()
</code> factory function, the
<code>Module
</code> class, and the
169 <code>Instance
</code> class are required for a Native Client application, the code
170 samples shown above don
’t actually do anything. Subsequent documents in the
171 Developer
’s Guide build on these code samples and add more interesting
175 {{/partials.standard_nacl_article}}