PlugIn 94.8 KB
Newer Older
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
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : PlugIn.c                               */
/*                                                 */
/* Purpose: Supporting the generic RISC OS browser */
/*          Plug-In interface.                     */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 05-Oct-97: Created.                    */
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "swis.h"
#include "flex.h"

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
#include "Utils.h"

#include "Browser.h"
#include "Fetch.h" /* (Which itself includes URLstat.h) */
#include "FetchHTML.h"
49
#include "FetchPage.h"
50
#include "Filetypes.h"
51
#include "Frames.h"
52 53 54 55 56 57 58 59
#include "Handlers.h"
#include "Images.h"
#include "MimeMap.h"
#include "MiscDefs.h"
#include "Object.h"
#include "Protocols.h"
#include "Redraw.h"
#include "Reformat.h"
60
#include "RMA.h"
61
#include "TokenUtils.h"
62
#include "Toolbars.h"
63
#include "URLveneer.h"
64
#include "Windows.h"
65 66 67

#include "PlugIn.h"

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
/**************************************************************************************************/
/*                                                                                                */
/* Allocation of browser handles for the Plug-In protocol                                         */
/* ======================================================                                         */
/*                                                                                                */
/* In the Plug-In protocol, a browser supplies a private word (the browser instance handle) and   */
/* the Plug-In supplies a private word (the Plug-In instance handle). Each is considered an       */
/* opaque value to the other.                                                                     */
/*                                                                                                */
/* Because the browser, in this case, needs to know both the browser_data structure owning a      */
/* Plug-In and the HStream structure representing it, the plugin_insts structure is used to form  */
/* a linked list containing information on a Plug-In. The browser points to items in this list    */
/* to create the browser instance handle.                                                         */
/*                                                                                                */
/* When streaming data, two new handles are used, one for the Plug-In's side of the stream, and   */
/* one for the browser's side. In this case, we only need to know the browser_data struct that is */
/* handling the fetching of data, so the browser stream instance handle is a pointer to that      */
/* structure. This structure has a 'pstream' field which points to a plugin_stream structure.     */
/* This is used to hold a lot of information on the fetch, and is an externally visible structure */
/* unlike plugin_insts.                                                                           */
/*                                                                                                */
/*                                                                                                */
/* Other structures                                                                               */
/* ================                                                                               */
/*                                                                                                */
/* Less related to the protocol are two other important structures - the plugin_files struct and  */
/* the plugin_queue. The plugin_queue is a simple structure added to whenever the browser finds   */
/* a token representing a Plug-In and wants to try launching that Plug-In. The head item of the   */
/* queue is launched and removed when the protocol completes (successfully or otherwise),         */
/* allowing the next item to rise to the head and be launched, and so-forth.                      */
/*                                                                                                */
/* The plugin_files struct is used in place of a browser cache. Since the core browser has no     */
/* cache itself, and given that the Plug-In protocol expects one (no method for deleting          */
/* temporary files is provided other than implicit cache expiry), an alternative is needed.       */
/* Whenever a temporary file is created as part of Plug-In streaming (i.e. everything except the  */
/* Params file used at launch time), an entry holding the URL relating to the file, and its       */
/* pathname, is placed in the list. Instead of fetching new data as soon as asked to by a Plug-In */
/* the browser will first scan the list, and if a matching URL is found, use the existing file    */
/* instead.                                                                                       */
/*                                                                                                */
/* Files are cached for the duration of a session, and are deleted on browser shutdown.           */
/**************************************************************************************************/

/* Local structures */

typedef struct plugin_files
{
  struct plugin_files * next;     /* When a Plug-In fetch to a file has successfully completed,  */
                                  /* this records the filename so it can be deleted on shutdown. */
  char                * url;
  char                * pathname;
}
plugin_files;

typedef struct plugin_insts
{
  struct plugin_insts * next;     /* This is what a browser instance handle is actually a pointer to */
                                  /* (but not a browser stream instance handle; see comments above). */
  browser_data        * browser;
  HStream             * token;

  unsigned int          plugin_task_handle;
}
plugin_insts;

133 134
/* Statics */

135 136 137
static plugin_queue * plugin_queue_head           = NULL;
static plugin_files * plugin_files_head           = NULL;
static plugin_insts * plugin_insts_head           = NULL;
138

139 140 141
static int            plugin_open_reference       = 0;
static int            plugin_open_tried_once      = 0;
static char         * plugin_open_filename        = NULL;
142

143
static int            plugin_stream_new_reference = 0;
144 145 146

/* Static function prototypes */

147 148
static browser_data    * plugin_return_browser        (unsigned int browser_instance_handle);

149 150 151 152 153
static _kernel_oserror * plugin_write_params_word     (int fh, int word);
static _kernel_oserror * plugin_write_params_string   (int fh, const char * string);
static _kernel_oserror * plugin_write_params_entry    (int fh, int type, const char * name, const char * data, const char * mime);

static _kernel_oserror * plugin_remove_head_item      (void);
154

155 156 157 158 159 160 161 162 163 164 165 166
static _kernel_oserror * plugin_broadcast_open        (void);

static int               plugin_close_stream          (int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle);

static _kernel_oserror * plugin_add_file_entry        (const char * pathname, const char * url);
static const char      * plugin_find_file             (const char * url);
static void              plugin_flush_files           (void);

static _kernel_oserror * plugin_add_instance_entry    (browser_data * b, HStream * t, unsigned int task);
static plugin_insts    * plugin_find_instance_entry   (browser_data * b, HStream * t);
static void              plugin_remove_instance_entry (plugin_insts * remove);

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
/*************************************************/
/* plugin_return_string()                        */
/*                                               */
/* Takes a message block and string_value from   */
/* within it, and returns the string it points   */
/* to.                                           */
/*                                               */
/* Parameters: Pointer to a WimpMessage struct   */
/*             holding the string_value field;   */
/*                                               */
/*             Pointer to the string_value field */
/*             inside that message.              */
/*                                               */
/* Returns:    Pointer to the string the         */
/*             string_value references, or NULL  */
/*             if the value seems invalid.       */
/*************************************************/

const char * plugin_return_string(WimpMessage * m, string_value * sv)
{
  int offset = ((char *) sv) - ((char *) m);

  if (!m || !sv || !sv->offset) return NULL;

  /* Is the offset of the string_value field within the message */
  /* block out of range (i.e. the string_value does not appear  */
  /* to lie in the given message block)?                        */

  if (offset >= m->hdr.size || offset < sizeof(m->hdr)) return NULL;

  /* Now read the string_value field itself */

  if (sv->offset < 256)
  {
    if (sizeof(m->hdr) + offset >= m->hdr.size) return NULL;

    else return (const char *) (((char *) m) + sizeof(m->hdr) + sv->offset);
  }
  else return (const char *) sv->offset;
}

/*************************************************/
/* plugin_write_params_word()                    */
/*                                               */
/* Writes a word to a given parameters file,     */
/* in little-endian order.                       */
/*                                               */
/* Parameters: RISC OS file handle of the file;  */
/*                                               */
/*             Word to write.                    */
/*************************************************/

static _kernel_oserror * plugin_write_params_word(int fh, int word)
{
221
  char integer[4];
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

  /* #define Brain_Off... */

  integer[0] = (word & 0xff);
  integer[1] = ((word & 0xff00) >> 8);
  integer[2] = ((word & 0xff0000) >> 16);
  integer[3] = ((word & 0xff000000) >> 24);

  RetError(_swix(OS_BPut,
                 _INR(0,1),

                 integer[0],
                 fh));

  RetError(_swix(OS_BPut,
                 _INR(0,1),

                 integer[1],
                 fh));

  RetError(_swix(OS_BPut,
                 _INR(0,1),

                 integer[2],
                 fh));

  RetError(_swix(OS_BPut,
                 _INR(0,1),

                 integer[3],
                 fh));

  return NULL;
}

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
/*************************************************/
/* plugin_return_browser()                       */
/*                                               */
/* Converts a browser instance handle to a       */
/* browser_data structure pointer.               */
/*                                               */
/* Parameters: A browser instance handle.        */
/*                                               */
/* Returns:    Pointer to a browser_data struct  */
/*             represented by the instance       */
/*             handle, or NULL if none is found. */
/*************************************************/

