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.

Figure 1. Hello World in C++ application

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++:

  1. 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
    
  2. 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);
      }
    };
    
  3. Define the instance-specific behavior.

    1. 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.

    2. 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:

      1. Verify the received object.
      2. Convert the message to a std::string object and append it to the predefined echo message.
      3. Send the message back to the JavaScript component, using the PostMessage() function.
    3. 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.