source: trunk/icedtea-web/plugin/icedteanp/IcedTeaPluginUtils.h@ 384

Last change on this file since 384 was 375, checked in by dmik, 13 years ago

icedtea-web: Remove IcedTeaNPPlugin.h dependency in IcedTeaPluginUtils.h.

This allows to include IcedTeaPluginUtils.h alone (needed for PLUGIN_DEBUG).

File size: 10.3 KB
Line 
1/* IcedTeaPluginUtils.h
2
3 Copyright (C) 2009, 2010 Red Hat
4
5This file is part of IcedTea.
6
7IcedTea is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12IcedTea is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with IcedTea; see the file COPYING. If not, write to the
19Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301 USA.
21
22Linking this library statically or dynamically with other modules is
23making a combined work based on this library. Thus, the terms and
24conditions of the GNU General Public License cover the whole
25combination.
26
27As a special exception, the copyright holders of this library give you
28permission to link this library with independent modules to produce an
29executable, regardless of the license terms of these independent
30modules, and to copy and distribute the resulting executable under
31terms of your choice, provided that you also meet, for each linked
32independent module, the terms and conditions of the license of that
33module. An independent module is a module which is not derived from
34or based on this library. If you modify this library, you may extend
35this exception to your version of the library, but you are not
36obligated to do so. If you do not wish to do so, delete this
37exception statement from your version. */
38
39/**
40 * Utility classes for the IcedTeaPlugin
41 */
42
43#ifndef __ICEDTEAPLUGINUTILS_H__
44#define __ICEDTEAPLUGINUTILS_H__
45
46#include <pthread.h>
47#include <stdio.h>
48
49#include <cstring>
50#include <iostream>
51#include <list>
52#include <map>
53#include <queue>
54#include <sstream>
55#include <string>
56#include <vector>
57
58#include <npapi.h>
59
60#if MOZILLA_VERSION_COLLAPSED < 1090100
61#include <npupp.h>
62#else
63#include <npapi.h>
64#include <npruntime.h>
65#endif
66
67extern int plugin_debug; // defined in IcedTeaNPPlugin.cc
68
69#define PLUGIN_DEBUG(...) \
70 do \
71 { \
72 if (plugin_debug) \
73 { \
74 fprintf (stderr, "ITNPP Thread# %ld: ", pthread_self()); \
75 fprintf (stderr, __VA_ARGS__); \
76 } \
77 } while (0)
78
79#define CHECK_JAVA_RESULT(result_data) \
80{ \
81 if (((JavaResultData*) result_data)->error_occurred) \
82 { \
83 printf("Error: Error occurred on Java side: %s.\n", \
84 ((JavaResultData*) result_data)->error_msg->c_str()); \
85 return; \
86 } \
87}
88
89#define HEX_TO_INT(c) \
90 ((*c >= 'a') ? *c - 'a' + 10 : \
91 (*c >= 'A') ? *c - 'A' + 10 : \
92 *c - '0')
93
94#define IS_VALID_HEX(c) \
95 ((*c >= '0' && *c <= '9') || \
96 (*c >= 'a' && *c <= 'f') || \
97 (*c >= 'A' && *c <= 'F'))
98
99/*
100 * This struct holds data specific to a Java operation requested by the plugin
101 */
102typedef struct java_result_data
103{
104
105 // Return identifier (if applicable)
106 int return_identifier;
107
108 // Return string (if applicable)
109 std::string* return_string;
110
111 // Return wide/mb string (if applicable)
112 std::wstring* return_wstring;
113
114 // Error message (if an error occurred)
115 std::string* error_msg;
116
117 // Boolean indicating if an error occurred
118 bool error_occurred;
119
120} JavaResultData;
121
122/**
123 * This struct holds data to do calls that need to be run in the plugin thread
124 */
125typedef struct plugin_thread_call
126{
127 // The plugin instance
128 NPP instance;
129
130 // The function to call
131 void (*func) (void *);
132
133 // The data to pass to the function
134 void *userData;
135
136} PluginThreadCall;
137
138/**
139 * Data structure passed to functions called in a new thread.
140 */
141
142typedef struct async_call_thread_data
143{
144 std::vector<void*> parameters;
145 std::string result;
146 bool result_ready;
147 bool call_successful;
148} AsyncCallThreadData;
149
150/*
151 * Misc. utility functions
152 *
153 * This class is never instantiated and should contain static functions only
154 */
155
156/* Function to process all pending async calls */
157void processAsyncCallQueue(void*);
158
159class IcedTeaPluginUtilities
160{
161
162 private:
163 static int reference; /* Reference count */
164
165 /* Mutex lock for updating reference count */
166 static pthread_mutex_t reference_mutex;
167
168 /* Map holding window pointer<->instance relationships */
169 static std::map<void*, NPP>* instance_map;
170
171 /* Map holding java-side-obj-key->NPObject relationship */
172 static std::map<std::string, NPObject*>* object_map;
173
174 /* Posts a call in the async call queue */
175 static bool postPluginThreadAsyncCall(NPP instance, void (*func) (void *), void* data);
176
177 public:
178
179 /* Constructs message prefix with given context */
180 static void constructMessagePrefix(int context,
181 std::string* result);
182
183 /* Constructs message prefix with given context and reference */
184 static void constructMessagePrefix(int context, int reference,
185 std::string* result);
186
187 /* Constructs message prefix with given context, reference and src */
188 static void constructMessagePrefix(int context, int reference,
189 std::string address,
190 std::string* result);
191
192 /* Converts given pointer to a string representation */
193 static void JSIDToString(void* id, std::string* result);
194
195 /* Converts the given string representation to a pointer */
196 static void* stringToJSID(std::string id_str);
197 static void* stringToJSID(std::string* id_str);
198
199 /* Increments reference count and returns it */
200 static int getReference();
201
202 /* Decrements reference count */
203 static void releaseReference();
204
205 /* Converts the given integer to a string */
206 static void itoa(int i, std::string* result);
207
208 /* Frees the given vector and the strings that its contents point to */
209 static void freeStringPtrVector(std::vector<std::string*>* v);
210
211 /* Splits the given string based on the delimiter provided */
212 static std::vector<std::string*>* strSplit(const char* str,
213 const char* delim);
214
215 /* Converts given unicode integer byte array to UTF8 string */
216 static void getUTF8String(int length, int begin,
217 std::vector<std::string*>* unicode_byte_array,
218 std::string* result_unicode_str);
219
220 /* Converts given UTF8 string to unicode integer byte array */
221 static void convertStringToUTF8(std::string* str,
222 std::string* utf_str);
223
224 /* Converts given unicode integer byte array to UTF16LE/UCS-2 string */
225 static void getUTF16LEString(int length, int begin,
226 std::vector<std::string*>* unicode_byte_array,
227 std::wstring* result_unicode_str);
228
229 /* Prints contents of given string vector */
230 static void printStringVector(const char* prefix, std::vector<std::string>* cv);
231
232 /* Prints contents of given string pointer vector */
233 static void printStringPtrVector(const char* prefix, std::vector<std::string*>* cv);
234
235 static std::string* variantToClassName(NPVariant variant);
236
237 static void printNPVariant(NPVariant variant);
238
239 static void NPVariantToString(NPVariant variant, std::string* result);
240
241 static bool javaResultToNPVariant(NPP instance,
242 std::string* java_result,
243 NPVariant* variant);
244
245 static const char* getSourceFromInstance(NPP instance);
246
247 static void storeInstanceID(void* member_ptr, NPP instance);
248
249 static void removeInstanceID(void* member_ptr);
250
251 static NPP getInstanceFromMemberPtr(void* member_ptr);
252
253 static NPObject* getNPObjectFromJavaKey(std::string key);
254
255 static void storeObjectMapping(std::string key, NPObject* object);
256
257 static void removeObjectMapping(std::string key);
258
259 static void invalidateInstance(NPP instance);
260
261 static bool isObjectJSArray(NPP instance, NPObject* object);
262
263 static void decodeURL(const char* url, char** decoded_url);
264
265 /* Posts call in async queue and waits till execution completes */
266 static void callAndWaitForResult(NPP instance, void (*func) (void *), AsyncCallThreadData* data);
267};
268
269/*
270 * A bus subscriber interface. Implementors must implement the newMessageOnBus
271 * method.
272 */
273class BusSubscriber
274{
275 private:
276
277 public:
278 BusSubscriber() {}
279
280 /* Notifies this subscriber that a new message as arrived */
281 virtual bool newMessageOnBus(const char* message) = 0;
282};
283
284/*
285 * This implementation is very simple and is therefore folded into this file
286 * rather than a new one.
287 */
288class JavaMessageSender : public BusSubscriber
289{
290 private:
291 public:
292
293 /* Sends given message to Java side */
294 virtual bool newMessageOnBus(const char* message);
295};
296
297/*
298 * Represents a message bus.
299 * The bus can also have subscribers who are notified when a new message
300 * arrives.
301 */
302class MessageBus
303{
304 private:
305 /* Mutex for locking the message queue */
306 pthread_mutex_t msg_queue_mutex;
307
308 /* Mutex used when adjusting subscriber list */
309 pthread_mutex_t subscriber_mutex;
310
311 /* Subscriber list */
312 std::list<BusSubscriber*> subscribers;
313
314 /* Queued messages */
315 std::queue<char*> msgQueue;
316
317 public:
318 MessageBus();
319
320 ~MessageBus();
321
322 /* subscribe to this bus */
323 void subscribe(BusSubscriber* b);
324
325 /* unsubscribe from this bus */
326 void unSubscribe(BusSubscriber* b);
327
328 /* Post a message on to the bus (it is safe to free the message pointer
329 after this function returns) */
330 void post(const char* message);
331};
332
333#endif // __ICEDTEAPLUGINUTILS_H__
Note: See TracBrowser for help on using the repository browser.