static browser_data * plugin_return_browser(unsigned int browser_instance_handle)
{
  browser_data * b;

  if (!browser_instance_handle) return NULL;

  b = ((plugin_insts *) browser_instance_handle)->browser;

  if (!is_known_browser(b)) return NULL;

  return b;
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/*************************************************/
/* plugin_write_params_string()                  */
/*                                               */
/* Writes a string to a given parameters file,   */
/* padding with zeros to a length which is a     */
/* multiple of 4. If the string length, not      */
/* including terminator, is already a multiple   */
/* of four, no extra bytes are written.          */
/*                                               */
/* Parameters: RISC OS file handle of the file;  */
/*                                               */
/*             Pointer to the string.            */
/*************************************************/

static _kernel_oserror * plugin_write_params_string(int fh, const char * string)
{
299 300 301
  const char * s;
  int          i = 0;
  int          pad;
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

  /* Write the main string */

  s = string ? string : "";

  while (s[i])
  {
    RetError(_swix(OS_BPut,
                   _INR(0,1),

                   s[i],
                   fh));

    i++;
  }

  /* Pad if necessary */

  pad = 4 - (strlen(s) % 4);

  if (pad < 4)
  {
    for (i = 0; i < pad; i ++)
    {
      RetError(_swix(OS_BPut,
                     _INR(0,1),

                     '\0',
                     fh));
    }
  }

  return NULL;
}

/*************************************************/
/* plugin_write_params_entry()                   */
/*                                               */
/* Writes an entry to the given parameters file. */
/*                                               */
/* Parameters: RISC OS file handle of the file;  */
/*                                               */
/*             Type of the entry (see            */
345
/*             PlugIn.h);                        */
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
/*                                               */
/*             Pointer to the name string;       */
/*                                               */
/*             Pointer to the data string;       */
/*                                               */
/*             Pointer to the Mime string.       */
/*                                               */
/* Assumes:    Any of the pointers may be NULL   */
/*             or a zero length string to write  */
/*             a zero length field for that      */
/*             item.                             */
/*************************************************/

static _kernel_oserror * plugin_write_params_entry(int fh, int type, const char * name, const char * data, const char * mime)
{
361 362 363 364
  int          length;
  const char * n;
  const char * d;
  const char * m;
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

  /* Make life easier */

  n = name ? name : "";
  d = data ? data : "";
  m = mime ? mime : "";

  /* Write the type */

  RetError(plugin_write_params_word(fh, type));

  /* Calculate and write the entry length */

  length = 4 + strlen(n);
  length = (int) WordAlign(length);

  length += 4 + strlen(d);
  length = (int) WordAlign(length);

  length += 4 + strlen(m);
  length = (int) WordAlign(length);

  RetError(plugin_write_params_word(fh, length));

  /* The name field */

  RetError(plugin_write_params_word(fh, strlen(n)));
  RetError(plugin_write_params_string(fh, n));

  /* The data field */

  RetError(plugin_write_params_word(fh, strlen(d)));
  RetError(plugin_write_params_string(fh, d));

  /* The mime type field */

  RetError(plugin_write_params_word(fh, strlen(m)));
  RetError(plugin_write_params_string(fh, m));

  return NULL;
}

/*************************************************/
/* plugin_write_params()                         */
/*                                               */
/* Writes a parameters file to a unique filename */
/* based on the given HStream.                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the HStream;          */
/*                                               */
/*             Pointer to the HStream struct     */
/*             representing the OBJECT, APPLET   */
/*             or EMBED tag that the parameters  */
/*             file is for;                      */
/*                                               */
/*             Buffer for the filename that was  */
/*             used, or if the buffer size given */
/*             is zero, pointer to the filename  */
/*             to use;                           */
/*                                               */
/*             Size of the buffer, or zero if    */
/*             the above parameter is a pointer  */
/*             to the filename to use instead of */
/*             a pointer to a buffer to receive  */
/*             a locally generated system unique */
431 432 433 434
/*             filename;                         */
/*                                               */
/*             Pointer to a BBox describing the  */
/*             size of the Plug-In window.       */
435 436
/*************************************************/

437
_kernel_oserror * plugin_write_params(browser_data * b, HStream * t, char * buffer, int buffer_size, BBox * position)
438 439 440 441 442
{
  _kernel_oserror * e  = NULL;
  int               fh = 0;
  char            * data;
  char              version[5];
443
  char              number [64];
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

  if (buffer_size) protocols_util_make_unique_name(buffer, buffer_size);

  RetError(_swix(OS_Find,
                 _INR(0,1) | _OUT(0),

                 0x8c, /* Create empty file with r/w access, return errors */
                 buffer,

                 &fh));

  /* Write the browser special fields */

  data = browser_current_url(b);
  if (!data) data = browser_fetch_url(b);

  e = plugin_write_params_entry(fh,
                                PlugIn_ParamType_BrowserSpecial,
                                "BASEHREF",
                                data,
                                NULL);

  if (e) goto plugin_write_params_exit;

  data = lookup_token("_TaskName", 1, 0);

  e = plugin_write_params_entry(fh,
                                PlugIn_ParamType_BrowserSpecial,
                                "USERAGENT",
                                data,
                                NULL);

  if (e) goto plugin_write_params_exit;

  data = lookup_token("Version", 1, 0);
  StrNCpy0(version, data);

  e = plugin_write_params_entry(fh,
                                PlugIn_ParamType_BrowserSpecial,
                                "UAVERSION",
                                version,
                                NULL);

  if (e) goto plugin_write_params_exit;

  e = plugin_write_params_entry(fh,
                                PlugIn_ParamType_BrowserSpecial,
                                "APIVERSION",
                                "1.10",
                                NULL);

  if (e) goto plugin_write_params_exit;

497 498 499 500 501 502 503 504 505 506 507 508 509
  if (redraw_backcol(b) != -1)
  {
    char word[9];

    sprintf(word, "%08x", redraw_backcol(b));

    e = plugin_write_params_entry(fh,
                                  PlugIn_ParamType_BrowserSpecial,
                                  "BGCOLOR",
                                  word,
                                  NULL);
  }

510 511
  /* Now write details based on the Object type */

512 513
  if (
       (
514 515
         HtmlOBJECTclassid(t)   &&
         *HtmlOBJECTclassid(t)
516 517
       )
     )
518
  {
519 520
    /* A code object, e.g. Java applet */

521 522 523
    const char * cis = HtmlOBJECTclassid(t);
    const char * cie;

524
    /* Get details from the CLASSID attribute, giving just the leafname */
525 526 527 528 529 530 531 532 533 534 535 536 537 538

    cie = cis + strlen(cis) - 1;

    while (cie > cis && *cie != '/') cie --;
    if (*cie == '/') cie++;

    e = plugin_write_params_entry(fh,
                                  PlugIn_ParamType_URLFromOBJECT,
                                  "CLASSID",
                                  cie,
                                  HtmlOBJECTcodetype(t));

    if (e) goto plugin_write_params_exit;

539
    /* CODEBASE, if we have it */
540 541 542 543 544 545 546 547 548 549 550

    if (HtmlOBJECTcodebase(t) && *HtmlOBJECTcodebase(t))
    {
      e = plugin_write_params_entry(fh,
                                    PlugIn_ParamType_DataFromOBJECT,
                                    "CODEBASE",
                                    HtmlOBJECTcodebase(t),
                                    NULL);

      if (e) goto plugin_write_params_exit;
    }
551 552 553 554
  }
  else if (HtmlOBJECTdata(t) && *HtmlOBJECTdata(t))
  {
    /* A data object, e.g. Shockwave movie */
555

556 557 558 559 560 561
    e = plugin_write_params_entry(fh,
                                  PlugIn_ParamType_URLFromOBJECT,
                                  "DATA",
                                  HtmlOBJECTdata(t),
                                  HtmlOBJECTtype(t));
  }
562

563 564 565 566
  /* Some generic bits */

  if (HtmlOBJECTstandby(t) && *HtmlOBJECTstandby(t))
  {
567 568 569
    e = plugin_write_params_entry(fh,
                                  PlugIn_ParamType_DataFromOBJECT,
                                  "STANDBY",
570
                                  HtmlOBJECTstandby(t),
571 572 573
                                  NULL);

    if (e) goto plugin_write_params_exit;
574
  }
575

576
  /* Width and height */
577

578
  sprintf(number, "%d", (position->ymax - position->ymin) / 2);
579

580 581 582 583 584
  e = plugin_write_params_entry(fh,
                                PlugIn_ParamType_DataFromOBJECT,
                                "HEIGHT",
                                number,
                                NULL);
585

586
  if (e) goto plugin_write_params_exit;
587

588
  sprintf(number, "%d", (position->xmax - position->xmin) / 2);
589

590 591 592 593 594
  e = plugin_write_params_entry(fh,
                                PlugIn_ParamType_DataFromOBJECT,
                                "WIDTH",
                                number,
                                NULL);
595

596
  if (e) goto plugin_write_params_exit;
597

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
  /* If we have a child token stream, look for PARAM tags */

  if (HtmlOBJECTstream(t))
  {
    HStream * child = HtmlOBJECTstream(t);

    while (child)
    {
      if (child->tagno == TAG_PARAM)
      {
        int type;

        /* Get the entry type from the valuetype field of the tag */

        switch (HtmlPARAMvaluetype(child))
        {
          default:
          case paramtype_DATA:   type = PlugIn_ParamType_DataFromPARAM;   break;
          case paramtype_OBJECT: type = PlugIn_ParamType_ObjectFromPARAM; break;
          case paramtype_REF:    type = PlugIn_ParamType_URLFromPARAM;    break;
        }

        /* Write the entry */

        e = plugin_write_params_entry(fh,
                                      type,
                                      HtmlPARAMname(child),
                                      HtmlPARAMvalue(child),
                                      HtmlPARAMtype(child));

        if (e) goto plugin_write_params_exit;
      }

      child = child->next;
    }
  }

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  /* Write the terminating entry */

  RetError(plugin_write_params_word(fh, PlugIn_ParamType_Terminator));

plugin_write_params_exit:

  /* Exit by closing the file and returning any */
  /* generated error.                           */

  if (fh)
  {
    _swix(OS_Find,
     _INR(0,1),

     0, /* Close file */
     fh);

    /* Set the filetype */

    _swix(OS_File,
     _INR(0,2),

     18,
     buffer,
     FileType_DATA);
  }

  return e;
}

/*************************************************/
666
/* plugin_add_queue_item()                       */
667
/*                                               */
668 669 670
/* Add details of a new Plug-In to the queue of  */
/* Plug-Ins. If there were none already in the   */
/* queue, then start this new one immediately.   */
671
/*                                               */
672 673 674 675 676 677 678 679 680 681 682
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Pointer to the HStream struct     */
/*             representing the OBJECT, APPLET   */
/*             or EMBED tag (Object) that the    */
/*             message is for;                   */
/*                                               */
/*             Pointer to a BBox describing the  */
/*             where the Plug-In should open, in */
/*             parent window coordinates.        */
683 684
/*************************************************/

685
_kernel_oserror * plugin_add_queue_item(browser_data * b, HStream * t, BBox * position)
686
{
687 688
  plugin_queue * new;
  int            start_now = 0;
689 690

  #ifdef TRACE
691
    if (tl & (1u<<30)) Printf("plugin_add_queue_item: Called for %p, %p\n",b,t);
692 693
  #endif

694
  /* Allocate memory for the item */
695

696 697
  new = malloc(sizeof(plugin_queue));
  if (!new) return make_no_memory_error(21);
698

699 700 701
  if (plugin_queue_head)
  {
    /* Insert the new item in between the existing first and second items */
702

703 704
    new->next               = plugin_queue_head->next;
    plugin_queue_head->next = new;
705
  }
706 707 708 709
  else
  {
    new->next               = NULL;
    plugin_queue_head       = new;
710

711 712
    /* This is the only item in the queue, so try to start a Plug-In */
    /* for it as soon as possible.                                   */
713

714 715
    start_now = 1;
  }
716

717
  /* Fill in the rest of the structure */
718

719 720 721
  new->browser  = b;
  new->token    = t;
  new->position = *position;
722

723
  /* If required, try to start the Plug-In */
724

725 726 727 728 729
  if (start_now)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_add_queue_item: Exitting through plugin_broadcast_open\n");
    #endif
730

731 732
    return plugin_broadcast_open();
  }
733 734

  #ifdef TRACE
735
    if (tl & (1u<<30)) Printf("plugin_add_queue_item: Successful\n");
736 737
  #endif

738 739
  return NULL;
}
740

741 742 743 744 745 746 747 748
/*************************************************/
/* plugin_remove_item()                          */
/*                                               */
/* Remove an item from the Plug-In queue.        */
/*                                               */
/* Parameters: Pointer to the plugin_queue       */
/*             struct to remove.                 */
/*************************************************/
749

