1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/* IcedTeaPluginUtils.h
Copyright (C) 2009, 2010 Red Hat
This file is part of IcedTea.
IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
IcedTea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
/**
* Utility classes for the IcedTeaPlugin
*/
#ifndef __ICEDTEAPLUGINUTILS_H__
#define __ICEDTEAPLUGINUTILS_H__
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <npapi.h>
#include <glib.h>
#include <npruntime.h>
#include "IcedTeaParseProperties.h"
void *flush_pre_init_messages(void* data);
void push_pre_init_messages(char * ldm);
void reset_pre_init_messages();
// debugging macro.
#define initialize_debug() \
do \
{ \
if (!debug_initiated) { \
debug_initiated = true; \
plugin_debug = getenv ("ICEDTEAPLUGIN_DEBUG") != NULL || is_debug_on(); \
plugin_debug_headers = is_debug_header_on(); \
plugin_debug_to_file = is_logging_to_file(); \
plugin_debug_to_streams = is_logging_to_stds(); \
plugin_debug_to_system = is_logging_to_system(); \
plugin_debug_to_console = is_java_console_enabled(); \
if (plugin_debug_to_file) { \
IcedTeaPluginUtilities::initFileLog(); \
} \
if (plugin_debug_to_console) { \
/*initialisation done during jvm startup*/ \
} \
IcedTeaPluginUtilities::printDebugStatus(); \
} \
} while (0)
#define HEADER_SIZE 500
#define BODY_SIZE 500
#define MESSAGE_SIZE HEADER_SIZE + BODY_SIZE
#define LDEBUG_MESSAGE_SIZE MESSAGE_SIZE+50
//header is destination char array
#define CREATE_HEADER(ldebug_header) \
do \
{ \
char times[100]; \
time_t t = time(NULL); \
struct tm p; \
localtime_r(&t, &p); \
strftime(times, 100, "%a %b %d %H:%M:%S %Z %Y", &p);\
const char *userNameforDebug = (getenv("USERNAME") == NULL) ? "unknown user" : getenv("USERNAME"); \
/*this message is parsed in JavaConsole*/ \
snprintf(ldebug_header, HEADER_SIZE, "[%s][ITW-C-PLUGIN][MESSAGE_DEBUG][%s][%s:%d] ITNPP Thread# %ld, gthread %p: ", \
userNameforDebug, times, __FILE__, __LINE__, pthread_self(), g_thread_self ()); \
} while (0)
#define PLUGIN_DEBUG(...) \
do \
{ \
initialize_debug(); \
if (plugin_debug) { \
char ldebug_header[HEADER_SIZE]; \
char ldebug_body[BODY_SIZE]; \
char ldebug_message[MESSAGE_SIZE];\
if (plugin_debug_headers) { \
CREATE_HEADER(ldebug_header); \
} else { \
sprintf(ldebug_header,""); \
} \
snprintf(ldebug_body, BODY_SIZE, __VA_ARGS__); \
if (plugin_debug_to_streams) { \
snprintf(ldebug_message, MESSAGE_SIZE, "%s%s", ldebug_header, ldebug_body); \
fprintf (stdout, "%s", ldebug_message);\
} \
if (plugin_debug_to_file) { \
snprintf(ldebug_message, MESSAGE_SIZE, "%s%s", ldebug_header, ldebug_body); \
fprintf (plugin_file_log, "%s", ldebug_message); \
fflush(plugin_file_log); \
} \
if (plugin_debug_to_console) { \
/*headers are always going to console*/ \
if (!plugin_debug_headers){ \
CREATE_HEADER(ldebug_header); \
} \
snprintf(ldebug_message, MESSAGE_SIZE, "%s%s", ldebug_header, ldebug_body); \
char ldebug_channel_message[LDEBUG_MESSAGE_SIZE]; \
struct timeval current_time; \
gettimeofday (¤t_time, NULL);\
if (jvm_up) { \
snprintf(ldebug_channel_message, LDEBUG_MESSAGE_SIZE, "%s %ld %s", "plugindebug", current_time.tv_sec*1000000L+current_time.tv_usec, ldebug_message); \
push_pre_init_messages(ldebug_channel_message); \
} else { \
snprintf(ldebug_channel_message, LDEBUG_MESSAGE_SIZE, "%s %ld %s", "preinit_plugindebug", current_time.tv_sec*1000000L+current_time.tv_usec, ldebug_message); \
push_pre_init_messages(ldebug_channel_message); \
} \
} \
} \
} while (0)
#define PLUGIN_ERROR(...) \
do \
{ \
initialize_debug(); \
char ldebug_header[HEADER_SIZE]; \
char ldebug_body[BODY_SIZE]; \
char ldebug_message[MESSAGE_SIZE]; \
if (plugin_debug_headers) { \
CREATE_HEADER(ldebug_header); \
} else { \
sprintf(ldebug_header,""); \
} \
snprintf(ldebug_body, BODY_SIZE, __VA_ARGS__); \
if (plugin_debug_to_streams) { \
snprintf(ldebug_message, MESSAGE_SIZE, "%s%s", ldebug_header, ldebug_body); \
fprintf (stderr, "%s", ldebug_message); \
} \
if (plugin_debug_to_file) { \
snprintf(ldebug_message, MESSAGE_SIZE, "%s%s", ldebug_header, ldebug_body); \
fprintf (plugin_file_log, "%s", ldebug_message); \
fflush(plugin_file_log); \
} \
if (plugin_debug_to_console) { \
/*headers are always going to console*/ \
if (!plugin_debug_headers){ \
CREATE_HEADER(ldebug_header); \
} \
snprintf(ldebug_message, MESSAGE_SIZE, "%s%s", ldebug_header, ldebug_body); \
char ldebug_channel_message[LDEBUG_MESSAGE_SIZE]; \
struct timeval current_time; \
gettimeofday (¤t_time, NULL);\
if (jvm_up) { \
snprintf(ldebug_channel_message, LDEBUG_MESSAGE_SIZE, "%s %ld %s", "pluginerror", current_time.tv_sec*1000000L+current_time.tv_usec, ldebug_message); \
push_pre_init_messages(ldebug_channel_message); \
} else { \
snprintf(ldebug_channel_message, LDEBUG_MESSAGE_SIZE, "%s %ld %s", "preinit_pluginerror", current_time.tv_sec*1000000L+current_time.tv_usec, ldebug_message); \
push_pre_init_messages(ldebug_channel_message); \
} \
} \
} while (0)
#define CHECK_JAVA_RESULT(result_data) \
{ \
if (((JavaResultData*) result_data)->error_occurred) \
{ \
PLUGIN_ERROR("Error: Error occurred on Java side: %s.\n", \
((JavaResultData*) result_data)->error_msg->c_str()); \
return; \
} \
}
#define HEX_TO_INT(c) \
((*c >= 'a') ? *c - 'a' + 10 : \
(*c >= 'A') ? *c - 'A' + 10 : \
*c - '0')
#define IS_VALID_HEX(c) \
((*c >= '0' && *c <= '9') || \
(*c >= 'a' && *c <= 'f') || \
(*c >= 'A' && *c <= 'F'))
//long long max ~ 19 chars + terminator
//leave some room for converting strings like "<var> = %d"
const size_t NUM_STR_BUFFER_SIZE = 32;
/*
* This struct holds data specific to a Java operation requested by the plugin
*/
typedef struct java_result_data
{
// Return identifier (if applicable)
int return_identifier;
// Return string (if applicable)
std::string* return_string;
// Return wide/mb string (if applicable)
std::wstring* return_wstring;
// Error message (if an error occurred)
std::string* error_msg;
// Boolean indicating if an error occurred
bool error_occurred;
} JavaResultData;
/**
* This struct holds data to do calls that need to be run in the plugin thread
*/
typedef struct plugin_thread_call
{
// The plugin instance
NPP instance;
// The function to call
void (*func) (void *);
// The data to pass to the function
void *userData;
} PluginThreadCall;
/**
* Data structure passed to functions called in a new thread.
*/
typedef struct async_call_thread_data
{
std::vector<void*> parameters;
std::string result;
bool result_ready;
bool call_successful;
} AsyncCallThreadData;
/*
* Misc. utility functions
*
* This class is never instantiated and should contain static functions only
*/
/* Function to process all pending async calls */
void processAsyncCallQueue(void*);
class IcedTeaPluginUtilities
{
private:
static int reference; /* Reference count */
/* Mutex lock for updating reference count */
static pthread_mutex_t reference_mutex;
/* Map holding window pointer<->instance relationships */
static std::map<void*, NPP>* instance_map;
/* Map holding java-side-obj-key->NPObject relationship */
static std::map<std::string, NPObject*>* object_map;
/* Posts a call in the async call queue */
static bool postPluginThreadAsyncCall(NPP instance, void (*func) (void *), void* data);
public:
/* Constructs message prefix with given context */
static void constructMessagePrefix(int context,
std::string* result);
/* Constructs message prefix with given context and reference */
static void constructMessagePrefix(int context, int reference,
std::string* result);
/* Constructs message prefix with given context, reference and src */
static void constructMessagePrefix(int context, int reference,
std::string address,
std::string* result);
/* Converts given pointer to a string representation */
static void JSIDToString(void* id, std::string* result);
/* Converts the given string representation to a pointer */
static void* stringToJSID(std::string id_str);
static void* stringToJSID(std::string* id_str);
/* Increments reference count and returns it */
static int getReference();
/* Decrements reference count */
static void releaseReference();
/* Converts the given integer to a string */
static void itoa(int i, std::string* result);
/* Copies a variant data type into a C++ string */
static std::string NPVariantAsString(NPVariant variant);
/* This must be freed with browserfunctions.memfree */
static NPString NPStringCopy(const std::string& result);
/* This must be freed with browserfunctions.releasevariantvalue */
static NPVariant NPVariantStringCopy(const std::string& result);
/* Returns an std::string represented by the given identifier. */
static std::string NPIdentifierAsString(NPIdentifier id);
/* Frees the given vector and the strings that its contents point to */
static void freeStringPtrVector(std::vector<std::string*>* v);
/* Splits the given string based on the delimiter provided */
static std::vector<std::string*>* strSplit(const char* str,
const char* delim);
/* Converts given unicode integer byte array to UTF8 string */
static void getUTF8String(int length, int begin,
std::vector<std::string*>* unicode_byte_array,
std::string* result_unicode_str);
/* Converts given UTF8 string to unicode integer byte array */
static void convertStringToUTF8(std::string* str,
std::string* utf_str);
/* Converts given unicode integer byte array to UTF16LE/UCS-2 string */
static void getUTF16LEString(int length, int begin,
std::vector<std::string*>* unicode_byte_array,
std::wstring* result_unicode_str);
/* Prints contents of given string vector */
static void printStringVector(const char* prefix, std::vector<std::string>* cv);
/* Prints contents of given string pointer vector */
static void printStringPtrVector(const char* prefix, std::vector<std::string*>* cv);
static std::string* variantToClassName(NPVariant variant);
static void printNPVariant(NPVariant variant);
static void NPVariantToString(NPVariant variant, std::string* result);
static bool javaResultToNPVariant(NPP instance,
std::string* java_result,
NPVariant* variant);
static const gchar* getSourceFromInstance(NPP instance);
static void storeInstanceID(void* member_ptr, NPP instance);
static void removeInstanceID(void* member_ptr);
/* Clear object_map. Useful for tests. */
static void clearInstanceIDs();
static NPP getInstanceFromMemberPtr(void* member_ptr);
static NPObject* getNPObjectFromJavaKey(std::string key);
static void storeObjectMapping(std::string key, NPObject* object);
static void removeObjectMapping(std::string key);
/* Clear object_map. Useful for tests. */
static void clearObjectMapping();
static void invalidateInstance(NPP instance);
static bool isObjectJSArray(NPP instance, NPObject* object);
static void decodeURL(const char* url, char** decoded_url);
/* Returns a vector of gchar* pointing to the elements of the vector string passed in*/
static std::vector<gchar*> vectorStringToVectorGchar(const std::vector<std::string>* stringVec);
/* Posts call in async queue and waits till execution completes */
static void callAndWaitForResult(NPP instance, void (*func) (void *), AsyncCallThreadData* data);
/*cutting whitespaces from end and start of string*/
static void trim(std::string& str);
static bool file_exists(std::string filename);
//file-loggers helpers
static std::string generateLogFileName();
static void initFileLog();
static void printDebugStatus();
static std::string getTmpPath();
static std::string getRuntimePath();
};
/*
* A bus subscriber interface. Implementors must implement the newMessageOnBus
* method.
*/
class BusSubscriber
{
private:
public:
BusSubscriber() {}
/* Notifies this subscriber that a new message as arrived */
virtual bool newMessageOnBus(const char* message) = 0;
};
/*
* This implementation is very simple and is therefore folded into this file
* rather than a new one.
*/
class JavaMessageSender : public BusSubscriber
{
private:
public:
/* Sends given message to Java side */
virtual bool newMessageOnBus(const char* message);
};
/*
* Represents a message bus.
* The bus can also have subscribers who are notified when a new message
* arrives.
*/
class MessageBus
{
private:
/* Mutex for locking the message queue */
pthread_mutex_t msg_queue_mutex;
/* Mutex used when adjusting subscriber list */
pthread_mutex_t subscriber_mutex;
/* Subscriber list */
std::list<BusSubscriber*> subscribers;
/* Queued messages */
std::queue<char*> msgQueue;
public:
MessageBus();
~MessageBus();
/* subscribe to this bus */
void subscribe(BusSubscriber* b);
/* unsubscribe from this bus */
void unSubscribe(BusSubscriber* b);
/* Post a message on to the bus (it is safe to free the message pointer
after this function returns) */
void post(const char* message);
};
#endif // __ICEDTEAPLUGINUTILS_H__
|