Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / native_client_sdk / doc_generated / devguide / coding / native-client-modules.html
bloba42918795fb888971d59306de50bf70c202a3ad6
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>
14 </ul>
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 &#8220;Pepper <em>browser</em>&#8221;) are implemented in the browser and they are called
26 from your module. Interfaces starting with <code>PPP_</code> (&#8220;Pepper <em>plugin</em>&#8221;) 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>
34 </ul>
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 &lt;stdlib.h&gt;
49 #include &lt;string.h&gt;
50 #include &quot;ppapi/c/pp_errors.h&quot;
51 #include &quot;ppapi/c/ppp.h&quot;
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 &quot;ppapi/c/ppb_instance.h&quot;
56 #include &quot;ppapi/c/ppp_instance.h&quot;
57 #include &quot;ppapi/c/ppb_input_event.h&quot;
58 #include &quot;ppapi/c/ppp_input_event.h&quot;
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,
67 uint32_t argc,
68 const char* argn[],
69 const char* argv[]) {
70 return PP_TRUE;
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 &amp;Instance_DidCreate,
84 // The definitions of these functions are not shown
85 &amp;Instance_DidDestroy,
86 &amp;Instance_DidChangeView,
87 &amp;Instance_DidChangeFocus,
88 &amp;Instance_HandleDocumentLoad
90 return &amp;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 &amp;Instance_HandleInput,
98 return &amp;input_interface;
100 // Return NULL for interfaces that you do not implement.
101 return NULL;
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));
109 return PP_OK;
111 </pre>
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>
117 class)</li>
118 <li>Code that defines your own Instance class (derived from the <code>pp:Instance</code>
119 class)</li>
120 </ul>
121 <p>In the &#8220;Hello tutorial&#8221; 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">
125 namespace pp {
126 Module* CreateModule() {
127 return new HelloTutorialModule();
130 </pre>
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 &#8220;Hello tutorial&#8221; example:</p>
137 <pre class="prettyprint">
138 class HelloTutorialModule : public pp::Module {
139 public:
140 HelloTutorialModule() : pp::Module() {}
141 virtual ~HelloTutorialModule() {}
143 virtual pp::Instance* CreateInstance(PP_Instance instance) {
144 return new HelloTutorialInstance(instance);
147 </pre>
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>&lt;embed&gt;</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 &#8220;Hello tutorial&#8221; example:</p>
153 <pre class="prettyprint">
154 class HelloTutorialInstance : public pp::Instance {
155 public:
156 explicit HelloTutorialInstance(PP_Instance instance) : pp::Instance(instance) {}
157 virtual ~HelloTutorialInstance() {}
159 virtual void HandleMessage(const pp::Var&amp; var_message) {}
161 </pre>
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&#8217;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&#8217;t actually do anything. Subsequent documents in the
171 Developer&#8217;s Guide build on these code samples and add more interesting
172 functionality.</p>
173 </section>
175 {{/partials.standard_nacl_article}}