File: sqUnixMozilla.c

package info (click to toggle)
squeak-vm 1%3A4.10.2.2614-4.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,284 kB
  • ctags: 15,344
  • sloc: ansic: 75,096; cs: 11,191; objc: 5,494; sh: 3,170; asm: 1,533; cpp: 449; pascal: 372; makefile: 366; awk: 103
file content (551 lines) | stat: -rw-r--r-- 12,936 bytes parent folder | download | duplicates (8)
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* sqUnixMozilla.c -- support for Squeak Netscape plugin
 *
 * Author: Bert Freudenberg <bert@isg.cs.uni-magdeburg.de>
 * 
 * Last edited: 2007-09-07 13:53:35 by piumarta on emilia
 *
 * Originally based on Andreas Raab's sqWin32PluginSupport
 * 
 * Notes: The plugin window handling stuff is in sqXWindow.c.
 *        browserProcessCommand() is called when data is available
 */

#include "sq.h"

#if defined(USE_X11)

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>

#include "FilePlugin.h"

#undef DEBUG

#ifdef DEBUG
void DPRINT(char *format, ...)
{
  static int debug= 42;
  if (42 == debug) 
    debug= (NULL != getenv("NPSQUEAK_DEBUG"));

  if (!debug)
    {
      return;
    }
  else
    {
      static FILE *file= 0;
      if (!file) 
	{
	  file= fopen("/tmp/npsqueak.log", "a+");
	}

      if (file) {
	va_list ap;
	va_start(ap, format);
	vfprintf(file, format, ap);
	va_end(ap);
	fflush(file);
      }
    }
}
#else
void DPRINT(char *format, ...) { }
#endif

/* from sqXWindow.c */
extern Display* stDisplay;
extern Window   stWindow;
extern Window   browserWindow;
extern Window   stParent;
extern int      browserPipes[2];

/* from interpret.c */
sqInt stackObjectValue(sqInt);
sqInt stackIntegerValue(sqInt);
sqInt isBytes(sqInt);
sqInt byteSizeOf(sqInt);
void *firstIndexableField(sqInt);
sqInt push(sqInt);
sqInt pop(sqInt);
sqInt positive32BitIntegerFor(sqInt);
sqInt nilObject(void);
sqInt instantiateClassindexableSize(sqInt, sqInt);
sqInt classByteArray(void);
sqInt failed(void);
sqInt pushBool(sqInt);

/* prototypes */

static void browserReceive(void *buf, size_t count);
static void browserSend(const void *buf, size_t count);
static void browserSendInt(int value);
static void browserReceiveData();
static void browserGetURLRequest(int id, char* url, int urlSize,
				char* target, int targetSize);
static void browserPostURLRequest(int id, char* url, int urlSize, 
				 char* target, int targetSize, 
				 char* postData, int postDataSize);

typedef struct sqStreamRequest {
  char *localName;
  int semaIndex;
  int state;
} sqStreamRequest;

#define MAX_REQUESTS 128

#define SQUEAK_READ 0
#define SQUEAK_WRITE 1

#define inBrowser\
  (-1 != browserPipes[SQUEAK_READ])

#define CMD_BROWSER_WINDOW 1
#define CMD_GET_URL        2
#define CMD_POST_URL       3
#define CMD_RECEIVE_DATA   4

static sqStreamRequest *requests[MAX_REQUESTS];



/* primitives called from Squeak */



/*
  primitivePluginBrowserReady
  Return true if a connection to the browser
  has been established. Only necessary if some
  sort of asynchronous communications are used.
*/
int display_primitivePluginBrowserReady()
{
  if (inBrowser)
    {
      pop(1);
      pushBool(1);
    }
  else
    primitiveFail();
  return 1;
}


/*
  primitivePluginRequestUrlStream: url with: semaIndex
  Request a URL from the browser. Signal semaIndex
  when the result of the request can be queried.
  Returns a handle used in subsequent calls to plugin
  stream functions.
  Note: A request id is the index into requests[].
*/
int display_primitivePluginRequestURLStream()
{
  sqStreamRequest *req;
  int id, url, length, semaIndex;

  if (!inBrowser) return primitiveFail();

  DPRINT("VM: primitivePluginRequestURLStream()\n");

  for (id=0; id<MAX_REQUESTS; id++) {
    if (!requests[id]) break;
  }
  if (id >= MAX_REQUESTS) return primitiveFail();

  semaIndex= stackIntegerValue(0);
  url= stackObjectValue(1);
  if (failed()) return 0;

  if (!isBytes(url)) return primitiveFail();

  req= calloc(1, sizeof(sqStreamRequest));
  if (!req) return primitiveFail();
  req->localName= NULL;
  req->semaIndex= semaIndex;
  req->state= -1;
  requests[id]= req;

  length= byteSizeOf(url);
  browserGetURLRequest(id, firstIndexableField(url), length, NULL, 0);
  pop(3);
  push(positive32BitIntegerFor(id));
  DPRINT("VM:   request id: %i\n", id);
  return 1;
}


