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 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++:
In the pp namespace, create a module object derived from the pp::Module class:
namespace pp {
/**
* This function is mandatory and is the entry point to a NaCl plugin
*/
Module* CreateModule() {
return new HelloWorldModule();
}
} // namespace pp
Implement the module logic.
Define the CreateInstance()function, which creates an instance object derived from the pp::Instance class.
In the sample application, the class constructor and destructor are declared explicitly, but have been left empty.
/**
* Each NaCl application must have a class that implements pp::Module, which
* implements a class that inherits pp::Instance
*/
class HelloWorldModule : public pp::Module {
public:
HelloWorldModule()
: pp::Module() {
}
virtual ~HelloWorldModule() {
}
/**
* To enable launching the NaCl plugin, this function must be implemented
* It is called whenever a browser encounters an <embed> element on a Web page
*/
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new HelloWorldInstance(instance);
}
};
Define the instance-specific behavior.
Implement the explicit class constructor for the instance.
To initialize the instance properly, the constructor must call the base pp::Instance(PP_Instance) class. In the sample application, the class destructor has also been declared explicitly, but has been left empty.
Classes that inherit from the pp::Instance class can overload multiple functions to handle various plugin events. To receive messages from the JavaScript component, overload the HandleMessage() function:
Verify the received object.
Convert the message to a std::string object and append it to the predefined echo message.
Send the message back to the JavaScript component, using the PostMessage() function.
To notify the browser that instance initialization is successful, implement the Init() function.
const char* kEcho = "Echo from NaCl: ";
/**
* The base of a NaCl application
* Each NaCl <embed> element on a Web page has a pp::Instance object
*/
class HelloWorldInstance : public pp::Instance {
public:
/**
* To initialize the instance properly, the constructor must call the
* base pp::Instance(PP_Instance) class
*/
explicit HelloWorldInstance(PP_Instance instance)
: pp::Instance(instance) {
}
virtual ~HelloWorldInstance() {
}
/**
* Handle messages sent from JS with the nacl_module.postMessage() function
*/
virtual void HandleMessage(const pp::Var& message) {
if (message.is_string()) {
PostMessage(kEcho + message.AsString() + "\n");
}
}
/**
* Initialize this instance with the arguments provided in the <embed> tag
*/
virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
return true;
}
};
For more information on the HandleMessage() and Init() functions, see the "include/ppapi/cpp/instance.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.