2 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * Post-message based test for testing crash detection.
16 #include <sys/fcntl.h>
20 #include "ppapi/cpp/instance.h"
21 #include "ppapi/cpp/module.h"
22 #include "ppapi/cpp/var.h"
24 void Initialize(const pp::Var
& message_data
, std::string
* out
) {
28 void RunExit0(const pp::Var
& message_data
, std::string
* out
) {
29 *out
= "good bye cruel world";
30 // the out string should not actually get sent back in reply, since
31 // we exit immediately.
35 void RunExit7(const pp::Var
& message_data
, std::string
* out
) {
36 *out
= "good bye cruel world";
37 // the out string should not actually get sent back in reply, since
38 // we exit immediately.
42 void RunExit254(const pp::Var
& message_data
, std::string
* out
) {
43 *out
= "good bye cruel world";
44 // the out string should not actually get sent back in reply, since
45 // we exit immediately.
49 void RunExitNeg2(const pp::Var
& message_data
, std::string
* out
) {
50 *out
= "good bye cruel world";
51 // the out string should not actually get sent back in reply, since
52 // we exit immediately.
56 struct PostMessageHandlerDesc
{
58 void (*handler
)(const pp::Var
& message_data
, std::string
* out
);
61 // This object represents one time the page says <embed>.
62 class MyInstance
: public pp::Instance
{
64 explicit MyInstance(PP_Instance instance
) : pp::Instance(instance
) {}
65 virtual ~MyInstance() {}
66 virtual void HandleMessage(const pp::Var
& message_data
);
69 // HandleMessage gets invoked when postMessage is called on the DOM
70 // element associated with this plugin instance. In this case, if we
71 // are given a string, we'll post a message back to JavaScript with a
72 // reply string -- essentially treating this as a string-based RPC.
73 void MyInstance::HandleMessage(const pp::Var
& message_data
) {
74 static struct PostMessageHandlerDesc kMsgHandlers
[] = {
75 { "init", Initialize
},
76 { "exit0", RunExit0
},
77 { "exit7", RunExit7
},
78 { "exit254", RunExit254
},
79 { "exitneg2", RunExitNeg2
},
80 { reinterpret_cast<char const *>(NULL
),
81 reinterpret_cast<void (*)(const pp::Var
&, std::string
*)>(NULL
) }
84 if (message_data
.is_string()) {
85 std::string
op_name(message_data
.AsString());
88 fprintf(stderr
, "Searching for handler for request \"%s\".\n",
93 for (size_t ix
= 0; kMsgHandlers
[ix
].request
!= NULL
; ++ix
) {
94 if (op_name
== kMsgHandlers
[ix
].request
) {
95 fprintf(stderr
, "found at index %u\n", ix
);
96 kMsgHandlers
[ix
].handler(message_data
, &sb
);
101 len
= strlen(sb
.c_str());
102 fprintf(stderr
, "posting reply len %d\n", len
);
103 fprintf(stderr
, "posting reply \"%s\".\n", sb
.c_str());
106 PostMessage(pp::Var(sb
));
107 fprintf(stderr
, "returning\n");
112 // This object is the global object representing this plugin library as long
114 class MyModule
: public pp::Module
{
116 MyModule() : pp::Module() {}
117 virtual ~MyModule() {}
119 // Override CreateInstance to create your customized Instance object.
120 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
121 return new MyInstance(instance
);
127 // Factory function for your specialization of the Module object.
128 Module
* CreateModule() {
129 printf("hello world from CreateModule\n"); fflush(NULL
);
130 return new MyModule();