750 751 752
_kernel_oserror * plugin_remove_item(plugin_queue * remove)
{
  plugin_queue * prev;
753

754 755 756
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_remove_item: Called for %p\n",remove);
  #endif
757

758
  /* Can't do anything if there are no items */
759

760
  if (!plugin_queue_head || !remove)
761
  {
762
    #ifdef TRACE
763

764 765 766 767 768 769
      erb.errnum  = Utils_Error_Custom_Normal;
      sprintf(erb.errmess,
              "NULL queue head (0x%08x) or remove item (0x%08x)in plugin_remove_item",
              (int) plugin_queue_head,
              (int) remove);
      return &erb;
770

771
    #endif
772

773
    return NULL;
774 775
  }

776
  /* Find the previous item */
777

778
  prev = plugin_queue_head;
779

780
  while (prev && prev->next != remove) prev = prev->next;
781

782 783 784
  /* If we've not found the item we were asked to remove, and */
  /* the remove item isn't at the queue head, 'remove' must   */
  /* be invalid - so exit.                                    */
785

786 787 788
  if (!prev && remove != plugin_queue_head)
  {
    #ifdef TRACE
789

790 791 792 793 794
      erb.errnum  = Utils_Error_Custom_Normal;
      sprintf(erb.errmess,
              "Can't find item that points to given structure %0x08x in plugin_remove_item",
              (int) remove);
      return &erb;
795

796
    #endif
797

798 799
    return NULL;
  }
800

801
  /* OK, unlink it */
802

803 804
  if (prev) prev->next   = remove->next;
  else plugin_queue_head = remove->next;
805

806
  /* Free the item - finished. */
807

808
  free(remove);
809 810

  #ifdef TRACE
811
    if (tl & (1u<<30)) Printf("plugin_remove_item: Successful\n");
812 813 814 815 816 817
  #endif

  return NULL;
}

/*************************************************/
818
/* plugin_remove_head_item()                     */
819
/*                                               */
820 821
/* Remove the head item from the Plug-In queue,  */
/* launching the next item if there is one.      */
822 823
/*************************************************/

824
static _kernel_oserror * plugin_remove_head_item(void)
825 826
{
  #ifdef TRACE
827
    if (tl & (1u<<30)) Printf("plugin_remove_head_item: Called\n");
828 829
  #endif

830 831 832
  RetError(plugin_remove_item(plugin_queue_head));

  if (plugin_queue_head)
833 834
  {
    #ifdef TRACE
835
      if (tl & (1u<<30)) Printf("plugin_remove_head_item: Exitting through plugin_broadcast_open\n");
836 837
    #endif

838 839 840 841 842 843 844
    return plugin_broadcast_open();
  }
  else
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_remove_head_item: Successful\n");
    #endif
845

846 847 848
    return NULL;
  }
}
849

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
/*************************************************/
/* plugin_flush_queue()                          */
/*                                               */
/* Remove selected, or all Plug-Ins in the       */
/* queue.                                        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Plug-Ins to       */
/*             remove, or NULL for all items;    */
/*                                               */
/*             If one of the removed items was   */
/*             at the head of the queue, 1 to    */
/*             launch the new head item Plug-In, */
/*             else 0.                           */
/*************************************************/

_kernel_oserror * plugin_flush_queue(browser_data * b, int start_now)
{
868 869 870
  plugin_queue * old_head = plugin_queue_head;
  plugin_queue * current;
  plugin_queue * next;
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_flush_queue: Called for %p\n",b);
  #endif

  current = plugin_queue_head;

  /* For each item, remove it only if it belongs to the given */
  /* browser, or the given browser is NULL.                   */

  while (current)
  {
    next = current->next;

    if (!b || (b && next->browser == b)) RetError(plugin_remove_item(current));

    current = next;
  }

  /* If there are still queue items, the head item has changed and */
  /* the start_now parameter passed in says to do so, launch the   */
  /* new head item Plug-In.                                        */

  if (
       plugin_queue_head             &&
       plugin_queue_head != old_head &&
       start_now
     )
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_flush_queue: Exitting through plugin_broadcast_open\n");
    #endif

    return plugin_broadcast_open();
  }
  else
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_flush_queue: Successful\n");
    #endif

    return NULL;
  }
}

/*************************************************/
/* plugin_broadcast_open()                       */
/*                                               */
/* Where it all starts - broadcast a             */
/* Message_PlugIn_Open for the Plug-In at the    */
/* head of the queue.                            */
/*                                               */
/* External functions should invoke this by      */
/* adding to the Plug-In queue, not by a direct  */
/* call here.                                    */
/*************************************************/

static _kernel_oserror * plugin_broadcast_open(void)
{
  _kernel_oserror * e;
  char              params[Limits_OS_Pathname];
  const char      * type;
  WimpMessage       m;
  MPlugIn_Open    * open = (MPlugIn_Open *) &m.data;
  browser_data    * b;
  unsigned int      browser_instance;
  HStream         * t;
  BBox            * position;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_broadcast_open: Called, head item is %p\n", plugin_queue_head);
  #endif

  /* Can't do anything if there are no queue items */

  if (!plugin_queue_head)
  {
    #ifdef TRACE

      erb.errnum  = Utils_Error_Custom_Normal;
      strcpy(erb.errmess,
             "NULL queue head in plugin_broadcast_open");
      return &erb;

    #endif

    return NULL;
  }

  /* Make life a bit less verbose... */

  b = plugin_queue_head->browser;
  t = plugin_queue_head->token;

  position = &plugin_queue_head->position;

  #ifdef TRACE
    if (tl & (1u<<30))
    {
      char debugbuf[1024];
      sprintf(debugbuf,"plugin_broadcast_open: Open at BBox %d, %d, %d, %d",position->xmin,position->ymin,position->xmax,position->ymax);
      Printf("%s\n",debugbuf);
    }
  #endif

  /* Write the parameters file */

  RetError(plugin_write_params(b, t, params, sizeof(params), position));

  // For debugging, this may be useful...
  //
  // plugin_write_params(b, t, "ADFS::4.$.DebugParam", 0, position);

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_broadcast_open: Params file is at '%s'\n",params);
  #endif

  /* Fill in the header */

  m.hdr.size        = sizeof(MPlugIn_Open) + sizeof(m.hdr);
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Message_PlugIn_Open;

  /* Get an instance handle */

  RetError(plugin_obtain_instance(b, t, &browser_instance));

  /* Fill in the body */

  open->flags                   = 0;
  open->reserved                = 0;
  open->browser_instance_handle = browser_instance;
  open->parent_window_handle    = b->window_handle;
  open->parent_area             = *position;

  open->file_type = 0;

  /* Get the Mime type */

  type = HtmlOBJECTcodetype(t);
  if (!type || !*type) type = HtmlOBJECTtype(t);

  /* If we've got one, ask the Mime Mapper what RISC OS filetype this is */

  if (type && *type)
  {
    if (mimemap_mime_to_riscos(type, &open->file_type)) open->file_type = FileType_DATA;
  }

  /* Otherwise get it from the filename extension */

  else
  {
    const char * data = HtmlOBJECTclassid(t);
    const char * ext;

    open->file_type = FileType_DATA;

    if (!data || !*data) data = HtmlOBJECTdata(t);

    if (data && * data)
    {
      ext = data + strlen(data) - 1;

      while (ext >= data && *ext != '.') ext--;

      if (*ext == '.')
      {
        if (mimemap_extension_to_riscos(ext, &open->file_type)) open->file_type = FileType_DATA;
      }
    }
  }

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_broadcast_open: Filetype to use: 0x%03X\n", open->file_type);
  #endif

  /* If we already have RMA space for the filename, free it */

  if (plugin_open_filename)
  {
    rma_release(NULL, plugin_open_filename);
    plugin_open_filename = NULL;
  }

  /* (Re)claim RMA space for the filename */

  e = rma_claim(NULL, strlen(params) + 1, (void **) &plugin_open_filename);

  if (e) return e;
  if (!plugin_open_filename) return NULL;

  /* Copy it in and fill in the message field */

  strcpy(plugin_open_filename, params);

  open->file_name.ptr = plugin_open_filename;

  /* Broadcast the message */

  RetError(wimp_send_message(Wimp_EUserMessageRecorded, &m, 0, 0, NULL));

  /* Record my_ref in case it bounces */

  plugin_open_reference  = m.hdr.my_ref;
  plugin_open_tried_once = 0; /* We haven't tried once until we get the first bounce */

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_broadcast_open: Successful\n",b,t);
  #endif

  return NULL;
}

/*************************************************/
/* plugin_open_bounced()                         */
/*                                               */
/* Handle a Message_PlugIn_Open bounce.          */
/*                                               */
/* Parameters: Pointer to a Wimp_Message struct  */
/*             holding the bounced message.      */
/*************************************************/

_kernel_oserror * plugin_open_bounced(WimpMessage * m)
{
1096 1097
  browser_data * b;
  MPlugIn_Open * open = (MPlugIn_Open *) &m->data;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

  #ifdef TRACE
    if (!plugin_open_tried_once)
    {
      if (tl & (1u<<30)) Printf("plugin_open_bounced: Called\n");
    }
    else
    {
      if (tl & (1u<<30)) Printf("plugin_open_bounced: Called again\n");
    }
  #endif

  /* For a bounce, we get the same message back - so the message reference */
  /* is still in my_ref, it hasn't been copied to your_ref by a receiver.  */

  if (m->hdr.my_ref == plugin_open_reference)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_open_bounced: Message reference %x is known\n", m->hdr.my_ref);
    #endif

    if (plugin_open_tried_once)
    {
      /* Yuk, everything collapsed in a nasty great heap */

      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_open_bounced: Plug-In launch failed\n");
      #endif

      /* Delete the parameters file */

      remove(plugin_open_filename);

      /* Need to free up the RMA space used for the filename */

      rma_release(NULL, plugin_open_filename);

      plugin_open_filename   = NULL;

      plugin_open_reference  = 0;
      plugin_open_tried_once = 0;

      /* Remove the item from the queue, launching a new one */
      /* if there is anything else there.                    */

      RetError(plugin_remove_head_item());
    }
    else
    {
      char             combuf[16]; /* Size of '@PlugInType_xxx' plus terminator */
      char             sysvar[22]; /* As above, preceeded by 'Alias$'           */
      _kernel_swi_regs r;
      int              required;
      BBox             new;

      /* Try launching the Plug-In through Wimp_StartTask */

      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_open_bounced: Attempting Plug-In launch\n");
      #endif

      plugin_open_tried_once = 1;

      if (open->file_type >= 0 && open->file_type <= 4095) sprintf(combuf, "@PlugInType_%03x", open->file_type);
      else                                                 sprintf(combuf, "@PlugInType_%03x", FileType_DATA);

      strcpy(sysvar, "Alias$");
      strcat(sysvar, combuf);

      /* Does the system variable exist? */

      r.r[0] = (int) sysvar;
      r.r[1] = (int) NULL;
      r.r[2] = -1;
      r.r[3] = 0;
      r.r[4] = 0;

      /* _swix will not work correctly for this particular SWI if */
      /* requiring the returned R2 value. Something to do with    */
      /* the call relying on generating an error, but _swix spots */
      /* it and pulls out earlier than the call expects. Or some  */
      /* such thing...                                            */

      _kernel_swi(OS_ReadVarVal, &r, &r);

      required = -r.r[2];

      if (required)
      {
        show_error_ret(_swix(Wimp_StartTask,
                             _IN(0),

                             combuf));
      }

      #ifdef TRACE

        else
        {
          if (tl & (1u<<30)) Printf("plugin_open_bounced: System variable '%s' is not set\n", sysvar);
        }

      #endif

      /* Now rebroadcast the message. Note we can't use the same position, */
      /* as between the two redrawing may have occurred that has changed   */
      /* where the Object now sits.                                        */

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
      b = plugin_return_browser(open->browser_instance_handle);

      if (!b)
      {
        #ifdef TRACE
          if (tl & (1u<<30)) Printf("plugin_open_bounced: \0211Invalid browser instance handle suspected.\0217\n");
        #endif

        return NULL;
      }

1217
      if (
1218
           !object_get_token_object_box(b,
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
                                        ((plugin_insts *) open->browser_instance_handle)->token,
                                        &new)
         )
         open->parent_area = new;

      #ifdef TRACE
        if (tl & (1u<<30))
        {
          char debugbuf[1024];
          sprintf(debugbuf,"plugin_open_bounced: New BBox %d, %d, %d, %d",new.xmin,new.ymin,new.xmax,new.ymax);
          Printf("%s\n",debugbuf);

          if (tl & (1u<<30)) Printf("plugin_open_bounced: Rebroadcasting Message_PlugIn_Open\n");
        }
      #endif

      m->hdr.my_ref = m->hdr.your_ref = 0;

      RetError(wimp_send_message(Wimp_EUserMessageRecorded, m, 0, 0, NULL));

      /* Record my_ref in case it bounces for a second time */

      plugin_open_reference = m->hdr.my_ref;
    }
  }

  #ifdef TRACE

    else
    {
      if (tl & (1u<<30)) Printf("plugin_open_bounced: Message reference %x is not known\n", m->hdr.my_ref);

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Unknown message reference %x in plugin_open_bounced",
              m->hdr.my_ref);

      return &erb;
    }

    if (tl & (1u<<30)) Printf("plugin_open_bounced: Successful\n");
  #endif

  return NULL;
}

