This source file includes following definitions.
- load_manifest
- HandleMessage
- CreateInstance
- CreateModule
- main
#include <stdio.h>
#include <string.h>
#include <string>
#include <sstream>
#include "native_client/src/untrusted/irt/irt.h"
#include "native_client/src/untrusted/nacl/nacl_irt.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
std::string str;
void load_manifest(TYPE_nacl_irt_query *query_func) {
struct nacl_irt_resource_open nacl_irt_resource_open;
if (sizeof(nacl_irt_resource_open) !=
(*query_func)(
NACL_IRT_RESOURCE_OPEN_v0_1,
&nacl_irt_resource_open,
sizeof(nacl_irt_resource_open))) {
str = "irt manifest api not found";
return;
}
int desc;
int error;
error = nacl_irt_resource_open.open_resource("test_file", &desc);
if (0 != error) {
str = "Can't open file";
printf("Can't open file, error=%d", error);
return;
}
str = "File Contents:\n";
char buffer[4096];
int len;
while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) {
int len = strlen(buffer);
if (len >= 2 && buffer[len-1] == '\n' && buffer[len-2] == '\r') {
buffer[len-2] = '\n';
buffer[len-1] = '\0';
}
buffer[len] = 0;
str += buffer;
}
printf("file loaded: %s\n", str.c_str());
return;
}
class TestInstance : public pp::Instance {
public:
explicit TestInstance(PP_Instance instance) : pp::Instance(instance) {}
virtual ~TestInstance() {}
virtual void HandleMessage(const pp::Var& var_message) {
if (!var_message.is_string()) {
return;
}
if (var_message.AsString() != "hello") {
return;
}
pp::Var reply = pp::Var(str);
PostMessage(reply);
}
};
class TestModule : public pp::Module {
public:
TestModule() : pp::Module() {}
virtual ~TestModule() {}
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new TestInstance(instance);
}
};
namespace pp {
Module* CreateModule() {
return new TestModule();
}
}
int main() {
load_manifest(&__nacl_irt_query);
return PpapiPluginMain();
}