1// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "ppapi/cpp/instance.h" 6#include "ppapi/cpp/module.h" 7#include "ppapi/cpp/var.h" 8 9namespace { 10 11// The expected string sent by the browser. 12const char* const kHelloString = "hello"; 13// The string sent back to the browser upon receipt of a message 14// containing "hello". 15const char* const kReplyString = "hello from NaCl"; 16 17} // namespace 18 19class HelloTutorialInstance : public pp::Instance { 20 public: 21 explicit HelloTutorialInstance(PP_Instance instance) 22 : pp::Instance(instance) {} 23 virtual ~HelloTutorialInstance() {} 24 25 virtual void HandleMessage(const pp::Var& var_message) { 26 // Ignore the message if it is not a string. 27 if (!var_message.is_string()) 28 return; 29 30 // Get the string message and compare it to "hello". 31 std::string message = var_message.AsString(); 32 if (message == kHelloString) { 33 // If it matches, send our response back to JavaScript. 34 pp::Var var_reply(kReplyString); 35 PostMessage(var_reply); 36 } 37 } 38}; 39 40class HelloTutorialModule : public pp::Module { 41 public: 42 HelloTutorialModule() : pp::Module() {} 43 virtual ~HelloTutorialModule() {} 44 45 virtual pp::Instance* CreateInstance(PP_Instance instance) { 46 return new HelloTutorialInstance(instance); 47 } 48}; 49 50namespace pp { 51 52Module* CreateModule() { 53 return new HelloTutorialModule(); 54} 55 56} // namespace pp 57