Native client-application-native Client Modules

Source: Internet
Author: User
Tags strcmp

This document describes the classes and functions so need to implement in a Native Client module in order for Chrome To load, initialize, and run it. The requirements is the same regardless of whether or not the module uses PNACL, but depend on whether the module is writ Ten in C or C + +. Introduction

Native Client modules do not has a main () function. When a-module loads, the Native Client runtime calls the code in the module to create an instance and initialize the Inter Faces for the APIs the module uses. This initialization sequence depends on whether the module was written in C or C + + and requires that you implement specific Functions in each case. Writing modules in C

The C API uses a prefix convention to show whether an interface are implemented in the browser or in a module. Interfaces starting with ppb_ (which can be read as "Pepper browser") is implemented in the browser and they is called F Rom your module. Interfaces Starting with Ppp_ ("Pepper plugin") is implemented in the module; They is called from the browser and would execute on the main thread of the module instance.

When you implement a Native Client module in C You must include these components:the functions Ppp_initializemodule and P Pp_getinterface Code that implements the interface Ppp_instance and any other C interfaces that your module uses

For each PPP interface, you must implement all of its functions, create the struct through which the browser calls the INT Erface, and insure the function ppp_getinterface returns the appropriate struct for the interface.

For each PPB interface, you must declare a pointer to the interface and initialize the pointer with a call Toget_browser I Nside Ppp_initializemodule.

These steps is illustrated in the code excerpt below, which shows the implementation and initialization of the required P Pp_instance interface. The code excerpt also shows the initialization of three additional interfaces which is not required:ppb_instance (throug H which the Native Client module calls back to the browser) andppb_inputevent and ppp_inputevent.

#include <stdlib.h> #include <string.h> #include "ppapi/c/pp_errors.h" #include "ppapi/c/ppp.h"//Include
The interface headers.
PPB APIs describe calls from the module to the browser.
PPP APIs describe calls from the browser to the functions defined in your module. #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 per PPB interface that your module uses.
static ppb_instance* ppb_instance_interface = NULL;

static ppb_inputevent* ppb_input_event_interface = NULL;
Define all the functions is PPP interface that your module uses.
Here's a stub for the first function in Ppp_instance.
                                  Static Pp_bool instance_didcreate (Pp_instance Instance, uint32_t argc, Const char* argn[], const char* argv[]) {return pp_true;}// ... more API functioNS ...//Define ppp_getinterface.
This function should return a Non-null value for every interface is using.
The string for the name of the interface are defined in the interface ' s header file.
The browser calls this function to get pointers to the interfaces that your module implements.
        Pp_export Const void* ppp_getinterface (const char* interface_name) {//Create structs for each PPP interface.
         Assign the interface functions to the data fields.
                        if (strcmp (interface_name, ppp_instance_interface) = = 0) {static Ppp_instance Instance_interface = {
                        &instance_didcreate,//The definitions of these functions is not shown
                        &instance_diddestroy, &instance_didchangeview,
                &instance_didchangefocus, &instance_handledocumentload}; Return &inStance_interface; } if (strcmp (interface_name, ppp_input_event_interface) = = 0) {static ppp_inputevent Input_inte
                        Rface = {//the definition of this function was not shown.
                &instance_handleinput,};
         Return &input_interface;
         }//Return NULL for interfaces this do not implement.
return NULL;
}//Define Ppp_initializemodule, the entry point of your module.
Retrieve the API for the Browser-side (PPB) interfaces you'll use. Pp_export int32_t ppp_initializemodule (pp_module a_module_id, Ppb_getinterface get_browser) {Ppb_instance_interfa
        CE = (ppb_instance*) (Get_browser (ppb_instance_interface));
        Ppb_input_event_interface = (ppb_inputevent*) (Get_browser (ppb_input_event_interface));
return PP_OK; }
Writing Modules in C + +

When you implement a Native Client module in C + + you must include these components:the factory function called Createmodu Le () code that defines your own Module class (derived from the Pp::module class) Code that defines your own Instance class (derived from the Pp:instance class)

In the ' Hello Tutorial ' example (in the Getting_started/part1 directory of the NaCl SDK), these three components is speci Fied in the file hello_tutorial.cc. Here is the factory function:

Namespace pp {
  module* createmodule () {
    return new hellotutorialmodule ();
  }
}

The createmodule ()  factory function is the main binding point between a module and the browser, and serves a s the entry point into the module. The browser calls 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.