/*************************************************/
/* plugin_got_opening()                          */
/*                                               */
/* Handle reception of a Message_PlugIn_Opening. */
/*                                               */
/* Parameters: Pointer to a Wimp_Message struct  */
/*             relevant to the message.          */
/*************************************************/
1274

1275 1276 1277
_kernel_oserror * plugin_got_opening(WimpMessage * m)
{
  _kernel_oserror * e       = NULL;
1278
  browser_data    * b;
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
  MPlugIn_Opening * opening = (MPlugIn_Opening *) &m->data;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_got_opening: Called\n");
  #endif

  if (m->hdr.your_ref == plugin_open_reference)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_opening: Message reference %x is known\n", m->hdr.your_ref);
    #endif

    if (plugin_open_filename)
    {
      if (!(opening->flags & MPlugIn_Opening_WillDeleteParamsItself))
      {
        #ifdef TRACE
          if (tl & (1u<<30)) Printf("plugin_got_opening: Removing parameters file\n");
        #endif

        remove(plugin_open_filename);
      }

      /* Need to free up the RMA space used for the filename */

      rma_release(NULL, plugin_open_filename);
      plugin_open_filename = NULL;
    }

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    b = plugin_return_browser(opening->browser_instance_handle);

    if (!b)
    {
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_got_opening: \0211Invalid browser instance handle suspected.\0217\n");
      #endif

      return NULL;
    }

1319 1320
    /* Tell the Object the PlugIn details */

1321
    object_set_token_object_plugin(b,
1322 1323 1324 1325 1326 1327 1328 1329
                                   ((plugin_insts *) opening->browser_instance_handle)->token,
                                   opening->plugin_instance_handle,
                                   m->hdr.sender);

    /* Deal with cases where the Plug-In wants stuff fetching */

    if (opening->flags & MPlugIn_Opening_WantsDataResourceFetched)
    {
1330
      const char * url = NULL;
1331 1332 1333 1334 1335 1336 1337

      if (b) url = HtmlRelativiseURL(browser_current_url(b),
                                     HtmlOBJECTdata(((plugin_insts *) opening->browser_instance_handle)->token),
                                     b->stream);

      if (!url) url = HtmlOBJECTdata(((plugin_insts *) opening->browser_instance_handle)->token);

1338 1339 1340 1341 1342
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_got_opening: Plug-In wants data resource fetched\n");
      #endif

      e = plugin_start_new_stream(opening->browser_instance_handle,
1343
                                  url,
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
                                  opening->plugin_instance_handle,
                                  m->hdr.sender);
    }

    plugin_open_reference  = 0;
    plugin_open_tried_once = 0;
    plugin_open_filename   = NULL;

    if (e) return e;

    /* Remove the item from the queue, launching a new one */
    /* if there is anything else there.                    */

    RetError(plugin_remove_head_item());
  }

  #ifdef TRACE

    else
    {
      if (tl & (1u<<30)) Printf("plugin_got_opening: Message reference %x is not known\n", m->hdr.my_ref);

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Unknown message reference %x in plugin_got_opening",
              m->hdr.your_ref);

      return &erb;
    }

    if (tl & (1u<<30)) Printf("plugin_got_opening: Successful\n");
  #endif

  return NULL;
}

/*************************************************/
/* plugin_send_close()                           */
/*                                               */
/* Tell a Plug-In to close down.                 */
/*                                               */
/* Parameters: Browser instance handle for the   */
/*             browser owning the Plug-In;       */
/*                                               */
/*             Task handle for the Plug-In;      */
/*                                               */
/*             Plug-In instance handle.          */
/*************************************************/

_kernel_oserror * plugin_send_close(unsigned int b, unsigned int task, unsigned int instance)
{
  _kernel_oserror * e;
  WimpMessage       m;
  MPlugIn_Close   * close = (MPlugIn_Close *) &m.data;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_send_close: Called\n");
  #endif

  /* Fill in the header... */

  m.hdr.size        = sizeof(MPlugIn_Close) + sizeof(m.hdr);
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Message_PlugIn_Close;

  /* ...and the body... */

  close->flags                   = 0;
  close->plugin_instance_handle  = instance;
  close->browser_instance_handle = b;

  /* ...then send it. */

  e = wimp_send_message(Wimp_EUserMessageRecorded,
                        &m,
                        task,
                        0,
                        NULL);

  #ifdef TRACE
    if (tl & (1u<<30))
    {
      if (e) Printf("plugin_send_close: Exitting with error\n");
      else   Printf("plugin_send_close: Successful\n");
    }
  #endif

  return e;
}

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
/*************************************************/
/* plugin_got_reshape_request()                  */
/*                                               */
/* Handle reception of a                         */
/* Message_PlugIn_ReshapeRequest.                */
/*                                               */
/* Parameters: Pointer to a Wimp_Message struct  */
/*             relevant to the message.          */
/*************************************************/

_kernel_oserror * plugin_got_reshape_request(WimpMessage * m)
{
  MPlugIn_ReshapeRequest * request = (MPlugIn_ReshapeRequest *) &m->data;
  browser_data           * b;
  HStream                * t;
  unsigned int             plugin_task;
  int                      line;
  BBox                     size;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_got_reshape_request: Called\n");
  #endif

  /* Does this look valid? */

  if (!request->browser_instance_handle)
  {
    /* No; do nothing */

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_reshape_request: Message held a NULL browser instance handle\n");
    #endif

    return NULL;
  }

  /* Find the Object representing this Plug-In */

  b = plugin_return_browser(request->browser_instance_handle);

  if (
       !is_known_browser(b) ||
       !object_return_info(b,
                           request->plugin_instance_handle,
                           &t,
                           &plugin_task)
     )
  {
    /* Can't find the Object / not a valid browser */

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_reshape_request: %p isn't a known browser, or can't find an Object to match the given handle\n", b);
    #endif

    return NULL;
  }

  /* Does the task handle match? */

  if (plugin_task != (unsigned int) m->hdr.sender)
  {
    /* No, so exit */

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_reshape_request: Senders task handle of %08x doens't match Object's handle, %08x\n", m->hdr.sender, plugin_task);
    #endif

    return NULL;
  }

  /* Right, this looks like a valid request. Find out */
  /* the ancestor line it lies in so we can reformat  */
  /* the page from here.                              */

  line = tokenutils_find_ancestor_line(b, t);

  /* Get the object's current size */

  RetError(object_get_token_object_size(b, t, &size));

  /* Only proceed if the size has changed */

  if (
       size.xmax - size.xmin != request->width  ||
       size.ymax - size.ymin != request->height
     )
  {
    size.xmin = 0;
    size.ymin = 0;
    size.xmax = request->width;
    size.ymax = request->height;

    RetError(object_set_token_object_size(b, t, &size));

    /* Do a reformat if we had a valid line */

    if (line >= 0) RetError(reformat_format_from(b,
                                                 line - 1,
                                                 1,
                                                 0));
  }

  return NULL;
}

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
/*************************************************/
/* plugin_send_original_reshape()                */
/*                                               */
/* Tell a Plug-In to reposition or resize itself */
/* - not as part of a Plug-In request to resize. */
/*                                               */
/* Parameters: Browser instance handle for the   */
/*             browser owning the Plug-In;       */
/*                                               */
/*             Plug-In task handle;              */
/*                                               */
/*             Plug-In instance handle;          */
/*                                               */
/*             New bounding box for the Plug-In  */
/*             child window (in (parent) window  */
/*             coordinates).                     */
/*************************************************/

_kernel_oserror * plugin_send_original_reshape(unsigned int b, unsigned int task, unsigned int instance, BBox * position)
{
  _kernel_oserror * e;
  WimpMessage       m;
  MPlugIn_Reshape * reshape = (MPlugIn_Reshape *) &m.data;

  #ifdef TRACE
    if (tl & (1u<<30))
    {
      char debugbuf[1024];
      sprintf(debugbuf,"plugin_send_original_reshape: Called for %p to move instance 0x%08x to %d, %d, %d, %d",(plugin_insts *) b,instance,position->xmin,position->ymin,position->xmax,position->ymax);
      Printf("%s\n",debugbuf);
    }
  #endif

  /* Fill in the header... */

  m.hdr.size        = sizeof(MPlugIn_Reshape) + sizeof(m.hdr);
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Message_PlugIn_Reshape;

  /* ...and the body... */

  reshape->flags                   = 0;
  reshape->plugin_instance_handle  = instance;
  reshape->browser_instance_handle = b;
  reshape->parent_window_handle    = ((plugin_insts *) b)->browser->window_handle;
  reshape->parent_area             = *position;

  /* ...then send it. */

  e = wimp_send_message(Wimp_EUserMessageRecorded, &m, task, 0, NULL);

  #ifdef TRACE
    if (tl & (1u<<30))
    {
      if (e) Printf("plugin_send_original_reshape: Exitting with error\n");
      else   Printf("plugin_send_original_reshape: Successful\n");
    }
  #endif

  return e;
}

/*************************************************/
/* plugin_got_url_access()                       */
/*                                               */
/* Handle reception of a                         */
/* Message_PlugIn_URLAccess                      */
/*                                               */
/* Parameters: Pointer to a Wimp_Message struct  */
/*             relevant to the message.          */
/*************************************************/

