OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2008 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can | |
4 * be found in the LICENSE file. | |
5 */ | |
6 | |
7 | |
8 #include <signal.h> | |
9 #include <string.h> | |
10 | |
11 #include "native_client/src/shared/platform/nacl_sync.h" | |
12 #include "native_client/src/shared/platform/nacl_threads.h" | |
13 | |
14 #include "native_client/src/trusted/desc/nacl_desc_imc.h" | |
15 | |
16 #include "native_client/src/trusted/plugin/srpc/connected_socket.h" | |
17 #include "native_client/src/trusted/plugin/srpc/plugin.h" | |
18 #include "native_client/src/trusted/plugin/srpc/srpc_client.h" | |
19 #include "native_client/src/trusted/plugin/srpc/utility.h" | |
20 #include "native_client/src/trusted/plugin/npapi/video.h" | |
21 | |
22 namespace { | |
23 | |
24 PLUGIN_JMPBUF socket_env; | |
25 | |
26 void SignalHandler(int value) { | |
27 PLUGIN_PRINTF(("ConnectedSocket::SignalHandler()\n")); | |
28 | |
29 PLUGIN_LONGJMP(socket_env, value); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 namespace plugin { | |
35 | |
36 // ConnectedSocket implements a method for each method exported from | |
37 // the NaCl service runtime | |
38 bool ConnectedSocket::InvokeEx(uintptr_t method_id, | |
39 CallType call_type, | |
40 SrpcParams *params) { | |
41 // All ConnectedSocket does for dynamic calls | |
42 // is forward it to the SrpcClient object | |
43 PLUGIN_PRINTF(("ConnectedSocket::InvokeEx()\n")); | |
44 if (srpc_client_) | |
45 return srpc_client_->Invoke(method_id, params); | |
46 return PortableHandle::InvokeEx(method_id, call_type, params); | |
47 } | |
48 | |
49 bool ConnectedSocket::HasMethodEx(uintptr_t method_id, CallType call_type) { | |
50 if (srpc_client_ && (METHOD_CALL == call_type)) | |
51 return srpc_client_->HasMethod(method_id); | |
52 return PortableHandle::HasMethodEx(method_id, call_type); | |
53 } | |
54 | |
55 bool ConnectedSocket::InitParamsEx(uintptr_t method_id, | |
56 CallType call_type, | |
57 SrpcParams *params) { | |
58 UNREFERENCED_PARAMETER(call_type); | |
59 if (srpc_client_) { | |
60 return srpc_client_->InitParams(method_id, params); | |
61 } | |
62 return false; | |
63 } | |
64 | |
65 void ConnectedSocket::StartJSObjectProxy(Plugin* plugin) { | |
66 if (srpc_client_) { | |
67 srpc_client_->StartJSObjectProxy(plugin); | |
68 } | |
69 } | |
70 | |
71 ConnectedSocket* ConnectedSocket::New(Plugin* plugin, | |
72 nacl::DescWrapper* desc) { | |
73 PLUGIN_PRINTF(("ConnectedSocket::New()\n")); | |
74 | |
75 ConnectedSocket* connected_socket = new(std::nothrow) ConnectedSocket(); | |
76 | |
77 if (connected_socket == NULL || | |
78 !connected_socket->Init(plugin, desc)) { | |
79 // Ok to delete if NULL. | |
80 delete connected_socket; | |
81 return NULL; | |
82 } | |
83 | |
84 return connected_socket; | |
85 } | |
86 | |
87 bool ConnectedSocket::Init(Plugin* plugin, | |
88 nacl::DescWrapper* wrapper) { | |
89 // TODO(sehr): this lock seems like it should be movable to PluginNpapi. | |
90 VideoScopedGlobalLock video_lock; | |
91 | |
92 if (!DescBasedHandle::Init(plugin, wrapper)) { | |
93 PLUGIN_PRINTF(("ConnectedSocket::Init - DescBasedHandle::Init failed\n")); | |
94 return false; | |
95 } | |
96 | |
97 PLUGIN_PRINTF(("ConnectedSocket::Init(%p, %p)\n", | |
98 static_cast<void*>(plugin), | |
99 static_cast<void*>(wrapper))); | |
100 | |
101 // Get SRPC client interface going over socket. Only the JavaScript main | |
102 // channel may use proxied NPAPI (not the command channels). | |
103 srpc_client_ = new(std::nothrow) SrpcClient(); | |
104 if (NULL == srpc_client_) { | |
105 // Return an error. | |
106 // TODO(sehr): make sure that clients check for this as well. | |
107 // BUG: This leaks socket. | |
108 PLUGIN_PRINTF(("ConnectedSocket::Init -- new failed.\n")); | |
109 return false; | |
110 } | |
111 if (!srpc_client_->Init(browser_interface(), this)) { | |
112 delete srpc_client_; | |
113 srpc_client_ = NULL; | |
114 // BUG: This leaks socket. | |
115 PLUGIN_PRINTF(("ConnectedSocket::Init -- SrpcClient::Init failed.\n")); | |
116 return false; | |
117 } | |
118 return true; | |
119 } | |
120 | |
121 ConnectedSocket::ConnectedSocket() | |
122 : srpc_client_(NULL) { | |
123 PLUGIN_PRINTF(("ConnectedSocket::ConnectedSocket(%p)\n", | |
124 static_cast<void*>(this))); | |
125 } | |
126 | |
127 ConnectedSocket::~ConnectedSocket() { | |
128 PLUGIN_PRINTF(("ConnectedSocket::~ConnectedSocket(%p)\n", | |
129 static_cast<void*>(this))); | |
130 | |
131 // Free the SRPC connection. | |
132 delete srpc_client_; | |
133 } | |
134 | |
135 } // namespace plugin | |
OLD | NEW |