OLD | NEW |
(Empty) | |
| 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 // This is a simple C++ Pepper plugin that demonstrates HandleMessage and |
| 12 // PostMessage. |
| 13 |
| 14 // This object represents one time the page says <embed>. |
| 15 class MyInstance : public pp::Instance { |
| 16 public: |
| 17 explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {} |
| 18 virtual ~MyInstance() {} |
| 19 virtual void HandleMessage(const pp::Var& message_data); |
| 20 }; |
| 21 |
| 22 // HandleMessage gets invoked when postMessage is called on the DOM element |
| 23 // associated with this plugin instance. |
| 24 // In this case, if we are given a string, we'll post a message back to |
| 25 // JavaScript indicating whether or not that string is a palindrome. |
| 26 void MyInstance::HandleMessage(const pp::Var& message_data) { |
| 27 if (message_data.is_string()) { |
| 28 std::string string_copy(message_data.AsString()); |
| 29 std::reverse(string_copy.begin(), string_copy.end()); |
| 30 bool is_palindrome(message_data.AsString() == string_copy); |
| 31 |
| 32 PostMessage(pp::Var(is_palindrome)); |
| 33 } |
| 34 } |
| 35 |
| 36 // This object is the global object representing this plugin library as long |
| 37 // as it is loaded. |
| 38 class MyModule : public pp::Module { |
| 39 public: |
| 40 MyModule() : pp::Module() {} |
| 41 virtual ~MyModule() {} |
| 42 |
| 43 // Override CreateInstance to create your customized Instance object. |
| 44 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 45 return new MyInstance(instance); |
| 46 } |
| 47 }; |
| 48 |
| 49 namespace pp { |
| 50 |
| 51 // Factory function for your specialization of the Module object. |
| 52 Module* CreateModule() { |
| 53 return new MyModule(); |
| 54 } |
| 55 |
| 56 } // namespace pp |
| 57 |
OLD | NEW |