home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / include / libmocha.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  16.3 KB  |  544 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * Header file for Mocha in the Navigator (libmocha).
  21.  */
  22.  
  23. #ifndef libmocha_h___
  24. #define libmocha_h___
  25.  
  26. #include "ntypes.h"
  27. #include "il_types.h"
  28. #include "prtypes.h"
  29. #include "plhash.h"
  30. #include "prthread.h"
  31. #include "jsapi.h"
  32.  
  33. /* enable JavaScript Debugger support */
  34. #if defined (_WIN32) || defined(XP_UNIX) || defined(powerc) || defined(__powerc) || defined(XP_OS2)
  35. #ifdef JAVA
  36. #define JSDEBUGGER 1 
  37. #endif
  38. #endif
  39.  
  40. NSPR_BEGIN_EXTERN_C
  41.  
  42. typedef struct JSTimeout JSTimeout;
  43. typedef struct JSPrincipalsList JSPrincipalsList;
  44. typedef struct JSNestingUrl JSNestingUrl;
  45.  
  46. /*
  47.  * There exists one MochaDecoder per top-level MWContext that decodes Mocha,
  48.  * either from an HTML page or from a "mocha:[expr]" URL.
  49.  */
  50. typedef struct MochaDecoder {
  51.     int32           forw_count;            /* forward reference count */
  52.     int32           back_count;            /* back (up the tree) count */
  53.     JSContext        *js_context;
  54.     MWContext       *window_context;
  55.     JSObject        *window_object;
  56.     NET_StreamClass *stream;
  57.     int32           stream_owner;   /* id of layer that's loading the stream */
  58.     URL_Struct      *url_struct;
  59.     JSTimeout        *timeouts;
  60.     JSTimeout       *saved_timeouts;
  61.     uint16          signature_ordinal;
  62.     PRPackedBool    replace_location;
  63.     PRPackedBool    resize_reload;
  64.     PRPackedBool    load_event_sent;
  65.     PRPackedBool    visited;
  66.     PRPackedBool    writing_input;
  67.     PRPackedBool    free_stream_on_close;
  68.     PRPackedBool    in_window_quota;
  69.     PRPackedBool    called_win_close;
  70.     PRPackedBool    principals_compromised;
  71.     const char      *source_url;
  72.     JSNestingUrl    *nesting_url;
  73.     uint32          error_count;
  74.     uint32          event_mask;
  75.     int32           active_layer_id;
  76.     uint32          active_form_id;
  77.     uint32        event_bit;
  78.     int32        doc_id;
  79.  
  80.     /*
  81.      * Class prototype objects, in alphabetical order.  Must be CLEARed (set
  82.      * to null) in LM_PutMochaDecoder, HELD (GC roots added) in lm_NewWindow,
  83.      * and DROPped (removed as GC roots) in lm_DestroyWindow.
  84.      * XXXbe clean up, clear via bzero, using a sub-structure.
  85.      */
  86.     JSObject        *anchor_prototype;
  87.     JSObject        *bar_prototype;
  88.     JSObject        *document_prototype;
  89.     JSObject        *event_prototype;
  90.     JSObject        *event_capturer_prototype;
  91.     JSObject        *event_receiver_prototype;
  92.     JSObject        *form_prototype;
  93.     JSObject        *image_prototype;
  94.     JSObject        *input_prototype;
  95.     JSObject        *layer_prototype;
  96.     JSObject        *option_prototype;
  97.     JSObject        *rect_prototype;
  98.     JSObject        *url_prototype;
  99.  
  100.     /*
  101.      * Window sub-objects.  These must also follow the CLEAR/HOLD/DROP
  102.      * protocol mentioned above.
  103.      */
  104.     JSObject        *document;
  105.     JSObject        *history;
  106.     JSObject        *location;
  107.     JSObject        *navigator;
  108.     JSObject        *components;
  109.     JSObject        *screen;
  110.     JSObject        *hardware;
  111.     JSObject        *crypto;
  112.     JSObject        *pkcs11;
  113.  
  114.     /*
  115.      * Ad-hoc GC roots.
  116.      */
  117.     JSObject        *event_receiver;
  118.     JSObject        *opener;
  119.  
  120.     JSVersion        firstVersion;   /* First JS script tag version. */
  121.  
  122.     /*
  123.      * Security info for all of this decoder's scripts, except those
  124.      * contained in layers.
  125.      */
  126.     JSPrincipals    *principals;
  127.     JSPrincipalsList*early_access_list;
  128.  
  129.     IL_GroupContext *image_context; /* Image context for anonymous images */
  130.  
  131.     /* 
  132.      * Table that maintains an id to JS object mapping for reflected
  133.      * elements. This is used during resize to reestablish the connection
  134.      * between layout elements and their corresponding JS object. 
  135.      * Form elements are special, since they can't use the same keying
  136.      */
  137.     PRHashTable     *id_to_object_map;
  138. } MochaDecoder;
  139.  
  140. /* 
  141.  * Number of buckets for the id-to-object hash table.
  142.  */
  143. #define LM_ID_TO_OBJ_MAP_SIZE 20
  144. #define LM_FORM_ELEMENT_MAP_SIZE 10
  145.  
  146. /*
  147.  * Types of objects reflected into Mocha
  148.  */
  149. typedef enum {
  150.     LM_APPLETS = 0,
  151.     LM_FORMS,
  152.     LM_LINKS,
  153.     LM_NAMEDANCHORS,
  154.     LM_EMBEDS,
  155.     LM_IMAGES,
  156.     LM_FORMELEMENTS,
  157.     LM_LAYERS
  158. } ReflectedObject;
  159.  
  160. /*
  161.  * Generates an id-to-object mapping key from the ReflectedObject
  162.  * type, the containing layer id and the id of the object itself.
  163.  * The key is 4 bits type, 14 bits layer_id and 14 bits id.
  164.  */
  165. #define LM_GET_MAPPING_KEY(obj_type, layer_id, id)    \
  166.      (void *)(((((uint32)obj_type) << 28) & 0xF0000000UL) |     \
  167.               ((((uint32)layer_id) << 14) & 0x0FFFC000UL) |     \
  168.               (((uint32)id) & 0x00003FFFUL))
  169.  
  170. /*
  171.  * Public, well-known string constants.
  172.  */
  173. extern char js_language_name[];      /* "JavaScript" */
  174. extern char js_content_type[];       /* "application/x-javascript" */
  175.  
  176. /*
  177.  * Initialize and finalize Mocha-in-the-client.
  178.  */
  179. extern void LM_InitMocha(void);
  180. extern void LM_FinishMocha(void);
  181.  
  182. /*
  183.  * Force mocha on in the given context, even if the user pref is set to
  184.  * disable mocha.
  185.  */
  186. extern void LM_ForceJSEnabled(MWContext *cx);
  187.  
  188. /*
  189.  * Initialize and finalize Mocha-Java connection
  190.  */
  191. #define LM_MOJA_UNINITIALIZED   0
  192. #define LM_MOJA_OK              1
  193. #define LM_MOJA_JAVA_FAILED     2
  194. #define LM_MOJA_OUT_OF_MEMORY   3
  195. extern int LM_InitMoja(void);
  196. extern void LM_FinishMoja(void);
  197. extern int LM_IsMojaInitialized(void);
  198.  
  199. /*
  200.  * Enter or leave the big mocha lock.  Any thread which wants to
  201.  * preserve JavaScript run-to-completion semantics must bracket
  202.  * JavaScript evaluation with these calls.
  203.  */
  204. typedef void
  205. (PR_CALLBACK *JSLockReleaseFunc)(void * data);
  206.  
  207.  
  208. extern void PR_CALLBACK LM_LockJS(void);
  209. extern void PR_CALLBACK LM_UnlockJS(void);
  210. extern JSBool PR_CALLBACK LM_AttemptLockJS(JSLockReleaseFunc fn, void * data);
  211. extern JSBool PR_CALLBACK LM_ClearAttemptLockJS(JSLockReleaseFunc fn, void * data);
  212. extern PRBool PR_CALLBACK
  213. LM_HandOffJSLock(PRThread * oldOwner, PRThread *newOwner);
  214.  
  215. /*
  216.  * For interruption purposes we will sometimes need to know the
  217.  *   context who is holding the JS lock
  218.  */
  219. extern void LM_JSLockSetContext(MWContext * context);
  220. extern MWContext * LM_JSLockGetContext(void);
  221.  
  222. /*
  223.  * Enable/disable for Mocha-in-the-client.
  224.  */
  225. #define LM_SwitchMocha(toggle)    LM_SetMochaEnabled(toggle)
  226.  
  227. extern JSBool
  228. LM_GetMochaEnabled(void);
  229.  
  230. /*
  231.  * Get (create if necessary) a MochaDecoder for context, adding a reference
  232.  * to its window_object.  Put drops the reference, destroying window_object
  233.  * when the count reaches zero.  These functions should only be called in
  234.  * the mocha thread or while holding the JS-lock
  235.  */
  236. extern MochaDecoder *
  237. LM_GetMochaDecoder(MWContext *context);
  238.  
  239. extern void
  240. LM_PutMochaDecoder(MochaDecoder *decoder);
  241.  
  242. /*
  243.  * Get the source URL for script being loaded by document.  This URL will be
  244.  * the document's URL for inline script, or the SRC= URL for included script.
  245.  * The returned pointer is safe only within the extent of the function that
  246.  * calls LM_GetSourceURL().
  247.  */
  248. extern const char *
  249. LM_GetSourceURL(MochaDecoder *decoder);
  250.  
  251. /*
  252.  * Set the current layer and hence the current scope for script evaluation.
  253.  */
  254. extern void
  255. LM_SetActiveLayer(MWContext * context, int32 layer_id);
  256.  
  257. /*
  258.  * Get the current layer and hence the current scope for script evaluation.
  259.  */
  260. extern int32
  261. LM_GetActiveLayer(MWContext * context);
  262.  
  263. /*
  264.  * Evaluate the contents of a SCRIPT tag. You can specify the JSObject
  265.  * to use as the base scope. Pass NULL to use the default window_object
  266.  */
  267. extern JSBool
  268. LM_EvaluateBuffer(MochaDecoder *decoder, void *base, size_t length,
  269.           uint lineno, char * scope_to, struct JSPrincipals *principals,
  270.           JSBool unicode, jsval *result);
  271.  
  272. /*
  273.  * Evaluate an expression entity in an HTML attribute (WIDTH="&{height/2};").
  274.  * Returns null on error, otherwise a pointer to the malloc'd string result.
  275.  * The caller is responsible for freeing the string result.
  276.  */
  277. extern char *
  278. LM_EvaluateAttribute(MWContext *context, char *expr, uint lineno);
  279.  
  280. /*
  281.  * Remove any MochaDecoder window_context pointer to an MWContext that's
  282.  * being destroyed.
  283.  */
  284. extern void
  285. LM_RemoveWindowContext(MWContext *context, History_entry * he);
  286.  
  287. extern void
  288. LM_DropSavedWindow(MWContext *context, void *window);
  289.  
  290. /*
  291.  * Set and clear the HTML stream and URL for the MochaDecoder
  292.  *   associated with the given context
  293.  */
  294. extern JSBool
  295. LM_SetDecoderStream(MWContext * context, NET_StreamClass *stream,
  296.                     URL_Struct *url_struct, JSBool free_stream_on_close);
  297.  
  298. /*
  299.  * Start caching HTML or plain text generated by document.write() where the
  300.  * script is running on mc, the document is being generated into decoder's
  301.  * window, and url_struct tells about the generator.
  302.  */
  303. extern NET_StreamClass *
  304. LM_WysiwygCacheConverter(MWContext *context, URL_Struct *url_struct,
  305.              const char * wysiwyg_url, const char * base_href);
  306.  
  307. /*
  308.  * Skip over the "wysiwyg://docid/" in url_string and return a pointer to the
  309.  * real URL hidden after the prefix.  If url_string is not of "wysiwyg:" type,
  310.  * just return url_string.  Never returns null.
  311.  */
  312. extern const char *
  313. LM_StripWysiwygURLPrefix(const char *url_string);
  314.  
  315. /*
  316.  * This function works only on "wysiwyg:" type URLs -- don't call it unless
  317.  * you know that NET_URL_Type(url_string) is WYSIWYG_TYPE_URL.  It'll return
  318.  * null if url_string seems too short, or if it can't find the third slash.
  319.  */
  320. extern const char *
  321. LM_SkipWysiwygURLPrefix(const char *url_string);
  322.  
  323. /*
  324.  * Return a pointer to a malloc'd string of the form "<BASE HREF=...>" where
  325.  * the "..." URL is the directory of cx's origin URL.  Such a base URL is the
  326.  * default base for relative URLs in generated HTML.
  327.  */
  328. extern char *
  329. LM_GetBaseHrefTag(JSContext *cx, JSPrincipals *principals);
  330.  
  331. /*
  332.  * XXX Make these public LO_... typedefs in lo_ele.h/ntypes.h?
  333.  */
  334. struct lo_FormData_struct;
  335. struct lo_NameList_struct;
  336.  
  337. extern struct lo_FormData_struct *
  338. LO_GetFormDataByID(MWContext *context, int32 layer_id, intn form_id);
  339.  
  340. extern uint
  341. LO_EnumerateForms(MWContext *context, int32 layer_id);
  342.  
  343. extern struct LO_ImageStruct_struct *
  344. LO_GetImageByIndex(MWContext *context, int32 layer_id, intn image_id);
  345.  
  346. extern uint
  347. LO_EnumerateImages(MWContext *context, int32 layer_id);
  348.  
  349. /*
  350.  * Reflect display layers into Mocha.
  351.  */
  352. extern JSObject *
  353. LM_ReflectLayer(MWContext *context, int32 layer_id, int32 parent_layer_id,
  354.                 PA_Tag *tag);
  355.  
  356. extern LO_FormElementStruct *
  357. LO_GetFormElementByIndex(struct lo_FormData_struct *form_data, int32 index);
  358.  
  359. extern uint
  360. LO_EnumerateFormElements(MWContext *context,
  361.              struct lo_FormData_struct *form_data);
  362.  
  363. /*
  364.  * Layout helper function to find a named anchor by its index in the
  365.  * document.anchors[] array.
  366.  */
  367. extern struct lo_NameList_struct *
  368. LO_GetNamedAnchorByIndex(MWContext *context, int32 layer_id, uint index);
  369.  
  370. extern uint
  371. LO_EnumerateNamedAnchors(MWContext *context, int32 layer_id);
  372.  
  373. /*
  374.  * Layout Mocha helper function to find an HREF Anchor by its index in the
  375.  * document.links[] array.
  376.  */
  377. extern LO_AnchorData *
  378. LO_GetLinkByIndex(MWContext *context, int32 layer_id, uint index);
  379.  
  380. extern uint
  381. LO_EnumerateLinks(MWContext *context, int32 layer_id);
  382.  
  383. extern LO_JavaAppStruct *
  384. LO_GetAppletByIndex(MWContext *context, int32 layer_id, uint index);
  385.  
  386. extern uint
  387. LO_EnumerateApplets(MWContext *context, int32 layer_id);
  388.  
  389. extern LO_EmbedStruct *
  390. LO_GetEmbedByIndex(MWContext *context, int32 layer_id, uint index);
  391.  
  392. extern uint
  393. LO_EnumerateEmbeds(MWContext *context, int32 layer_id);
  394.  
  395. /*
  396.  * Get and set a color attribute in the current document state.
  397.  */
  398. extern void
  399. LO_GetDocumentColor(MWContext *context, int type, LO_Color *color);
  400.  
  401. extern void
  402. LO_SetDocumentColor(MWContext *context, int type, LO_Color *color);
  403.  
  404. /*
  405.  * Layout function to reallocate the lo_FormElementOptionData array pointed at
  406.  * by lo_FormElementSelectData's options member to include space for the number
  407.  * of options given by selectData->option_cnt.
  408.  */
  409. extern XP_Bool
  410. LO_ResizeSelectOptions(lo_FormElementSelectData *selectData);
  411.  
  412. /*
  413.  * Discard the current document and all its subsidiary objects.
  414.  */
  415. extern void
  416. LM_ReleaseDocument(MWContext *context, JSBool resize_reload);
  417.  
  418. /*
  419.  * Search if a the event is being captured in the frame hierarchy.
  420.  */
  421. extern XP_Bool
  422. LM_EventCaptureCheck(MWContext *context, uint32 current_event);
  423.  
  424. /*
  425.  * Scroll a window to the given point.
  426.  */
  427. extern void LM_SendOnScroll(MWContext *context, int32 x, int32 y);
  428.  
  429. /*
  430.  * Display a help topic.
  431.  */
  432. extern void LM_SendOnHelp(MWContext *context);
  433.  
  434. /*
  435.  * Send a load or abort event for an image to a callback.
  436.  */
  437. typedef enum LM_ImageEvent {
  438.     LM_IMGUNBLOCK   = 0,
  439.     LM_IMGLOAD      = 1,
  440.     LM_IMGABORT     = 2,
  441.     LM_IMGERROR     = 3,
  442.     LM_LASTEVENT    = 3
  443. } LM_ImageEvent;
  444.  
  445. extern void
  446. LM_ProcessImageEvent(MWContext *context, LO_ImageStruct *image_data,
  447.           LM_ImageEvent event);
  448.  
  449. /* This should be called when a named anchor is located. */
  450. extern JSBool
  451. LM_SendOnLocate(MWContext *context, struct lo_NameList_struct *name_rec);
  452.  
  453. extern JSObject *
  454. LM_ReflectApplet(MWContext *context, LO_JavaAppStruct *applet_data,
  455.                  PA_Tag * tag, int32 layer_id, uint index);
  456.  
  457. extern JSObject *
  458. LM_ReflectEmbed(MWContext *context, LO_EmbedStruct *lo_embed,
  459.                 PA_Tag * tag, int32 layer_id, uint index);
  460.  
  461. struct lo_FormData_struct;
  462. struct lo_NameList_struct;
  463.  
  464. extern JSObject *
  465. LM_ReflectForm(MWContext *context, struct lo_FormData_struct *form_data, 
  466.            PA_Tag * tag, int32 layer_id, uint index);
  467.  
  468. extern JSObject *
  469. LM_ReflectFormElement(MWContext *context, int32 layer_id, int32 form_id, 
  470.                       int32 element_id, PA_Tag * tag);
  471.  
  472. extern JSObject *
  473. LM_ReflectLink(MWContext *context, LO_AnchorData *anchor_data, PA_Tag * tag, 
  474.                int32 layer_id, uint index);
  475.  
  476. extern JSObject *
  477. LM_ReflectNamedAnchor(MWContext *context, struct lo_NameList_struct *name_rec, 
  478.                       PA_Tag * tag, int32 layer_id, uint index);
  479.  
  480. extern JSObject *
  481. LM_ReflectImage(MWContext *context, LO_ImageStruct *image_data, 
  482.                 PA_Tag * tag, int32 layer_id, uint index);
  483.  
  484. extern JSBool
  485. LM_CanDoJS(MWContext *context);
  486.  
  487. extern JSBool
  488. LM_IsActive(MWContext *context);
  489.  
  490. /*
  491.  * Security.
  492.  */
  493.  
  494. extern JSPrincipals *
  495. LM_NewJSPrincipals(URL_Struct *archive, char *name, const char *codebase);
  496.  
  497. extern char *
  498. LM_ExtractFromPrincipalsArchive(JSPrincipals *principals, char *name, 
  499.                                 uint *length);
  500.  
  501. extern JSBool
  502. LM_SetUntransformedSource(JSPrincipals *principals, char *original, 
  503.                           char *transformed);
  504.  
  505. extern JSPrincipals * PR_CALLBACK
  506. LM_GetJSPrincipalsFromJavaCaller(JSContext *cx, int callerDepth);
  507.  
  508. /*
  509.  * LM_RegisterPrincipals will verify and register a set of principals 
  510.  * in the decoder, modifying decoder->principals in the process. It
  511.  * returns the modified decoder.
  512.  * 
  513.  * The "name" parameter may be NULL if "principals" was created with a name.
  514.  */
  515.  
  516. extern JSPrincipals *
  517. LM_RegisterPrincipals(MochaDecoder *decoder, JSPrincipals *principals, 
  518.                       char *name, char *src);
  519. /*
  520.  * JavaScript Debugger support
  521.  */
  522. #ifdef JSDEBUGGER 
  523.  
  524. extern NET_StreamClass*
  525. LM_StreamBuilder( int         format_out,
  526.                   void        *data_obj,
  527.                   URL_Struct  *URL_s,
  528.                   MWContext   *mwcontext );
  529.  
  530. extern JSBool
  531. LM_GetJSDebugActive(void);
  532.  
  533. extern void
  534. LM_JamSourceIntoJSDebug( const char *filename,
  535.                          const char *str, 
  536.                          int32      len,
  537.                          MWContext  *mwcontext );
  538.  
  539. #endif
  540.  
  541. NSPR_END_EXTERN_C
  542.  
  543. #endif /* libmocha_h___ */
  544.