_kernel_oserror * plugin_got_url_access(WimpMessage * m)
{
  MPlugIn_URLAccess * access = (MPlugIn_URLAccess *) &m->data;
  const char        * url;
  const char        * target;
  browser_data      * targetted;
1618
  browser_data      * b = plugin_return_browser(access->browser_instance_handle);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
  browser_data      * ancestor = utils_ancestor(b);

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_got_url_access: Called\n");
  #endif

  url    = plugin_return_string(m, &access->url);
  target = plugin_return_string(m, &access->target);

  #ifdef TRACE
    if (tl & (1u<<30))
    {
      if (url)    Printf("plugin_got_url_access: URL '%s'\n", url);
      else        Printf("plugin_got_url_access: No URL\n");
      if (target) Printf("plugin_got_url_access: Target '%s'\n", target);
      else        Printf("plugin_got_url_access: No Target\n");
    }
  #endif

  if (url)
  {
1640 1641 1642 1643 1644 1645 1646 1647 1648
    /* Relativise the URL if possible */

    if (b)
    {
      char * new_url = HtmlRelativiseURL(browser_current_url(b), url, b->stream);

      if (new_url) url = new_url;
    }

1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
    if (target)
    {
      /* If a target is given, this is a standard URL fetch in a window */

      if (target) targetted = frames_find_target(b, target);
      else        targetted = NULL;

      /* Don't want to ever open a new window if configured */
      /* to run full screen.                                */

      if (targetted || choices.full_screen)
      {
        /* If a named target was found, open in that. Otherwise we must */
        /* be running full screen, so can't open a new window; in this  */
        /* case, open in the ancestor.                                  */

        RetError(fetchpage_new(targetted ? targetted : ancestor,
                               url,
                               1,
                               1));
      }
      else
      {
        /* If we've reached here, a named target wasn't found but the */
        /* browser isn't running full screen either, so open a new    */
        /* window with the name specified in the link.                */

        RetError(windows_create_browser((char *) url,
                                        NULL,
                                        NULL,
                                        (char *) target,
1680
                                        Windows_CreateBrowser_Normal));
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
      }
    }
    else
    {
      /* If a target is not given, we're supposed to fetch a file */
      /* on behalf of a Plug-In.                                  */

      RetError(plugin_start_new_stream(access->browser_instance_handle,
                                       url,
                                       access->plugin_instance_handle,
                                       m->hdr.sender));
    }
  }

  return NULL;
}

1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
/*************************************************/
/* plugin_got_status()                           */
/*                                               */
/* Handle reception of a Message_PlugIn_Status.  */
/*                                               */
/* Parameters: Pointer to a Wimp_Message struct  */
/*             relevant to the message.          */
/*************************************************/

_kernel_oserror * plugin_got_status(WimpMessage * m)
{
  MPlugIn_Status * status   = (MPlugIn_Status *) &m->data;
  browser_data   * b        = plugin_return_browser(status->browser_instance_handle);
  browser_data   * ancestor = utils_ancestor(b);
  const char     * message;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_got_status: Called\n");
  #endif

  if (!b || !ancestor)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_status: \0211Can't find a valid [ancestor] browser\0217\n");
    #endif

    return NULL;
  }

  message = plugin_return_string(m, &status->status);

  if (!message || !*message)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_status: Null message string or pointer; cancelling Plug-in message status\n");
    #endif

    free(ancestor->plugin_status);
    ancestor->plugin_status = NULL;

    return toolbars_cancel_status(ancestor, Toolbars_Status_PlugIn);
  }

  /* Otherwise, we need to allocate space for the message in */
  /* the browser_data structure so the Toolbar routines can  */
  /* get at it.                                              */

  free(ancestor->plugin_status);
  ancestor->plugin_status = malloc(strlen(message) + 1);

  if (ancestor->plugin_status)
  {
    /* Allocation successful - copy the string over and update */
    /* the status bar.                                         */

    strcpy(ancestor->plugin_status, message);

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_status: Successful with '%s'\n", message);
    #endif

    return toolbars_update_status(ancestor, Toolbars_Status_PlugIn);
  }
  else
  {
    /* Allocation failed - cancel any existing Plug-In message */

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_status: Allocation for message string failed, cancelling status\n");
    #endif

    return toolbars_cancel_status(ancestor, Toolbars_Status_PlugIn);
  }
}

/*************************************************/
/* plugin_got_busy()                             */
/*                                               */
/* Handle reception of a Message_PlugIn_Busy.    */
/*                                               */
/* Parameters: Pointer to a Wimp_Message struct  */
/*             relevant to the message.          */
/*************************************************/

_kernel_oserror * plugin_got_busy(WimpMessage * m)
{
  MPlugIn_Busy * busy     = (MPlugIn_Busy *) &m->data;
  browser_data * b        = plugin_return_browser(busy->browser_instance_handle);
  browser_data * ancestor = utils_ancestor(b);
  int            active;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_got_busy: Called\n");
  #endif

  if (!b || !ancestor)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_busy: \0211Can't find a valid [ancestor] browser\0217\n");
    #endif

    return NULL;
  }

  /* Are we setting or clearing a busy state? */

  active = busy->flags & MPlugIn_Busy_IsBusy;

  if (ancestor->plugin_active != active)
  {
    ancestor->plugin_active = active;

    /* Are we turning it off? */

    if (!active)
    {
      /* If we've only got a handler because of the Plug-In, turn it off */
      /* and if required go to the drift handler instead.                */

      if (!b->anim_handler)
      {
        deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation,b);
        b->anim_handler = 0;

        /* If the Controls say to install the 'drift' handler to ensure   */
        /* the animation finishes on the first frame, and that handler is */
        /* not already installed, install it.                             */

        if (controls.anim_drift && !b->anim_drift)
        {
          register_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation_drift,b);
          b->anim_drift = 1;
        }
      }
    }

    /* The animation should be turned on / kept on */

    else
    {
      /* Deregister any existing drift handler */

      if (b->anim_drift)
      {
        deregister_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation_drift,b);
        b->anim_drift = 0;
      }

      /* Register the full time animation handler, if it isn't already registered */

      if (!b->anim_handler)
      {
        /* Is there an appropriate gadget in the status bar? */

        ObjectId lower = toolbars_get_lower(b);
        BBox     box;

        if (!gadget_get_bbox(0, lower, StatusBarAnimAnim, &box))
        {
          /* Yes, so install an animation handler. Don't set the */
          /* anim_handler flag! The fact that anim_handler is    */
          /* unset and plugin_active is set is used by other     */
          /* bits of code.                                       */

          register_null_claimant(Wimp_ENull,(WimpEventHandler *) toolbars_animation,b);
        }
      }
    }
  }

  return NULL;
}

1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
/*************************************************/
/* plugin_setup_stream()                         */
/*                                               */
/* Sets up a plugin_stream structure in a new    */
/* browser so it may fetch data for a plugin     */
/* owned by another browser.                     */
/*                                               */
/* Not all fields are filled in. The browser     */
/* and token details are, and the URL field is   */
/* filled in. The Mime type is also filled in    */
/* on the basis of a filename extension from the */
/* URL, if possible. Other fields are zeroed.    */
/*                                               */
/* All string_value values are allocated in the  */
/* RMA. It is therefore essential to ensure that */
/* these are always completely freed.            */
/*                                               */
/* Parameters: Browser instance handle for the   */
/*             browser owning the Plug-In;       */
/*                                               */
/*             Pointer to a browser_data struct  */
/*             for the browser that will fetch   */
/*             the data;                         */
/*                                               */
/*             URL to fetch.                     */
/*************************************************/

_kernel_oserror * plugin_setup_stream(unsigned int owner, browser_data * fetcher, const char * url)
{
1900 1901
  plugin_stream * stream;
  HStream       * object = ((plugin_insts *) owner)->token;
1902 1903 1904 1905 1906

  if (!url) return NULL;

  /* Allocate memory for the item */

1907
  stream = calloc(1, sizeof(plugin_stream));
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
  if (!stream) return make_no_cont_memory_error(11);

  /* Now fill it in */

  fetcher->pstream = stream;

  stream->browser_instance_handle = owner;
  stream->token                   = object;

  /* Claim RMA space for the URL */

  RetError(rma_claim(fetcher,
                     strlen(url) + 1,
                     (void **) &stream->url.ptr));

  strcpy(stream->url.ptr, url);

  /* Fill in the Mime type if there's a filename extension */

  if (strrchr(url, '.'))
  {
    char mimetype[MimeMap_MaximumBufferSizeRequired];

    if (!mimemap_extension_to_mime(strrchr(url, '.'),
                                   mimetype,
                                   sizeof(mimetype)))
    {
      RetError(rma_claim(fetcher,
                         strlen(mimetype) + 1,
                         (void **) &stream->mime.ptr));
1938

1939
      strcpy(stream->mime.ptr, mimetype);
1940
    }
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    else stream->mime.ptr = NULL;
  }
  else stream->mime.ptr = NULL;

  /* Finished */

  return NULL;
}

/*************************************************/
/* plugin_start_new_stream()                     */
/*                                               */
/* Begin a fetch to a temporary file. When we    */
/* have enough data to know the data type,       */
/* the fetcher will start the stream message     */
/* protocol, by calling                          */
/* plugin_send_original_stream_new.              */
/*                                               */
/* Parameters: Browser instance handle for the   */
/*             browser owning the Plug-In;       */
/*                                               */
/*             URL to fetch;                     */
/*                                               */
1964
/*             Plug-In instance handle;          */
1965 1966 1967 1968 1969 1970
/*                                               */
/*             Plug-In task handle.              */
/*************************************************/

_kernel_oserror * plugin_start_new_stream(unsigned int b, const char * data, unsigned int instance, unsigned int task)
{
1971 1972
  plugin_stream * pstream;
  const char    * file;
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_start_new_stream: Called for %p, instance 0x%08x\n",(plugin_insts *) b,instance);
  #endif

  if (!data || !*data)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_start_new_stream: No URL to fetch - exitting\n");
    #endif

    return NULL;
  }

  /* See if we've already got a temporary file in Scrap relating to this URL */

  file = plugin_find_file(data);

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_start_new_stream: Initiate fetch for '%s'\n",data);
  #endif

  /* Initiate a fetch for this item. If we've got a file holding */
  /* the data already, give a null URL - windows_create_browser  */
  /* knows what this means (in the context of an object fetch).  */

  RetError(windows_create_browser(file ? NULL : (char *) data,
                                  NULL,
                                  NULL,
                                  NULL,
2003
                                  Windows_CreateBrowser_ForPlugIn));
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037

  /* Add stream related information to the browser */

  RetError(plugin_setup_stream(b,
                               last_browser,
                               data));

  /* Fill in more information for the stream we're going to */
  /* try and establish                                      */

  pstream = last_browser->pstream;

  pstream->browser_instance_handle = b;
  pstream->token                   = ((plugin_insts *) b)->token; /* (Because this is externally visible, whereas plugin_insts structures aren't) */

  pstream->plugin_instance_handle  = instance;
  pstream->plugin_task_handle      = task;

  if (file)
  {
    /* If we've got a file already, need to remember its filename and */
    /* start the message protocol to tell the Plug-In about it.       */

    RetError(rma_claim(last_browser,
                       strlen(file) + 1,
                       (void **) &pstream->filename.ptr));

    strcpy(pstream->filename.ptr, file);

    RetError(plugin_send_original_stream_new(last_browser));
  }

  /* OK, finished. The fetcher will now be called on Nulls (since we */
  /* did windows_create_browser with a URL to start fetching on and  */
