2 * Copyright 2014 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 simple rpc based access to name services.
17 #include <sys/fcntl.h>
22 #include "native_client/src/include/nacl_base.h"
23 #include "native_client/src/public/imc_syscalls.h"
24 #include "native_client/src/public/name_service.h"
25 #include "native_client/src/shared/platform/nacl_sync.h"
26 #include "native_client/src/shared/platform/nacl_sync_checked.h"
27 #include "native_client/src/shared/platform/nacl_sync_raii.h"
28 #include "native_client/src/shared/srpc/nacl_srpc.h"
30 // TODO(bsy): move weak_ref module to the shared directory
31 #include "native_client/src/trusted/weak_ref/weak_ref.h"
33 #include "ppapi/cpp/instance.h"
34 #include "ppapi/cpp/module.h"
35 #include "ppapi/cpp/var.h"
37 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
38 #include "ppapi/native_client/src/trusted/weak_ref/call_on_main_thread.h"
39 #include "ppapi/native_client/src/untrusted/nacl_ppapi_util/nacl_ppapi_util.h"
40 #include "ppapi/native_client/src/untrusted/nacl_ppapi_util/string_buffer.h"
42 std::string *manifest_contents = NULL;
44 void TestManifestContents() {
45 nacl::StringBuffer sb;
48 struct NaClSrpcChannel manifest_channel;
49 struct NaClSrpcChannel ns_channel;
52 nacl_nameservice(&ns);
53 printf("ns = %d\n", ns);
55 int connected_socket = imc_connect(ns);
56 assert(-1 != connected_socket);
57 if (!NaClSrpcClientCtor(&ns_channel, connected_socket)) {
59 sb.Printf("Srpc client channel ctor failed\n");
60 manifest_contents = new std::string(sb.ToString());
63 sb.Printf("NaClSrpcClientCtor succeeded\n");
65 // name service lookup for the manifest service descriptor
66 if (NACL_SRPC_RESULT_OK !=
67 NaClSrpcInvokeBySignature(&ns_channel, NACL_NAME_SERVICE_LOOKUP,
68 "ManifestNameService", O_RDWR,
69 &status, &manifest) ||
70 NACL_NAME_SERVICE_SUCCESS != status) {
71 sb.Printf("nameservice lookup failed, status %d\n", status);
72 manifest_contents = new std::string(sb.ToString());
75 sb.Printf("Got manifest descriptor %d\n", manifest);
77 manifest_contents = new std::string(sb.ToString());
81 // connect to manifest name server
82 int manifest_conn = imc_connect(manifest);
84 sb.Printf("got manifest connection %d\n", manifest_conn);
85 if (-1 == manifest_conn) {
86 sb.Printf("could not connect\n");
87 manifest_contents = new std::string(sb.ToString());
91 // build the SRPC connection (do service discovery)
92 if (!NaClSrpcClientCtor(&manifest_channel, manifest_conn)) {
93 sb.Printf("could not build srpc client\n");
94 manifest_contents = new std::string(sb.ToString());
100 sb.Printf("Invoking name service lookup\n");
101 if (NACL_SRPC_RESULT_OK !=
102 NaClSrpcInvokeBySignature(&manifest_channel,
103 NACL_NAME_SERVICE_LOOKUP,
104 "files/test_file", O_RDONLY,
106 sb.Printf("manifest lookup RPC failed\n");
107 NaClSrpcDtor(&manifest_channel);
108 manifest_contents = new std::string(sb.ToString());
113 sb.Printf("File Contents:\n");
117 while ((len = read(desc, buffer, sizeof buffer - 1)) > 0) {
120 sb.Printf("%s", buffer);
124 sb.Printf("Opening non-existent file:\n");
125 if (NACL_SRPC_RESULT_OK !=
126 NaClSrpcInvokeBySignature(&manifest_channel,
127 NACL_NAME_SERVICE_LOOKUP,
128 "foobar/baz", O_RDONLY,
130 sb.Printf("bogus manifest lookup RPC failed\n");
131 NaClSrpcDtor(&manifest_channel);
132 manifest_contents = new std::string(sb.ToString());
135 sb.Printf("Got descriptor %d, status %d\n", desc, status);
140 NaClSrpcDtor(&manifest_channel);
141 manifest_contents = new std::string(sb.ToString());
144 class PostStringMessageWrapper
145 : public nacl_ppapi::EventThreadWorkStateWrapper<nacl_ppapi::VoidResult> {
147 PostStringMessageWrapper(nacl_ppapi::EventThreadWorkState<
148 nacl_ppapi::VoidResult>
150 const std::string &msg)
151 : nacl_ppapi::EventThreadWorkStateWrapper<nacl_ppapi::VoidResult>(
154 ~PostStringMessageWrapper();
155 const std::string &msg() const { return msg_; }
159 DISALLOW_COPY_AND_ASSIGN(PostStringMessageWrapper);
162 // ---------------------------------------------------------------------------
166 // This object represents one time the page says <embed>.
167 class MyInstance : public nacl_ppapi::NaClPpapiPluginInstance {
169 explicit MyInstance(PP_Instance instance);
170 virtual ~MyInstance();
171 virtual void HandleMessage(const pp::Var& message_data);
174 DISALLOW_COPY_AND_ASSIGN(MyInstance);
177 // ---------------------------------------------------------------------------
179 MyInstance::MyInstance(PP_Instance instance)
180 : nacl_ppapi::NaClPpapiPluginInstance(instance) {
183 MyInstance::~MyInstance() {}
185 // ---------------------------------------------------------------------------
187 // HandleMessage gets invoked when postMessage is called on the DOM
188 // element associated with this plugin instance. In this case, if we
189 // are given a string, we'll post a message back to JavaScript with a
190 // reply -- essentially treating this as a string-based RPC.
191 void MyInstance::HandleMessage(const pp::Var& message) {
192 if (message.is_string()) {
193 if (message.AsString() == "manifest_data") {
194 PostMessage(*manifest_contents);
196 fprintf(stderr, "HandleMessage: Unrecognized request \"%s\".\n",
197 message.AsString().c_str());
200 fprintf(stderr, "HandleMessage: message is not a string\n");
205 // This object is the global object representing this plugin library as long
207 class MyModule : public pp::Module {
209 MyModule() : pp::Module() {}
210 virtual ~MyModule() {}
212 // Override CreateInstance to create your customized Instance object.
213 virtual pp::Instance *CreateInstance(PP_Instance instance);
215 DISALLOW_COPY_AND_ASSIGN(MyModule);
218 pp::Instance *MyModule::CreateInstance(PP_Instance pp_instance) {
219 MyInstance *instance = new MyInstance(pp_instance);
220 fprintf(stderr, "CreateInstance: returning instance %p\n",
221 reinterpret_cast<void *>(instance));
228 // Factory function for your specialization of the Module object.
229 Module* CreateModule() {
230 fprintf(stderr, "CreateModule invoked\n"); fflush(NULL);
231 return new MyModule();
237 NaClSrpcModuleInit();
238 TestManifestContents();
239 return PpapiPluginMain();