Add Python REPL that works inside the browser
[nativeclient.git] / npapi_plugin / npapi_bridge / npmodule.h
blob6a38c619ea7db8bfeaefed4e2c85f93b85589cc5
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // NaCl-NPAPI Interface
35 #ifndef NATIVE_CLIENT_NPAPI_PLUGIN_NPAPI_BRIDGE_NPMODULE_H_
36 #define NATIVE_CLIENT_NPAPI_PLUGIN_NPAPI_BRIDGE_NPMODULE_H_
38 #if !NACL_WINDOWS
39 #include <pthread.h>
40 #endif
41 #include <string.h>
42 #include <stdlib.h>
44 #if NACL_LINUX && defined(MOZ_X11)
45 #include <X11/Xlib.h>
46 #include <X11/Intrinsic.h>
47 #endif // NACL_LINUX && defined(MOZ_X11)
49 #include <string>
51 #include "native_client/intermodule_comm/nacl_imc.h"
52 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
53 #include "native_client/npapi_plugin/npinstance.h"
54 #include "native_client/tools/npapi_runtime/nacl_npapi.h"
55 #include "native_client/tools/npapi_runtime/npbridge.h"
56 #include "native_client/tools/npapi_runtime/nprpc.h"
58 namespace nacl {
60 // Represents the plugin end of the connection to the NaCl module. The opposite
61 // end is the NPNavigator.
62 class NPModule : public NPBridge, public NPInstance {
63 // The child process that executes the NaCl module program.
64 SelLdrLauncher* subprocess_;
65 // The child process handle.
66 Handle child_;
68 // The proxy NPObject that represents the plugin's scriptable object.
69 NPObjectProxy* proxy_;
71 // The URL origin of the HTML page that started this module.
72 std::string origin_;
74 // The name of the file currently being loaded by NPN_GetURLNotify().
75 char* filename_;
76 // The URL currently being loaded by NPN_GetURLNotify().
77 char* url_;
78 // The file handle of the file currently being loaded by NPN_GetURLNotify().
79 Handle stream_;
81 // The number of arguments to pass to the child process.
82 int argc_;
83 // The array of arguments to pass to the child process.
84 char const* argv_[kMaxArg];
86 // The thread that monitors the NPAPI runtime behavior.
87 #if NACL_WINDOWS
88 HANDLE monitor_thread_;
89 #else
90 pthread_t monitor_thread_;
91 bool monitor_thread_started_;
92 // TODO: investigate a cleaner solution than this.
93 #endif
95 // The NPWindow of this plugin instance.
96 NPWindow* window_;
97 // The shared memory object handle that keeps the window content rendered by
98 // the child process.
99 HtpHandle bitmap_shm_;
100 // The start address of bitmap_shm_ mapped in the browser's address space.
101 void* bitmap_data_;
102 // The size of bitmap_shm_.
103 size_t bitmap_size_;
104 bool origin_valid_;
106 #if NACL_WINDOWS
107 // The original window procedure for the NPWindow.
108 WNDPROC original_window_procedure_;
109 // The window procedure that intercepts messages sent to the NPWindow.
110 static LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
111 #endif
112 #if NACL_LINUX && defined(MOZ_X11)
113 // The event handler that processes events sent to the NPWindow.
114 static void EventHandler(Widget widget, NPModule* module,
115 XEvent* xevent, Boolean* b);
116 #endif
118 // Redraws the NPWindow with bitmap_data_.
119 void Redraw();
121 public:
122 // Creates a new instance of NPModule. All parameters should be unmodified
123 // variables from NPP_New().
124 explicit NPModule(NPP npp, int argc, char* argn[], char* argv[]);
125 ~NPModule();
127 const char* GetApplicationName() const {
128 if (!subprocess_) {
129 return NULL;
131 return subprocess_->GetApplicationName();
134 Handle child() const {
135 return child_;
138 const char* filename() const {
139 return filename_;
141 void set_filename(const char* filename) {
142 if (filename_ != NULL) {
143 free(filename_);
144 filename_ = NULL;
146 if (filename) {
147 #if NACL_WINDOWS
148 filename_ = _strdup(filename);
149 #else
150 filename_ = strdup(filename);
151 #endif // NACL_WINDOWS
155 const char* url() const {
156 return url_;
158 void set_url(const char* url) {
159 if (url_ != NULL) {
160 free(url_);
161 url_ = NULL;
163 if (url) {
164 #if NACL_WINDOWS
165 url_ = _strdup(url);
166 #else
167 url_ = strdup(url);
168 #endif // NACL_WINDOWS
172 #if !NACL_WINDOWS
173 bool monitor_thread_started() {
174 return monitor_thread_started_;
176 void set_monitor_thread_started(bool mon_thread_started) {
177 monitor_thread_started_ = mon_thread_started;
179 #endif // !NACL_WINDOWS
181 // Spawns a child process that executes the binary specified by
182 // application_name. Start returns the child process handle.
183 Handle Start(const char* application_name);
185 // Dispatches the received request message.
186 virtual int Dispatch(RpcHeader* request, int length);
188 // Processes NPN_GetValue() request from the child process.
189 int GetValue(RpcHeader* request, int length, NPNVariable variable);
190 // Processes NPN_Status() request from the child process.
191 int SetStatus(RpcHeader* request, int length);
192 // Processes NPN_InvalidateRect() request from the child process.
193 int InvalidateRect(RpcHeader* request, int length);
194 // Processes NPN_ForceRedraw() request from the child process.
195 int ForceRedraw(RpcHeader* request, int length);
196 // Processes NaClNPN_CreateArray() request from the child process.
197 int CreateArray(RpcHeader* request, int length);
198 // Processes NaClNPN_OpenURL() request from the child process.
199 int OpenURL(RpcHeader* request, int length);
201 // Invokes NPP_New() in the child process.
202 NPError New();
205 // NPInstance methods
208 // Processes NPP_Destroy() invocation from the browser.
209 NPError Destroy(NPSavedData** save);
210 // Processes NPP_SetWindow() invocation from the browser.
211 NPError SetWindow(NPWindow* window);
212 // Processes NPP_GetValue() invocation from the browser.
213 NPError GetValue(NPPVariable variable, void *value);
214 // Processes NPP_HandleEvent() invocation from the browser.
215 int16_t HandleEvent(void* event);
216 // Processes NPP_GetScriptableInstance() invocation from the browser.
217 NPObject* GetScriptableInstance();
218 // Processes NPP_NewStream() invocation from the browser.
219 NPError NewStream(NPMIMEType type,
220 NPStream* stream, NPBool seekable,
221 uint16_t* stype);
222 // Processes NPP_StreamAsFile() invocation from the browser.
223 void StreamAsFile(NPStream* stream, const char* filename);
224 // Processes NPP_DestroyStream() invocation from the browser.
225 NPError DestroyStream(NPStream *stream, NPError reason);
226 // Processes NPP_URLNotify() invocation from the browser.
227 void URLNotify(const char* url, NPReason reason, void* notify_data);
229 // The timeout value in seconds to pop up a message box to terminate the
230 // non-responding child process.
231 static const int kTimeout = 5;
232 // sel_ldr instances initially communicate with the browser over channel 5.
233 // After the requisite connects/accepts have been done, this channel is
234 // no longer used.
235 static const int kStartupChannel = 5;
238 } // namespace nacl
240 #endif // NATIVE_CLIENT_NPAPI_PLUGIN_NPAPI_BRIDGE_NPMODULE_H_