2038
  /* set the flag to say 'for a Plug-In'), so the fetcher will now   */
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
  /* deal with things from here for a while.                         */

  return NULL;
}

/*************************************************/
/* plugin_send_original_stream_new()             */
/*                                               */
/* Sent out a Message_PlugIn_StreamNew to start  */
/* a fetch on a Plug-Ins behalf.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the stream - its      */
/*             'pstream' field should point to a */
/*             valid plugin_stream struct.       */
/*************************************************/

_kernel_oserror * plugin_send_original_stream_new(browser_data * b)
{
  WimpMessage         m;
  MPlugIn_StreamNew * stream  = (MPlugIn_StreamNew *) &m.data;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_send_original_stream_new: Called for %p\n",b);
  #endif

  if (!b->pstream)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_send_original_stream_new: No plugin_stream - exitting\n");
    #endif

    return NULL;
  }

  /* Fill in the header... */

  m.hdr.size        = sizeof(MPlugIn_StreamNew) + sizeof(m.hdr);
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Message_PlugIn_StreamNew;

  /* ...and the body... */

  stream->flags                          = 0;

  stream->plugin_instance_handle         = b->pstream->plugin_instance_handle;
  stream->browser_instance_handle        = (unsigned int) b->pstream->browser_instance_handle;

  stream->browser_stream_instance_handle = (unsigned int) b;

  stream->url.ptr                        = b->pstream->url.ptr;

  stream->eos                            = 0;
  stream->last_modified                  = 0;
  stream->notify                         = 0;

  stream->mime_type.ptr                  = b->pstream->mime.ptr;
  stream->window_target.ptr              = b->pstream->target.ptr;

  /* Now send it. The reply should give us details on the flags (type */
  /* of stream) and the plugin's stream instance handle.              */
  /*                                                                  */
  /* Of course, if it bounces we need to cancel the fetch, free up    */
  /* all the various bits and pieces assocated with the stream        */
  /* including the RMA space, and so-forth.                           */

  RetError(wimp_send_message(Wimp_EUserMessageRecorded,
                             &m,
                             b->pstream->plugin_task_handle,
                             -1,
                             NULL));

  plugin_stream_new_reference = m.hdr.my_ref;

  return NULL;
}

/*************************************************/
/* plugin_got_stream_new()                       */
/*                                               */
/* Handles reception of a                        */
/* Message_PlugIn_StreamNew. Will consequently   */
/* continue a fetch on behalf of a Plug-In,      */
/* issuing Message_PlugIn_StreamAsFile when the  */
/* fetch is complete.                            */
/*                                               */
/* True streaming is not supported yet.          */
/*                                               */
/* Parameters: Pointer to a WimpMessage struct   */
/*             holding the message details.      */
/*************************************************/

_kernel_oserror * plugin_got_stream_new(WimpMessage * m)
{
  MPlugIn_StreamNew * stream = (MPlugIn_StreamNew *) &m->data;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_got_stream_new: Called\n");
  #endif

  if (m->hdr.your_ref == plugin_stream_new_reference)
  {
    browser_data * b;

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_got_stream_new: Message reference %x is known\n", m->hdr.your_ref);
    #endif

    /* Get the browser that is fetching - this is stored as the */
    /* stream instance handle.                                  */

    b = (browser_data *) stream->browser_stream_instance_handle;

    if (!is_known_browser(b))
2153
    {
2154 2155 2156
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_got_stream_new: Browser %p is not known...\n", b);
      #endif
2157

2158 2159 2160 2161 2162
      return NULL;
    }

    b->pstream->plugin_stream_handle = stream->plugin_stream_instance_handle;
    b->pstream->stream_flags         = stream->flags;
2163

2164 2165 2166 2167 2168
    /* We should take different action according to the flags, but at */
    /* the moment only 'fetch as file' is supported.                  */

    if (b->pstream->stream_flags & MPlugIn_StreamNew_StreamTypeMask != MPlugIn_StreamNew_StreamTypeAsFileOnly)
    {
2169
      #ifdef TRACE
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180

        if (tl & (1u<<30)) Printf("plugin_got_stream_new: Can't handle stream type %d\n", b->pstream->stream_flags & MPlugIn_StreamNew_StreamTypeMask);

        erb.errnum = Utils_Error_Custom_Message;

        sprintf(erb.errmess,
                "Can't handle stream type %d in plugin_got_stream_new",
                b->pstream->stream_flags & MPlugIn_StreamNew_StreamTypeMask);

        show_error_ret(&erb);

2181 2182
      #endif

2183 2184 2185
      return plugin_send_original_stream_destroy(b,
                                                 MPlugIn_StreamDestroy_Reason_Error);
    }
2186

2187 2188 2189 2190 2191
    /* OK, we can handle the stream type. In that case, just exit;  */
    /* the fetcher will keep fetching, and when complete, will give */
    /* the file to the Plug-In.                                     */
    /*                                                              */
    /* So that it knows to do this, mark the stream as active.      */
2192

2193 2194
    b->pstream->active = 1;
  }
2195

2196
  #ifdef TRACE
2197

2198 2199 2200
    else
    {
      if (tl & (1u<<30)) Printf("plugin_got_stream_new: Message reference %x is not known\n", m->hdr.your_ref);
2201

2202
      erb.errnum = Utils_Error_Custom_Normal;
2203

2204 2205 2206
      sprintf(erb.errmess,
              "Unknown message reference %x in plugin_got_stream_new",
              m->hdr.your_ref);
2207

2208 2209
      return &erb;
    }
2210

2211
    if (tl & (1u<<30)) Printf("plugin_got_stream_new: Successful\n");
2212

2213
  #endif
2214

2215 2216
  return NULL;
}
2217

2218 2219 2220 2221 2222 2223 2224 2225 2226
/*************************************************/
/* plugin_stream_new_bounced()                   */
/*                                               */
/* Handles a Message_PlugIn_StreamNew bounce,    */
/* aborting any associated fetch.                */
/*                                               */
/* Parameters: Pointer to a WimpMessage struct   */
/*             holding the message details.      */
/*************************************************/
2227

2228 2229 2230
_kernel_oserror * plugin_stream_new_bounced(WimpMessage * m)
{
  MPlugIn_StreamNew * stream = (MPlugIn_StreamNew *) &m->data;
2231

2232 2233 2234
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_stream_new_bounced: Called\n");
  #endif
2235

2236 2237
  /* Do we recognise the message (checking my_ref not your_ref since this */
  /* is a bounce, not a reply)?                                           */
2238

2239 2240 2241
  if (m->hdr.my_ref == plugin_stream_new_reference)
  {
    browser_data * b;
2242

2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_stream_new_bounced: Message reference %x is known\n", m->hdr.my_ref);
    #endif

    /* Get the browser that is fetching - this is stored as the */
    /* stream instance handle.                                  */

    b = (browser_data *) stream->browser_stream_instance_handle;

    if (!is_known_browser(b))
    {
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_stream_new_bounced: Browser %p is not known...\n", b);
2256 2257
      #endif

2258 2259
      return NULL;
    }
2260

2261 2262
    /* The browser is known (current), so close it down. This takes */
    /* care of everything, including memory management issues.      */
2263

2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
    windows_close_browser(b);
  }

  #ifdef TRACE

    else
    {
      if (tl & (1u<<30)) Printf("plugin_stream_new_bounced: Message reference %x is not known\n", m->hdr.my_ref);

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Unknown message reference %x in plugin_stream_new_bounced",
              m->hdr.my_ref);
2278

2279
      return &erb;
2280
    }
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323

    if (tl & (1u<<30)) Printf("plugin_stream_new_bounced: Successful\n");
  #endif

  return NULL;
}

/*************************************************/
/* plugin_send_original_stream_destroy()         */
/*                                               */
/* Send a Message_PlugIn_StreamDestroy to a      */
/* Plug-In, to close down a stream between a     */
/* browser and a Plug-In.                        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             that was fetching data for the    */
/*             Plug-In, not the one that owns    */
/*             the actual Plug-In instance.      */
/*                                               */
/*             Reason for shutting down the      */
/*             stream (see reason code           */
/*             definitions in PlugIn.h).         */
/*************************************************/

_kernel_oserror * plugin_send_original_stream_destroy(browser_data * b, int reason)
{
  WimpMessage             m;
  MPlugIn_StreamDestroy * destroy = (MPlugIn_StreamDestroy *) &m.data;

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_send_original_stream_destroy: Called for %p\n",b);
  #endif

  /* Can't do anything if we don't have a browser with an */
  /* associated plugin_stream structure                   */

  if (!b || !b->pstream)
  {
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_send_original_stream_destroy: Null browser or plugin_stream pointer; exitting\n");
    #endif

    return NULL;
2324 2325
  }

2326 2327 2328 2329 2330 2331 2332 2333 2334
  /* Fill in the header... */

  m.hdr.size        = sizeof(MPlugIn_StreamDestroy) + sizeof(m.hdr);
  m.hdr.your_ref    = 0;
  m.hdr.action_code = Message_PlugIn_StreamDestroy;

  /* ...and the body... */

  destroy->flags                          = 0;
2335

2336 2337
  destroy->plugin_instance_handle         = b->pstream->plugin_instance_handle;
  destroy->browser_instance_handle        = b->pstream->browser_instance_handle;
2338

2339 2340
  destroy->plugin_stream_instance_handle  = b->pstream->plugin_stream_handle;
  destroy->browser_stream_instance_handle = (unsigned int) b;
2341

2342
  destroy->url.ptr                        = b->pstream->url.ptr;
2343

2344 2345 2346
  destroy->eos                            = b->pstream->eos;
  destroy->last_modified                  = b->pstream->last_modified;
  destroy->notify                         = b->pstream->notify;
2347

2348
  destroy->reason                         = reason;
2349

2350
  /* Send it */
2351

2352 2353 2354 2355 2356
  return wimp_send_message(Wimp_EUserMessage,
                           &m,
                           b->pstream->plugin_task_handle,
                           -1,
                           NULL);
2357 2358 2359
}

/*************************************************/
2360
/* plugin_abort_stream()                         */
2361
/*                                               */
2362 2363 2364 2365
/* Stop any fetch on behalf of a Plug-In, and    */
/* free all associated structures (with the      */
/* exception of the fetching browser_data        */
/* struct itself).                               */
2366
/*                                               */
2367 2368 2369
/* Parameters: Pointer to a browser_data struct  */
/*             which was dealing with the data   */
/*             streaming.                        */
2370 2371
/*************************************************/

