Hello World in C++
This topic describes the "Hello World in C++" sample application implementation.
Samples
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 thepp::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 theCreateInstance()
function, which creates an instance object derived from thepp::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 basepp::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 theHandleMessage()
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()
andInit()
functions, see the "include/ppapi/cpp/instance.h" file in your "nacl_sdk/pepper_xx" directory. -