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

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

Merge icedtea-web v1.3 to trunk.

File size: 10.5 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//long long max ~ 19 chars + terminator
100//leave some room for converting strings like "<var> = %d"
101const size_t NUM_STR_BUFFER_SIZE = 32;
102
103/*
104 * This struct holds data specific to a Java operation requested by the plugin
105 */
106typedef struct java_result_data
107{
108
109 // Return identifier (if applicable)
110 int return_identifier;
111
112 // Return string (if applicable)
113 std::string* return_string;
114
115 // Return wide/mb string (if applicable)
116 std::wstring* return_wstring;
117
118 // Error message (if an error occurred)
119 std::string* error_msg;
120
121 // Boolean indicating if an error occurred
122 bool error_occurred;
123
124} JavaResultData;
125
126/**
127 * This struct holds data to do calls that need to be run in the plugin thread
128 */
129typedef struct plugin_thread_call
130{
131 // The plugin instance
132 NPP instance;
133
134 // The function to call
135 void (*func) (void *);
136
137 // The data to pass to the function
138 void *userData;
139
140} PluginThreadCall;
141
142/**
143 * Data structure passed to functions called in a new thread.
144 */
145
146typedef struct async_call_thread_data
147{
148 std::vector<void*> parameters;
149 std::string result;
150 bool result_ready;
151 bool call_successful;
152} AsyncCallThreadData;
153
154/*
155 * Misc. utility functions
156 *
157 * This class is never instantiated and should contain static functions only
158 */
159
160/* Function to process all pending async calls */
161void processAsyncCallQueue(void*);
162
163class IcedTeaPluginUtilities
164{
165
166 private:
167 static int reference; /* Reference count */
168
169 /* Mutex lock for updating reference count */
170 static pthread_mutex_t reference_mutex;
171
172 /* Map holding window pointer<->instance relationships */
173 static std::map<void*, NPP>* instance_map;
174
175 /* Map holding java-side-obj-key->NPObject relationship */
176 static std::map<std::string, NPObject*>* object_map;
177
178 /* Posts a call in the async call queue */
179 static bool postPluginThreadAsyncCall(NPP instance, void (*func) (void *), void* data);
180
181 public:
182
183 /* Constructs message prefix with given context */
184 static void constructMessagePrefix(int context,
185 std::string* result);
186
187 /* Constructs message prefix with given context and reference */
188 static void constructMessagePrefix(int context, int reference,
189 std::string* result);
190
191 /* Constructs message prefix with given context, reference and src */
192 static void constructMessagePrefix(int context, int reference,
193 std::string address,
194 std::string* result);
195
196 /* Converts given pointer to a string representation */
197 static void JSIDToString(void* id, std::string* result);
198
199 /* Converts the given string representation to a pointer */
200 static void* stringToJSID(std::string id_str);
201 static void* stringToJSID(std::string* id_str);
202
203 /* Increments reference count and returns it */
204 static int getReference();
205
206 /* Decrements reference count */
207 static void releaseReference();
208
209 /* Converts the given integer to a string */
210 static void itoa(int i, std::string* result);
211
212 /* Copies a variant data type into a C++ string */
213 static std::string NPVariantAsString(NPVariant variant);
214
215 /* Frees the given vector and the strings that its contents point to */
216 static void freeStringPtrVector(std::vector<std::string*>* v);
217
218 /* Splits the given string based on the delimiter provided */
219 static std::vector<std::string*>* strSplit(const char* str,
220 const char* delim);
221
222 /* Converts given unicode integer byte array to UTF8 string */
223 static void getUTF8String(int length, int begin,
224 std::vector<std::string*>* unicode_byte_array,
225 std::string* result_unicode_str);
226
227 /* Converts given UTF8 string to unicode integer byte array */
228 static void convertStringToUTF8(std::string* str,
229 std::string* utf_str);
230
231 /* Converts given unicode integer byte array to UTF16LE/UCS-2 string */
232 static void getUTF16LEString(int length, int begin,
233 std::vector<std::string*>* unicode_byte_array,
234 std::wstring* result_unicode_str);
235
236 /* Prints contents of given string vector */
237 static void printStringVector(const char* prefix, std::vector<std::string>* cv);
238
239 /* Prints contents of given string pointer vector */
240 static void printStringPtrVector(const char* prefix, std::vector<std::string*>* cv);
241
242 static std::string* variantToClassName(NPVariant variant);
243
244 static void printNPVariant(NPVariant variant);
245
246 static void NPVariantToString(NPVariant variant, std::string* result);
247
248 static bool javaResultToNPVariant(NPP instance,
249 std::string* java_result,
250 NPVariant* variant);
251
252 static const char* getSourceFromInstance(NPP instance);
253
254 static void storeInstanceID(void* member_ptr, NPP instance);
255
256 static void removeInstanceID(void* member_ptr);
257
258 static NPP getInstanceFromMemberPtr(void* member_ptr);
259
260 static NPObject* getNPObjectFromJavaKey(std::string key);
261
262 static void storeObjectMapping(std::string key, NPObject* object);
263
264 static void removeObjectMapping(std::string key);
265
266 static void invalidateInstance(NPP instance);
267
268 static bool isObjectJSArray(NPP instance, NPObject* object);
269
270 static void decodeURL(const char* url, char** decoded_url);
271
272 /* Posts call in async queue and waits till execution completes */
273 static void callAndWaitForResult(NPP instance, void (*func) (void *), AsyncCallThreadData* data);
274};
275
276/*
277 * A bus subscriber interface. Implementors must implement the newMessageOnBus
278 * method.
279 */
280class BusSubscriber
281{
282 private:
283
284 public:
285 BusSubscriber() {}
286
287 /* Notifies this subscriber that a new message as arrived */
288 virtual bool newMessageOnBus(const char* message) = 0;
289};
290
291/*
292 * This implementation is very simple and is therefore folded into this file
293 * rather than a new one.
294 */
295class JavaMessageSender : public BusSubscriber
296{
297 private:
298 public:
299
300 /* Sends given message to Java side */
301 virtual bool newMessageOnBus(const char* message);
302};
303
304/*
305 * Represents a message bus.
306 * The bus can also have subscribers who are notified when a new message
307 * arrives.
308 */
309class MessageBus
310{
311 private:
312 /* Mutex for locking the message queue */
313 pthread_mutex_t msg_queue_mutex;
314
315 /* Mutex used when adjusting subscriber list */
316 pthread_mutex_t subscriber_mutex;
317
318 /* Subscriber list */
319 std::list<BusSubscriber*> subscribers;
320
321 /* Queued messages */
322 std::queue<char*> msgQueue;
323
324 public:
325 MessageBus();
326
327 ~MessageBus();
328
329 /* subscribe to this bus */
330 void subscribe(BusSubscriber* b);
331
332 /* unsubscribe from this bus */
333 void unSubscribe(BusSubscriber* b);
334
335 /* Post a message on to the bus (it is safe to free the message pointer
336 after this function returns) */
337 void post(const char* message);
338};
339
340#endif // __ICEDTEAPLUGINUTILS_H__
Note: See TracBrowser for help on using the repository browser.