- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / examples / scripting / post_message.cc
1 // Copyright (c) 2011 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 <algorithm>
6
7 #include "ppapi/cpp/instance.h"
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h"
10
11 // When compiling natively on Windows, PostMessage can be #define-d to
12 // something else.
13 #ifdef PostMessage
14 #undef PostMessage
15 #endif
16
17 // This is a simple C++ Pepper plugin that demonstrates HandleMessage and
18 // PostMessage.
19
20 // This object represents one time the page says <embed>.
21 class MyInstance : public pp::Instance {
22  public:
23   explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {}
24   virtual ~MyInstance() {}
25   virtual void HandleMessage(const pp::Var& message_data);
26 };
27
28 // HandleMessage gets invoked when postMessage is called on the DOM element
29 // associated with this plugin instance.
30 // In this case, if we are given a string, we'll post a message back to
31 // JavaScript indicating whether or not that string is a palindrome.
32 void MyInstance::HandleMessage(const pp::Var& message_data) {
33   if (message_data.is_string()) {
34     std::string string_copy(message_data.AsString());
35     std::reverse(string_copy.begin(), string_copy.end());
36     bool is_palindrome(message_data.AsString() == string_copy);
37
38     PostMessage(pp::Var(is_palindrome));
39   }
40 }
41
42 // This object is the global object representing this plugin library as long
43 // as it is loaded.
44 class MyModule : public pp::Module {
45  public:
46   MyModule() : pp::Module() {}
47   virtual ~MyModule() {}
48
49   // Override CreateInstance to create your customized Instance object.
50   virtual pp::Instance* CreateInstance(PP_Instance instance) {
51     return new MyInstance(instance);
52   }
53 };
54
55 namespace pp {
56
57 // Factory function for your specialization of the Module object.
58 Module* CreateModule() {
59   return new MyModule();
60 }
61
62 }  // namespace pp
63