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.
5 #include "ppapi/cpp/instance.h"
6 #include "ppapi/cpp/module.h"
7 #include "ppapi/cpp/var.h"
11 // The expected string sent by the browser.
12 const char* const kHelloString
= "hello";
13 // The string sent back to the browser upon receipt of a message
14 // containing "hello".
15 const char* const kReplyString
= "hello from NaCl";
19 class HelloTutorialInstance
: public pp::Instance
{
21 explicit HelloTutorialInstance(PP_Instance instance
)
22 : pp::Instance(instance
) {}
23 virtual ~HelloTutorialInstance() {}
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())
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
);
40 class HelloTutorialModule
: public pp::Module
{
42 HelloTutorialModule() : pp::Module() {}
43 virtual ~HelloTutorialModule() {}
45 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
46 return new HelloTutorialInstance(instance
);
52 Module
* CreateModule() {
53 return new HelloTutorialModule();