This document describes the classes and methods that you need to implement for Chrome to load, initialize, and run the Native Client module. Regardless of whether the module uses PNACL, the dependencies are the same, depending on if the module is written in C or C + +.
Introduced
The Native client module does not require the main () function. When the module is loaded, the Native client runtime invokes the code in the module to create an instance and initialize the interface to be used by the module. This initialization process depends on whether the module is written in C or C + + and requires you to implement a specific function for two situations.
The C API uses a prefix convention to indicate whether an interface is implemented in a browser or module. Ppb_ interface (can be read as "Pepper browser") is implemented in the browser and is called from your module. The interface ("Pepper plugin") that begins with Ppp_ is implemented in a module and is invoked from the browser and executed in the main thread of the module instance.
Writing a module in C
These components must be included when you implement a native Client module in C:
The Ppp_initializemodule function and the Ppp_getinterface function.
Implement the code for the Ppp_instance interface and all of the other interfaces that your module uses.
For each PPP interface, you must implement all of its functions, create the struct used by the browser to invoke the interface, and make sure that the Ppp_getinterface function returns the corresponding structure of the interface.
For each PPB interface, you must define a pointer to the interface and initialize the pointer in Ppp_initializemodule by calling Get_browser.
The code excerpt below illustrates these steps, and the code demonstrates the necessary ppp_instance interfaces for implementation and initialization. The excerpt code also demonstrates the initialization of three additional nonessential interfaces: Ppb_instance (Native client module via it callback browser) and Ppb_inputevent and Ppp_inputevent.
#include <stdlib.h>#include<string.h>#include"Ppapi/c/pp_errors.h"#include"Ppapi/c/ppp.h"//introduce a header file.//the PPB APIs describe the calls from the module to the browser.//PPP APIs Describe the invocation of a function from the browser to your module definition.#include"ppapi/c/ppb_instance.h"#include"ppapi/c/ppp_instance.h"#include"ppapi/c/ppb_input_event.h"#include"ppapi/c/ppp_input_event.h"//create pointers for each PPB interface that your module uses.Staticppb_instance* Ppb_instance_interface =NULL;Staticppb_inputevent* Ppb_input_event_interface =NULL;//define functions for each PPP interface that your module uses.//This is the stub of the first function in Ppp_instance.Staticpp_bool instance_didcreate (pp_instance Instance, uint32_t argc, Const Char*argn[],Const Char*argv[]) { returnpp_true;}// ... Other API Functions ...//define Ppp_getinterface.//This function returns a non-null value for each interface you use.//The interface name string is defined in the header file of the interface.//The browser calls this function to get pointers to the interfaces that your module implements.Pp_exportConst void* Ppp_getinterface (Const Char*interface_name) { //create a struct for each PPP interface. //put the interface function into the data structure. if(strcmp (interface_name, ppp_instance_interface) = =0) { StaticPpp_instance Instance_interface = { &Instance_didcreate,//The definition of these functions does not show the&Instance_diddestroy,&Instance_didchangeview,&Instance_didchangefocus,&Instance_handledocumentload}; return&Instance_interface; } if(strcmp (interface_name, ppp_input_event_interface) = =0) { StaticPpp_inputevent Input_interface = { //The definition of this function is not shown.&Instance_handleinput,}; return&Input_interface; } //an interface that is not implemented returns NULL. returnNULL;}//defines the Ppp_initializemodule, the entry point of the module.//gets the API for the browser-side (PPB) interface that will be used.pp_export int32_t ppp_initializemodule (pp_module a_module_id, Ppb_getinterface get_browser) {Ppb_instance_int Erface= (ppb_instance*) (Get_browser (ppb_instance_interface)); Ppb_input_event_interface= (ppb_inputevent*) (Get_browser (ppb_input_event_interface)); returnPp_ok;}
Native client Module (translated)