/*
  primitivePluginRequestURL: url target: target semaIndex: semaIndex
  Request a URL into the given target.
*/
int display_primitivePluginRequestURL()
{
  sqStreamRequest *req;
  int url, urlLength;
  int target, targetLength;
  int id, semaIndex;

  if (!browserWindow) return primitiveFail();
  for (id=0; id<MAX_REQUESTS; id++) {
    if (!requests[id]) break;
  }

  if (id >= MAX_REQUESTS) return primitiveFail();

  semaIndex= stackIntegerValue(0);
  target= stackObjectValue(1);
  url= stackObjectValue(2);

  if (failed()) return 0;
  if (!isBytes(url) || !isBytes(target)) return primitiveFail();

  urlLength= byteSizeOf(url);
  targetLength= byteSizeOf(target);

  req= calloc(1, sizeof(sqStreamRequest));
  if(!req) return primitiveFail();
  req->localName= NULL;
  req->semaIndex= semaIndex;
  req->state= -1;
  requests[id]= req;

  browserGetURLRequest(id, firstIndexableField(url), urlLength, firstIndexableField(target), targetLength);
  pop(4);
  push(positive32BitIntegerFor(id));
  return 1;
}


/*
  primitivePluginPostURL: url target: target data: data semaIndex: semaIndex
  Post data to a URL into the given target.
*/
int display_primitivePluginPostURL()
{
  sqStreamRequest *req;
  int url, urlLength;
  int target, targetLength;
  int data, dataLength;
  int id, semaIndex;

  if (!browserWindow) return primitiveFail();
  for (id=0; id<MAX_REQUESTS; id++) {
    if (!requests[id]) break;
  }

  if (id >= MAX_REQUESTS) return primitiveFail();

  semaIndex= stackIntegerValue(0);
  data= stackObjectValue(1);
  target= stackObjectValue(2);
  url= stackObjectValue(3);

  if (failed()) return 0;
  if (target == nilObject()) target= 0;
  if (!isBytes(url) || !isBytes(data) || !(!target || isBytes(target))) return primitiveFail();

  urlLength= byteSizeOf(url);
  targetLength= target ? byteSizeOf(target) : 0;
  dataLength= byteSizeOf(data);

  req= calloc(1, sizeof(sqStreamRequest));
  if(!req) return primitiveFail();
  req->localName= NULL;
  req->semaIndex= semaIndex;
  req->state= -1;
  requests[id]= req;

  browserPostURLRequest(id, firstIndexableField(url), urlLength, 
			target ? firstIndexableField(target) : NULL, targetLength,
			firstIndexableField(data), dataLength);
  pop(4);
  push(positive32BitIntegerFor(id));
  return 1;
}

/* 
  primitivePluginRequestFileHandle: id
  After a URL file request has been successfully
  completed, return a file handle for the received
  data. Note: The file handle must be read-only for
  security reasons.
*/
int display_primitivePluginRequestFileHandle()
{
  sqStreamRequest *req;
  int id, fileOop;
  void *openFn;

  id= stackIntegerValue(0);
  if (failed()) return 0;
  if (id < 0 || id >= MAX_REQUESTS) return primitiveFail();

  req= requests[id];
  if (!req || !req->localName) return primitiveFail();

  fileOop= nilObject();

  if (req->localName)
    {
      DPRINT("VM: Creating file handle for %s\n", req->localName);
 
      openFn= ioLoadFunctionFrom("fileOpenNamesizewritesecure", "FilePlugin");
      if (!openFn)
      {
	DPRINT("VM:   Couldn't load fileOpenName:size:write:secure: from FilePlugin!\n");
	return primitiveFail();
      }
  
      fileOop= ((sqInt (*)(char *, sqInt, sqInt, sqInt))openFn)
	(req->localName, strlen(req->localName), 0 /* readonly */, 0 /* insecure */);
 
      /* if file ends in a $, it was a temp link created by the plugin */
      if ('$' == req->localName[strlen(req->localName) - 1])
      {
	DPRINT("VM:   unlink %s\n", req->localName);
	if (-1 == unlink(req->localName))
	  DPRINT("VM:   unlink failed: %s\n", strerror(errno));
      }

      if (failed()) 
	{
	  DPRINT("VM:   file open failed\n");
	  return 0;
	}
    }
  pop(2);
  push(fileOop);
  return 1;
}


/*
  primitivePluginDestroyRequest: id
  Destroy a request that has been issued before.
*/
sqInt display_primitivePluginDestroyRequest()
{
  sqStreamRequest *req;
  int id;

  id= stackIntegerValue(0);
  if (id < 0 || id >= MAX_REQUESTS) return primitiveFail();
  req= requests[id];
  if (req) {
    if (req->localName) free(req->localName);
    free(req);
  }
  requests[id]= NULL;
  pop(1);
  return 1;
}


/*
  primitivePluginRequestState: id
  Return true if the operation was successfully completed.
  Return false if the operation was aborted.
  Return nil if the operation is still in progress.
*/
sqInt display_primitivePluginRequestState()
{
  sqStreamRequest *req;
  int id;

  id= stackIntegerValue(0);
  if (id < 0 || id >= MAX_REQUESTS) return primitiveFail();
  req= requests[id];
  if (!req) return primitiveFail();
  pop(2);
  if (req->state == -1) push(nilObject());
  else pushBool(req->state);
  return 1;
}



