ImportantDue to NaCl deprecation by the Chromium project, Tizen TV will continue its support for NaCl only until 2021-year products. Meanwhile, Tizen TV will start focusing on high-performance, cross-browser WebAssembly from 2020-year products.
This topic describes the "Hello World in C" sample application implementation.
This tutorial describes how to implement a simple "Hello World" Native Client (NaCl) application in C. The NaCl module waits for a message from the JavaScript component, creates a reply message by appending the received string to a predefined string, and sends it as a reply back to the JavaScript component, which displays the message on the screen.
The following figure shows the sample application. When the NaCl module has loaded, the "Status" field text changes to "Loaded". In the "NaCl messages" section, the message "Echo from NaCl: Hello World from JS" appears.
For information on how to access the sample application cheat sheet and run the application, see Sample-based Tutorials.
To implement the "Hello World" NaCl application in C:
Initialize the module.
A NaCl module in C must implement the following 2 functions:
The PPP_InitializeModule() function is an entry point for the NaCl plugin and is called when the module is loaded on the Web page. You must initialize interfaces and resources in this function, including the following mandatory interfaces:
The PPB_Messaging interface implements the PostMessage() function, which allows the NaCl plugin to send messages to the JavaScript component. This interface is used to send the reply message to the JavaScript component.
The PPB_Var interface creates and manages PP_Var objects, which are wrappers for JavaScript variables in C code. This interface is used to convert C-style strings to JavaScript variables, which can be sent to the JavaScript component using the PPB_Messaging interface.
/* Global pointers to selected browser interfaces */
const PPB_Messaging* g_messaging_interface;
const PPB_Var* g_var_interface;
/**
* Entry point to the NaCl module, called while the module is being initialized
* This function is obligatory; initialize interfaces in this function
*/
PP_EXPORT int32_t PPP_InitializeModule(PP_Module module_id,
PPB_GetInterface get_browser_interface) {
// Initialize pointers to used browser interfaces
g_messaging_interface = (const PPB_Messaging*) get_browser_interface(
PPB_MESSAGING_INTERFACE);
g_var_interface = (const PPB_Var*) get_browser_interface(PPB_VAR_INTERFACE);
return PP_OK;
}
The PPP_ShutdownModule() function is used internally by Google Chrome™ and must be left empty:
/**
* Implementation-specific function for internal use only
* This function is obligatory, but leave it empty
* @see <code>PPP_ShutdownModule</code> in ppp.h file.
*/
PP_EXPORT void PPP_ShutdownModule() {
}
For more information on the above functions, see the "include/ppapi/c/ppp.h" file in your "nacl_sdk/pepper_xx" directory.
ImportantTo compile the NaCl code successfully, the PPP_InitializeModule() and PPP_ShutdownModule() functions must be implemented with these exact names.
Implement the instance-specific functions.
When developing a NaCl application in C, you must implement all the following functions specific to the PPP_Instance interface, even if they are empty:
In the Instance_DidCreate() function, define the actions to perform when the instance is created, such as its initialization logic. The function returns PP_TRUE if the instance creation was successful, or PP_FALSE otherwise.
In the sample application, no initialization logic is needed, so the function always returns PP_TRUE:
In the Instance_DidDestroy() function, define the actions to perform when the instance is destroyed, such as releasing any acquired instance-specific resources.
In the sample application, the instance has nothing to release, so the function is empty:
For more information on the above functions, see the "include/ppapi/c/ppp_instance.h" file in your "nacl_sdk/pepper_xx" directory.
To receive messages, implement the Messaging_HandleMessage() function of the PPP_Messaging interface.
When the NaCl module receives a message from the JavaScript component:
Extract the message from the PP_Var object.
Concatenate the message to the predefined echo string, using the C standard library snprintf() function.
Convert the char buffer to a PP_Var object using the PPB_Var interface.
Send the message to the JavaScript component using the PostMessage() function of the PPB_Messaging interface.
const char* kEcho = "Echo from NaCl: ";
/**
* Handles messages from JS sent by the nacl_module.postMessage(...) function
*/
void Messaging_HandleMessage(PP_Instance instance, struct PP_Var message) {
char str_buff[64];
uint32_t len;
// Extract the char array from the message PP_Var structure
const char* message_str = g_var_interface->VarToUtf8(message, &len);
if (message_str == NULL)
return;
snprintf(str_buff, sizeof(str_buff), "%s%s\n", kEcho, message_str);
// Create a PP_Var containing the message body
struct PP_Var var_response = g_var_interface->VarFromUtf8(str_buff,
strlen(str_buff));
// Post the message to the JavaScript layer
g_messaging_interface->PostMessage(instance, var_response);
}
For more information on the HandleMessage() function, see the "include/ppapi/c/ppp_messaging.h" file in your "nacl_sdk/pepper_xx" directory.
Connect the interfaces to the NaCl module.
To connect the implemented interfaces, you must implement the PPP_GetInterface() function, which takes a C-style string as a parameter and returns a const void pointer. The function is called multiple times during application initialization, checking for PPP interfaces that have been implemented. For each implemented interface, return a structure containing pointers to the interface functions:
/**
* This mandatory function retrieves the interface implementations that this module supports
*/
PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) {
// The module must fill this structure (interface) in the following order
// and return it in this case
static PPP_Instance instance_interface = {
&Instance_DidCreate,
&Instance_DidDestroy,
&Instance_DidChangeView,
&Instance_DidChangeFocus,
&Instance_HandleDocumentLoad
};
return &instance_interface;
}
if (strcmp(interface_name, PPP_MESSAGING_INTERFACE) == 0) {
// To handle messages, implement the PPP_Messaging interface and
// return it as a pointer to this structure
static PPP_Messaging messaging_interface = {
&Messaging_HandleMessage
};
return &messaging_interface;
}
return NULL;
}
For more information on the PPP_GetInterface() function, see the "include/ppapi/c/ppp.h" file in your "nacl_sdk/pepper_xx" directory.
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.