- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / nacl / exit_status / pm_exit_status_test.cc
1 /*
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.
5  */
6
7 /*
8  * Post-message based test for testing crash detection.
9  */
10 #include <string>
11
12 #include <assert.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <inttypes.h>
16 #include <sys/fcntl.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "ppapi/cpp/instance.h"
21 #include "ppapi/cpp/module.h"
22 #include "ppapi/cpp/var.h"
23
24 void Initialize(const pp::Var& message_data, std::string* out) {
25   *out = "hello world";
26 }
27
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.
32   exit(0);
33 }
34
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.
39   exit(7);
40 }
41
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.
46   exit(254);
47 }
48
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.
53   exit(-2);
54 }
55
56 struct PostMessageHandlerDesc {
57   char const *request;
58   void (*handler)(const pp::Var& message_data, std::string* out);
59 };
60
61 // This object represents one time the page says <embed>.
62 class MyInstance : public pp::Instance {
63  public:
64   explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {}
65   virtual ~MyInstance() {}
66   virtual void HandleMessage(const pp::Var& message_data);
67 };
68
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) }
82   };
83
84   if (message_data.is_string()) {
85     std::string op_name(message_data.AsString());
86     size_t len;
87
88     fprintf(stderr, "Searching for handler for request \"%s\".\n",
89             op_name.c_str());
90
91     std::string sb;
92
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);
97         break;
98       }
99     }
100
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());
104     fflush(stderr);
105
106     PostMessage(pp::Var(sb));
107     fprintf(stderr, "returning\n");
108     fflush(stderr);
109   }
110 }
111
112 // This object is the global object representing this plugin library as long
113 // as it is loaded.
114 class MyModule : public pp::Module {
115  public:
116   MyModule() : pp::Module() {}
117   virtual ~MyModule() {}
118
119   // Override CreateInstance to create your customized Instance object.
120   virtual pp::Instance* CreateInstance(PP_Instance instance) {
121     return new MyInstance(instance);
122   }
123 };
124
125 namespace pp {
126
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();
131 }
132
133 }  // namespace pp