2372
_kernel_oserror * plugin_abort_stream(browser_data * b)
2373 2374
{
  #ifdef TRACE
2375
    if (tl & (1u<<30)) Printf("plugin_abort_stream: Called for browser %p\n", b);
2376 2377
  #endif

2378
  if (!b) return NULL;
2379

2380 2381
  /* Do we have an active stream? If so, must tell the Plug-In */
  /* that the stream is being destroyed.                       */
2382

2383 2384 2385
  if (b->pstream->active)
  {
    RetError(plugin_send_original_stream_destroy(b, MPlugIn_StreamDestroy_Reason_User));
2386

2387 2388 2389
    /* If we have a temporary file, remove it */

    if (b->pstream->filename.ptr) remove(b->pstream->filename.ptr);
2390 2391
  }

2392
  /* Now free the various RMA components */
2393

2394 2395 2396 2397
  if (b->pstream->url.ptr)      rma_release(b, b->pstream->url.ptr);
  if (b->pstream->mime.ptr)     rma_release(b, b->pstream->mime.ptr);
  if (b->pstream->target.ptr)   rma_release(b, b->pstream->target.ptr);
  if (b->pstream->filename.ptr) rma_release(b, b->pstream->filename.ptr);
2398

2399
  /* Finally, free the plugin_stream structure itself */
2400

2401 2402
  free(b->pstream);
  b->pstream = NULL;
2403

2404
  /* Finished */
2405

2406
  #ifdef TRACE
2407
    if (tl & (1u<<30)) Printf("plugin_abort_stream: Successful\n");
2408 2409 2410 2411 2412 2413
  #endif

  return NULL;
}

/*************************************************/
2414
/* plugin_fetch_completed()                      */
2415
/*                                               */
2416 2417
/* Called when a fetch for a Plug-In has been    */
/* completed (e.g. by fetchpage_fetch).          */
2418
/*                                               */
2419 2420 2421 2422
/* Parameters: Pointer to the browser_data       */
/*             struct doing the fetch, with its  */
/*             'pstream' field pointing to a     */
/*             valid plugin_stream struct.       */
2423 2424
/*************************************************/

2425
_kernel_oserror * plugin_fetch_completed(browser_data * b)
2426
{
2427 2428
  WimpMessage            m;
  MPlugIn_StreamAsFile * stream = (MPlugIn_StreamAsFile *) &m.data;
2429 2430

  #ifdef TRACE
2431
    if (tl & (1u<<30)) Printf("plugin_fetch_completed: Called for %p\n", b);
2432 2433
  #endif

2434
  if (!b || !b->pstream) return NULL;
2435

2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
  if (!b->pstream->active)
  {
    #ifdef TRACE

      if (tl & (1u<<30)) Printf("plugin_fetch_completed: Stream is inactive, exitting\n");

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Stream associated with browser %p is not active in plugin_fetch_completed",
              b);

      show_error_ret(&erb);

    #endif

    return NULL;
  }

  /* First, tell the Plug-In about the stream */

  m.hdr.size        = sizeof(MPlugIn_StreamAsFile) + sizeof(m.hdr);
2458
  m.hdr.your_ref    = 0;
2459
  m.hdr.action_code = Message_PlugIn_StreamAsFile;
2460

2461
  /* Fill in the body */
2462

2463
  stream->flags                          = 0;
2464

2465 2466
  stream->plugin_instance_handle         = b->pstream->plugin_instance_handle;
  stream->browser_instance_handle        = b->pstream->browser_instance_handle;
2467

2468 2469 2470 2471
  stream->plugin_stream_instance_handle  = b->pstream->plugin_stream_handle;
  stream->browser_stream_instance_handle = (int) b;

  stream->url.ptr                        = b->pstream->url.ptr;
2472

2473 2474 2475
  stream->eos                            = b->pstream->eos;
  stream->last_modified                  = b->pstream->last_modified;
  stream->notify                         = b->pstream->notify;
2476

2477 2478 2479
  stream->pathname.ptr                   = b->pstream->filename.ptr;

  /* Send the message */
2480 2481

  #ifdef TRACE
2482
    if (tl & (1u<<30)) Printf("plugin_fetch_completed: Sending Message_PlugIn_StreamAsFile\n");
2483 2484
  #endif

2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
  RetError(wimp_send_message(Wimp_EUserMessage,
                             &m,
                             b->pstream->plugin_task_handle,
                             -1,
                             NULL));

  /* Install a null handler to close the stream down. Must do this */
  /* to give the Plug-In time to deal with the message we've sent  */
  /* before we free all the strings it points to!                  */
  /*                                                               */
  /* Mark that this has been installed in the plugin_stream struct */
  /* so fetcher functions know not to close the window themselves. */

  b->pstream->will_close_itself = 1;

  register_null_claimant(Wimp_ENull, (WimpEventHandler *) plugin_close_stream, b);

  /* OK, finished. */

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_fetch_completed: Successful\n");
  #endif

  return NULL;
2509 2510 2511
}

/*************************************************/
2512
/* plugin_close_stream()                         */
2513
/*                                               */
2514 2515 2516
/* Closes down a stream for a browser by marking */
/* it as inactive and calling                    */
/* windows_close_browser.                        */
2517
/*                                               */
2518 2519 2520 2521
/* Parameters are as for a standard Wimp event   */
/* handler (this is called on null events).      */
/* The handle should point to a browser_data     */
/* struct that is dealing with the stream.       */
2522 2523
/*************************************************/

2524
static int plugin_close_stream(int eventcode, WimpPollBlock * b, IdBlock * idb, browser_data * handle)
2525 2526
{
  #ifdef TRACE
2527
    if (tl & (1u<<30)) Printf("plugin_close_stream: Called for %p\n",handle);
2528 2529
  #endif

2530
  /* Only proceed if this is a known browser */
2531

2532 2533 2534 2535
  if (is_known_browser(handle))
  {
    handle->pstream->active            = 0;
    handle->pstream->will_close_itself = 0;
2536

2537 2538 2539 2540 2541 2542
    /* We've flagged the stream as inactive, so windows_close_browser */
    /* (see below) won't end up sending Message_PlugIn_StreamDestroy  */
    /* to the Plug-In. So we must send it now (quoting a reason code  */
    /* of success, rather than error or user, as would have been sent */
    /* via. windows_close_browser if we'd left the stream flagged as  */
    /* active).                                                       */
2543

2544 2545
    plugin_send_original_stream_destroy(handle,
                                        MPlugIn_StreamDestroy_Reason_Success);
2546

2547 2548 2549
    /* Record the temporary file details so it can be removed on shutdown. */
    /* Only do this if the browser had a URL to fetch, since otherwise,    */
    /* we went through an existing file entry.                             */
2550

2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
    plugin_add_file_entry(handle->pstream->filename.ptr, handle->pstream->url.ptr);

    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_close_stream: Calling windows_close_browser\n");
    #endif

    /* Now shut the fetch down, freeing browser allocated memory */
    /* associated with the stream in passing.                    */

    windows_close_browser(handle);
  }
2562 2563

  #ifdef TRACE
2564 2565

    else
2566
    {
2567 2568 2569
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_close_stream: This is not a recognised current browser...\n");
      #endif
2570
    }
2571

2572 2573
  #endif

2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
  /* Deregister the handler */

  deregister_null_claimant(Wimp_ENull, (WimpEventHandler *) plugin_close_stream, handle);

  /* Finished */

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_close_stream: Successful\n");
  #endif

  return 0;
2585
}
2586

2587
/*************************************************/
2588
/* plugin_add_file_entry()                       */
2589
/*                                               */
2590 2591 2592 2593
/* Adds a record of a Plug-In temporary file to  */
/* the list of such records. Such files are      */
/* deleted later, for example, on calling        */
/* plugin_shutdown.                              */
2594
/*                                               */
2595 2596
/* Parameters: Pointer to the pathname to        */
/*             record;                           */
2597
/*                                               */
2598 2599
/*             Pointer to the URL this relates   */
/*             to, or NULL if unknown.           */
2600 2601
/*************************************************/

2602
static _kernel_oserror * plugin_add_file_entry(const char * pathname, const char * url)
2603
{
2604 2605
  plugin_files * entry;
  int            plen, ulen;
2606 2607

  #ifdef TRACE
2608
    if (tl & (1u<<30)) Printf("plugin_add_file_entry: Called\n");
2609 2610
  #endif

2611
  if (!pathname || !*pathname)
2612 2613
  {
    #ifdef TRACE
2614
      if (tl & (1u<<30)) Printf("plugin_add_file_entry: Null pathname or null string; exitting\n");
2615 2616 2617 2618 2619
    #endif

    return NULL;
  }

2620
  /* Is this entry already present? */
2621

2622
  entry = plugin_files_head;
2623

2624 2625 2626
  while (entry)
  {
    /* If so, just exit */
2627

2628
    if (entry->url && url && !strcmp(entry->url, url)) return NULL;
2629

2630 2631
    entry = entry->next;
  }
2632

2633
  /* Find out string lengths */
2634

2635 2636
  plen = strlen(pathname);
  ulen = url ? strlen(url) : 0;
2637

2638
  /* Allocate memory for the structure itself */
2639

2640 2641
  entry = malloc(sizeof(plugin_files));
  if (!entry) return make_no_memory_error(23);
2642

2643
  /* Allocate memory for the strings and copy them in */
2644

2645 2646 2647 2648
  if (plen)
  {
    entry->pathname = malloc(plen + 1);
    if (!entry->pathname) return make_no_memory_error(23);
2649

2650 2651 2652
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_add_file_entry: Adding pathname '%s'\n", pathname);
    #endif
2653

2654 2655 2656
    strcpy(entry->pathname, pathname);
  }
  else entry->pathname = NULL;
2657

2658 2659 2660 2661
  if (ulen)
  {
    entry->url = malloc(ulen + 1);
    if (!entry->url) return make_no_memory_error(23);
2662

2663 2664 2665
    #ifdef TRACE
      if (tl & (1u<<30)) Printf("plugin_add_file_entry: Adding URL '%s'\n", url);
    #endif
2666

2667 2668 2669
    strcpy(entry->url, url);
  }
  else entry->url = NULL;
2670

2671
  /* Link the item in */
2672

2673 2674
  entry->next       = plugin_files_head;
  plugin_files_head = entry;
2675

2676
  /* Finished */
2677

2678 2679 2680
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_add_file_entry: Successful\n");
  #endif
2681 2682 2683 2684 2685

  return NULL;
}

/*************************************************/
2686
/* plugin_find_file()                            */
2687
/*                                               */
2688 2689 2690
/* Checks the records of Plug-In temporary files */
/* to see if one matches the given URL, and if   */
/* so returns a pointer to the filename.         */
2691
/*                                               */
2692
/* Parameters: Pointer to the URL to check.      */
2693 2694
/*                                               */
/*                                               */
2695 2696
/* Returns:    Pointer to the filename relates   */
/*             to, or NULL if unknown.           */
2697 2698
/*************************************************/