/* helper functions */

static void browserReceive(void *buf, size_t count)
{
  ssize_t n;
  n= read(browserPipes[SQUEAK_READ], buf, count);
  if (n == -1)
    perror("Squeak read failed:");
  if (n < count)
    fprintf(stderr, "Squeak read too few data from pipe\n");
}


static void browserSend(const void *buf, size_t count)
{
  ssize_t n;
  n= write(browserPipes[SQUEAK_WRITE], buf, count);
  if (n == -1)
    perror("Squeak plugin write failed:");
  if (n < count)
    fprintf(stderr, "Squeak wrote too few data to pipe\n");
}

static void browserSendInt(int value)
{
  browserSend(&value, 4);
}


/*
  browserReceiveData:
  Called in response to a CMD_RECEIVE_DATA message.
  Retrieves the data file name and signals the semaphore.
*/
static void browserReceiveData()
{
  char *localName= NULL;
  int id, ok;

  browserReceive(&id, 4);
  browserReceive(&ok, 4);

  DPRINT("VM:  receiving data id: %i state %i\n", id, ok);

  if (ok == 1) {
    int length= 0;
    browserReceive(&length, 4);
    if (length) {
      localName= malloc(length+1);
      browserReceive(localName, length);
      localName[length]= 0;
      DPRINT("VM:   got filename %s\n", localName);
    }
  }
  if (id >= 0 && id < MAX_REQUESTS) {
    sqStreamRequest *req= requests[id];
    if (req) {
      req->localName= localName;
      req->state= ok;
      DPRINT("VM:  signaling semaphore, state=%i\n", ok);
      /*  synchronizedSignalSemaphoreWithIndex(req->semaIndex);*/
      signalSemaphoreWithIndex(req->semaIndex);
    }
  }
}


/*
  browserGetURLRequest:
  Notify plugin to get the specified url into target
*/
static void browserGetURLRequest(int id, char* url, int urlSize, 
				char* target, int targetSize)
{
  if (!inBrowser) {
    fprintf(stderr, "Cannot submit URL request -- "
	    "there is no connection to a browser\n");
    return;
  }

  browserSendInt(CMD_GET_URL);
  browserSendInt(id);

  browserSendInt(urlSize);
  if (urlSize > 0)
    browserSend(url, urlSize);

  browserSendInt(targetSize);
  if (targetSize > 0)
    browserSend(target, targetSize);
}


/*
  browserPostURLRequest:
  Notify plugin to post data to the specified url and get result into target
*/
static void browserPostURLRequest(int id, char* url, int urlSize, 
				 char* target, int targetSize, 
				 char* postData, int postDataSize)
{
  if (!inBrowser) {
    fprintf(stderr, "Cannot submit URL post request -- "
	    "there is no connection to a browser\n");
    return;
  }

  browserSendInt(CMD_POST_URL);
  browserSendInt(id);

  browserSendInt(urlSize);
  if (urlSize > 0)
    browserSend(url, urlSize);

  browserSendInt(targetSize);
  if (targetSize > 0)
    browserSend(target, targetSize);

  browserSendInt(postDataSize);
  if (postDataSize > 0)
    browserSend(postData, postDataSize);
}


/***************************************************************
 * Functions called from sqXWindow.c
 ***************************************************************/

/*
  browserProcessCommand:
  Handle commands sent by the plugin.
*/
void browserProcessCommand(void)
{
  static int firstTime= 1;
  int cmd, n;

  if (firstTime)
    {
      firstTime= 0;
      /* enable non-blocking reads */
      fcntl(browserPipes[SQUEAK_READ], F_SETFL, O_NONBLOCK);
    }
  DPRINT("VM: browserProcessCommand()\n");

  n= read(browserPipes[SQUEAK_READ], &cmd, 4);
  if (0 == n || (-1 == n && EAGAIN == errno))
    return;

  switch (cmd)
    {
    case CMD_RECEIVE_DATA:
      /* Data is coming in */
      browserReceiveData();
      break;
    case CMD_BROWSER_WINDOW:
      /* Parent window has changed () */
      browserReceive(&browserWindow, 4);
      stParent= browserWindow;
      DPRINT("VM:  got browser window 0x%X\n", browserWindow);
      break;
    default:
      fprintf(stderr, "Unknown command from Plugin: %i\n", cmd);
    }
}


#else /* !defined(USE_X11) */

sqInt display_primitivePluginBrowserReady()		{ return primitiveFail(); }
sqInt display_primitivePluginRequestURLStream()		{ return primitiveFail(); }
sqInt display_primitivePluginRequestURL()		{ return primitiveFail(); }
sqInt display_primitivePluginPostURL()			{ return primitiveFail(); }
sqInt display_primitivePluginRequestFileHandle()	{ return primitiveFail(); }
sqInt display_primitivePluginDestroyRequest()		{ return primitiveFail(); }
sqInt display_primitivePluginRequestState()		{ return primitiveFail(); }

#endif