Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: ppapi/examples/scripting/post_message.cc

Issue 8930010: Implement in-process PPB_VarArrayBuffer_Dev. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix ppapi_unittests Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "ppapi/cpp/dev/var_array_buffer_dev.h"
7 #include "ppapi/cpp/instance.h" 11 #include "ppapi/cpp/instance.h"
8 #include "ppapi/cpp/module.h" 12 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h" 13 #include "ppapi/cpp/var.h"
10 14
11 // This is a simple C++ Pepper plugin that demonstrates HandleMessage and 15 // This is a simple C++ Pepper plugin that demonstrates HandleMessage and
12 // PostMessage. 16 // PostMessage.
13 17
14 // This object represents one time the page says <embed>. 18 // This object represents one time the page says <embed>.
15 class MyInstance : public pp::Instance { 19 class MyInstance : public pp::Instance {
16 public: 20 public:
17 explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {} 21 explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {}
18 virtual ~MyInstance() {} 22 virtual ~MyInstance() {}
19 virtual void HandleMessage(const pp::Var& message_data); 23 virtual void HandleMessage(const pp::Var& message_data);
20 }; 24 };
21 25
22 // HandleMessage gets invoked when postMessage is called on the DOM element 26 // HandleMessage gets invoked when postMessage is called on the DOM element
23 // associated with this plugin instance. 27 // associated with this plugin instance.
24 // In this case, if we are given a string, we'll post a message back to 28 // 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. 29 // JavaScript indicating whether or not that string is a palindrome.
26 void MyInstance::HandleMessage(const pp::Var& message_data) { 30 void MyInstance::HandleMessage(const pp::Var& message_data) {
27 if (message_data.is_string()) { 31 if (message_data.is_string()) {
28 std::string string_copy(message_data.AsString()); 32 std::string string_copy(message_data.AsString());
29 std::reverse(string_copy.begin(), string_copy.end()); 33 pp::VarArrayBuffer_Dev buff(string_copy.size());
30 bool is_palindrome(message_data.AsString() == string_copy); 34 char* str = static_cast<char*>(buff.Map());
35 memcpy(str, string_copy.c_str(), buff.ByteLength());
31 36
32 PostMessage(pp::Var(is_palindrome)); 37 PostMessage(buff);
38 } else if (message_data.is_array_buffer()) {
39 pp::VarArrayBuffer_Dev buff_var(message_data);
40 char* buff = static_cast<char*>(buff_var.Map());
41 for (size_t i=0; i < buff_var.ByteLength(); ++i)
42 printf("%d ", int(buff[i]));
43 printf("\n");
33 } 44 }
34 } 45 }
35 46
36 // This object is the global object representing this plugin library as long 47 // This object is the global object representing this plugin library as long
37 // as it is loaded. 48 // as it is loaded.
38 class MyModule : public pp::Module { 49 class MyModule : public pp::Module {
39 public: 50 public:
40 MyModule() : pp::Module() {} 51 MyModule() : pp::Module() {}
41 virtual ~MyModule() {} 52 virtual ~MyModule() {}
42 53
43 // Override CreateInstance to create your customized Instance object. 54 // Override CreateInstance to create your customized Instance object.
44 virtual pp::Instance* CreateInstance(PP_Instance instance) { 55 virtual pp::Instance* CreateInstance(PP_Instance instance) {
45 return new MyInstance(instance); 56 return new MyInstance(instance);
46 } 57 }
47 }; 58 };
48 59
49 namespace pp { 60 namespace pp {
50 61
51 // Factory function for your specialization of the Module object. 62 // Factory function for your specialization of the Module object.
52 Module* CreateModule() { 63 Module* CreateModule() {
53 return new MyModule(); 64 return new MyModule();
54 } 65 }
55 66
56 } // namespace pp 67 } // namespace pp
57 68
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698