2699
static const char * plugin_find_file(const char * url)
2700
{
2701
  plugin_files * entry = plugin_files_head;
2702

2703 2704 2705 2706 2707
  while (entry)
  {
    if (entry->url && !strcmp(entry->url, url)) return entry->pathname;
    else entry = entry->next;
  }
2708

2709 2710
  return NULL;
}
2711

2712 2713 2714 2715 2716 2717
/*************************************************/
/* plugin_flush_files()                          */
/*                                               */
/* Removes all records of Plug-In temporary      */
/* files, deleteing (sp?) the files as it goes.  */
/*************************************************/
2718

2719 2720 2721 2722
static void plugin_flush_files(void)
{
  plugin_files * entry = plugin_files_head;
  plugin_files * next;
2723

2724 2725 2726 2727 2728
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_flush_files: Called\n");
  #endif

  while (entry)
2729
  {
2730
    next = entry->next;
2731

2732 2733 2734
    if (entry->url)
    {
      /* Free the URL string */
2735

2736 2737 2738
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_flush_files: Free URL '%s'\n", entry->url);
      #endif
2739

2740 2741
      free(entry->url);
    }
2742

2743 2744 2745
    if (entry->pathname)
    {
      /* Delete the temporary file and free the pathname string */
2746

2747 2748 2749 2750 2751 2752 2753 2754 2755
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_flush_files: Remove and free pathname '%s'\n", entry->pathname);
      #endif

      remove(entry->pathname);
      free  (entry->pathname);
    }

    /* Free the structure itself */
2756 2757

    #ifdef TRACE
2758
      if (tl & (1u<<30)) Printf("plugin_flush_files: Free entry '%p'\n", entry);
2759 2760
    #endif

2761 2762 2763 2764 2765
    free(entry);

    /* Move to the next item */

    entry = next;
2766 2767 2768
  }

  #ifdef TRACE
2769
    if (tl & (1u<<30)) Printf("plugin_flush_files: Successful\n");
2770 2771
  #endif

2772 2773 2774
  plugin_files_head = NULL;

  return;
2775 2776 2777
}

/*************************************************/
2778
/* plugin_add_instance_entry()                   */
2779
/*                                               */
2780 2781 2782 2783 2784 2785
/* Adds a record of a Plug-In to the list of     */
/* records, which the browser points to in the   */
/* browser instance handles quoted back to those */
/* Plug-Ins. This allows a one word handle to    */
/* give back information on the browser, token,  */
/* Plug-In task handle, etc.                     */
2786
/*                                               */
2787 2788 2789 2790 2791 2792 2793 2794
/* Parameters: Pointer to a browser_data struct  */
/*             which owns the Plug-In;           */
/*                                               */
/*             Pointer to an HStream struct      */
/*             representing the Plug-In;         */
/*                                               */
/*             The Plug-In task handle, if       */
/*             known (else 0).                   */
2795 2796
/*************************************************/

2797
static _kernel_oserror * plugin_add_instance_entry(browser_data * b, HStream * t, unsigned int task)
2798
{
2799
  plugin_insts * entry;
2800 2801

  #ifdef TRACE
2802
    if (tl & (1u<<30)) Printf("plugin_add_instance_entry: Called for %p, %p, 0x%08x\n",b,t,task);
2803 2804
  #endif

2805
  /* Allocate space for the entry */
2806

2807 2808
  entry = malloc(sizeof(plugin_insts));
  if (!entry) return make_no_memory_error(24);
2809

2810
  /* Fill the entry in */
2811

2812 2813 2814
  entry->browser            = b;
  entry->token              = t;
  entry->plugin_task_handle = task;
2815

2816 2817
  entry->next               = plugin_insts_head;
  plugin_insts_head         = entry;
2818

2819
  /* Finished */
2820

2821 2822 2823
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_add_instance_entry: Successful (new entry %p)\n",entry);
  #endif
2824

2825 2826
  return NULL;
}
2827

2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
/*************************************************/
/* plugin_find_instance_entry()                  */
/*                                               */
/* Finds an entry in the list of structures      */
/* holding information on Plug-Ins, based on the */
/* owning browser and representing HStream       */
/* struct.                                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which owns the Plug-In;           */
/*                                               */
/*             Pointer to an HStream struct      */
/*             representing the Plug-In.         */
/*                                               */
/* Returns:    Pointer to the entry, or NULL if  */
/*             not found.                        */
/*************************************************/
2845

2846 2847 2848
static plugin_insts * plugin_find_instance_entry(browser_data * b, HStream * t)
{
  plugin_insts * entry = plugin_insts_head;
2849

2850 2851 2852
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_find_instance_entry: Called for %p, %p\n",b,t);
  #endif
2853

2854 2855 2856 2857 2858 2859 2860
  while (entry)
  {
    if (entry->browser == b && entry->token == t)
    {
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_find_instance_entry: Found one, returning %p\n",entry);
      #endif
2861

2862 2863
      return entry;
    }
2864

2865 2866
    entry = entry->next;
  }
2867 2868

  #ifdef TRACE
2869
    if (tl & (1u<<30)) Printf("plugin_find_instance_entry: No entry found, returning NULL\n");
2870 2871 2872 2873 2874 2875
  #endif

  return NULL;
}

/*************************************************/
2876
/* plugin_obtain_instance()                      */
2877
/*                                               */
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892
/* Returns an instance handle for a Plug-In      */
/* based on the owning browser and representing  */
/* HStream struct.                               */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             which owns the Plug-In;           */
/*                                               */
/*             Pointer to an HStream struct      */
/*             representing the Plug-In;         */
/*                                               */
/*             Pointer to an unsigned int, in    */
/*             which the handle is written.      */
/*                                               */
/* Assumes:    The int pointer may *not* be      */
/*             NULL.                             */
2893 2894
/*************************************************/

2895
_kernel_oserror * plugin_obtain_instance(browser_data * b, HStream * t, unsigned int * instance)
2896
{
2897
  plugin_insts * browser_instance = NULL;
2898 2899

  *instance = 0;
2900 2901

  #ifdef TRACE
2902
    if (tl & (1u<<30)) Printf("plugin_obtain_instance: Called for %p, %p\n",b,t);
2903 2904
  #endif

2905
  browser_instance = plugin_find_instance_entry(b, t);
2906

2907
  if (!browser_instance)
2908 2909
  {
    #ifdef TRACE
2910
      if (tl & (1u<<30)) Printf("plugin_obtain_instance: No existing item, creating a new one\n");
2911 2912
    #endif

2913
    RetError(plugin_add_instance_entry(b, t, 0));
2914

2915
    browser_instance = plugin_insts_head;
2916
  }
2917 2918 2919 2920 2921 2922 2923 2924

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_obtain_instance: Returning %p\n",b,t);
  #endif

  *instance = (unsigned int) browser_instance;

  return NULL;
2925 2926 2927
}

/*************************************************/
2928
/* plugin_remove_instance_entry()                */
2929
/*                                               */
2930 2931
/* Removes a record of a Plug-In from the list   */
/* of such records.                              */
2932
/*                                               */
2933 2934 2935 2936
/* Parameters: Pointer to the entry (i.e. the    */
/*             browser instance handle quoted to */
/*             the Plug-In to which the entry    */
/*             refers).                          */
2937 2938
/*************************************************/

2939
static void plugin_remove_instance_entry(plugin_insts * remove)
2940
{
2941 2942
  plugin_insts * entry = plugin_insts_head;
  plugin_insts * prev  = NULL;
2943 2944

  #ifdef TRACE
2945
    if (tl & (1u<<30)) Printf("plugin_remove_instance_entry: Called for %p\n",remove);
2946 2947
  #endif

2948
  /* Find the item */
2949

2950
  while (entry)
2951
  {
2952 2953 2954 2955
    if (entry == remove)
    {
      /* Found item, so remove it, linking the previous */
      /* item to its next item as required.             */
2956

2957 2958 2959
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_remove_instance_entry: Found it, removing item\n");
      #endif
2960

2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
      if (prev) prev->next   = entry->next;
      else plugin_insts_head = entry->next;

      free(entry);

      break;
    }

    /* Not found it, so move on to the next item */

    prev  = entry;
    entry = entry->next;
2973 2974
  }

2975 2976 2977
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_remove_instance_entry: Successful\n");
  #endif
2978

2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
  return;
}

/*************************************************/
/* plugin_flush_instance_entries()               */
/*                                               */
/* Removes all Plug-In instance entries for a    */
/* given browser, or unconditionally, all items. */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             for which items will be removed,  */
/*             or NULL for all of them.          */
/*************************************************/

void plugin_flush_instance_entries(browser_data * b)
{
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_flush_instance_entries: Called for %p\n",b);
  #endif

  if (!b)
3000
  {
3001
    /* Simple case first */
3002

3003 3004 3005 3006 3007 3008 3009 3010
    while (plugin_insts_head)
    {
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("plugin_flush_instance_entries: Removing entry %p\n", plugin_insts_head);
      #endif

      plugin_remove_instance_entry(plugin_insts_head);
    }
3011 3012 3013
  }
  else
  {
3014 3015
    plugin_insts * entry = plugin_insts_head;
    plugin_insts * next;
3016

3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
    /* Slightly more complex case for a specific browser */

    while (entry)
    {
      next = entry->next;

      if (entry->browser == b)
      {
        #ifdef TRACE
          if (tl & (1u<<30)) Printf("plugin_flush_instance_entries: Removing entry %p\n", entry);
        #endif

        plugin_remove_instance_entry(entry);
      }

      entry = next;
    }
3034
  }
3035 3036 3037 3038 3039 3040

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_flush_instance_entries: Successful %p\n",b);
  #endif

  return;
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057
}

/*************************************************/
/* plugin_shutdown()                             */
/*                                               */
/* Closes down all activities, flushing the      */
/* Plug-In queue and freeing any RMA associated  */
/* with string_value fields for in-transit       */
/* messages.                                     */
/*************************************************/

void plugin_shutdown(void)
{
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_shutdown: Called\n");
  #endif

3058 3059
  /* In transit Message_PlugIn_Open broadcasts */

3060 3061
  if (plugin_open_filename)
  {
3062 3063
    rma_release(NULL, plugin_open_filename);
    plugin_open_filename = NULL;
3064 3065
  }

3066
  /* Clear other message fields */
3067 3068 3069 3070

  plugin_open_reference  = 0;
  plugin_open_tried_once = 0;

3071 3072
  /* For now that's all we have to do for messages. Now */
  /* flush out the various queues and lists.            */
3073 3074

  plugin_flush_queue(NULL, 0);
3075 3076 3077 3078
  plugin_flush_files();
  plugin_flush_instance_entries(NULL);

  /* Finished */
3079 3080 3081 3082 3083 3084 3085

  #ifdef TRACE
    if (tl & (1u<<30)) Printf("plugin_shutdown: Successful\n");
  #endif

  return;
}